Falkon Develop
Cross-platform Qt-based web browser
enhancedmenu.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "enhancedmenu.h"
19
20#include <QMouseEvent>
21#include <QApplication>
22
23Menu::Menu(QWidget* parent)
24 : QMenu(parent)
25{
26}
27
28Menu::Menu(const QString &title, QWidget* parent)
29 : QMenu(title, parent)
30{
31}
32
34{
35 return m_closeOnMiddleClick;
36}
37
39{
40 m_closeOnMiddleClick = close;
41}
42
43void Menu::mouseReleaseEvent(QMouseEvent* e)
44{
45 QAction* qact = activeAction();
46 auto* act = qobject_cast<Action*> (qact);
47
48 if (qact && qact->menu()) {
49 Menu* m = qobject_cast<Menu*> (qact->menu());
50 if (!m) {
51 QMenu::mouseReleaseEvent(e);
52 return;
53 }
54
55 if (e->button() == Qt::MiddleButton || (e->button() == Qt::LeftButton && e->modifiers() == Qt::ControlModifier)) {
56 closeAllMenus();
57 Q_EMIT menuMiddleClicked(m);
58 }
59 }
60
61 if (!act) {
62 QMenu::mouseReleaseEvent(e);
63 return;
64 }
65
66 if ((e->button() == Qt::LeftButton || e->button() == Qt::RightButton) && e->modifiers() == Qt::NoModifier) {
67 closeAllMenus();
68 act->trigger();
69 e->accept();
70 }
71 else if (e->button() == Qt::MiddleButton || (e->button() == Qt::LeftButton && e->modifiers() == Qt::ControlModifier)) {
72 if ((e->button() == Qt::MiddleButton && m_closeOnMiddleClick) || e->button() != Qt::MiddleButton) {
73 closeAllMenus();
74 }
75 act->emitCtrlTriggered();
76 e->accept();
77 }
78 else if (e->button() == Qt::LeftButton && e->modifiers() == Qt::ShiftModifier) {
79 closeAllMenus();
80 act->emitShiftTriggered();
81 e->accept();
82 }
83}
84
85void Menu::keyPressEvent(QKeyEvent* e)
86{
87 if (e->key() != Qt::Key_Enter && e->key() != Qt::Key_Return) {
88 QMenu::keyPressEvent(e);
89 return;
90 }
91
92 QAction* qact = activeAction();
93 auto* act = qobject_cast<Action*> (qact);
94
95 if (!act) {
96 QMenu::keyPressEvent(e);
97 return;
98 }
99
100 if (e->modifiers() == Qt::NoModifier || e->modifiers() == Qt::KeypadModifier) {
101 closeAllMenus();
102 act->trigger();
103 e->accept();
104 }
105 else if (e->modifiers() == Qt::ControlModifier) {
106 closeAllMenus();
107 act->emitCtrlTriggered();
108 e->accept();
109 }
110 else if (e->modifiers() == Qt::ShiftModifier) {
111 closeAllMenus();
112 act->emitShiftTriggered();
113 e->accept();
114 }
115}
116
117void Menu::closeAllMenus()
118{
119 QMenu* menu = this;
120
121 while (menu) {
122 menu->close();
123 menu = qobject_cast<QMenu*>(QApplication::activePopupWidget());
124 }
125}
126
127Action::Action(QObject* parent)
128 : QAction(parent)
129{
130}
131
132Action::Action(const QString &text, QObject* parent)
133 : QAction(text, parent)
134{
135}
136
137Action::Action(const QIcon &icon, const QString &text, QObject* parent)
138 : QAction(icon, text, parent)
139{
140}
141
143{
144 Q_EMIT ctrlTriggered();
145}
146
148{
149 Q_EMIT shiftTriggered();
150}
void shiftTriggered()
void emitShiftTriggered()
Action(QObject *parent=nullptr)
void ctrlTriggered()
void emitCtrlTriggered()
void setCloseOnMiddleClick(bool close)
Menu(QWidget *parent=nullptr)
bool closeOnMiddleClick() const
void menuMiddleClicked(Menu *)