2011-09-23 22:06:21 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 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
|
|
|
#include "buttonwithmenu.h"
|
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QWheelEvent>
|
|
|
|
|
2011-12-12 21:14:43 +01:00
|
|
|
ButtonWithMenu::ButtonWithMenu(QWidget* parent)
|
|
|
|
: ToolButton(parent)
|
2011-11-06 17:01:23 +01:00
|
|
|
, m_menu(new QMenu(this))
|
2011-06-06 19:38:37 +02:00
|
|
|
{
|
|
|
|
setPopupMode(QToolButton::InstantPopup);
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
setFocusPolicy(Qt::ClickFocus);
|
|
|
|
setMenu(m_menu);
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
connect(m_menu, SIGNAL(aboutToShow()), this, SLOT(generateMenu()));
|
2011-06-06 19:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonWithMenu::setCurrentItem()
|
|
|
|
{
|
|
|
|
if (QAction* action = qobject_cast<QAction*>(sender())) {
|
2011-10-21 23:26:34 +02:00
|
|
|
setCurrentItem(action->data().value<Item>());
|
2011-06-06 19:38:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
void ButtonWithMenu::clearItems()
|
|
|
|
{
|
|
|
|
m_menu->clear();
|
|
|
|
m_items.clear();
|
|
|
|
}
|
|
|
|
|
2011-06-06 19:38:37 +02:00
|
|
|
void ButtonWithMenu::addItem(const Item &item)
|
|
|
|
{
|
|
|
|
m_items.append(item);
|
|
|
|
|
2012-02-28 18:57:05 +01:00
|
|
|
if (m_items.count() == 1) {
|
2011-10-21 23:26:34 +02:00
|
|
|
setCurrentItem(item);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-06-06 19:38:37 +02:00
|
|
|
|
|
|
|
emit itemAdded(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonWithMenu::addItems(const QList<Item> &items)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(const Item & item, items) {
|
2011-06-06 19:38:37 +02:00
|
|
|
addItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonWithMenu::removeItem(const Item &item)
|
|
|
|
{
|
|
|
|
int index = m_items.indexOf(item);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (index < 0) {
|
2011-06-06 19:38:37 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-06-06 19:38:37 +02:00
|
|
|
|
|
|
|
m_items.removeOne(item);
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
if (m_items.count() == 0) {
|
|
|
|
setIcon(QIcon());
|
|
|
|
return;
|
|
|
|
}
|
2011-06-06 19:38:37 +02:00
|
|
|
|
2012-02-28 18:57:05 +01:00
|
|
|
if (m_currentItem == item) {
|
2012-03-28 16:42:50 +02:00
|
|
|
setCurrentItem(m_items.at(0));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-06-06 19:38:37 +02:00
|
|
|
}
|
|
|
|
|
2012-02-28 18:57:05 +01:00
|
|
|
void ButtonWithMenu::setCurrentItem(const Item &item, bool emitSignal)
|
2011-06-06 19:38:37 +02:00
|
|
|
{
|
|
|
|
int index = m_items.indexOf(item);
|
2012-02-28 18:57:05 +01:00
|
|
|
if (index < 0 || m_currentItem == item) {
|
2011-06-06 19:38:37 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-06-06 19:38:37 +02:00
|
|
|
|
2012-02-28 18:57:05 +01:00
|
|
|
m_currentItem = item;
|
2011-06-06 19:38:37 +02:00
|
|
|
|
2012-02-28 18:57:05 +01:00
|
|
|
setIcon(m_currentItem.icon);
|
|
|
|
setToolTip(m_currentItem.text);
|
2011-06-06 19:38:37 +02:00
|
|
|
|
2012-02-28 18:57:05 +01:00
|
|
|
if (emitSignal) {
|
|
|
|
emit activeItemChanged(m_currentItem);
|
|
|
|
}
|
2011-06-06 19:38:37 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void ButtonWithMenu::wheelEvent(QWheelEvent* event)
|
2011-06-06 19:38:37 +02:00
|
|
|
{
|
2012-02-28 18:57:05 +01:00
|
|
|
int currItemIndex = m_items.indexOf(m_currentItem);
|
2011-06-06 19:38:37 +02:00
|
|
|
int itemsCount = m_items.count();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (itemsCount == 0) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-07-13 18:00:03 +02:00
|
|
|
if (event->delta() > 0) {
|
2011-11-06 17:01:23 +01:00
|
|
|
if (currItemIndex != 0) {
|
2011-10-21 23:26:34 +02:00
|
|
|
setCurrentItem(m_items.at(currItemIndex - 1));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (currItemIndex < itemsCount - 1) {
|
2011-11-06 19:46:03 +01:00
|
|
|
setCurrentItem(m_items.at(currItemIndex + 1));
|
2011-06-06 19:38:37 +02:00
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
|
2011-06-06 19:38:37 +02:00
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
2012-02-28 18:57:05 +01:00
|
|
|
ButtonWithMenu::Item ButtonWithMenu::currentItem()
|
2011-06-06 19:38:37 +02:00
|
|
|
{
|
|
|
|
return m_currentItem;
|
|
|
|
}
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
QMenu* ButtonWithMenu::menu() const
|
|
|
|
{
|
|
|
|
return m_menu;
|
|
|
|
}
|
|
|
|
|
2011-06-06 19:38:37 +02:00
|
|
|
void ButtonWithMenu::generateMenu()
|
|
|
|
{
|
|
|
|
m_menu->clear();
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const Item & item, m_items) {
|
2011-10-21 23:26:34 +02:00
|
|
|
QVariant variant;
|
|
|
|
variant.setValue<Item>(item);
|
|
|
|
m_menu->addAction(item.icon, item.text, this, SLOT(setCurrentItem()))->setData(variant);
|
|
|
|
}
|
2011-06-06 19:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ButtonWithMenu::~ButtonWithMenu()
|
|
|
|
{
|
|
|
|
}
|