Falkon Develop
Cross-platform Qt-based web browser
buttonwithmenu.h
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2017 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#ifndef BUTTONWITHMENU_H
19#define BUTTONWITHMENU_H
20
21#include <QVariant>
22
23#include "toolbutton.h"
24#include "wheelhelper.h"
25
26// Only to be used in WebSearchBar
28{
29 Q_OBJECT
30public:
31 struct Item {
32 QString text;
33 QIcon icon;
34 QVariant userData;
35
36 Item(const QString &a = QString(), const QIcon &b = QIcon()) {
37 text = a;
38 icon = b;
39 }
40
41 bool operator==(const Item &a) const {
42 return (a.text == text) && (a.icon.pixmap(16).toImage() == icon.pixmap(16).toImage());
43 }
44
45 bool isEmpty() {
46 return (text.isEmpty() && icon.isNull());
47 }
48
49 void clear() {
50 text = QString();
51 icon = QIcon();
52 userData = QVariant();
53 }
54 };
55
56 explicit ButtonWithMenu(QWidget* parent = nullptr);
57 ~ButtonWithMenu() override;
58
59 void addItem(const Item &item);
60 void addItems(const QVector<Item> &items);
61 void removeItem(const Item &item);
62 void setCurrentItem(const Item &item, bool emitSignal = true);
63 void setCurrentIndex(int index, bool emitSignal = true);
64
66 QVector<Item> allItems() { return m_items; }
67 QMenu* menu() const;
68
69Q_SIGNALS:
73
74public Q_SLOTS:
75 void clearItems();
76
77 void selectNextItem();
78 void selectPreviousItem();
79
80private Q_SLOTS:
81 void setCurrentItem();
82 void generateMenu();
83
84private:
85 void mousePressEvent(QMouseEvent *event) override;
86 void wheelEvent(QWheelEvent* event) override;
87
88 QMenu* m_menu;
89 QVector<Item> m_items;
90 Item m_currentItem;
91 WheelHelper m_wheelHelper;
92};
93
94// Hint to QVector to use std::realloc on item moving
96
98
99#endif // BUTTONWITHMENU_H
Q_DECLARE_TYPEINFO(ButtonWithMenu::Item, Q_MOVABLE_TYPE)
QMenu * menu() const
void selectPreviousItem()
void addItem(const Item &item)
void setCurrentIndex(int index, bool emitSignal=true)
void itemAdded(const ButtonWithMenu::Item &item)
QVector< Item > allItems()
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 itemRemoved(const ButtonWithMenu::Item &item)
Q_DECLARE_METATYPE(FlashCookie)
bool operator==(const Item &a) const
Item(const QString &a=QString(), const QIcon &b=QIcon())