aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaolo Angelelli <paolo.angelelli@qt.io>2019-05-27 23:36:31 +0200
committerpaolo <paolo.angelelli@qt.io>2019-06-25 15:35:05 +0200
commitab0583eb432bd44d32605e8594cdb3fe2c0a7b8f (patch)
tree5d9348a71d6e8f1046ad1b2e053c6f88627e448b /src
parent1762513bad4ca36d8fd78fc87d6e3979391f4626 (diff)
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 <shawn.rutledge@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/quick/items/qquickpathview.cpp8
-rw-r--r--src/quick/util/qquickpath.cpp23
-rw-r--r--src/quick/util/qquickpath_p.h2
3 files changed, 26 insertions, 7 deletions
diff --git a/src/quick/items/qquickpathview.cpp b/src/quick/items/qquickpathview.cpp
index e4480b335a..4e81573356 100644
--- a/src/quick/items/qquickpathview.cpp
+++ b/src/quick/items/qquickpathview.cpp
@@ -438,7 +438,7 @@ void QQuickPathViewPrivate::updateItem(QQuickItem *item, qreal percent)
att->setOnPath(percent < 1.0);
}
QQuickItemPrivate::get(item)->setCulled(percent >= 1.0);
- QPointF pf = path->pointAt(qMin(percent, qreal(1.0)));
+ QPointF pf = path->pointAtPercent(qMin(percent, qreal(1.0)));
item->setX(pf.x() - item->width()/2);
item->setY(pf.y() - item->height()/2);
}
@@ -1584,12 +1584,12 @@ QPointF QQuickPathViewPrivate::pointNear(const QPointF &point, qreal *nearPercen
qreal res = pathLength / samples;
qreal mindist = 1e10; // big number
- QPointF nearPoint = path->pointAt(0);
+ QPointF nearPoint = path->pointAtPercent(0);
qreal nearPc = 0;
// get rough pos
for (qreal i=1; i < samples; i++) {
- QPointF pt = path->pointAt(i/samples);
+ QPointF pt = path->pointAtPercent(i/samples);
QPointF diff = pt - point;
qreal dist = diff.x()*diff.x() + diff.y()*diff.y();
if (dist < mindist) {
@@ -1602,7 +1602,7 @@ QPointF QQuickPathViewPrivate::pointNear(const QPointF &point, qreal *nearPercen
// now refine
qreal approxPc = nearPc;
for (qreal i = approxPc-1.0; i < approxPc+1.0; i += 1/(2*res)) {
- QPointF pt = path->pointAt(i/samples);
+ QPointF pt = path->pointAtPercent(i/samples);
QPointF diff = pt - point;
qreal dist = diff.x()*diff.x() + diff.y()*diff.y();
if (dist < mindist) {
diff --git a/src/quick/util/qquickpath.cpp b/src/quick/util/qquickpath.cpp
index f0705d19bd..840a8c6a2c 100644
--- a/src/quick/util/qquickpath.cpp
+++ b/src/quick/util/qquickpath.cpp
@@ -900,9 +900,28 @@ QPointF QQuickPath::backwardsPointAt(const QPainterPath &path, const qreal &path
return QPointF(0,0);
}
-QPointF QQuickPath::pointAt(qreal p) const
+/*!
+ \qmlmethod point Path::pointAtPercent(real t)
+
+ Returns the point at the percentage \a t of the current path.
+ The argument \a t has to be between 0 and 1.
+
+ \note Similarly to other percent methods in \l QPainterPath,
+ the percentage measurement is not linear with regards to the length,
+ if curves are present in the path.
+ When curves are present, the percentage argument is mapped to the \c t
+ parameter of the Bezier equations.
+
+ \sa QPainterPath::pointAt
+
+ \since QtQuick 2.14
+*/
+QPointF QQuickPath::pointAtPercent(qreal t) const
{
Q_D(const QQuickPath);
+ if (d->isShapePath) // this since ShapePath does not calculate the length at all,
+ return d->_path.pointAtPercent(t); // in order to be faster.
+
if (d->_pointCache.isEmpty()) {
createPointCache();
if (d->_pointCache.isEmpty())
@@ -910,7 +929,7 @@ QPointF QQuickPath::pointAt(qreal p) const
}
const int segmentCount = d->_pointCache.size() - 1;
- qreal idxf = p*segmentCount;
+ qreal idxf = t*segmentCount;
int idx1 = qFloor(idxf);
qreal delta = idxf - idx1;
if (idx1 > segmentCount)
diff --git a/src/quick/util/qquickpath_p.h b/src/quick/util/qquickpath_p.h
index 5a939f49cb..998f4e3123 100644
--- a/src/quick/util/qquickpath_p.h
+++ b/src/quick/util/qquickpath_p.h
@@ -488,7 +488,7 @@ public:
QPainterPath path() const;
QStringList attributes() const;
qreal attributeAt(const QString &, qreal) const;
- QPointF pointAt(qreal) const;
+ Q_REVISION(14) Q_INVOKABLE QPointF pointAtPercent(qreal t) const;
QPointF sequentialPointAt(qreal p, qreal *angle = nullptr) const;
void invalidateSequentialHistory() const;