From 2b40ed698ab9bda6a13fed6ad3ae531c365b3241 Mon Sep 17 00:00:00 2001 From: Jens Bache-Wiig Date: Thu, 11 Mar 2010 10:56:03 +0100 Subject: [PATCH] Add animation to detail widgets --- src/libs/utils/detailsbutton.cpp | 38 +++++++++++++++++++++++++++++++- src/libs/utils/detailsbutton.h | 7 ++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/libs/utils/detailsbutton.cpp b/src/libs/utils/detailsbutton.cpp index de6752acb04..cd038d09f93 100644 --- a/src/libs/utils/detailsbutton.cpp +++ b/src/libs/utils/detailsbutton.cpp @@ -32,12 +32,13 @@ #include #include #include +#include #include using namespace Utils; -DetailsButton::DetailsButton(QWidget *parent) : QAbstractButton(parent) +DetailsButton::DetailsButton(QWidget *parent) : QAbstractButton(parent), m_fader(0) { setCheckable(true); setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); @@ -49,12 +50,42 @@ QSize DetailsButton::sizeHint() const return QSize(80, 22); } +bool DetailsButton::event(QEvent *e) +{ + switch(e->type()) { + case QEvent::Enter: + { + QPropertyAnimation *animation = new QPropertyAnimation(this, "fader"); + animation->setDuration(200); + animation->setEndValue(1.0); + animation->start(QAbstractAnimation::DeleteWhenStopped); + } + break; + case QEvent::Leave: + { + QPropertyAnimation *animation = new QPropertyAnimation(this, "fader"); + animation->setDuration(200); + animation->setEndValue(0.0); + animation->start(QAbstractAnimation::DeleteWhenStopped); + } + break; + default: + return QAbstractButton::event(e); + } + return false; +} void DetailsButton::paintEvent(QPaintEvent *e) { QWidget::paintEvent(e); QPainter p(this); +#ifndef Q_WS_MAC + // draw hover animation + if(!isDown() && m_fader>0) + p.fillRect(rect().adjusted(1, 1, -2, -2), QColor(255, 255, 255, int(m_fader*180))); +#endif + if (isChecked()) { if (m_checkedPixmap.isNull() || m_checkedPixmap.size() != contentsRect().size()) m_checkedPixmap = cacheRendering(contentsRect().size(), true); @@ -64,6 +95,11 @@ void DetailsButton::paintEvent(QPaintEvent *e) m_uncheckedPixmap = cacheRendering(contentsRect().size(), false); p.drawPixmap(contentsRect(), m_uncheckedPixmap); } + if (isDown()) { + p.setPen(Qt::NoPen); + p.setBrush(QColor(0, 0, 0, 20)); + p.drawRoundedRect(rect().adjusted(1, 1, -1, -1), 1, 1); + } } QPixmap DetailsButton::cacheRendering(const QSize &size, bool checked) diff --git a/src/libs/utils/detailsbutton.h b/src/libs/utils/detailsbutton.h index af09641d403..f72f9840534 100644 --- a/src/libs/utils/detailsbutton.h +++ b/src/libs/utils/detailsbutton.h @@ -40,18 +40,25 @@ namespace Utils { class QTCREATOR_UTILS_EXPORT DetailsButton : public QAbstractButton { Q_OBJECT + + Q_PROPERTY(float fader READ fader WRITE setFader) + public: DetailsButton(QWidget *parent = 0); QSize sizeHint() const; + float fader() { return m_fader; } + void setFader(float value) { m_fader = value; update(); } protected: void paintEvent(QPaintEvent *e); + bool event(QEvent *e); private: QPixmap cacheRendering(const QSize &size, bool checked); QPixmap m_checkedPixmap; QPixmap m_uncheckedPixmap; + float m_fader; }; } #endif // DETAILSBUTTON_H