diff options
author | Paolo Angelelli <paolo.angelelli@qt.io> | 2018-01-12 18:12:27 +0100 |
---|---|---|
committer | Paolo Angelelli <paolo.angelelli@qt.io> | 2018-01-22 19:17:44 +0000 |
commit | 723b69ecd818e42cf3f0b63e69872edc847be549 (patch) | |
tree | f5357708a73d52aa41d6d557e594f1f7ebe60f1a /src | |
parent | 20c21955c2ccfd2bf5ec285b8b488f3bf89c269b (diff) | |
download | qtlocation-723b69ecd818e42cf3f0b63e69872edc847be549.tar.gz |
Move QML/Cpp list conversions into locationvaluetypehelper
Change-Id: Iee7f48f7d24b40a2824775654030397617c40640
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src')
4 files changed, 47 insertions, 57 deletions
diff --git a/src/location/declarativemaps/locationvaluetypehelper.cpp b/src/location/declarativemaps/locationvaluetypehelper.cpp index 5f75e225..cda37b5f 100644 --- a/src/location/declarativemaps/locationvaluetypehelper.cpp +++ b/src/location/declarativemaps/locationvaluetypehelper.cpp @@ -36,6 +36,8 @@ #include "locationvaluetypehelper_p.h" #include <QVariantMap> +#include <QtQml/QQmlInfo> +#include <private/qqmlengine_p.h> QGeoCoordinate parseCoordinate(const QJSValue &value, bool *ok) @@ -145,3 +147,42 @@ QGeoCircle parseCircle(const QJSValue &value, bool *ok) return c; } + +QJSValue fromList(const QObject *object, const QList<QGeoCoordinate> &list) +{ + QQmlContext *context = QQmlEngine::contextForObject(object); + QQmlEngine *engine = context->engine(); + QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine); + + QV4::Scope scope(v4); + QV4::Scoped<QV4::ArrayObject> pathArray(scope, v4->newArrayObject(list.length())); + int i = 0; + for (const auto &val : list) { + QV4::ScopedValue cv(scope, v4->fromVariant(QVariant::fromValue(val))); + pathArray->putIndexed(i++, cv); + } + + return QJSValue(v4, pathArray.asReturnedValue()); +} + +QList<QGeoCoordinate> toList(const QObject *object, const QJSValue &value) +{ + if (!value.isArray()) + return {}; + + QList<QGeoCoordinate> pathList; + quint32 length = value.property(QStringLiteral("length")).toUInt(); + for (quint32 i = 0; i < length; ++i) { + bool ok; + QGeoCoordinate c = parseCoordinate(value.property(i), &ok); + + if (!ok || !c.isValid()) { + qmlWarning(object) << "Unsupported path type"; + return {}; + } + + pathList.append(c); + } + + return pathList; +} diff --git a/src/location/declarativemaps/locationvaluetypehelper_p.h b/src/location/declarativemaps/locationvaluetypehelper_p.h index 09f3ab06..7b005fe6 100644 --- a/src/location/declarativemaps/locationvaluetypehelper_p.h +++ b/src/location/declarativemaps/locationvaluetypehelper_p.h @@ -58,5 +58,6 @@ QGeoCoordinate parseCoordinate(const QJSValue &value, bool *ok = nullptr); QGeoCoordinate parseCoordinate(const QVariant &value, bool *ok = nullptr); QGeoRectangle parseRectangle(const QJSValue &value, bool *ok); QGeoCircle parseCircle(const QJSValue &value, bool *ok); - +QJSValue fromList(const QObject *object, const QList<QGeoCoordinate> &list); +QList<QGeoCoordinate> toList(const QObject *object, const QJSValue &value); #endif diff --git a/src/location/declarativemaps/qdeclarativepolygonmapitem.cpp b/src/location/declarativemaps/qdeclarativepolygonmapitem.cpp index 7460a376..8c7afc17 100644 --- a/src/location/declarativemaps/qdeclarativepolygonmapitem.cpp +++ b/src/location/declarativemaps/qdeclarativepolygonmapitem.cpp @@ -380,20 +380,7 @@ void QDeclarativePolygonMapItem::setMap(QDeclarativeGeoMap *quickMap, QGeoMap *m */ QJSValue QDeclarativePolygonMapItem::path() const { - QQmlContext *context = QQmlEngine::contextForObject(this); - QQmlEngine *engine = context->engine(); - QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine); - - QV4::Scope scope(v4); - QV4::Scoped<QV4::ArrayObject> pathArray(scope, v4->newArrayObject(geopath_.path().length())); - for (int i = 0; i < geopath_.path().length(); ++i) { - const QGeoCoordinate &c = geopath_.coordinateAt(i); - - QV4::ScopedValue cv(scope, v4->fromVariant(QVariant::fromValue(c))); - pathArray->putIndexed(i, cv); - } - - return QJSValue(v4, pathArray.asReturnedValue()); + return fromList(this, geopath_.path()); } void QDeclarativePolygonMapItem::setPath(const QJSValue &value) @@ -401,19 +388,7 @@ void QDeclarativePolygonMapItem::setPath(const QJSValue &value) if (!value.isArray()) return; - QList<QGeoCoordinate> pathList; - quint32 length = value.property(QStringLiteral("length")).toUInt(); - for (quint32 i = 0; i < length; ++i) { - bool ok; - QGeoCoordinate c = parseCoordinate(value.property(i), &ok); - - if (!ok || !c.isValid()) { - qmlWarning(this) << "Unsupported path type"; - return; - } - - pathList.append(c); - } + QList<QGeoCoordinate> pathList = toList(this, value); // Equivalent to QDeclarativePolylineMapItem::setPathFromGeoList if (geopath_.path() == pathList) diff --git a/src/location/declarativemaps/qdeclarativepolylinemapitem.cpp b/src/location/declarativemaps/qdeclarativepolylinemapitem.cpp index a97271aa..7c1b922f 100644 --- a/src/location/declarativemaps/qdeclarativepolylinemapitem.cpp +++ b/src/location/declarativemaps/qdeclarativepolylinemapitem.cpp @@ -455,20 +455,7 @@ void QDeclarativePolylineMapItem::setMap(QDeclarativeGeoMap *quickMap, QGeoMap * QJSValue QDeclarativePolylineMapItem::path() const { - QQmlContext *context = QQmlEngine::contextForObject(this); - QQmlEngine *engine = context->engine(); - QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine); - - QV4::Scope scope(v4); - QV4::Scoped<QV4::ArrayObject> pathArray(scope, v4->newArrayObject(geopath_.path().length())); - for (int i = 0; i < geopath_.path().length(); ++i) { - const QGeoCoordinate &c = geopath_.coordinateAt(i); - - QV4::ScopedValue cv(scope, v4->fromVariant(QVariant::fromValue(c))); - pathArray->putIndexed(i, cv); - } - - return QJSValue(v4, pathArray.asReturnedValue()); + return fromList(this, geopath_.path()); } void QDeclarativePolylineMapItem::setPath(const QJSValue &value) @@ -476,21 +463,7 @@ void QDeclarativePolylineMapItem::setPath(const QJSValue &value) if (!value.isArray()) return; - QList<QGeoCoordinate> pathList; - quint32 length = value.property(QStringLiteral("length")).toUInt(); - for (quint32 i = 0; i < length; ++i) { - bool ok; - QGeoCoordinate c = parseCoordinate(value.property(i), &ok); - - if (!ok || !c.isValid()) { - qmlWarning(this) << "Unsupported path type"; - return; - } - - pathList.append(c); - } - - setPathFromGeoList(pathList); + setPathFromGeoList(toList(this, value)); } /*! |