move toolbutton to new animation api

svn path=/trunk/KDE/kdelibs/; revision=1077756
This commit is contained in:
Igor Trindade Oliveira 2010-01-20 20:27:41 +00:00
parent 874a2b1a44
commit b6a0c0775c
2 changed files with 38 additions and 25 deletions

View File

@ -24,6 +24,7 @@
#include <QDir>
#include <QToolButton>
#include <QApplication>
#include <QPropertyAnimation>
#include <kicon.h>
#include <kiconeffect.h>
@ -47,8 +48,6 @@ public:
: ActionWidgetInterface<ToolButton>(toolButton),
q(toolButton),
background(0),
animId(0),
fadeIn(false),
svg(0),
customFont(false),
underMouse(false)
@ -109,8 +108,7 @@ public:
ToolButton *q;
FrameSvg *background;
int animId;
bool fadeIn;
QPropertyAnimation *animation;
qreal opacity;
QRectF activeRect;
@ -155,12 +153,7 @@ void ToolButtonPrivate::syncBorders()
void ToolButtonPrivate::animationUpdate(qreal progress)
{
if (progress == 1) {
animId = 0;
fadeIn = true;
}
opacity = fadeIn ? progress : 1 - progress;
opacity = progress;
// explicit update
q->update();
@ -186,13 +179,28 @@ ToolButton::ToolButton(QGraphicsWidget *parent)
d->syncBorders();
setAcceptHoverEvents(true);
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
d->animation = new QPropertyAnimation(this, "animationUpdate");
d->animation->setStartValue(0);
d->animation->setEndValue(1);
}
ToolButton::~ToolButton()
{
delete d->animation;
delete d;
}
void ToolButton::setAnimationUpdate(qreal progress)
{
d->animationUpdate(progress);
}
qreal ToolButton::animationUpdate() const
{
return d->opacity;
}
void ToolButton::setAction(QAction *action)
{
d->setAction(action);
@ -339,8 +347,9 @@ void ToolButton::paint(QPainter *painter,
buttonOpt.iconSize = button->iconSize();
buttonOpt.toolButtonStyle = button->toolButtonStyle();
if (button->isEnabled() && (d->animId || !button->autoRaise() || d->underMouse || (buttonOpt.state & QStyle::State_On) || button->isChecked() || button->isDown())) {
bool animationState = (d->animation->state() == QAbstractAnimation::Running)? \
1:0;
if (button->isEnabled() && (animationState || !button->autoRaise() || d->underMouse || (buttonOpt.state & QStyle::State_On) || button->isChecked() || button->isDown())) {
if (button->isDown() || (buttonOpt.state & QStyle::State_On) || button->isChecked()) {
d->background->setElementPrefix("pressed");
} else {
@ -348,7 +357,7 @@ void ToolButton::paint(QPainter *painter,
}
d->background->resizeFrame(size());
if (d->animId) {
if (animationState) {
QPixmap buffer = d->background->framePixmap();
QPainter bufferPainter(&buffer);
@ -391,12 +400,12 @@ void ToolButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
const int FadeInDuration = 75;
if (d->animId) {
Plasma::Animator::self()->stopCustomAnimation(d->animId);
if (d->animation->state() != QAbstractAnimation::Stopped) {
d->animation->stop();
}
d->animId = Plasma::Animator::self()->customAnimation(
40 / (1000 / FadeInDuration), FadeInDuration,
Plasma::Animator::LinearCurve, this, "animationUpdate");
d->animation->setDuration(FadeInDuration);
d->animation->setDirection(QAbstractAnimation::Forward);
d->animation->start();
d->background->setElementPrefix("active");
@ -412,14 +421,13 @@ void ToolButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
const int FadeOutDuration = 150;
if (d->animId) {
Plasma::Animator::self()->stopCustomAnimation(d->animId);
if (d->animation->state() != QAbstractAnimation::Stopped) {
d->animation->stop();
}
d->fadeIn = false;
d->animId = Plasma::Animator::self()->customAnimation(
40 / (1000 / FadeOutDuration), FadeOutDuration,
Plasma::Animator::LinearCurve, this, "animationUpdate");
d->animation->setDuration(FadeOutDuration);
d->animation->setDirection(QAbstractAnimation::Backward);
d->animation->start();
d->background->setElementPrefix("active");

View File

@ -49,6 +49,8 @@ class PLASMA_EXPORT ToolButton : public QGraphicsProxyWidget
Q_PROPERTY(QAction *action READ action WRITE setAction)
Q_PROPERTY(bool down READ isDown WRITE setDown)
Q_PROPERTY(qreal animationUpdate READ animationUpdate WRITE setAnimationUpdate)
public:
explicit ToolButton(QGraphicsWidget *parent = 0);
~ToolButton();
@ -183,9 +185,12 @@ protected:
void changeEvent(QEvent *event);
QSizeF sizeHint(Qt::SizeHint which, const QSizeF & constraint) const;
private slots:
void setAnimationUpdate(qreal progress);
qreal animationUpdate() const;
private:
Q_PRIVATE_SLOT(d, void syncBorders())
Q_PRIVATE_SLOT(d, void animationUpdate(qreal progress))
Q_PRIVATE_SLOT(d, void syncToAction())
Q_PRIVATE_SLOT(d, void clearAction())
Q_PRIVATE_SLOT(d, void setPixmap())