Add enabled property to Transition.
authorMichael Brasser <michael.brasser@nokia.com>
Thu, 4 Aug 2011 00:30:46 +0000 (10:30 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 4 Aug 2011 02:25:29 +0000 (04:25 +0200)
Task-number: QTBUG-14488
Change-Id: I3619a0d26e99e2c19f50a63013dedc151b07154c
Reviewed-on: http://codereview.qt.nokia.com/2591
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Martin Jones <martin.jones@nokia.com>

src/declarative/items/qsgpositioners.cpp
src/declarative/util/qdeclarativestategroup.cpp
src/declarative/util/qdeclarativetransition.cpp
src/declarative/util/qdeclarativetransition_p.h
tests/auto/declarative/qdeclarativeanimations/data/disabledTransition.qml [new file with mode: 0644]
tests/auto/declarative/qdeclarativeanimations/tst_qdeclarativeanimations.cpp
tests/auto/declarative/qsgpositioners/data/horizontal-animated-disabled.qml [new file with mode: 0644]
tests/auto/declarative/qsgpositioners/data/horizontal-animated.qml
tests/auto/declarative/qsgpositioners/tst_qsgpositioners.cpp

index da3c7fe..fe11943 100644 (file)
@@ -50,6 +50,7 @@
 #include <private/qdeclarativestate_p.h>
 #include <private/qdeclarativestategroup_p.h>
 #include <private/qdeclarativestateoperations_p.h>
+#include <private/qdeclarativetransition_p.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -213,7 +214,7 @@ void QSGBasePositioner::prePositioning()
     }
     QSizeF contentSize(0,0);
     doPositioning(&contentSize);
-    if(d->addTransition || d->moveTransition)
+    if (!d->addActions.isEmpty() || !d->moveActions.isEmpty())
         finishApplyTransitions();
     d->doingPositioning = false;
     //Set implicit size to the size of its children
@@ -226,12 +227,12 @@ void QSGBasePositioner::positionX(int x, const PositionedItem &target)
     Q_D(QSGBasePositioner);
     if(d->type == Horizontal || d->type == Both){
         if (target.isNew) {
-            if (!d->addTransition)
+            if (!d->addTransition || !d->addTransition->enabled())
                 target.item->setX(x);
             else
                 d->addActions << QDeclarativeAction(target.item, QLatin1String("x"), QVariant(x));
         } else if (x != target.item->x()) {
-            if (!d->moveTransition)
+            if (!d->moveTransition || !d->moveTransition->enabled())
                 target.item->setX(x);
             else
                 d->moveActions << QDeclarativeAction(target.item, QLatin1String("x"), QVariant(x));
@@ -244,12 +245,12 @@ void QSGBasePositioner::positionY(int y, const PositionedItem &target)
     Q_D(QSGBasePositioner);
     if(d->type == Vertical || d->type == Both){
         if (target.isNew) {
-            if (!d->addTransition)
+            if (!d->addTransition || !d->addTransition->enabled())
                 target.item->setY(y);
             else
                 d->addActions << QDeclarativeAction(target.item, QLatin1String("y"), QVariant(y));
         } else if (y != target.item->y()) {
-            if (!d->moveTransition)
+            if (!d->moveTransition || !d->moveTransition->enabled())
                 target.item->setY(y);
             else
                 d->moveActions << QDeclarativeAction(target.item, QLatin1String("y"), QVariant(y));
index c098a38..d52d54b 100644 (file)
@@ -372,6 +372,8 @@ QDeclarativeTransition *QDeclarativeStateGroupPrivate::findTransition(const QStr
 
     for (int ii = 0; !done && ii < transitions.count(); ++ii) {
         QDeclarativeTransition *t = transitions.at(ii);
+        if (!t->enabled())
+            continue;
         for (int ii = 0; ii < 2; ++ii)
         {
             if (ii && (!t->reversible() ||
index b63407c..45d0721 100644 (file)
@@ -113,7 +113,7 @@ class QDeclarativeTransitionPrivate : public QObjectPrivate
 public:
     QDeclarativeTransitionPrivate()
     : fromState(QLatin1String("*")), toState(QLatin1String("*")),
-      reversed(false), reversible(false), endState(0)
+      reversed(false), reversible(false), enabled(true), endState(0)
     {
         group.trans = this;
     }
@@ -122,6 +122,7 @@ public:
     QString toState;
     bool reversed;
     bool reversible;
+    bool enabled;
     ParallelAnimationWrapper group;
     QDeclarativeTransitionManager *endState;
 
@@ -319,6 +320,49 @@ void QDeclarativeTransition::setToState(const QString &t)
 }
 
 /*!
+    \qmlproperty bool Transition::enabled
+
+    This property holds whether the Transition will be run when moving
+    from the \c from state to the \c to state.
+
+    By default a Transition is enabled.
+
+    Note that in some circumstances disabling a Transition may cause an
+    alternative Transition to be used in its place. In the following
+    example, the generic Transition will be used to animate the change
+    from \c state1 to \c state2, as the more specific Transition has
+    been disabled.
+
+    \qml
+    Item {
+        states: [
+            State { name: "state1" ... }
+            State { name: "state2" ... }
+        ]
+        transitions: [
+            Transition { from: "state1"; to: "state2"; enabled: false ... }
+            Transition { ... }
+        ]
+    }
+    \endqml
+*/
+
+bool QDeclarativeTransition::enabled() const
+{
+    Q_D(const QDeclarativeTransition);
+    return d->enabled;
+}
+
+void QDeclarativeTransition::setEnabled(bool enabled)
+{
+    Q_D(QDeclarativeTransition);
+    if (d->enabled == enabled)
+        return;
+    d->enabled = enabled;
+    emit enabledChanged();
+}
+
+/*!
     \qmlproperty list<Animation> Transition::animations
     \default
 
index 115b947..db10258 100644 (file)
@@ -66,6 +66,7 @@ class Q_DECLARATIVE_EXPORT QDeclarativeTransition : public QObject
     Q_PROPERTY(QString to READ toState WRITE setToState NOTIFY toChanged)
     Q_PROPERTY(bool reversible READ reversible WRITE setReversible NOTIFY reversibleChanged)
     Q_PROPERTY(QDeclarativeListProperty<QDeclarativeAbstractAnimation> animations READ animations)
+    Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
     Q_CLASSINFO("DefaultProperty", "animations")
     Q_CLASSINFO("DeferredPropertyNames", "animations")
 
@@ -82,6 +83,9 @@ public:
     bool reversible() const;
     void setReversible(bool);
 
+    bool enabled() const;
+    void setEnabled(bool enabled);
+
     QDeclarativeListProperty<QDeclarativeAbstractAnimation> animations();
 
     void prepare(QDeclarativeStateOperation::ActionList &actions,
@@ -95,6 +99,7 @@ Q_SIGNALS:
     void fromChanged();
     void toChanged();
     void reversibleChanged();
+    void enabledChanged();
 };
 
 QT_END_NAMESPACE
diff --git a/tests/auto/declarative/qdeclarativeanimations/data/disabledTransition.qml b/tests/auto/declarative/qdeclarativeanimations/data/disabledTransition.qml
new file mode 100644 (file)
index 0000000..0fbafea
--- /dev/null
@@ -0,0 +1,30 @@
+import QtQuick 2.0
+
+Rectangle {
+    width: 400
+    height: 400
+    Rectangle {
+        id: theRect
+        objectName: "TheRect"
+        color: "red"
+        width: 50; height: 50
+        x: 100; y: 100
+    }
+
+    states: State {
+        name: "moved"
+        PropertyChanges {
+            target: theRect
+            x: 200
+        }
+    }
+    transitions: Transition {
+        enabled: false
+        NumberAnimation { targets: theRect; properties: "x" }
+    }
+
+    MouseArea {
+        anchors.fill: parent
+        onClicked: parent.state = "moved"
+    }
+}
index bd9c3e0..367de01 100644 (file)
@@ -77,6 +77,7 @@ private slots:
     void mixedTypes();
     void properties();
     void propertiesTransition();
+    void disabledTransition();
     void invalidDuration();
     void attached();
     void propertyValueSourceDefaultStart();
@@ -576,6 +577,32 @@ void tst_qdeclarativeanimations::propertiesTransition()
 
 }
 
+void tst_qdeclarativeanimations::disabledTransition()
+{
+    QDeclarativeEngine engine;
+    QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/disabledTransition.qml"));
+    QSGRectangle *rect = qobject_cast<QSGRectangle*>(c.create());
+    QVERIFY(rect);
+
+    QSGRectangle *myRect = rect->findChild<QSGRectangle*>("TheRect");
+    QVERIFY(myRect);
+
+    QDeclarativeTransition *trans = rect->findChild<QDeclarativeTransition*>();
+    QVERIFY(trans);
+
+    QCOMPARE(trans->enabled(), false);
+
+    QSGItemPrivate::get(rect)->setState("moved");
+    QCOMPARE(myRect->x(),qreal(200));
+
+    trans->setEnabled(true);
+
+    QSGItemPrivate::get(rect)->setState("");
+    QCOMPARE(myRect->x(),qreal(200));
+    QTest::qWait(300);
+    QTIMED_COMPARE(myRect->x(),qreal(100));
+}
+
 void tst_qdeclarativeanimations::invalidDuration()
 {
     QDeclarativePropertyAnimation *animation = new QDeclarativePropertyAnimation;
diff --git a/tests/auto/declarative/qsgpositioners/data/horizontal-animated-disabled.qml b/tests/auto/declarative/qsgpositioners/data/horizontal-animated-disabled.qml
new file mode 100644 (file)
index 0000000..8723ffc
--- /dev/null
@@ -0,0 +1,40 @@
+import QtQuick 2.0
+
+Item {
+    width: 640
+    height: 480
+
+    Row {
+        objectName: "row"
+        add: Transition {
+            enabled: false
+            NumberAnimation { properties: "x" }
+        }
+        move: Transition {
+            enabled: false
+            NumberAnimation { properties: "x" }
+        }
+        Rectangle {
+            objectName: "one"
+            color: "red"
+            x: -100;
+            width: 50
+            height: 50
+        }
+        Rectangle {
+            objectName: "two"
+            color: "blue"
+            x: -100;
+            visible: false
+            width: 50
+            height: 50
+        }
+        Rectangle {
+            objectName: "three"
+            x: -100;
+            color: "green"
+            width: 50
+            height: 50
+        }
+    }
+}
index b9b8591..a88c26b 100644 (file)
@@ -4,16 +4,19 @@ Item {
     width: 640
     height: 480
     property bool testRightToLeft: false
+    property bool testEnabled: false
 
     Row {
         objectName: "row"
         layoutDirection: testRightToLeft ? Qt.RightToLeft : Qt.LeftToRight
         add: Transition {
+            enabled: testEnabled ? false : true
             NumberAnimation {
                 properties: "x";
             }
         }
         move: Transition {
+            enabled: testEnabled ? false : true
             NumberAnimation {
                 properties: "x";
             }
index a9bdf42..3b2a504 100644 (file)
@@ -68,6 +68,7 @@ private slots:
     void test_horizontal_spacing_rightToLeft();
     void test_horizontal_animated();
     void test_horizontal_animated_rightToLeft();
+    void test_horizontal_animated_disabled();
     void test_vertical();
     void test_vertical_spacing();
     void test_vertical_animated();
@@ -328,6 +329,46 @@ void tst_qsgpositioners::test_horizontal_animated_rightToLeft()
     delete canvas;
 }
 
+void tst_qsgpositioners::test_horizontal_animated_disabled()
+{
+    QSGView *canvas = createView(SRCDIR "/data/horizontal-animated-disabled.qml");
+
+    QSGRectangle *one = canvas->rootObject()->findChild<QSGRectangle*>("one");
+    QVERIFY(one != 0);
+
+    QSGRectangle *two = canvas->rootObject()->findChild<QSGRectangle*>("two");
+    QVERIFY(two != 0);
+
+    QSGRectangle *three = canvas->rootObject()->findChild<QSGRectangle*>("three");
+    QVERIFY(three != 0);
+
+    QSGItem *row = canvas->rootObject()->findChild<QSGItem*>("row");
+    QVERIFY(row);
+
+    qApp->processEvents();
+
+    QCOMPARE(one->x(), 0.0);
+    QCOMPARE(one->y(), 0.0);
+    QCOMPARE(two->isVisible(), false);
+    QCOMPARE(two->x(), -100.0);//Not 'in' yet
+    QCOMPARE(two->y(), 0.0);
+    QCOMPARE(three->x(), 50.0);
+    QCOMPARE(three->y(), 0.0);
+
+    //Add 'two'
+    two->setVisible(true);
+    QCOMPARE(two->isVisible(), true);
+    qApp->processEvents();
+    QCOMPARE(row->width(), 150.0);
+    QCOMPARE(row->height(), 50.0);
+
+    qApp->processEvents();
+    QCOMPARE(two->x(), 50.0);
+    QCOMPARE(three->x(), 100.0);
+
+    delete canvas;
+}
+
 void tst_qsgpositioners::test_vertical()
 {
     QSGView *canvas = createView(SRCDIR "/data/vertical.qml");