Fine-tune animation's pause()/resume() behaviors
authorCharles Yin <yinyunqiao@gmail.com>
Fri, 16 Mar 2012 13:15:00 +0000 (23:15 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 17 Apr 2012 01:03:07 +0000 (03:03 +0200)
Only allow pause/resume to be used while running, and when stop the
animation reset the paused value to false.

Change-Id: Ia465045006478936146356f9e2e0632614c6b527
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
src/quick/util/qquickanimation.cpp
tests/auto/quick/qquickanimations/tst_qquickanimations.cpp

index 33a5d0a..6b51ac5 100644 (file)
@@ -231,6 +231,11 @@ void QQuickAbstractAnimation::setRunning(bool r)
             d->commence();
         emit started();
     } else {
+        if (d->paused) {
+            d->paused = false; //reset paused state to false when stopped
+            emit pausedChanged(d->paused);
+        }
+
         if (d->animationInstance) {
             if (d->alwaysRunToEnd) {
                 if (d->loopCount != 1)
@@ -260,6 +265,7 @@ void QQuickAbstractAnimation::setRunning(bool r)
 bool QQuickAbstractAnimation::isPaused() const
 {
     Q_D(const QQuickAbstractAnimation);
+    Q_ASSERT((d->paused && d->running) || !d->paused);
     return d->paused;
 }
 
@@ -269,6 +275,11 @@ void QQuickAbstractAnimation::setPaused(bool p)
     if (d->paused == p)
         return;
 
+    if (!d->running) {
+        qmlInfo(this) << "setPaused() cannot be used when animation isn't running.";
+        return;
+    }
+
     if (d->group || d->disableUserControl) {
         qmlInfo(this) << "setPaused() cannot be used on non-root animation nodes.";
         return;
@@ -445,8 +456,8 @@ void QQuickAbstractAnimation::start()
     \qmlmethod QtQuick2::Animation::pause()
     \brief Pauses the animation.
 
-    If the animation is already paused, calling this method has no effect.  The
-    \c paused property will be true following a call to \c pause().
+    If the animation is already paused or not \c running, calling this method has no effect.
+    The \c paused property will be true following a call to \c pause().
 */
 void QQuickAbstractAnimation::pause()
 {
@@ -457,8 +468,8 @@ void QQuickAbstractAnimation::pause()
     \qmlmethod QtQuick2::Animation::resume()
     \brief Resumes a paused animation.
 
-    If the animation is not paused, calling this method has no effect.  The
-    \c paused property will be false following a call to \c resume().
+    If the animation is not paused or not \c running, calling this method has no effect.
+    The \c paused property will be false following a call to \c resume().
 */
 void QQuickAbstractAnimation::resume()
 {
@@ -469,8 +480,8 @@ void QQuickAbstractAnimation::resume()
     \qmlmethod QtQuick2::Animation::stop()
     \brief Stops the animation.
 
-    If the animation is not running, calling this method has no effect.  The
-    \c running property will be false following a call to \c stop().
+    If the animation is not running, calling this method has no effect.  Both the
+    \c running and \c paused properties will be false following a call to \c stop().
 
     Normally \c stop() stops the animation immediately, and the animation has
     no further influence on property values.  In this example animation
index 878cd55..7460263 100644 (file)
@@ -624,6 +624,32 @@ void tst_qquickanimations::resume()
     QTest::qWait(400);
     animation.stop();
     QVERIFY(rect.x() > x);
+
+    animation.start();
+    QVERIFY(animation.isRunning());
+    animation.pause();
+    QVERIFY(animation.isPaused());
+    animation.resume();
+    QVERIFY(!animation.isPaused());
+
+    QSignalSpy spy(&animation, SIGNAL(pausedChanged(bool)));
+    animation.pause();
+    QCOMPARE(spy.count(), 1);
+    QVERIFY(animation.isPaused());
+    animation.stop();
+    QVERIFY(!animation.isPaused());
+    QCOMPARE(spy.count(), 2);
+
+    qmlRegisterType<QQuickPropertyAnimation>("QtQuick",2,0,"PropertyAnimation"); //make sure QQuickPropertyAnimation has correct qml type name
+    QByteArray message = "<Unknown File>: QML PropertyAnimation: setPaused() cannot be used when animation isn't running.";
+    QTest::ignoreMessage(QtWarningMsg, message);
+    animation.pause();
+    QCOMPARE(spy.count(), 2);
+    QVERIFY(!animation.isPaused());
+    animation.resume();
+    QVERIFY(!animation.isPaused());
+    QVERIFY(!animation.isRunning());
+    QCOMPARE(spy.count(), 2);
 }
 
 void tst_qquickanimations::dotProperty()