Falkon Develop
Cross-platform Qt-based web browser
buttonwithmenu.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2018 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 "buttonwithmenu.h"
19
20#include <QMenu>
21#include <QWheelEvent>
22
24 : ToolButton(parent)
25 , m_menu(new QMenu(this))
26{
27 setCursor(Qt::ArrowCursor);
28 setFocusPolicy(Qt::NoFocus);
29
30 connect(this, &ToolButton::aboutToShowMenu, this, &ButtonWithMenu::generateMenu);
31 connect(m_menu, &QMenu::aboutToShow, this, std::bind(&ButtonWithMenu::setDown, this, true));
32 connect(m_menu, &QMenu::aboutToHide, this, std::bind(&ButtonWithMenu::setDown, this, false));
33}
34
35void ButtonWithMenu::setCurrentItem()
36{
37 if (auto* action = qobject_cast<QAction*>(sender())) {
38 setCurrentItem(action->data().value<Item>());
39 }
40}
41
43{
44 m_menu->clear();
45 m_items.clear();
46}
47
49{
50 int index = m_items.indexOf(m_currentItem) + 1;
51
52 if (index < m_items.size()) {
53 setCurrentIndex(index);
54 }
55}
56
58{
59 int index = m_items.indexOf(m_currentItem) - 1;
60
61 if (index >= 0) {
62 setCurrentIndex(index);
63 }
64}
65
67{
68 m_items.append(item);
69
70 if (m_items.count() == 1) {
71 setCurrentItem(item);
72 }
73
74 Q_EMIT itemAdded(item);
75}
76
77void ButtonWithMenu::addItems(const QVector<Item> &items)
78{
79 for (const Item &item : items) {
80 addItem(item);
81 }
82}
83
85{
86 int index = m_items.indexOf(item);
87 if (index < 0) {
88 return;
89 }
90
91 m_items.remove(index);
92
93 if (m_items.isEmpty()) {
94 setIcon(QIcon());
95 return;
96 }
97
98 if (m_currentItem == item) {
99 setCurrentItem(m_items.at(0));
100 }
101}
102
103void ButtonWithMenu::setCurrentItem(const Item &item, bool emitSignal)
104{
105 int index = m_items.indexOf(item);
106 if (index < 0 || m_currentItem == item) {
107 return;
108 }
109
110 m_currentItem = item;
111
112 setIcon(m_currentItem.icon);
113 setToolTip(m_currentItem.text);
114
115 if (emitSignal) {
116 Q_EMIT activeItemChanged(m_currentItem);
117 }
118}
119
120void ButtonWithMenu::setCurrentIndex(int index, bool emitSignal)
121{
122 setCurrentItem(m_items.at(index), emitSignal);
123}
124
125void ButtonWithMenu::wheelEvent(QWheelEvent* event)
126{
127 m_wheelHelper.processEvent(event);
128 while (WheelHelper::Direction direction = m_wheelHelper.takeDirection()) {
129 switch (direction) {
133 break;
134
138 break;
139
140 default:
141 break;
142 }
143 }
144 event->accept();
145}
146
148{
149 return m_currentItem;
150}
151
153{
154 return m_menu;
155}
156
157void ButtonWithMenu::generateMenu()
158{
159 m_menu->clear();
160
161 for (const Item &item : std::as_const(m_items)) {
162 QVariant variant;
163 variant.setValue(item);
164 m_menu->addAction(item.icon, item.text, this, SLOT(setCurrentItem()))->setData(variant);
165 }
166}
167
168void ButtonWithMenu::mousePressEvent(QMouseEvent *event)
169{
170 if (event->buttons() == Qt::LeftButton && parentWidget() && parentWidget()->parentWidget()) {
171 Q_EMIT aboutToShowMenu();
172 QWidget *w = parentWidget()->parentWidget();
173 m_menu->popup(w->mapToGlobal(w->rect().bottomLeft()));
174 }
175
177}
178
180= default;
QMenu * menu() const
void selectPreviousItem()
void addItem(const Item &item)
void setCurrentIndex(int index, bool emitSignal=true)
void itemAdded(const ButtonWithMenu::Item &item)
void addItems(const QVector< Item > &items)
void removeItem(const Item &item)
~ButtonWithMenu() override
void activeItemChanged(const ButtonWithMenu::Item &item)
void setCurrentItem(const Item &item, bool emitSignal=true)
ButtonWithMenu(QWidget *parent=nullptr)
void mousePressEvent(QMouseEvent *e) override
Definition: toolbutton.cpp:186
void aboutToShowMenu()
void setIcon(const QIcon &icon)
Definition: toolbutton.cpp:85
Direction takeDirection()
Definition: wheelhelper.cpp:74
void processEvent(QWheelEvent *event)
Definition: wheelhelper.cpp:31