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-09-11 19:15:06 +02:00
|
|
|
#include "toolbutton.h"
|
|
|
|
|
2013-03-08 21:01:55 +01:00
|
|
|
#include <QMenu>
|
2014-04-19 13:10:20 +02:00
|
|
|
#include <QStyle>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QPainter>
|
2014-04-19 13:10:20 +02:00
|
|
|
#include <QMouseEvent>
|
2013-06-09 12:54:50 +02:00
|
|
|
#include <QApplication>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QStyleOptionToolButton>
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
ToolButton::ToolButton(QWidget* parent)
|
|
|
|
: QToolButton(parent)
|
2014-04-19 13:10:20 +02:00
|
|
|
, m_menu(0)
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2011-12-29 15:21:26 +01:00
|
|
|
setMinimumWidth(16);
|
2014-04-19 13:10:20 +02:00
|
|
|
|
|
|
|
QStyleOptionToolButton opt;
|
|
|
|
initStyleOption(&opt);
|
|
|
|
|
|
|
|
m_pressTimer.setSingleShot(true);
|
|
|
|
m_pressTimer.setInterval(QApplication::style()->styleHint(QStyle::SH_ToolButton_PopupDelay, &opt, this));
|
|
|
|
connect(&m_pressTimer, SIGNAL(timeout()), this, SLOT(showMenu()));
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2014-04-24 11:05:41 +02:00
|
|
|
QImage ToolButton::multiIcon() const
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2014-04-24 11:05:41 +02:00
|
|
|
return m_multiIcon;
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2014-04-24 11:05:41 +02:00
|
|
|
void ToolButton::setMultiIcon(const QImage &image)
|
2011-12-03 11:27:53 +01:00
|
|
|
{
|
2014-04-19 13:46:23 +02:00
|
|
|
m_options |= MultiIconOption;
|
2014-04-24 11:05:41 +02:00
|
|
|
m_multiIcon = image;
|
|
|
|
setFixedSize(m_multiIcon.width(), m_multiIcon.height() / 4);
|
2014-04-22 23:06:18 +02:00
|
|
|
|
|
|
|
update();
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2014-04-19 12:06:48 +02:00
|
|
|
QString ToolButton::themeIcon() const
|
2014-02-13 15:27:02 +01:00
|
|
|
{
|
2014-04-19 12:06:48 +02:00
|
|
|
return m_themeIcon;
|
2014-02-13 15:27:02 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 12:06:48 +02:00
|
|
|
void ToolButton::setThemeIcon(const QString &icon)
|
2013-03-08 21:01:55 +01:00
|
|
|
{
|
2014-04-19 12:06:48 +02:00
|
|
|
m_themeIcon = icon;
|
|
|
|
setIcon(QIcon::fromTheme(m_themeIcon));
|
2013-03-08 21:01:55 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 12:28:58 +01:00
|
|
|
QIcon ToolButton::fallbackIcon() const
|
|
|
|
{
|
|
|
|
return icon();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolButton::setFallbackIcon(const QIcon &fallbackIcon)
|
|
|
|
{
|
|
|
|
if (icon().isNull())
|
|
|
|
setIcon(fallbackIcon);
|
|
|
|
}
|
|
|
|
|
2014-04-19 13:10:20 +02:00
|
|
|
QIcon ToolButton::icon() const
|
|
|
|
{
|
2014-04-24 11:05:41 +02:00
|
|
|
return QToolButton::icon();
|
2014-04-19 13:10:20 +02:00
|
|
|
}
|
|
|
|
|
2014-04-19 12:06:48 +02:00
|
|
|
void ToolButton::setIcon(const QIcon &icon)
|
2013-03-08 21:01:55 +01:00
|
|
|
{
|
2014-04-19 13:46:23 +02:00
|
|
|
if (m_options & MultiIconOption)
|
2014-04-19 12:06:48 +02:00
|
|
|
setFixedSize(sizeHint());
|
2013-03-08 21:01:55 +01:00
|
|
|
|
2014-04-19 13:46:23 +02:00
|
|
|
m_options &= ~MultiIconOption;
|
2014-04-19 12:06:48 +02:00
|
|
|
QToolButton::setIcon(icon);
|
2013-11-26 14:14:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 13:10:20 +02:00
|
|
|
QMenu* ToolButton::menu() const
|
|
|
|
{
|
|
|
|
return m_menu;
|
|
|
|
}
|
|
|
|
|
2014-04-19 12:06:48 +02:00
|
|
|
void ToolButton::setMenu(QMenu* menu)
|
2013-11-26 14:14:36 +01:00
|
|
|
{
|
2014-04-19 12:06:48 +02:00
|
|
|
Q_ASSERT(menu);
|
2013-11-26 14:14:36 +01:00
|
|
|
|
2014-04-19 13:10:20 +02:00
|
|
|
if (m_menu)
|
|
|
|
disconnect(m_menu, SIGNAL(aboutToHide()), this, SLOT(menuAboutToHide()));
|
2014-04-19 12:06:48 +02:00
|
|
|
|
2014-04-19 13:10:20 +02:00
|
|
|
m_menu = menu;
|
|
|
|
connect(m_menu, SIGNAL(aboutToHide()), this, SLOT(menuAboutToHide()));
|
2013-11-26 14:14:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 12:06:48 +02:00
|
|
|
bool ToolButton::showMenuInside() const
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2014-04-19 13:46:23 +02:00
|
|
|
return m_options & ShowMenuInsideOption;
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2014-04-19 13:46:23 +02:00
|
|
|
void ToolButton::setShowMenuInside(bool enable)
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2014-04-19 13:46:23 +02:00
|
|
|
if (enable)
|
|
|
|
m_options |= ShowMenuInsideOption;
|
|
|
|
else
|
|
|
|
m_options &= ~ShowMenuInsideOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolButton::toolbarButtonLook() const
|
|
|
|
{
|
|
|
|
return m_options & ToolBarLookOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolButton::setToolbarButtonLook(bool enable)
|
|
|
|
{
|
|
|
|
if (enable) {
|
|
|
|
m_options |= ToolBarLookOption;
|
|
|
|
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.initFrom(this);
|
|
|
|
int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize, &opt, this);
|
|
|
|
setIconSize(QSize(size, size));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_options &= ~ToolBarLookOption;
|
|
|
|
}
|
2014-04-20 09:32:35 +02:00
|
|
|
|
|
|
|
setProperty("toolbar-look", QVariant(enable));
|
|
|
|
style()->unpolish(this);
|
|
|
|
style()->polish(this);
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2014-04-19 12:06:48 +02:00
|
|
|
void ToolButton::menuAboutToHide()
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2014-04-19 12:06:48 +02:00
|
|
|
setDown(false);
|
2014-04-19 13:10:20 +02:00
|
|
|
emit aboutToHideMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolButton::showMenu()
|
|
|
|
{
|
2014-04-19 18:50:44 +02:00
|
|
|
if (!m_menu || m_menu->isVisible())
|
2014-04-19 13:10:20 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
emit aboutToShowMenu();
|
|
|
|
|
|
|
|
QPoint pos;
|
|
|
|
|
2014-04-19 13:46:23 +02:00
|
|
|
if (m_options & ShowMenuInsideOption) {
|
2014-04-19 13:10:20 +02:00
|
|
|
pos = mapToGlobal(rect().bottomRight());
|
|
|
|
if (QApplication::layoutDirection() == Qt::RightToLeft)
|
|
|
|
pos.setX(pos.x() - rect().width());
|
|
|
|
else
|
|
|
|
pos.setX(pos.x() - m_menu->sizeHint().width());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pos = mapToGlobal(rect().bottomLeft());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_menu->popup(pos);
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void ToolButton::mousePressEvent(QMouseEvent* e)
|
2011-10-18 21:07:58 +02:00
|
|
|
{
|
2014-04-19 13:10:20 +02:00
|
|
|
if (popupMode() == QToolButton::DelayedPopup)
|
|
|
|
m_pressTimer.start();
|
|
|
|
|
2014-03-01 14:12:50 +01:00
|
|
|
if (e->buttons() == Qt::LeftButton && menu() && popupMode() == QToolButton::InstantPopup) {
|
2011-10-26 19:23:50 +02:00
|
|
|
setDown(true);
|
|
|
|
showMenu();
|
2011-10-18 21:07:58 +02:00
|
|
|
}
|
2014-04-19 13:10:20 +02:00
|
|
|
else if (e->buttons() == Qt::RightButton && menu()) {
|
2013-03-12 12:19:55 +01:00
|
|
|
setDown(true);
|
|
|
|
showMenu();
|
2016-08-17 09:49:48 +02:00
|
|
|
} else {
|
|
|
|
QToolButton::mousePressEvent(e);
|
2013-03-12 12:19:55 +01:00
|
|
|
}
|
2011-10-18 21:07:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 15:17:21 +01:00
|
|
|
void ToolButton::mouseReleaseEvent(QMouseEvent* e)
|
|
|
|
{
|
2014-04-19 13:10:20 +02:00
|
|
|
m_pressTimer.stop();
|
|
|
|
|
2011-12-08 21:52:03 +01:00
|
|
|
if (e->button() == Qt::MiddleButton && rect().contains(e->pos())) {
|
2011-12-07 15:17:21 +01:00
|
|
|
emit middleMouseClicked();
|
2012-01-09 22:17:05 +01:00
|
|
|
setDown(false);
|
2011-12-07 15:17:21 +01:00
|
|
|
}
|
2014-04-19 13:10:20 +02:00
|
|
|
else if (e->button() == Qt::LeftButton && rect().contains(e->pos()) && e->modifiers() == Qt::ControlModifier) {
|
2011-12-07 15:17:21 +01:00
|
|
|
emit controlClicked();
|
2012-01-09 22:17:05 +01:00
|
|
|
setDown(false);
|
2016-08-17 09:49:48 +02:00
|
|
|
} else {
|
|
|
|
QToolButton::mouseReleaseEvent(e);
|
2011-12-07 15:17:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 00:57:01 +01:00
|
|
|
void ToolButton::mouseDoubleClickEvent(QMouseEvent* e)
|
|
|
|
{
|
|
|
|
QToolButton::mouseDoubleClickEvent(e);
|
|
|
|
|
2014-04-19 13:10:20 +02:00
|
|
|
m_pressTimer.stop();
|
|
|
|
|
2014-03-01 14:12:50 +01:00
|
|
|
if (e->buttons() == Qt::LeftButton) {
|
2014-01-01 15:42:13 +01:00
|
|
|
emit doubleClicked();
|
|
|
|
}
|
2013-12-24 00:57:01 +01:00
|
|
|
}
|
|
|
|
|
2015-10-05 22:41:20 +02:00
|
|
|
void ToolButton::contextMenuEvent(QContextMenuEvent *e)
|
|
|
|
{
|
|
|
|
// Block to prevent showing both context menu and button menu
|
|
|
|
if (menu())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QToolButton::contextMenuEvent(e);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void ToolButton::paintEvent(QPaintEvent* e)
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2014-04-19 13:46:23 +02:00
|
|
|
if (!(m_options & MultiIconOption)) {
|
2011-09-11 19:15:06 +02:00
|
|
|
QToolButton::paintEvent(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainter p(this);
|
|
|
|
|
2014-04-24 11:05:41 +02:00
|
|
|
const int w = m_multiIcon.width();
|
|
|
|
const int h4 = m_multiIcon.height() / 4;
|
|
|
|
|
2014-04-19 12:06:48 +02:00
|
|
|
if (!isEnabled())
|
2014-04-24 11:05:41 +02:00
|
|
|
p.drawImage(0, 0, m_multiIcon, 0, h4 * 3, w, h4);
|
2014-04-19 12:06:48 +02:00
|
|
|
else if (isDown())
|
2014-04-24 11:05:41 +02:00
|
|
|
p.drawImage(0, 0, m_multiIcon, 0, h4 * 2, w, h4);
|
2014-04-19 12:06:48 +02:00
|
|
|
else if (underMouse())
|
2014-04-24 11:05:41 +02:00
|
|
|
p.drawImage(0, 0, m_multiIcon, 0, h4 * 1, w, h4);
|
2014-04-19 12:06:48 +02:00
|
|
|
else
|
2014-04-24 11:05:41 +02:00
|
|
|
p.drawImage(0, 0, m_multiIcon, 0, h4 * 0, w, h4);
|
2014-04-19 12:06:48 +02:00
|
|
|
}
|