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-09-11 19:15:06 +02:00
|
|
|
#include "toolbutton.h"
|
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOptionToolButton>
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
ToolButton::ToolButton(QWidget* parent)
|
|
|
|
: QToolButton(parent)
|
|
|
|
, m_usingMultiIcon(false)
|
|
|
|
{
|
2011-12-29 15:21:26 +01:00
|
|
|
setMinimumWidth(16);
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-03 11:27:53 +01:00
|
|
|
void ToolButton::setThemeIcon(const QString &image)
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2011-12-03 11:27:53 +01:00
|
|
|
m_themeIcon = image;
|
|
|
|
setIcon(QIcon::fromTheme(image));
|
2011-09-11 19:15:06 +02:00
|
|
|
m_usingMultiIcon = false;
|
|
|
|
}
|
|
|
|
|
2011-12-03 11:27:53 +01:00
|
|
|
void ToolButton::setFallbackIcon(const QIcon &image)
|
|
|
|
{
|
|
|
|
if (icon().isNull()) {
|
|
|
|
setIcon(image);
|
|
|
|
m_usingMultiIcon = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolButton::setIcon(const QIcon &image)
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_usingMultiIcon) {
|
2011-09-11 19:15:06 +02:00
|
|
|
setFixedSize(sizeHint());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-11 19:15:06 +02:00
|
|
|
m_usingMultiIcon = false;
|
|
|
|
|
2011-12-03 11:27:53 +01:00
|
|
|
QToolButton::setIcon(image);
|
2011-09-11 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolButton::setData(const QVariant &data)
|
|
|
|
{
|
|
|
|
m_data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ToolButton::data()
|
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
2011-12-03 11:27:53 +01:00
|
|
|
void ToolButton::setMultiIcon(const QPixmap &image)
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2011-12-03 11:27:53 +01:00
|
|
|
int w = image.width();
|
|
|
|
int h = image.height();
|
2011-09-11 19:15:06 +02:00
|
|
|
|
2011-12-03 11:27:53 +01:00
|
|
|
m_normalIcon = image.copy(0, 0, w, h / 4);
|
|
|
|
m_hoverIcon = image.copy(0, h / 4, w, h / 4);
|
|
|
|
m_activeIcon = image.copy(0, h / 2, w, h / 4);
|
|
|
|
m_disabledIcon = image.copy(0, 3 * h / 4, w, h / 4);
|
2011-09-11 19:15:06 +02:00
|
|
|
|
|
|
|
m_usingMultiIcon = true;
|
|
|
|
|
|
|
|
setFixedSize(m_normalIcon.size());
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void ToolButton::mousePressEvent(QMouseEvent* e)
|
2011-10-18 21:07:58 +02:00
|
|
|
{
|
2011-10-26 19:23:50 +02:00
|
|
|
if (e->button() == Qt::RightButton && menu()) {
|
|
|
|
setDown(true);
|
|
|
|
showMenu();
|
|
|
|
return;
|
2011-10-18 21:07:58 +02:00
|
|
|
}
|
|
|
|
|
2012-01-09 22:14:02 +01:00
|
|
|
if (e->button() == Qt::MiddleButton) {
|
|
|
|
setDown(true);
|
|
|
|
}
|
|
|
|
|
2011-10-18 21:07:58 +02:00
|
|
|
QToolButton::mousePressEvent(e);
|
|
|
|
}
|
|
|
|
|
2011-12-07 15:17:21 +01:00
|
|
|
void ToolButton::mouseReleaseEvent(QMouseEvent* e)
|
|
|
|
{
|
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
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e->button() == Qt::LeftButton && rect().contains(e->pos()) && e->modifiers() == Qt::ControlModifier) {
|
|
|
|
emit controlClicked();
|
2012-01-09 22:17:05 +01:00
|
|
|
setDown(false);
|
2011-12-07 15:17:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QToolButton::mouseReleaseEvent(e);
|
2012-01-09 22:17:05 +01:00
|
|
|
setDown(false);
|
2011-12-07 15:17:21 +01:00
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void ToolButton::paintEvent(QPaintEvent* e)
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
|
|
|
if (!m_usingMultiIcon) {
|
|
|
|
QToolButton::paintEvent(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainter p(this);
|
|
|
|
|
|
|
|
QStyleOptionToolButton opt;
|
|
|
|
opt.init(this);
|
|
|
|
|
|
|
|
if (!isEnabled()) {
|
|
|
|
p.drawPixmap(0, 0, m_disabledIcon);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDown()) {
|
|
|
|
p.drawPixmap(0, 0, m_activeIcon);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt.state & QStyle::State_MouseOver) {
|
|
|
|
p.drawPixmap(0, 0, m_hoverIcon);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.drawPixmap(0, 0, m_normalIcon);
|
|
|
|
}
|