Behavior on a value type should not cancel previous assignment.
authorMichael Brasser <michael.brasser@nokia.com>
Wed, 9 Nov 2011 03:01:56 +0000 (13:01 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 9 Nov 2011 07:23:57 +0000 (08:23 +0100)
The call to read the value type value causes the previously assigned
value to be overridden. This commit fixes the issue, but doesn't
solve the root cause, which would require changes at the
compiler/vme level.

Task-number: QTBUG-20827
Change-Id: I1a53ee7b777bea81c5929ab7e47e2932e6901967
Reviewed-by: Martin Jones <martin.jones@nokia.com>
src/declarative/util/qdeclarativebehavior.cpp
tests/auto/declarative/qdeclarativebehaviors/data/valueType.qml [new file with mode: 0644]
tests/auto/declarative/qdeclarativebehaviors/tst_qdeclarativebehaviors.cpp

index 8ff1a06..6fb3619 100644 (file)
@@ -60,7 +60,6 @@ public:
       , blockRunningChanged(false) {}
 
     QDeclarativeProperty property;
-    QVariant currentValue;
     QVariant targetValue;
     QDeclarativeGuard<QDeclarativeAbstractAnimation> animation;
     bool enabled;
@@ -185,7 +184,7 @@ void QDeclarativeBehavior::write(const QVariant &value)
     if (d->animation->isRunning() && value == d->targetValue)
         return;
 
-    d->currentValue = d->property.read();
+    const QVariant &currentValue = d->property.read();
     d->targetValue = value;
 
     if (d->animation->qtAnimation()->duration() != -1
@@ -197,7 +196,7 @@ void QDeclarativeBehavior::write(const QVariant &value)
     QDeclarativeStateOperation::ActionList actions;
     QDeclarativeAction action;
     action.property = d->property;
-    action.fromValue = d->currentValue;
+    action.fromValue = currentValue;
     action.toValue = value;
     actions << action;
 
@@ -213,7 +212,6 @@ void QDeclarativeBehavior::setTarget(const QDeclarativeProperty &property)
 {
     Q_D(QDeclarativeBehavior);
     d->property = property;
-    d->currentValue = property.read();
     if (d->animation)
         d->animation->setDefaultTarget(property);
 
diff --git a/tests/auto/declarative/qdeclarativebehaviors/data/valueType.qml b/tests/auto/declarative/qdeclarativebehaviors/data/valueType.qml
new file mode 100644 (file)
index 0000000..7bc8297
--- /dev/null
@@ -0,0 +1,13 @@
+import QtQuick 2.0
+Rectangle {
+    width: 400
+    height: 400
+
+    color.r: 1
+    color.g: 0
+    color.b: 1
+
+    Behavior on color.r { NumberAnimation { duration: 500; } }
+
+    function changeR() { color.r = 0 }
+}
index e84cd58..608cdac 100644 (file)
@@ -66,6 +66,7 @@ private slots:
     void replaceBinding();
     //void transitionOverrides();
     void group();
+    void valueType();
     void emptyBehavior();
     void explicitSelection();
     void nonSelectingBehavior();
@@ -237,6 +238,19 @@ void tst_qdeclarativebehaviors::group()
     }
 }
 
+void tst_qdeclarativebehaviors::valueType()
+{
+    QDeclarativeEngine engine;
+    QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("valueType.qml")));
+    QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
+    QVERIFY(rect);
+
+    //QTBUG-20827
+    QCOMPARE(rect->color(), QColor::fromRgb(255,0,255));
+
+    delete rect;
+}
+
 void tst_qdeclarativebehaviors::emptyBehavior()
 {
     QDeclarativeEngine engine;