1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #include "locationvaluetypehelper_p.h"
38 #include <QVariantMap>
39 #include <QtQml/QQmlInfo>
40 #include <private/qqmlengine_p.h>
41 #include <private/qv4scopedvalue_p.h>
42 #include <private/qv4arrayobject_p.h>
43 
44 
parseCoordinate(const QJSValue & value,bool * ok)45 QGeoCoordinate parseCoordinate(const QJSValue &value, bool *ok)
46 {
47     QGeoCoordinate c;
48     if (ok)
49         *ok = false;
50 
51     if (value.isObject()) {
52         if (value.hasProperty(QStringLiteral("latitude")))
53             c.setLatitude(value.property(QStringLiteral("latitude")).toNumber());
54         if (value.hasProperty(QStringLiteral("longitude")))
55             c.setLongitude(value.property(QStringLiteral("longitude")).toNumber());
56         if (value.hasProperty(QStringLiteral("altitude")))
57             c.setAltitude(value.property(QStringLiteral("altitude")).toNumber());
58 
59         if (ok)
60             *ok = true;
61     }
62 
63     return c;
64 }
65 
parseCoordinate(const QVariant & value,bool * ok)66 QGeoCoordinate parseCoordinate(const QVariant &value, bool *ok)
67 {
68     QGeoCoordinate c;
69     if (ok)
70         *ok = false;
71 
72     if (value.canConvert<QGeoCoordinate>()) {
73         c = value.value<QGeoCoordinate>();
74         if (ok)
75             *ok = true;
76     } else if (value.type() == QVariant::Map) {
77         const QVariantMap &map = value.toMap();
78 
79         if (map.contains(QStringLiteral("latitude")))
80             c.setLatitude(map.value(QStringLiteral("latitude")).toDouble());
81         if (map.contains(QStringLiteral("longitude")))
82             c.setLongitude(map.value(QStringLiteral("longitude")).toDouble());
83         if (map.contains(QStringLiteral("altitude")))
84             c.setAltitude(map.value(QStringLiteral("altitude")).toDouble());
85 
86         if (ok)
87             *ok = c.isValid(); // Not considering the case where the map is valid but containing NaNs.
88     }
89 
90     return c;
91 }
92 
parseRectangle(const QJSValue & value,bool * ok)93 QGeoRectangle parseRectangle(const QJSValue &value, bool *ok)
94 {
95     QGeoRectangle r;
96 
97     *ok = false;
98 
99     if (value.isObject()) {
100         if (value.hasProperty(QStringLiteral("bottomLeft"))) {
101             QGeoCoordinate c = parseCoordinate(value.property(QStringLiteral("bottomLeft")), ok);
102             if (*ok)
103                 r.setBottomLeft(c);
104         }
105         if (value.hasProperty(QStringLiteral("bottomRight"))) {
106             QGeoCoordinate c = parseCoordinate(value.property(QStringLiteral("bottomRight")), ok);
107             if (*ok)
108                 r.setBottomRight(c);
109         }
110         if (value.hasProperty(QStringLiteral("topLeft"))) {
111             QGeoCoordinate c = parseCoordinate(value.property(QStringLiteral("topLeft")), ok);
112             if (*ok)
113                 r.setTopLeft(c);
114         }
115         if (value.hasProperty(QStringLiteral("topRight"))) {
116             QGeoCoordinate c = parseCoordinate(value.property(QStringLiteral("topRight")), ok);
117             if (*ok)
118                 r.setTopRight(c);
119         }
120         if (value.hasProperty(QStringLiteral("center"))) {
121             QGeoCoordinate c = parseCoordinate(value.property(QStringLiteral("center")), ok);
122             if (*ok)
123                 r.setCenter(c);
124         }
125         if (value.hasProperty(QStringLiteral("height")))
126             r.setHeight(value.property(QStringLiteral("height")).toNumber());
127         if (value.hasProperty(QStringLiteral("width")))
128             r.setWidth(value.property(QStringLiteral("width")).toNumber());
129     }
130 
131     return r;
132 }
133 
parseCircle(const QJSValue & value,bool * ok)134 QGeoCircle parseCircle(const QJSValue &value, bool *ok)
135 {
136     QGeoCircle c;
137 
138     *ok = false;
139 
140     if (value.isObject()) {
141         if (value.hasProperty(QStringLiteral("center"))) {
142             QGeoCoordinate coord = parseCoordinate(value.property(QStringLiteral("center")), ok);
143             if (*ok)
144                 c.setCenter(coord);
145         }
146         if (value.hasProperty(QStringLiteral("radius")))
147             c.setRadius(value.property(QStringLiteral("radius")).toNumber());
148     }
149 
150     return c;
151 }
152 
fromList(const QObject * object,const QList<QGeoCoordinate> & list)153 QJSValue fromList(const QObject *object, const QList<QGeoCoordinate> &list)
154 {
155     QQmlContext *context = QQmlEngine::contextForObject(object);
156     QQmlEngine *engine = context->engine();
157     QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine);
158 
159     QV4::Scope scope(v4);
160     QV4::Scoped<QV4::ArrayObject> pathArray(scope, v4->newArrayObject(list.length()));
161     int i = 0;
162     for (const auto &val : list) {
163         QV4::ScopedValue cv(scope, v4->fromVariant(QVariant::fromValue(val)));
164         pathArray->put(i++, cv);
165     }
166 
167     return QJSValue(v4, pathArray.asReturnedValue());
168 }
169 
toList(const QObject * object,const QJSValue & value)170 QList<QGeoCoordinate> toList(const QObject *object, const QJSValue &value)
171 {
172     if (!value.isArray())
173         return {};
174 
175     QList<QGeoCoordinate> pathList;
176     quint32 length = value.property(QStringLiteral("length")).toUInt();
177     for (quint32 i = 0; i < length; ++i) {
178         bool ok;
179         QGeoCoordinate c = parseCoordinate(value.property(i), &ok);
180 
181         if (!ok || !c.isValid()) {
182             qmlWarning(object) << "Unsupported path type";
183             return {};
184         }
185 
186         pathList.append(c);
187     }
188 
189     return pathList;
190 }
191