1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
5 * https://lxqt.org
6 *
7 * Copyright: 2010-2012 Razor team
8 * Authors:
9 * Alexander Sokoloff <sokoloff.a@gmail.com>
10 *
11 * This program or library is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20
21 * You should have received a copy of the GNU Lesser General
22 * Public License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 *
26 * END_COMMON_COPYRIGHT_HEADER */
27
28
29 #include "popupmenu.h"
30 #include <QWidgetAction>
31 #include <QToolButton>
32 #include <QEvent>
33 #include <QKeyEvent>
34
35 static const char POPUPMENU_TITLE[] = "POPUP_MENU_TITLE_OBJECT_NAME";
36
37 /************************************************
38
39 ************************************************/
addTitle(const QIcon & icon,const QString & text)40 QAction* PopupMenu::addTitle(const QIcon &icon, const QString &text)
41 {
42 QAction *buttonAction = new QAction(this);
43 QFont font = buttonAction->font();
44 font.setBold(true);
45 buttonAction->setText(QString(text).replace(QLatin1String("&"), QLatin1String("&&")));
46 buttonAction->setFont(font);
47 buttonAction->setIcon(icon);
48
49 QWidgetAction *action = new QWidgetAction(this);
50 action->setObjectName(QLatin1String(POPUPMENU_TITLE));
51 QToolButton *titleButton = new QToolButton(this);
52 titleButton->installEventFilter(this); // prevent clicks on the title of the menu
53 titleButton->setDefaultAction(buttonAction);
54 titleButton->setDown(true); // prevent hover style changes in some styles
55 titleButton->setCheckable(true);
56 titleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
57 action->setDefaultWidget(titleButton);
58
59 addAction(action);
60 return action;
61 }
62
63
64 /************************************************
65
66 ************************************************/
addTitle(const QString & text)67 QAction* PopupMenu::addTitle(const QString &text)
68 {
69 return addTitle(QIcon(), text);
70 }
71
72
73 /************************************************
74
75 ************************************************/
eventFilter(QObject * object,QEvent * event)76 bool PopupMenu::eventFilter(QObject *object, QEvent *event)
77 {
78 Q_UNUSED(object);
79
80 if (event->type() == QEvent::Paint ||
81 event->type() == QEvent::KeyPress ||
82 event->type() == QEvent::KeyRelease
83 )
84 {
85 return false;
86 }
87
88 event->accept();
89 return true;
90 }
91
92
93 /************************************************
94
95 ************************************************/
keyPressEvent(QKeyEvent * e)96 void PopupMenu::keyPressEvent(QKeyEvent* e)
97 {
98 if (e->key() == Qt::Key_Up || e->key() == Qt::Key_Down)
99 {
100 QMenu::keyPressEvent(e);
101
102 QWidgetAction *action = qobject_cast<QWidgetAction*>(this->activeAction());
103 QWidgetAction *firstAction = action;
104
105 while (action && action->objectName() == QLatin1String(POPUPMENU_TITLE))
106 {
107 this->keyPressEvent(e);
108 action = qobject_cast<QWidgetAction*>(this->activeAction());
109
110 if (firstAction == action) // we looped and only found titles
111 {
112 this->setActiveAction(nullptr);
113 break;
114 }
115 }
116
117 return;
118 }
119
120 QMenu::keyPressEvent(e);
121 }
122
123
124