move applethandle to new animation api.

svn path=/trunk/KDE/kdelibs/; revision=1077217
This commit is contained in:
Igor Trindade Oliveira 2010-01-19 17:59:24 +00:00
parent 1202423649
commit d7e63f903c
2 changed files with 51 additions and 26 deletions

View File

@ -29,6 +29,8 @@
#include <QTouchEvent> #include <QTouchEvent>
#include <QMatrix> #include <QMatrix>
#include <QTransform> #include <QTransform>
#include <QWeakPointer>
#include <QPropertyAnimation>
#include <kcolorscheme.h> #include <kcolorscheme.h>
#include <kglobalsettings.h> #include <kglobalsettings.h>
@ -63,8 +65,7 @@ AppletHandle::AppletHandle(Containment *parent, Applet *applet, const QPointF &h
m_applet(applet), m_applet(applet),
m_iconSize(KIconLoader::SizeSmall), m_iconSize(KIconLoader::SizeSmall),
m_opacity(0.0), m_opacity(0.0),
m_anim(FadeIn), m_animType(FadeIn),
m_animId(0),
m_backgroundBuffer(0), m_backgroundBuffer(0),
m_currentView(applet->view()), m_currentView(applet->view()),
m_entryPos(hoverPos), m_entryPos(hoverPos),
@ -187,7 +188,7 @@ void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
//kDebug() << m_opacity << m_anim << FadeOut; //kDebug() << m_opacity << m_anim << FadeOut;
if (qFuzzyCompare(m_opacity + 1.0, 1.0)) { if (qFuzzyCompare(m_opacity + 1.0, 1.0)) {
if (m_anim == FadeOut) { if (m_animType == FadeOut) {
//kDebug() << "WOOOOOOOOO"; //kDebug() << "WOOOOOOOOO";
QTimer::singleShot(0, this, SLOT(emitDisappear())); QTimer::singleShot(0, this, SLOT(emitDisappear()));
} }
@ -215,8 +216,14 @@ void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
int(m_decorationRect.height()) + m_iconSize * 5 + 1); int(m_decorationRect.height()) + m_iconSize * 5 + 1);
const QSize iconSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall); const QSize iconSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall);
bool isRunning = false;
if (m_anim.data()) {
isRunning = m_anim.data()->state() == QAbstractAnimation::Running ? \
true : false;
}
//regenerate our buffer? //regenerate our buffer?
if (m_animId > 0 || !m_backgroundBuffer || m_backgroundBuffer->size() != pixmapSize) { if (isRunning || !m_backgroundBuffer || m_backgroundBuffer->size() != pixmapSize) {
QColor transparencyColor = Qt::black; QColor transparencyColor = Qt::black;
transparencyColor.setAlphaF(qMin(m_opacity, qreal(0.99))); transparencyColor.setAlphaF(qMin(m_opacity, qreal(0.99)));
@ -802,7 +809,7 @@ void AppletHandle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
m_leaveTimer->stop(); m_leaveTimer->stop();
} }
// if we're already fading out, fade back in // if we're already fading out, fade back in
else if (m_animId != 0 && m_anim == FadeOut) { else if (!m_anim.data() && m_animType == FadeOut) {
startFading(FadeIn, m_entryPos, true); startFading(FadeIn, m_entryPos, true);
} }
} }
@ -846,18 +853,11 @@ bool AppletHandle::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
return false; return false;
} }
void AppletHandle::fadeAnimation(qreal progress) void AppletHandle::setFadeAnimation(qreal progress)
{ {
//qreal endOpacity = (m_anim == FadeIn) ? 1.0 : -1.0; m_opacity = progress;
if (m_anim == FadeIn) {
m_opacity = progress;
} else {
m_opacity = 1 - progress;
}
//kDebug() << "progress" << progress << "m_opacity" << m_opacity << m_anim << "(" << FadeIn << ")"; //kDebug() << "progress" << progress << "m_opacity" << m_opacity << m_anim << "(" << FadeIn << ")";
if (qFuzzyCompare(progress, qreal(1.0))) { if (qFuzzyCompare(progress, 1.0)) {
m_animId = 0;
delete m_backgroundBuffer; delete m_backgroundBuffer;
m_backgroundBuffer = 0; m_backgroundBuffer = 0;
} }
@ -865,6 +865,11 @@ void AppletHandle::fadeAnimation(qreal progress)
update(); update();
} }
qreal AppletHandle::fadeAnimation() const
{
return m_opacity;
}
void AppletHandle::hoverTimeout() void AppletHandle::hoverTimeout()
{ {
startFading(FadeIn, m_entryPos); startFading(FadeIn, m_entryPos);
@ -896,16 +901,23 @@ void AppletHandle::setHoverPos(const QPointF &hoverPos)
void AppletHandle::startFading(FadeType anim, const QPointF &hoverPos, bool preserveSide) void AppletHandle::startFading(FadeType anim, const QPointF &hoverPos, bool preserveSide)
{ {
if (m_animId != 0) { QPropertyAnimation *propAnim = m_anim.data();
Animator::self()->stopCustomAnimation(m_animId);
if (anim == FadeIn) {
if (propAnim) {
propAnim->stop();
} else {
propAnim = new QPropertyAnimation(this, "fadeAnimation", this);
m_anim = propAnim;
}
} }
m_entryPos = hoverPos; m_entryPos = hoverPos;
qreal time = 100; qreal time = 100;
if (!m_applet) { if (!m_applet) {
m_anim = FadeOut; m_animType = FadeOut;
fadeAnimation(1.0); setFadeAnimation(1.0);
return; return;
} }
@ -940,7 +952,7 @@ void AppletHandle::startFading(FadeType anim, const QPointF &hoverPos, bool pres
} }
if (wasOnRight != m_buttonsOnRight && if (wasOnRight != m_buttonsOnRight &&
m_anim == FadeIn && m_animType == FadeIn &&
anim == FadeIn && anim == FadeIn &&
m_opacity <= 1) { m_opacity <= 1) {
m_opacity = 0.0; m_opacity = 0.0;
@ -951,10 +963,19 @@ void AppletHandle::startFading(FadeType anim, const QPointF &hoverPos, bool pres
time *= m_opacity; time *= m_opacity;
} }
m_anim = anim; propAnim->setStartValue(0);
propAnim->setEndValue(1);
propAnim->setDuration(time);
m_animType = anim;
//kDebug() << "animating for " << time << "ms"; //kDebug() << "animating for " << time << "ms";
m_animId = Animator::self()->customAnimation(80 * (time / 1000.0), (int)time, if (m_animType == FadeIn) {
Animator::EaseInCurve, this, "fadeAnimation"); propAnim->setDirection(QAbstractAnimation::Forward);
propAnim->start();
} else {
propAnim->setDirection(QAbstractAnimation::Backward);
propAnim->start(QAbstractAnimation::DeleteWhenStopped);
}
} }
void AppletHandle::forceDisappear() void AppletHandle::forceDisappear()

View File

@ -23,6 +23,8 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtGui/QGraphicsObject> #include <QtGui/QGraphicsObject>
#include <QTimer> #include <QTimer>
#include <QWeakPointer>
#include <QPropertyAnimation>
#include "animator.h" #include "animator.h"
#include "svg.h" #include "svg.h"
@ -39,6 +41,7 @@ class View;
class AppletHandle : public QGraphicsObject class AppletHandle : public QGraphicsObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(qreal fadeAnimation READ fadeAnimation WRITE setFadeAnimation);
Q_INTERFACES(QGraphicsItem) Q_INTERFACES(QGraphicsItem)
public: public:
enum FadeType { enum FadeType {
@ -82,7 +85,8 @@ class AppletHandle : public QGraphicsObject
void disappearDone(AppletHandle *self); void disappearDone(AppletHandle *self);
private Q_SLOTS: private Q_SLOTS:
void fadeAnimation(qreal progress); void setFadeAnimation(qreal progress);
qreal fadeAnimation() const;
void appletDestroyed(); void appletDestroyed();
void appletResized(); void appletResized();
void hoverTimeout(); void hoverTimeout();
@ -113,8 +117,8 @@ class AppletHandle : public QGraphicsObject
Applet *m_applet; Applet *m_applet;
int m_iconSize; int m_iconSize;
qreal m_opacity; qreal m_opacity;
FadeType m_anim; FadeType m_animType;;
int m_animId; QWeakPointer<QPropertyAnimation> m_anim;
qreal m_angle; qreal m_angle;
QColor m_gradientColor; QColor m_gradientColor;
QTimer *m_hoverTimer; QTimer *m_hoverTimer;