From 5624c360505a11b9380d2d1e47b2b3477ccc229c Mon Sep 17 00:00:00 2001 From: Leonardo Sobral Cunha Date: Thu, 11 Feb 2010 20:08:02 +0000 Subject: Animations are running by default when used as property source value When you assign an animation to a property (used as source value), the animation will automatically start running, which is the most common behavior for this use case. Reviewed-by: Michael Brasser --- src/declarative/util/qmlanimation.cpp | 10 +++++++- src/declarative/util/qmlanimation_p_p.h | 3 ++- .../declarative/animations/data/valuesource.qml | 14 ++++++++++ .../declarative/animations/data/valuesource2.qml | 14 ++++++++++ .../auto/declarative/animations/tst_animations.cpp | 30 ++++++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/auto/declarative/animations/data/valuesource.qml create mode 100644 tests/auto/declarative/animations/data/valuesource2.qml diff --git a/src/declarative/util/qmlanimation.cpp b/src/declarative/util/qmlanimation.cpp index ba096b3..abffefe 100644 --- a/src/declarative/util/qmlanimation.cpp +++ b/src/declarative/util/qmlanimation.cpp @@ -201,7 +201,8 @@ QmlAbstractAnimation::QmlAbstractAnimation(QmlAbstractAnimationPrivate &dd, QObj Animations can also be started and stopped imperatively from JavaScript using the \c start() and \c stop() methods. - By default, animations are not running. + By default, animations are not running. Though, when the animations are assigned to properties, + as property value sources, they are set to running by default. */ bool QmlAbstractAnimation::isRunning() const { @@ -237,6 +238,10 @@ QmlMetaProperty QmlAbstractAnimationPrivate::createProperty(QObject *obj, const void QmlAbstractAnimation::setRunning(bool r) { Q_D(QmlAbstractAnimation); + + if (r == false) + d->avoidPropertyValueSourceStart = true; + if (d->running == r) return; @@ -531,6 +536,9 @@ void QmlAbstractAnimation::setTarget(const QmlMetaProperty &p) Q_D(QmlAbstractAnimation); if (d->userProperty.isNull) d->userProperty = p; + + if (!d->avoidPropertyValueSourceStart) + setRunning(true); } //prepare is called before an animation begins diff --git a/src/declarative/util/qmlanimation_p_p.h b/src/declarative/util/qmlanimation_p_p.h index 2f911b4..d680194 100644 --- a/src/declarative/util/qmlanimation_p_p.h +++ b/src/declarative/util/qmlanimation_p_p.h @@ -205,7 +205,7 @@ public: QmlAbstractAnimationPrivate() : running(false), paused(false), alwaysRunToEnd(false), repeat(false), connectedTimeLine(false), componentComplete(true), startOnCompletion(false), - group(0) {} + avoidPropertyValueSourceStart(false), group(0) {} bool running:1; bool paused:1; @@ -215,6 +215,7 @@ public: bool componentComplete:1; bool startOnCompletion:1; + bool avoidPropertyValueSourceStart:1; void commence(); diff --git a/tests/auto/declarative/animations/data/valuesource.qml b/tests/auto/declarative/animations/data/valuesource.qml new file mode 100644 index 0000000..c35063d --- /dev/null +++ b/tests/auto/declarative/animations/data/valuesource.qml @@ -0,0 +1,14 @@ +import Qt 4.6 + +Rectangle { + width: 400 + height: 400 + Rectangle { + id: rect + objectName: "MyRect" + color: "red" + width: 50; height: 50 + x: 100; y: 100 + x: NumberAnimation { id: anim; objectName: "MyAnim"; to: 200 } + } +} diff --git a/tests/auto/declarative/animations/data/valuesource2.qml b/tests/auto/declarative/animations/data/valuesource2.qml new file mode 100644 index 0000000..1a60542 --- /dev/null +++ b/tests/auto/declarative/animations/data/valuesource2.qml @@ -0,0 +1,14 @@ +import Qt 4.6 + +Rectangle { + width: 400 + height: 400 + Rectangle { + id: rect + objectName: "MyRect" + color: "red" + width: 50; height: 50 + x: 100; y: 100 + x: NumberAnimation { id: anim; objectName: "MyAnim"; running: false; to: 200 } + } +} diff --git a/tests/auto/declarative/animations/tst_animations.cpp b/tests/auto/declarative/animations/tst_animations.cpp index 5026f6f..17085af 100644 --- a/tests/auto/declarative/animations/tst_animations.cpp +++ b/tests/auto/declarative/animations/tst_animations.cpp @@ -68,6 +68,7 @@ private slots: void easingStringConversion(); void invalidDuration(); void attached(); + void propertyValueSourceDefaultStart(); }; #define QTIMED_COMPARE(lhs, rhs) do { \ @@ -576,6 +577,35 @@ void tst_animations::attached() QVERIFY(rect); } +void tst_animations::propertyValueSourceDefaultStart() +{ + { + QmlEngine engine; + + QmlComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/valuesource.qml")); + + QmlGraphicsRectangle *rect = qobject_cast(c.create()); + QVERIFY(rect); + + QmlAbstractAnimation *myAnim = rect->findChild("MyAnim"); + QVERIFY(myAnim); + QVERIFY(myAnim->isRunning()); + } + + { + QmlEngine engine; + + QmlComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/valuesource2.qml")); + + QmlGraphicsRectangle *rect = qobject_cast(c.create()); + QVERIFY(rect); + + QmlAbstractAnimation *myAnim = rect->findChild("MyAnim"); + QVERIFY(myAnim); + QVERIFY(myAnim->isRunning() == false); + } +} + QTEST_MAIN(tst_animations) #include "tst_animations.moc" -- cgit v0.12