make rotationstacked work as expected

svn path=/trunk/KDE/kdelibs/; revision=1059023
This commit is contained in:
Igor Trindade Oliveira 2009-12-05 14:54:06 +00:00
parent 5001706078
commit 2746410f5c
2 changed files with 34 additions and 63 deletions

View File

@ -37,6 +37,8 @@ class RotationStackedAnimationPrivate {
public: public:
QGraphicsRotation *backRotation; QGraphicsRotation *backRotation;
QGraphicsRotation *frontRotation; QGraphicsRotation *frontRotation;
int frontStartAngle, frontEndAngle;
int backStartAngle, backEndAngle;
qint8 reference; qint8 reference;
/** /**
@ -46,7 +48,6 @@ public:
QWeakPointer<QGraphicsWidget> backWidget; QWeakPointer<QGraphicsWidget> backWidget;
StackedLayout *sLayout; StackedLayout *sLayout;
QWeakPointer<QSequentialAnimationGroup> animation;
}; };
RotationStackedAnimation::RotationStackedAnimation(QObject *parent) RotationStackedAnimation::RotationStackedAnimation(QObject *parent)
@ -77,10 +78,6 @@ RotationStackedAnimation::~RotationStackedAnimation()
void RotationStackedAnimation::setWidgetToAnimate(QGraphicsWidget *widget) void RotationStackedAnimation::setWidgetToAnimate(QGraphicsWidget *widget)
{ {
Animation::setWidgetToAnimate(widget); Animation::setWidgetToAnimate(widget);
if (d->animation.data()) {
delete d->animation.data();
d->animation.clear();
}
} }
void RotationStackedAnimation::setReference(const qint8 &reference) void RotationStackedAnimation::setReference(const qint8 &reference)
@ -106,7 +103,7 @@ void RotationStackedAnimation::setBackWidget(QGraphicsWidget *backWidget)
d->sLayout->addWidget(widgetToAnimate()); d->sLayout->addWidget(widgetToAnimate());
d->sLayout->addWidget(d->backWidget.data()); d->sLayout->addWidget(d->backWidget.data());
} }
render(parent()); //render(parent());
} }
QGraphicsLayoutItem *RotationStackedAnimation::layout() QGraphicsLayoutItem *RotationStackedAnimation::layout()
@ -114,33 +111,14 @@ QGraphicsLayoutItem *RotationStackedAnimation::layout()
return d->sLayout; return d->sLayout;
} }
QAbstractAnimation *RotationStackedAnimation::render(QObject *parent) void RotationStackedAnimation::updateState(
QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{ {
Q_UNUSED(parent);
bool dirty = false;
if (!backWidget()) { if (!backWidget()) {
return d->animation.data(); return;
} }
QPair<QGraphicsWidget *,QGraphicsWidget *> widgets = qMakePair(widgetToAnimate(), backWidget()); QPair<QGraphicsWidget *,QGraphicsWidget *> widgets = qMakePair(widgetToAnimate(), backWidget());
QPropertyAnimation *frontAnim, *backAnim;
QSequentialAnimationGroup *groupAnim = d->animation.data();
if (!groupAnim) {
groupAnim = new QSequentialAnimationGroup(parent);
frontAnim = new QPropertyAnimation(d->frontRotation, "angle", groupAnim);
backAnim = new QPropertyAnimation(d->backRotation, "angle", groupAnim);
d->animation = groupAnim;
dirty = true;
} else {
if (groupAnim->animationCount() == 2) {
frontAnim = dynamic_cast<QPropertyAnimation* >(groupAnim->animationAt(0));
backAnim = dynamic_cast<QPropertyAnimation* >(groupAnim->animationAt(1));
} else {
kDebug() << "_ Where are my little animations? Duh!";
return groupAnim;
}
}
const qreal widgetFrontWidth = widgets.first->size().width(); const qreal widgetFrontWidth = widgets.first->size().width();
const qreal widgetFrontHeight = widgets.first->size().height(); const qreal widgetFrontHeight = widgets.first->size().height();
@ -162,13 +140,11 @@ QAbstractAnimation *RotationStackedAnimation::render(QObject *parent)
if (d->animDirection == MoveLeft) { if (d->animDirection == MoveLeft) {
/* TODO: the order way */ /* TODO: the order way */
} else { } else {
d->backRotation->setAngle(265); d->frontStartAngle = 0;
backAnim->setStartValue(d->backRotation->angle()); d->frontEndAngle = 90;
frontAnim->setStartValue(0); d->backStartAngle = 265; //hack
backAnim->setEndValue(360); d->backEndAngle = 360;
frontAnim->setEndValue(90);
} }
} }
} }
@ -185,34 +161,31 @@ QAbstractAnimation *RotationStackedAnimation::render(QObject *parent)
widgets.first->setTransformations(frontTransformation); widgets.first->setTransformations(frontTransformation);
widgets.second->setTransformations(backTransformation); widgets.second->setTransformations(backTransformation);
frontAnim->setDuration(duration()/2); if (oldState == Stopped && newState == Running) {
backAnim->setDuration(duration()/2); d->frontRotation->setAngle(direction() == Forward ? d->frontStartAngle : d->frontEndAngle);
d->backRotation->setAngle(direction() == Forward ? d->backStartAngle : d->backEndAngle);
if (dirty) { } else if(newState == Stopped) {
connect(frontAnim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), d->frontRotation->setAngle(direction() == Forward ? d->frontEndAngle : d->frontStartAngle);
this, SLOT(animationStateChange(QAbstractAnimation::State, QAbstractAnimation::State))); d->backRotation->setAngle(direction() == Forward ? d->backEndAngle : d->backStartAngle);
groupAnim->addAnimation(frontAnim); }
groupAnim->addAnimation(backAnim);
} }
return groupAnim; void RotationStackedAnimation::updateCurrentTime(int currentTime)
}
void RotationStackedAnimation::animationStateChange(
QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{ {
Q_UNUSED(oldState); QGraphicsWidget *w = widgetToAnimate();
if (direction() == QAbstractAnimation::Backward) { if(w) {
if ((newState == QAbstractAnimation::Running) && qreal delta;
(oldState == QAbstractAnimation::Stopped)) { if (currentTime <= duration()/2) {
delta = (currentTime*2)/qreal(duration());
d->sLayout->setCurrentWidgetIndex(0); d->sLayout->setCurrentWidgetIndex(0);
} delta = d->frontEndAngle * delta;
d->frontRotation->setAngle(delta);
} else { } else {
if((newState == QAbstractAnimation::Stopped) && delta = (currentTime/2) / qreal(duration());
(oldState == QAbstractAnimation::Running)) {
d->sLayout->setCurrentWidgetIndex(1); d->sLayout->setCurrentWidgetIndex(1);
delta = d->backEndAngle * delta;
d->backRotation->setAngle(delta);
} }
} }
} }
} }

View File

@ -47,8 +47,6 @@ class RotationStackedAnimation : public Animation
RotationStackedAnimation(QObject *parent = 0); RotationStackedAnimation(QObject *parent = 0);
~RotationStackedAnimation(); ~RotationStackedAnimation();
QAbstractAnimation *render(QObject *parent = 0);
/** /**
* Set the animation direction * Set the animation direction
* @arg direction animation direction * @arg direction animation direction
@ -70,9 +68,9 @@ class RotationStackedAnimation : public Animation
void setWidgetToAnimate(QGraphicsWidget *widget); void setWidgetToAnimate(QGraphicsWidget *widget);
public Q_SLOTS: protected:
void animationStateChange(QAbstractAnimation::State newState, void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
QAbstractAnimation::State oldState); void updateCurrentTime(int currentTime);
private: private:
RotationStackedAnimationPrivate *d; RotationStackedAnimationPrivate *d;