File indexing completed on 2024-05-12 04:58:26
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #ifndef BUTTONWITHMENU_H 0019 #define BUTTONWITHMENU_H 0020 0021 #include <QVariant> 0022 0023 #include "toolbutton.h" 0024 #include "wheelhelper.h" 0025 0026 // Only to be used in WebSearchBar 0027 class ButtonWithMenu : public ToolButton 0028 { 0029 Q_OBJECT 0030 public: 0031 struct Item { 0032 QString text; 0033 QIcon icon; 0034 QVariant userData; 0035 0036 Item(const QString &a = QString(), const QIcon &b = QIcon()) { 0037 text = a; 0038 icon = b; 0039 } 0040 0041 bool operator==(const Item &a) const { 0042 return (a.text == text) && (a.icon.pixmap(16).toImage() == icon.pixmap(16).toImage()); 0043 } 0044 0045 bool isEmpty() { 0046 return (text.isEmpty() && icon.isNull()); 0047 } 0048 0049 void clear() { 0050 text = QString(); 0051 icon = QIcon(); 0052 userData = QVariant(); 0053 } 0054 }; 0055 0056 explicit ButtonWithMenu(QWidget* parent = nullptr); 0057 ~ButtonWithMenu() override; 0058 0059 void addItem(const Item &item); 0060 void addItems(const QVector<Item> &items); 0061 void removeItem(const Item &item); 0062 void setCurrentItem(const Item &item, bool emitSignal = true); 0063 void setCurrentIndex(int index, bool emitSignal = true); 0064 0065 Item currentItem(); 0066 QVector<Item> allItems() { return m_items; } 0067 QMenu* menu() const; 0068 0069 Q_SIGNALS: 0070 void activeItemChanged(const ButtonWithMenu::Item &item); 0071 void itemAdded(const ButtonWithMenu::Item &item); 0072 void itemRemoved(const ButtonWithMenu::Item &item); 0073 0074 public Q_SLOTS: 0075 void clearItems(); 0076 0077 void selectNextItem(); 0078 void selectPreviousItem(); 0079 0080 private Q_SLOTS: 0081 void setCurrentItem(); 0082 void generateMenu(); 0083 0084 private: 0085 void mousePressEvent(QMouseEvent *event) override; 0086 void wheelEvent(QWheelEvent* event) override; 0087 0088 QMenu* m_menu; 0089 QVector<Item> m_items; 0090 Item m_currentItem; 0091 WheelHelper m_wheelHelper; 0092 }; 0093 0094 // Hint to QVector to use std::realloc on item moving 0095 Q_DECLARE_TYPEINFO(ButtonWithMenu::Item, Q_MOVABLE_TYPE); 0096 0097 Q_DECLARE_METATYPE(ButtonWithMenu::Item) 0098 0099 #endif // BUTTONWITHMENU_H