From ebda84cd06faaf440a096bda7966fa795ca86318 Mon Sep 17 00:00:00 2001 From: Leonardo Sobral Cunha Date: Tue, 21 Apr 2009 15:39:37 +0200 Subject: Fixes QPropertyAnimation's default start value update condition The default start value is updated when the animation changes from Stopped to Running state. Reviewed-by: Jan-Arve --- src/corelib/animation/qpropertyanimation.cpp | 22 +++++++++++++++------- src/corelib/animation/qvariantanimation_p.h | 14 +------------- .../qpropertyanimation/tst_qpropertyanimation.cpp | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/corelib/animation/qpropertyanimation.cpp b/src/corelib/animation/qpropertyanimation.cpp index edcabaa..9a0c5bc 100644 --- a/src/corelib/animation/qpropertyanimation.cpp +++ b/src/corelib/animation/qpropertyanimation.cpp @@ -49,7 +49,7 @@ by itself as a simple animation class, or as part of more complex animations through QAnimationGroup. - The most common way to use QPropertyAnimation is to construct an instance + The most common way to use QPropertyAnimation is to construct an instance of it by passing a pointer to a QObject or a QWidget, and the name of the property you would like to animate to QPropertyAnimation's constructor. @@ -220,11 +220,20 @@ void QPropertyAnimation::updateCurrentValue(const QVariant &value) /*! \reimp + + If the startValue is not defined when the state of the animation changes from Stopped to Running, + the current property value is used as the initial value for the animation. */ void QPropertyAnimation::updateState(QAbstractAnimation::State oldState, QAbstractAnimation::State newState) { Q_D(QPropertyAnimation); + + if (!d->target) { + qWarning("QPropertyAnimation::updateState: Changing state of an animation without target"); + return; + } + QVariantAnimation::updateState(oldState, newState); QMutexLocker locker(guardHashLock()); QPropertyAnimationHash * hash = _q_runningAnimations(); @@ -235,20 +244,19 @@ void QPropertyAnimation::updateState(QAbstractAnimation::State oldState, if (oldAnim) { // try to stop the top level group QAbstractAnimation *current = oldAnim; - while(current->group() && current->state() != Stopped) current = current->group(); + while (current->group() && current->state() != Stopped) + current = current->group(); current->stop(); } hash->insert(key, this); - // Initialize start value - // ### review this line below, d->atEnd() ? - // ### avoid entering a state where start value is not set - if (d->target && (d->atBeginning() || d->atEnd())) { + + // update the default start value + if (oldState == Stopped) { d->setDefaultStartValue(d->target->property(d->propertyName.constData())); } } else if (hash->value(key) == this) { hash->remove(key); } - } #include "moc_qpropertyanimation.cpp" diff --git a/src/corelib/animation/qvariantanimation_p.h b/src/corelib/animation/qvariantanimation_p.h index 66910c1..14a3ef6 100644 --- a/src/corelib/animation/qvariantanimation_p.h +++ b/src/corelib/animation/qvariantanimation_p.h @@ -88,18 +88,6 @@ public: return q->d_func(); } - - //some helper functions - bool atBeginning() const - { - return currentTime == 0; - } - - bool atEnd() const - { - return currentTime == duration && currentLoop == (loopCount - 1); - } - void setDefaultStartValue(const QVariant &value); int duration; @@ -109,7 +97,7 @@ public: QVariant currentValue; QVariant defaultStartValue; bool hasStartValue; - + //this is used to keep track of the KeyValue interval in which we currently are struct { diff --git a/tests/auto/qpropertyanimation/tst_qpropertyanimation.cpp b/tests/auto/qpropertyanimation/tst_qpropertyanimation.cpp index 437c862..f0deab5 100644 --- a/tests/auto/qpropertyanimation/tst_qpropertyanimation.cpp +++ b/tests/auto/qpropertyanimation/tst_qpropertyanimation.cpp @@ -84,6 +84,7 @@ private slots: void deletion3(); void duration0(); void noStartValue(); + void noStartValueWithLoop(); void startWhenAnotherIsRunning(); void easingcurve_data(); void easingcurve(); @@ -416,6 +417,27 @@ void tst_QPropertyAnimation::noStartValue() QCOMPARE(o.values.last(), 420); } +void tst_QPropertyAnimation::noStartValueWithLoop() +{ + StartValueTester o; + o.setProperty("ole", 42); + o.values.clear(); + + QPropertyAnimation a(&o, "ole"); + a.setEndValue(420); + a.setDuration(250); + a.setLoopCount(2); + a.start(); + + a.setCurrentTime(250); + QCOMPARE(o.values.first(), 42); + QCOMPARE(a.currentValue().toInt(), 42); + QCOMPARE(o.values.last(), 42); + + a.setCurrentTime(500); + QCOMPARE(a.currentValue().toInt(), 420); +} + void tst_QPropertyAnimation::startWhenAnotherIsRunning() { StartValueTester o; -- cgit v0.12