use pixmaptransition animation

svn path=/trunk/KDE/kdelibs/; revision=1079688
This commit is contained in:
Marco Martin 2010-01-24 19:48:18 +00:00
parent 3403fea2ca
commit ee7be144b1
2 changed files with 34 additions and 59 deletions

View File

@ -39,6 +39,7 @@
#include "animator.h" #include "animator.h"
#include "paintutils.h" #include "paintutils.h"
#include "private/actionwidgetinterface_p.h" #include "private/actionwidgetinterface_p.h"
#include "animations/animation.h"
namespace Plasma namespace Plasma
{ {
@ -50,7 +51,6 @@ public:
: ActionWidgetInterface<PushButton>(pushButton), : ActionWidgetInterface<PushButton>(pushButton),
q(pushButton), q(pushButton),
background(0), background(0),
animId(-1),
fadeIn(false), fadeIn(false),
svg(0), svg(0),
customFont(false) customFont(false)
@ -106,22 +106,21 @@ public:
void syncActiveRect(); void syncActiveRect();
void syncBorders(); void syncBorders();
void animationUpdate(qreal progress);
PushButton *q; PushButton *q;
FrameSvg *background; FrameSvg *background;
int animId;
bool fadeIn; bool fadeIn;
qreal opacity; qreal opacity;
QRectF activeRect; QRectF activeRect;
Animation *hoverAnimation;
QString imagePath; QString imagePath;
QString absImagePath; QString absImagePath;
Svg *svg; Svg *svg;
QString svgElement; QString svgElement;
bool customFont; bool customFont;
QWeakPointer<QPropertyAnimation> anim;
}; };
void PushButtonPrivate::syncActiveRect() void PushButtonPrivate::syncActiveRect()
@ -155,18 +154,6 @@ void PushButtonPrivate::syncBorders()
syncActiveRect(); syncActiveRect();
} }
void PushButtonPrivate::animationUpdate(qreal progress)
{
if (progress == 1) {
animId = -1;
fadeIn = true;
}
opacity = fadeIn ? progress : 1 - progress;
// explicit update
q->update();
}
PushButton::PushButton(QGraphicsWidget *parent) PushButton::PushButton(QGraphicsWidget *parent)
: QGraphicsProxyWidget(parent), : QGraphicsProxyWidget(parent),
@ -177,6 +164,11 @@ PushButton::PushButton(QGraphicsWidget *parent)
d->background->setCacheAllRenderedFrames(true); d->background->setCacheAllRenderedFrames(true);
d->background->setElementPrefix("normal"); d->background->setElementPrefix("normal");
d->hoverAnimation = Animator::create(Animator::PixmapTransitionAnimation);
d->hoverAnimation->setTargetWidget(this);
d->background->setElementPrefix("normal");
d->hoverAnimation->setProperty("startPixmap", d->background->framePixmap());
KPushButton *native = new KPushButton; KPushButton *native = new KPushButton;
connect(native, SIGNAL(pressed()), this, SIGNAL(pressed())); connect(native, SIGNAL(pressed()), this, SIGNAL(pressed()));
connect(native, SIGNAL(released()), this, SIGNAL(released())); connect(native, SIGNAL(released()), this, SIGNAL(released()));
@ -325,11 +317,13 @@ void PushButton::resizeEvent(QGraphicsSceneResizeEvent *event)
d->background->setElementPrefix("active"); d->background->setElementPrefix("active");
d->background->resizeFrame(d->activeRect.size()); d->background->resizeFrame(d->activeRect.size());
d->hoverAnimation->setProperty("targetPixmap", d->background->framePixmap());
d->background->setElementPrefix("focus"); d->background->setElementPrefix("focus");
d->background->resizeFrame(d->activeRect.size()); d->background->resizeFrame(d->activeRect.size());
d->background->setElementPrefix("normal"); d->background->setElementPrefix("normal");
d->background->resizeFrame(size()); d->background->resizeFrame(size());
d->hoverAnimation->setProperty("startPixmap", d->background->framePixmap());
} }
QGraphicsProxyWidget::resizeEvent(event); QGraphicsProxyWidget::resizeEvent(event);
@ -353,9 +347,7 @@ void PushButton::paint(QPainter *painter,
} else { } else {
d->background->setElementPrefix("normal"); d->background->setElementPrefix("normal");
} }
if (d->animId == -1) {
d->background->paintFrame(painter);
}
//flat or disabled //flat or disabled
} else if (!isEnabled() || nativeWidget()->isFlat()) { } else if (!isEnabled() || nativeWidget()->isFlat()) {
bufferPixmap = QPixmap(rect().size().toSize()); bufferPixmap = QPixmap(rect().size().toSize());
@ -369,17 +361,16 @@ void PushButton::paint(QPainter *painter,
painter->drawPixmap(0, 0, bufferPixmap); painter->drawPixmap(0, 0, bufferPixmap);
} }
//if is under mouse draw the animated glow overlay //if is under mouse draw the animated glow overlay
if (!nativeWidget()->isDown() && !nativeWidget()->isChecked() && isEnabled() && acceptHoverEvents()) { if (!nativeWidget()->isDown() && !nativeWidget()->isChecked() && isEnabled() && acceptHoverEvents()) {
if (d->animId != -1) { if (d->hoverAnimation->state() == QAbstractAnimation::Running && !isUnderMouse() && !nativeWidget()->isDefault()) {
QPixmap normalPix = d->background->framePixmap();
d->background->setElementPrefix("active");
painter->drawPixmap(
d->activeRect.topLeft(),
PaintUtils::transition(d->background->framePixmap(), normalPix, 1 - d->opacity));
} else if (isUnderMouse() || nativeWidget()->isDefault()) {
d->background->setElementPrefix("active"); d->background->setElementPrefix("active");
d->background->paintFrame(painter, d->activeRect.topLeft()); d->background->paintFrame(painter, d->activeRect.topLeft());
} else {
painter->drawPixmap(
d->activeRect.topLeft(),
d->hoverAnimation->property("currentPixmap").value<QPixmap>());
} }
} }
@ -469,24 +460,15 @@ void PushButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
return; return;
} }
const int FadeInDuration = 75; d->hoverAnimation->setProperty("duration", 75);
d->background->setElementPrefix("normal");
d->hoverAnimation->setProperty("startPixmap", d->background->framePixmap());
QPropertyAnimation *anim = d->anim.data();
if (anim && (anim->direction() != QAbstractAnimation::Forward)) {
anim->stop();
anim = new QPropertyAnimation(this, "animationUpdate", this);
} else if (!anim) {
anim = new QPropertyAnimation(this, "animationUpdate", this);
}
d->anim = anim;
anim->setDuration(FadeInDuration);
anim->setStartValue(0);
anim->setEndValue(1);
anim->start();
d->background->setElementPrefix("active"); d->background->setElementPrefix("active");
d->hoverAnimation->setProperty("targetPixmap", d->background->framePixmap());
d->hoverAnimation->start();
QGraphicsProxyWidget::hoverEnterEvent(event); QGraphicsProxyWidget::hoverEnterEvent(event);
} }
@ -502,30 +484,24 @@ void PushButton::changeEvent(QEvent *event)
void PushButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void PushButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{ {
QPropertyAnimation *anim = d->anim.data(); if (nativeWidget()->isDown()) {
if (nativeWidget()->isDown() || !anim) {
return; return;
} }
const int FadeOutDuration = 150; d->hoverAnimation->setProperty("duration", 150);
anim->setDuration(FadeOutDuration);
anim->setDirection(QAbstractAnimation::Backward);
anim->start(QAbstractAnimation::DeleteWhenStopped);
d->background->setElementPrefix("active"); d->background->setElementPrefix("active");
d->hoverAnimation->setProperty("startPixmap", d->background->framePixmap());
d->background->setElementPrefix("normal");
d->hoverAnimation->setProperty("targetPixmap", d->background->framePixmap());
d->hoverAnimation->start();
QGraphicsProxyWidget::hoverLeaveEvent(event); QGraphicsProxyWidget::hoverLeaveEvent(event);
} }
void PushButton::setAnimationUpdate(qreal progress)
{
d->animationUpdate(progress);
}
qreal PushButton::animationUpdate() const
{
return d->opacity;
}
QSizeF PushButton::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const QSizeF PushButton::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
{ {

View File

@ -227,7 +227,6 @@ private:
private: private:
Q_PRIVATE_SLOT(d, void syncBorders()) Q_PRIVATE_SLOT(d, void syncBorders())
Q_PRIVATE_SLOT(d, void animationUpdate(qreal progress))
Q_PRIVATE_SLOT(d, void setPixmap()) Q_PRIVATE_SLOT(d, void setPixmap())
friend class PushButtonPrivate; friend class PushButtonPrivate;