Fix issue with interpolating more than one path segment backwards.
authorMichael Brasser <michael.brasser@nokia.com>
Thu, 29 Sep 2011 23:04:11 +0000 (09:04 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 30 Sep 2011 08:37:05 +0000 (10:37 +0200)
Change-Id: I463010ed63d41be80db96e8306aef8caa3863ccf
Reviewed-on: http://codereview.qt-project.org/5821
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Martin Jones <martin.jones@nokia.com>

src/declarative/util/qdeclarativepath.cpp
tests/auto/declarative/qdeclarativeanimations/data/pathInterpolatorBack.qml [new file with mode: 0644]
tests/auto/declarative/qdeclarativeanimations/tst_qdeclarativeanimations.cpp

index f923413..05d56e0 100644 (file)
@@ -608,7 +608,8 @@ QPointF QDeclarativePath::backwardsPointAt(const QPainterPath &path, const qreal
                 Q_ASSERT(!(currElement < firstElement));
                 currBez = nextBezier(path, &currElement, &bezLength, true /*reverse*/);
                 currLength = prevLength;
-                epc = (currLength - bezLength) / pathLength;
+                prevLength = currLength - bezLength;
+                epc = prevLength / pathLength;
             }
             prevBez.element = currElement;
             prevBez.bezLength = bezLength;
diff --git a/tests/auto/declarative/qdeclarativeanimations/data/pathInterpolatorBack.qml b/tests/auto/declarative/qdeclarativeanimations/data/pathInterpolatorBack.qml
new file mode 100644 (file)
index 0000000..41366ef
--- /dev/null
@@ -0,0 +1,11 @@
+import QtQuick 2.0
+
+PathInterpolator {
+    path: Path {
+        startX: 50; startY: 50
+        PathLine { x: 50; y: 100 }
+        PathLine { x: 100; y: 100 }
+        PathLine { x: 100; y: 50 }
+        PathLine { x: 200; y: 50 }
+    }
+}
index 303268e..652e357 100644 (file)
@@ -75,6 +75,7 @@ private slots:
     void simpleRotation();
     void simplePath();
     void pathInterpolator();
+    void pathInterpolatorBackwardJump();
     void pathWithNoStart();
     void alwaysRunToEnd();
     void complete();
@@ -316,6 +317,38 @@ void tst_qdeclarativeanimations::pathInterpolator()
     QCOMPARE(interpolator->angle(), qreal(0));
 }
 
+void tst_qdeclarativeanimations::pathInterpolatorBackwardJump()
+{
+    QDeclarativeEngine engine;
+    QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/pathInterpolatorBack.qml"));
+    QDeclarativePathInterpolator *interpolator = qobject_cast<QDeclarativePathInterpolator*>(c.create());
+    QVERIFY(interpolator);
+
+    QCOMPARE(interpolator->progress(), qreal(0));
+    QCOMPARE(interpolator->x(), qreal(50));
+    QCOMPARE(interpolator->y(), qreal(50));
+    QCOMPARE(interpolator->angle(), qreal(270));
+
+    interpolator->setProgress(.5);
+    QCOMPARE(interpolator->progress(), qreal(.5));
+    QCOMPARE(interpolator->x(), qreal(100));
+    QCOMPARE(interpolator->y(), qreal(75));
+    QCOMPARE(interpolator->angle(), qreal(90));
+
+    interpolator->setProgress(1);
+    QCOMPARE(interpolator->progress(), qreal(1));
+    QCOMPARE(interpolator->x(), qreal(200));
+    QCOMPARE(interpolator->y(), qreal(50));
+    QCOMPARE(interpolator->angle(), qreal(0));
+
+    //make sure we don't get caught in infinite loop here
+    interpolator->setProgress(0);
+    QCOMPARE(interpolator->progress(), qreal(0));
+    QCOMPARE(interpolator->x(), qreal(50));
+    QCOMPARE(interpolator->y(), qreal(50));
+    QCOMPARE(interpolator->angle(), qreal(270));
+}
+
 void tst_qdeclarativeanimations::pathWithNoStart()
 {
     QDeclarativeEngine engine;