Add animation to detail widgets

This commit is contained in:
Jens Bache-Wiig
2010-03-11 10:56:03 +01:00
parent 42ebc6b0f2
commit 2b40ed698a
2 changed files with 44 additions and 1 deletions

View File

@@ -32,12 +32,13 @@
#include <QtGui/QPaintEvent>
#include <QtGui/QPainter>
#include <QtGui/QStyleOption>
#include <QtCore/QPropertyAnimation>
#include <utils/stylehelper.h>
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)