From 6bdc6f90cebb4f47302fba3e1532a3769c406616 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Thu, 24 Apr 2014 11:05:41 +0200 Subject: [PATCH] [ToolButton] Use QImage for multiIcon Instead of 4 separate QPixmaps, use just one QImage and paint only the current part (according to button state) of the image on button. --- src/lib/tools/toolbutton.cpp | 30 +++++++++++++----------------- src/lib/tools/toolbutton.h | 11 ++++------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/lib/tools/toolbutton.cpp b/src/lib/tools/toolbutton.cpp index 009e7d3ec..2ac983814 100644 --- a/src/lib/tools/toolbutton.cpp +++ b/src/lib/tools/toolbutton.cpp @@ -38,23 +38,16 @@ ToolButton::ToolButton(QWidget* parent) connect(&m_pressTimer, SIGNAL(timeout()), this, SLOT(showMenu())); } -QPixmap ToolButton::multiIcon() const +QImage ToolButton::multiIcon() const { - return m_normalIcon; + return m_multiIcon; } -void ToolButton::setMultiIcon(const QPixmap &icon) +void ToolButton::setMultiIcon(const QImage &image) { - int w = icon.width(); - int h = icon.height(); - - m_normalIcon = icon.copy(0, 0, w, h / 4); - m_hoverIcon = icon.copy(0, h / 4, w, h / 4); - m_activeIcon = icon.copy(0, h / 2, w, h / 4); - m_disabledIcon = icon.copy(0, 3 * h / 4, w, h / 4); - m_options |= MultiIconOption; - setFixedSize(m_normalIcon.size()); + m_multiIcon = image; + setFixedSize(m_multiIcon.width(), m_multiIcon.height() / 4); update(); } @@ -72,7 +65,7 @@ void ToolButton::setThemeIcon(const QString &icon) QIcon ToolButton::icon() const { - return m_options & MultiIconOption ? multiIcon() : QToolButton::icon(); + return QToolButton::icon(); } void ToolButton::setIcon(const QIcon &icon) @@ -219,12 +212,15 @@ void ToolButton::paintEvent(QPaintEvent* e) QPainter p(this); + const int w = m_multiIcon.width(); + const int h4 = m_multiIcon.height() / 4; + if (!isEnabled()) - p.drawPixmap(0, 0, m_disabledIcon); + p.drawImage(0, 0, m_multiIcon, 0, h4 * 3, w, h4); else if (isDown()) - p.drawPixmap(0, 0, m_activeIcon); + p.drawImage(0, 0, m_multiIcon, 0, h4 * 2, w, h4); else if (underMouse()) - p.drawPixmap(0, 0, m_hoverIcon); + p.drawImage(0, 0, m_multiIcon, 0, h4 * 1, w, h4); else - p.drawPixmap(0, 0, m_normalIcon); + p.drawImage(0, 0, m_multiIcon, 0, h4 * 0, w, h4); } diff --git a/src/lib/tools/toolbutton.h b/src/lib/tools/toolbutton.h index 1aa62b027..07fabd058 100644 --- a/src/lib/tools/toolbutton.h +++ b/src/lib/tools/toolbutton.h @@ -30,7 +30,7 @@ class QUPZILLA_EXPORT ToolButton : public QToolButton Q_PROPERTY(QSize fixedsize READ size WRITE setFixedSize) Q_PROPERTY(int fixedwidth READ width WRITE setFixedWidth) Q_PROPERTY(int fixedheight READ height WRITE setFixedHeight) - Q_PROPERTY(QPixmap multiIcon READ multiIcon WRITE setMultiIcon) + Q_PROPERTY(QImage multiIcon READ multiIcon WRITE setMultiIcon) Q_PROPERTY(QIcon icon READ icon WRITE setIcon) Q_PROPERTY(QString themeIcon READ themeIcon WRITE setThemeIcon) @@ -38,8 +38,8 @@ public: explicit ToolButton(QWidget* parent = 0); // MultiIcon - Image containing pixmaps for all button states - QPixmap multiIcon() const; - void setMultiIcon(const QPixmap &icon); + QImage multiIcon() const; + void setMultiIcon(const QImage &image); // ThemeIcon - Standard QToolButton with theme icon QString themeIcon() const; @@ -84,10 +84,7 @@ protected: void paintEvent(QPaintEvent* e); private: - QPixmap m_normalIcon; - QPixmap m_hoverIcon; - QPixmap m_activeIcon; - QPixmap m_disabledIcon; + QImage m_multiIcon; QString m_themeIcon; QTimer m_pressTimer; QMenu* m_menu;