From 3961cf124af9d811da4bbe72bb28508aef71c7df Mon Sep 17 00:00:00 2001 From: Paolo Angelelli Date: Mon, 6 May 2019 20:37:42 +0200 Subject: Add scale property to QQuickPath With this property, Paths and ShapePaths become scalable. [ChangeLog][QQuick][Path] Added scale property to scale a path before sending it in to PathView/Shape. Change-Id: Id9ce7d1247d55e2cdb0d27a19c86fe0970e66268 Fixes: QTBUG-74456 Reviewed-by: Shawn Rutledge --- tests/auto/quick/qquickpath/tst_qquickpath.cpp | 156 +++++++++++++++++++------ 1 file changed, 120 insertions(+), 36 deletions(-) (limited to 'tests/auto/quick/qquickpath/tst_qquickpath.cpp') diff --git a/tests/auto/quick/qquickpath/tst_qquickpath.cpp b/tests/auto/quick/qquickpath/tst_qquickpath.cpp index 12a8c673b0..bac1fc5861 100644 --- a/tests/auto/quick/qquickpath/tst_qquickpath.cpp +++ b/tests/auto/quick/qquickpath/tst_qquickpath.cpp @@ -42,18 +42,44 @@ public: private slots: void arc(); void angleArc(); - void catmullromCurve(); - void closedCatmullromCurve(); + void catmullRomCurve(); + void closedCatmullRomCurve(); void svg(); void line(); + +private: + void arc(QSizeF scale); + void angleArc(QSizeF scale); + void catmullRomCurve(QSizeF scale, const QVector &points); + void closedCatmullRomCurve(QSizeF scale, const QVector &points); + void svg(QSizeF scale); + void line(QSizeF scale); }; -void tst_QuickPath::arc() +static void compare(const QPointF &point, const QSizeF &scale, int line, double x, double y) +{ + QVERIFY2(qFuzzyCompare(float(point.x()), float(x * scale.width())), + (QStringLiteral("Actual: ") + QString::number(point.x(),'g',14) + + QStringLiteral(" Expected: ") + QString::number(x * scale.width(),'g',14) + + QStringLiteral(" At: ") + QString::number(line)).toLatin1().data()); + QVERIFY2(qFuzzyCompare(float(point.y()), float(y * scale.height())), + (QStringLiteral("Actual: ") + QString::number(point.y(),'g',14) + + QStringLiteral(" Expected: ") + QString::number(y * scale.height(),'g',14) + + QStringLiteral(" At: ") + QString::number(line)).toLatin1().data()); +} +static void compare(const QPointF &point, int line, const QPointF &pt) +{ + return compare(point, QSizeF(1,1), line, pt.x(), pt.y()); +} + +void tst_QuickPath::arc(QSizeF scale) { QQmlEngine engine; QQmlComponent c(&engine, testFileUrl("arc.qml")); QQuickPath *obj = qobject_cast(c.create()); QVERIFY(obj != nullptr); + if (scale != QSizeF(1,1)) + obj->setProperty("scale", scale); QCOMPARE(obj->startX(), 0.); QCOMPARE(obj->startY(), 0.); @@ -76,19 +102,27 @@ void tst_QuickPath::arc() QPointF pos = obj->pointAt(0); QCOMPARE(pos, QPointF(0,0)); pos = obj->pointAt(.25); - QCOMPARE(pos.toPoint(), QPoint(39,8)); //fuzzy compare + compare(pos, scale, __LINE__, 38.9244897744, 7.85853964341); pos = obj->pointAt(.75); - QCOMPARE(pos.toPoint(), QPoint(92,61)); //fuzzy compare + compare(pos, scale, __LINE__, 92.141460356592, 61.07551022559); pos = obj->pointAt(1); - QCOMPARE(pos, QPointF(100,100)); + QCOMPARE(pos, QPointF(100 * scale.width(), 100 * scale.height())); } -void tst_QuickPath::angleArc() +void tst_QuickPath::arc() +{ + arc(QSizeF(1,1)); + arc(QSizeF(2.2,3.4)); +} + +void tst_QuickPath::angleArc(QSizeF scale) { QQmlEngine engine; QQmlComponent c(&engine, testFileUrl("anglearc.qml")); QQuickPath *obj = qobject_cast(c.create()); QVERIFY(obj != nullptr); + if (scale != QSizeF(1,1)) + obj->setProperty("scale", scale); QQmlListReference list(obj, "pathElements"); QCOMPARE(list.count(), 1); @@ -106,15 +140,14 @@ void tst_QuickPath::angleArc() QPainterPath path = obj->path(); QVERIFY(path != QPainterPath()); - // using QPoint to do fuzzy compare QPointF pos = obj->pointAt(0); - QCOMPARE(pos.toPoint(), QPoint(135,135)); + compare(pos, scale, __LINE__, 135.35533905867, 135.35533905867); pos = obj->pointAt(.25); - QCOMPARE(pos.toPoint(), QPoint(119,146)); + compare(pos, scale, __LINE__, 119.46222180396, 146.07068621369); pos = obj->pointAt(.75); - QCOMPARE(pos.toPoint(), QPoint(81,146)); + compare(pos, scale, __LINE__, 80.537778196007, 146.07068621366); pos = obj->pointAt(1); - QCOMPARE(pos.toPoint(), QPoint(65,135)); + compare(pos, scale, __LINE__, 64.644660941173, 135.35533905867); // if moveToStart is false, we should have a line starting from startX/Y arc->setMoveToStart(false); @@ -122,12 +155,20 @@ void tst_QuickPath::angleArc() QCOMPARE(pos, QPointF(0,0)); } -void tst_QuickPath::catmullromCurve() +void tst_QuickPath::angleArc() +{ + angleArc(QSizeF(1,1)); + angleArc(QSizeF(2.7,0.92)); +} + +void tst_QuickPath::catmullRomCurve(QSizeF scale, const QVector &points) { QQmlEngine engine; QQmlComponent c(&engine, testFileUrl("curve.qml")); QQuickPath *obj = qobject_cast(c.create()); QVERIFY(obj != nullptr); + if (scale != QSizeF(1,1)) + obj->setProperty("scale", scale); QCOMPARE(obj->startX(), 0.); QCOMPARE(obj->startY(), 0.); @@ -148,22 +189,36 @@ void tst_QuickPath::catmullromCurve() QPainterPath path = obj->path(); QVERIFY(path != QPainterPath()); - QPointF pos = obj->pointAt(0); - QCOMPARE(pos, QPointF(0,0)); - pos = obj->pointAt(.25); - QCOMPARE(pos.toPoint(), QPoint(63,26)); //fuzzy compare - pos = obj->pointAt(.75); - QCOMPARE(pos.toPoint(), QPoint(51,105)); //fuzzy compare - pos = obj->pointAt(1); - QCOMPARE(pos.toPoint(), QPoint(100,150)); + QPointF pos = path.pointAtPercent(0); + QCOMPARE(pos, points.at(0)); + pos = path.pointAtPercent(.25); + compare(pos, __LINE__, points.at(1)); + pos = path.pointAtPercent(.75); + compare(pos, __LINE__, points.at(2)); + pos = path.pointAtPercent(1); + compare(pos, __LINE__, points.at(3)); } -void tst_QuickPath::closedCatmullromCurve() +void tst_QuickPath::catmullRomCurve() +{ + catmullRomCurve(QSizeF(1,1), { QPointF(0,0), + QPointF(62.917022919131, 26.175485291549), + QPointF(51.194527196674 , 105.27985623074), + QPointF(100, 150) }); + catmullRomCurve(QSizeF(2,5.3), { QPointF(0,0), + QPointF(150.80562419914, 170.34065984615), + QPointF(109.08400252853 , 588.35165918579), + QPointF(200, 795) }); +} + +void tst_QuickPath::closedCatmullRomCurve(QSizeF scale, const QVector &points) { QQmlEngine engine; QQmlComponent c(&engine, testFileUrl("closedcurve.qml")); QQuickPath *obj = qobject_cast(c.create()); QVERIFY(obj != nullptr); + if (scale != QSizeF(1,1)) + obj->setProperty("scale", scale); QCOMPARE(obj->startX(), 50.); QCOMPARE(obj->startY(), 50.); @@ -181,22 +236,36 @@ void tst_QuickPath::closedCatmullromCurve() QPainterPath path = obj->path(); QVERIFY(path != QPainterPath()); - QPointF pos = obj->pointAt(0); - QCOMPARE(pos, QPointF(50,50)); - pos = obj->pointAt(.1); - QCOMPARE(pos.toPoint(), QPoint(67,56)); //fuzzy compare - pos = obj->pointAt(.75); - QCOMPARE(pos.toPoint(), QPoint(44,116)); //fuzzy compare - pos = obj->pointAt(1); - QCOMPARE(pos, QPointF(50,50)); + QPointF pos = path.pointAtPercent(0); + QCOMPARE(pos, points.at(0)); + pos = path.pointAtPercent(.1); + compare(pos, __LINE__, points.at(1)); + pos = path.pointAtPercent(.75); + compare(pos, __LINE__, points.at(2)); + pos = path.pointAtPercent(1); + compare(pos, __LINE__, points.at(3)); } -void tst_QuickPath::svg() +void tst_QuickPath::closedCatmullRomCurve() +{ + closedCatmullRomCurve(QSizeF(1,1), { QPointF(50,50), + QPointF(66.776225481812, 55.617435304145), + QPointF(44.10269379731 , 116.33512508175), + QPointF(50, 50) }); + closedCatmullRomCurve(QSizeF(2,3), { QPointF(100,150), + QPointF(136.49725836178, 170.25466686363), + QPointF(87.713232151943 , 328.29232737977), + QPointF(100, 150) }); +} + +void tst_QuickPath::svg(QSizeF scale) { QQmlEngine engine; QQmlComponent c(&engine, testFileUrl("svg.qml")); QQuickPath *obj = qobject_cast(c.create()); QVERIFY(obj != nullptr); + if (scale != QSizeF(1,1)) + obj->setProperty("scale", scale); QCOMPARE(obj->startX(), 0.); QCOMPARE(obj->startY(), 0.); @@ -212,16 +281,22 @@ void tst_QuickPath::svg() QVERIFY(path != QPainterPath()); QPointF pos = obj->pointAt(0); - QCOMPARE(pos, QPointF(200,300)); + QCOMPARE(pos, QPointF(200 * scale.width(),300 * scale.height())); pos = obj->pointAt(.25); - QCOMPARE(pos.toPoint(), QPoint(400,175)); //fuzzy compare + QCOMPARE(pos.toPoint(), QPoint(400 * scale.width(),175 * scale.height())); //fuzzy compare pos = obj->pointAt(.75); - QCOMPARE(pos.toPoint(), QPoint(800,425)); //fuzzy compare + QCOMPARE(pos.toPoint(), QPoint(800 * scale.width(),425 * scale.height())); //fuzzy compare pos = obj->pointAt(1); - QCOMPARE(pos, QPointF(1000,300)); + QCOMPARE(pos, QPointF(1000 * scale.width(),300 * scale.height())); } -void tst_QuickPath::line() +void tst_QuickPath::svg() +{ + svg(QSizeF(1,1)); + svg(QSizeF(5,3)); +} + +void tst_QuickPath::line(QSizeF scale) { QQmlEngine engine; QQmlComponent c1(&engine); @@ -234,6 +309,8 @@ void tst_QuickPath::line() QScopedPointer o1(c1.create()); QQuickPath *path1 = qobject_cast(o1.data()); QVERIFY(path1); + if (scale != QSizeF(1,1)) + path1->setProperty("scale", scale); QQmlComponent c2(&engine); c2.setData( @@ -246,6 +323,8 @@ void tst_QuickPath::line() QScopedPointer o2(c2.create()); QQuickPath *path2 = qobject_cast(o2.data()); QVERIFY(path2); + if (scale != QSizeF(1,1)) + path2->setProperty("scale", scale); for (int i = 0; i < 167; ++i) { qreal t = i / 167.0; @@ -258,6 +337,11 @@ void tst_QuickPath::line() } } +void tst_QuickPath::line() +{ + line(QSizeF(1,1)); + line(QSizeF(7.23,7.23)); +} QTEST_MAIN(tst_QuickPath) -- cgit v1.2.3 From ab0583eb432bd44d32605e8594cdb3fe2c0a7b8f Mon Sep 17 00:00:00 2001 From: Paolo Angelelli Date: Mon, 27 May 2019 23:36:31 +0200 Subject: Expose QQuickPath::pointAtPercent as Q_INVOKABLE So it can be used in QML. Useful in combination with ShapePath, in order to add graphics along a Shape. [ChangeLog][QtQuick][Shapes] Exposed QQuickPath::pointAtPercent as invokable in QML. Change-Id: Ia8aeb2b74003410ce16d9d2a0c62d79a021530af Reviewed-by: Shawn Rutledge --- tests/auto/quick/qquickpath/tst_qquickpath.cpp | 30 +++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'tests/auto/quick/qquickpath/tst_qquickpath.cpp') diff --git a/tests/auto/quick/qquickpath/tst_qquickpath.cpp b/tests/auto/quick/qquickpath/tst_qquickpath.cpp index bac1fc5861..c89ce730a8 100644 --- a/tests/auto/quick/qquickpath/tst_qquickpath.cpp +++ b/tests/auto/quick/qquickpath/tst_qquickpath.cpp @@ -99,13 +99,13 @@ void tst_QuickPath::arc(QSizeF scale) QPainterPath path = obj->path(); QVERIFY(path != QPainterPath()); - QPointF pos = obj->pointAt(0); + QPointF pos = obj->pointAtPercent(0); QCOMPARE(pos, QPointF(0,0)); - pos = obj->pointAt(.25); + pos = obj->pointAtPercent(.25); compare(pos, scale, __LINE__, 38.9244897744, 7.85853964341); - pos = obj->pointAt(.75); + pos = obj->pointAtPercent(.75); compare(pos, scale, __LINE__, 92.141460356592, 61.07551022559); - pos = obj->pointAt(1); + pos = obj->pointAtPercent(1); QCOMPARE(pos, QPointF(100 * scale.width(), 100 * scale.height())); } @@ -140,18 +140,18 @@ void tst_QuickPath::angleArc(QSizeF scale) QPainterPath path = obj->path(); QVERIFY(path != QPainterPath()); - QPointF pos = obj->pointAt(0); + QPointF pos = obj->pointAtPercent(0); compare(pos, scale, __LINE__, 135.35533905867, 135.35533905867); - pos = obj->pointAt(.25); + pos = obj->pointAtPercent(.25); compare(pos, scale, __LINE__, 119.46222180396, 146.07068621369); - pos = obj->pointAt(.75); + pos = obj->pointAtPercent(.75); compare(pos, scale, __LINE__, 80.537778196007, 146.07068621366); - pos = obj->pointAt(1); + pos = obj->pointAtPercent(1); compare(pos, scale, __LINE__, 64.644660941173, 135.35533905867); // if moveToStart is false, we should have a line starting from startX/Y arc->setMoveToStart(false); - pos = obj->pointAt(0); + pos = obj->pointAtPercent(0); QCOMPARE(pos, QPointF(0,0)); } @@ -280,13 +280,13 @@ void tst_QuickPath::svg(QSizeF scale) QPainterPath path = obj->path(); QVERIFY(path != QPainterPath()); - QPointF pos = obj->pointAt(0); + QPointF pos = obj->pointAtPercent(0); QCOMPARE(pos, QPointF(200 * scale.width(),300 * scale.height())); - pos = obj->pointAt(.25); + pos = obj->pointAtPercent(.25); QCOMPARE(pos.toPoint(), QPoint(400 * scale.width(),175 * scale.height())); //fuzzy compare - pos = obj->pointAt(.75); + pos = obj->pointAtPercent(.75); QCOMPARE(pos.toPoint(), QPoint(800 * scale.width(),425 * scale.height())); //fuzzy compare - pos = obj->pointAt(1); + pos = obj->pointAtPercent(1); QCOMPARE(pos, QPointF(1000 * scale.width(),300 * scale.height())); } @@ -329,10 +329,10 @@ void tst_QuickPath::line(QSizeF scale) for (int i = 0; i < 167; ++i) { qreal t = i / 167.0; - QPointF p1 = path1->pointAt(t); + QPointF p1 = path1->pointAtPercent(t); QCOMPARE(p1.x(), p1.y()); - QPointF p2 = path2->pointAt(t); + QPointF p2 = path2->pointAtPercent(t); QCOMPARE(p1.toPoint(), p2.toPoint()); } } -- cgit v1.2.3