diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-08-16 11:18:38 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-09-02 22:23:03 +0200 |
commit | 811b15bd161d12e5c85e093f9f492a0c4fa278d6 (patch) | |
tree | 1efe0017f207b62d799711be410adc7f1db1bfb8 /src/quick/util/qquickpath.cpp | |
parent | 6dc319155a282cd274a93887c5b83c8ed8b82d90 (diff) |
PathMultiline: allow lists/vectors of polygons or point lists/vectors
If a C++ model object can make a vector of vectors of points available
directly, and it is bound to a PathMultiline's paths property to provide
the view layer, it's a waste of time to convert it to a QVariantList of
QVariantLists and back again. Changing the type of the property to
QVariant instead of QVariantList enables an extensible set of supported
types: all those that make sense.
Fixes: QTBUG-77929
Change-Id: If749c2171173e7b9933fc9ecdf6d2741dc1c7500
Reviewed-by: Paolo Angelelli <paolo.angelelli@qt.io>
Diffstat (limited to 'src/quick/util/qquickpath.cpp')
-rw-r--r-- | src/quick/util/qquickpath.cpp | 70 |
1 files changed, 38 insertions, 32 deletions
diff --git a/src/quick/util/qquickpath.cpp b/src/quick/util/qquickpath.cpp index 6d4e469e44..bebe9b2563 100644 --- a/src/quick/util/qquickpath.cpp +++ b/src/quick/util/qquickpath.cpp @@ -2532,48 +2532,54 @@ void QQuickPathPolyline::addToPath(QPainterPath &path, const QQuickPathData &/*d \qmlproperty list<list<point>> QtQuick::PathMultiline::paths This property defines the vertices of the polylines. + + It can be a JS array of JS arrays of points constructed with \c Qt.point(), + a QList or QVector of QPolygonF, or QVector<QVector<QPointF>>. + If you are binding this to a custom property in some C++ object, + QVector<QPolygonF> or QVector<QVector<QPointF>> is the most + appropriate type to use. */ QQuickPathMultiline::QQuickPathMultiline(QObject *parent) : QQuickCurve(parent) { } -QVariantList QQuickPathMultiline::paths() const +QVariant QQuickPathMultiline::paths() const { - QVariantList res; - for (int j = 0; j < m_paths.length(); ++j) { - const QVector<QPointF> &path = m_paths.at(j); - QVariantList p; - for (int i = 0; i < path.length(); ++i) { - const QPointF &c = path.at(i); - p.append(QVariant::fromValue(c)); - } - res.append(p); - } - return res; -} - -void QQuickPathMultiline::setPaths(const QVariantList &paths) -{ - QVector<QVector<QPointF>> pathsList; - for (int j = 0; j < paths.length(); ++j) { - if (paths.at(j).type() != QVariant::List) - qWarning() << "QQuickPathMultiLine::setPaths: elements in argument not of type List"; - QVariantList path = paths.at(j).toList(); - QVector<QPointF> l; - for (int i = 0; i < path.length(); ++i) { - const QVariant &element = path.at(i); - const QVariant::Type elementType = element.type(); - if (elementType == QVariant::PointF || elementType == QVariant::Point) { - const QPointF c = element.toPointF(); - l.append(c); + return QVariant::fromValue(m_paths); +} + +void QQuickPathMultiline::setPaths(const QVariant &paths) +{ + if (paths.canConvert<QVector<QPolygonF>>()) { + const QVector<QPolygonF> pathPolygons = paths.value<QVector<QPolygonF>>(); + QVector<QVector<QPointF>> pathVectors; + for (const QPolygonF &p : pathPolygons) + pathVectors << p; + setPaths(pathVectors); + } else if (paths.canConvert<QVector<QVector<QPointF>>>()) { + setPaths(paths.value<QVector<QVector<QPointF>>>()); + } else if (paths.canConvert<QVariantList>()) { + // This handles cases other than QVector<QPolygonF> or QVector<QVector<QPointF>>, such as + // QList<QVector<QPointF>>, QList<QList<QPointF>>, QVariantList of QVector<QPointF>, + // QVariantList of QVariantList of QPointF, QVector<QList<QPoint>> etc. + QVector<QVector<QPointF>> pathsList; + QVariantList vll = paths.value<QVariantList>(); + for (const QVariant &v : vll) { + QVariantList vl = v.value<QVariantList>(); + QVector<QPointF> l; + for (const QVariant &point : vl) { + if (point.canConvert<QPointF>()) + l.append(point.toPointF()); } + if (l.size() >= 2) + pathsList.append(l); } - if (l.size() >= 2) - pathsList.append(l); + setPaths(pathsList); + } else { + qWarning() << "PathMultiline: paths of type" << paths.type() << "not supported"; + setPaths(QVector<QVector<QPointF>>()); } - - setPaths(pathsList); } void QQuickPathMultiline::setPaths(const QVector<QVector<QPointF>> &paths) |