2011-09-23 22:06:21 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-09-23 22:06:21 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-06-06 19:38:37 +02:00
|
|
|
#ifndef BUTTONWITHMENU_H
|
|
|
|
#define BUTTONWITHMENU_H
|
|
|
|
|
2014-09-26 19:28:24 +02:00
|
|
|
#include <QVariant>
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
#include "toolbutton.h"
|
2014-02-26 20:03:20 +01:00
|
|
|
#include "qzcommon.h"
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2014-02-19 22:12:32 +01:00
|
|
|
class QUPZILLA_EXPORT ButtonWithMenu : public ToolButton
|
2011-06-06 19:38:37 +02:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
struct Item {
|
|
|
|
QString text;
|
|
|
|
QIcon icon;
|
|
|
|
QVariant userData;
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
Item(const QString &a = QString(), const QIcon &b = QIcon()) {
|
2011-06-06 19:38:37 +02:00
|
|
|
text = a;
|
|
|
|
icon = b;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
bool operator==(const Item &a) {
|
|
|
|
return (a.text == text) && (a.icon.pixmap(16, 16).toImage() == icon.pixmap(16, 16).toImage());
|
2011-06-06 19:38:37 +02:00
|
|
|
}
|
2012-02-28 18:57:05 +01:00
|
|
|
|
|
|
|
bool isEmpty() {
|
|
|
|
return (text.isEmpty() && icon.isNull());
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
text = QString();
|
|
|
|
icon = QIcon();
|
|
|
|
userData = QVariant();
|
|
|
|
}
|
2011-06-06 19:38:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
explicit ButtonWithMenu(QWidget* parent = 0);
|
|
|
|
~ButtonWithMenu();
|
|
|
|
|
|
|
|
void addItem(const Item &item);
|
2013-02-26 12:56:11 +01:00
|
|
|
void addItems(const QVector<Item> &items);
|
2011-06-06 19:38:37 +02:00
|
|
|
void removeItem(const Item &item);
|
2012-02-28 18:57:05 +01:00
|
|
|
void setCurrentItem(const Item &item, bool emitSignal = true);
|
2013-11-21 18:23:11 +01:00
|
|
|
void setCurrentIndex(int index, bool emitSignal = true);
|
2011-06-06 19:38:37 +02:00
|
|
|
|
2012-02-28 18:57:05 +01:00
|
|
|
Item currentItem();
|
2013-02-26 12:56:11 +01:00
|
|
|
QVector<Item> allItems() { return m_items; }
|
2011-10-21 23:26:34 +02:00
|
|
|
QMenu* menu() const;
|
2011-06-06 19:38:37 +02:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void activeItemChanged(const ButtonWithMenu::Item &item);
|
|
|
|
void itemAdded(const ButtonWithMenu::Item &item);
|
|
|
|
void itemRemoved(const ButtonWithMenu::Item &item);
|
|
|
|
|
|
|
|
public slots:
|
2011-10-21 23:26:34 +02:00
|
|
|
void clearItems();
|
2011-06-06 19:38:37 +02:00
|
|
|
|
2013-11-21 18:23:11 +01:00
|
|
|
void selectNextItem();
|
|
|
|
void selectPreviousItem();
|
|
|
|
|
2011-06-06 19:38:37 +02:00
|
|
|
private slots:
|
|
|
|
void setCurrentItem();
|
2011-10-21 23:26:34 +02:00
|
|
|
void generateMenu();
|
2011-06-06 19:38:37 +02:00
|
|
|
|
|
|
|
private:
|
2011-11-06 17:01:23 +01:00
|
|
|
void wheelEvent(QWheelEvent* event);
|
2011-06-06 19:38:37 +02:00
|
|
|
|
|
|
|
QMenu* m_menu;
|
2013-02-26 12:56:11 +01:00
|
|
|
QVector<Item> m_items;
|
2012-02-28 18:57:05 +01:00
|
|
|
Item m_currentItem;
|
2011-06-06 19:38:37 +02:00
|
|
|
};
|
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
// Hint to QVector to use std::realloc on item moving
|
|
|
|
Q_DECLARE_TYPEINFO(ButtonWithMenu::Item, Q_MOVABLE_TYPE);
|
|
|
|
|
2011-06-06 19:38:37 +02:00
|
|
|
Q_DECLARE_METATYPE(ButtonWithMenu::Item)
|
|
|
|
|
|
|
|
#endif // BUTTONWITHMENU_H
|