2011-09-23 22:06:21 +02:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2017-01-21 12:23:24 +01:00
|
|
|
* Copyright (C) 2010-2017 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
|
|
|
{
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
setFocusPolicy(Qt::ClickFocus);
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2014-04-19 13:10:20 +02:00
|
|
|
connect(this, SIGNAL(aboutToShowMenu()), 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();
|
|
|
|
}
|
|
|
|
|
2013-11-21 18:23:11 +01:00
|
|
|
void ButtonWithMenu::selectNextItem()
|
|
|
|
{
|
|
|
|
int index = m_items.indexOf(m_currentItem) + 1;
|
|
|
|
|
|
|
|
if (index < m_items.size()) {
|
|
|
|
setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonWithMenu::selectPreviousItem()
|
|
|
|
{
|
|
|
|
int index = m_items.indexOf(m_currentItem) - 1;
|
|
|
|
|
2014-02-20 17:06:21 +01:00
|
|
|
if (index >= 0) {
|
2013-11-21 18:23:11 +01:00
|
|
|
setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
void ButtonWithMenu::addItems(const QVector<Item> &items)
|
2011-06-06 19:38:37 +02:00
|
|
|
{
|
2013-03-06 09:05:41 +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
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
m_items.remove(index);
|
2011-06-06 19:38:37 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-21 18:23:11 +01:00
|
|
|
void ButtonWithMenu::setCurrentIndex(int index, bool emitSignal)
|
|
|
|
{
|
|
|
|
setCurrentItem(m_items.at(index), emitSignal);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void ButtonWithMenu::wheelEvent(QWheelEvent* event)
|
2011-06-06 19:38:37 +02:00
|
|
|
{
|
2017-01-21 12:23:24 +01:00
|
|
|
m_wheelHelper.processEvent(event);
|
|
|
|
while (WheelHelper::Direction direction = m_wheelHelper.takeDirection()) {
|
|
|
|
switch (direction) {
|
|
|
|
case WheelHelper::WheelUp:
|
|
|
|
case WheelHelper::WheelLeft:
|
|
|
|
selectPreviousItem();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WheelHelper::WheelDown:
|
|
|
|
case WheelHelper::WheelRight:
|
|
|
|
selectNextItem();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
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
|
|
|
|
2013-03-06 09:05:41 +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
|
|
|
}
|
|
|
|
|
2016-01-25 14:42:57 +01:00
|
|
|
void ButtonWithMenu::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (parentWidget() && parentWidget()->parentWidget()) {
|
|
|
|
emit aboutToShowMenu();
|
|
|
|
QWidget *w = parentWidget()->parentWidget();
|
|
|
|
m_menu->popup(w->mapToGlobal(w->rect().bottomLeft()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolButton::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
2011-06-06 19:38:37 +02:00
|
|
|
ButtonWithMenu::~ButtonWithMenu()
|
|
|
|
{
|
|
|
|
}
|