Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
locationsingleton.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include <QtPositioning/private/qwebmercator_p.h>
7#include <QtPositioning/private/qdoublevector2d_p.h>
8#include <QDebug>
9
11
12static QGeoCoordinate parseCoordinate(const QJSValue &value, bool *ok)
13{
14 QGeoCoordinate c;
15
16 if (value.isObject()) {
17 if (value.hasProperty(QStringLiteral("latitude")))
18 c.setLatitude(value.property(QStringLiteral("latitude")).toNumber());
19 if (value.hasProperty(QStringLiteral("longitude")))
20 c.setLongitude(value.property(QStringLiteral("longitude")).toNumber());
21 if (value.hasProperty(QStringLiteral("altitude")))
22 c.setAltitude(value.property(QStringLiteral("altitude")).toNumber());
23
24 if (ok)
25 *ok = true;
26 } else if (ok) {
27 *ok = false;
28 }
29
30 return c;
31}
32
33
34/*!
35 \qmltype QtPositioning
36 \inqmlmodule QtPositioning
37 \since 5.2
38
39 \brief The QtPositioning global object provides useful functions for working with location-based
40 types in QML.
41
42 \qml
43 import QtPositioning
44
45 Item {
46 property var coordinate: QtPositioning.coordinate(-27.5, 153.1)
47 }
48 \endqml
49*/
50
51LocationSingleton::LocationSingleton(QObject *parent)
52: QObject(parent)
53{
54}
55
56/*!
57 \qmlmethod coordinate QtPositioning::coordinate()
58
59 Constructs an invalid coordinate.
60
61*/
62QGeoCoordinate LocationSingleton::coordinate() const
63{
64 return QGeoCoordinate();
65}
66
67/*!
68 \qmlmethod coordinate QtPositioning::coordinate(real latitude, real longitude, real altitude) const
69
70 Constructs a coordinate with the specified \a latitude, \a longitude and optional \a altitude.
71 Both \a latitude and \a longitude must be valid, otherwise an invalid coordinate is returned.
72
73 \sa {coordinate}
74*/
75QGeoCoordinate LocationSingleton::coordinate(double latitude, double longitude, double altitude) const
76{
77 return QGeoCoordinate(latitude, longitude, altitude);
78}
79
80/*!
81 \qmlmethod geoShape QtPositioning::shape() const
82
83 Constructs an invalid geoShape.
84
85 \sa {geoShape}
86*/
87QGeoShape LocationSingleton::shape() const
88{
89 return QGeoShape();
90}
91
92/*!
93 \qmlmethod geoRectangle QtPositioning::rectangle() const
94
95 Constructs an invalid geoRectangle.
96
97 \sa {geoRectangle}
98*/
99QGeoRectangle LocationSingleton::rectangle() const
100{
101 return QGeoRectangle();
102}
103
104/*!
105 \qmlmethod geoRectangle QtPositioning::rectangle(coordinate center, real width, real height) const
106
107 Constructs a geoRectangle centered at \a center with a width of \a width degrees and a hight of
108 \a height degrees.
109
110 \sa {geoRectangle}
111*/
112QGeoRectangle LocationSingleton::rectangle(const QGeoCoordinate &center,
113 double width, double height) const
114{
115 return QGeoRectangle(center, width, height);
116}
117
118/*!
119 \qmlmethod geoRectangle QtPositioning::rectangle(coordinate topLeft, coordinate bottomRight) const
120
121 Constructs a geoRectangle with its top left corner positioned at \a topLeft and its bottom
122 right corner positioned at \a {bottomRight}.
123
124 \sa {geoRectangle}
125*/
126QGeoRectangle LocationSingleton::rectangle(const QGeoCoordinate &topLeft,
127 const QGeoCoordinate &bottomRight) const
128{
129 return QGeoRectangle(topLeft, bottomRight);
130}
131
132/*!
133 \qmlmethod geoRectangle QtPositioning::rectangle(list<coordinate> coordinates) const
134
135 Constructs a geoRectangle from the list of coordinates, the returned list is the smallest possible
136 containing all the coordinates.
137
138 \sa {geoRectangle}
139*/
140QGeoRectangle LocationSingleton::rectangle(const QVariantList &coordinates) const
141{
142 QList<QGeoCoordinate> internalCoordinates;
143 for (const auto &coordinate : coordinates) {
144 if (coordinate.canConvert<QGeoCoordinate>())
145 internalCoordinates << coordinate.value<QGeoCoordinate>();
146 }
147
148 return QGeoRectangle(internalCoordinates);
149}
150
151/*!
152 \qmlmethod geoCircle QtPositioning::circle() const
153
154 Constructs an invalid geoCircle.
155
156 \sa {geoCircle}
157*/
158QGeoCircle LocationSingleton::circle() const
159{
160 return QGeoCircle();
161}
162
163/*!
164 \qmlmethod geoCircle QtPositioning::circle(coordinate center, real radius) const
165
166 Constructs a geoCircle centered at \a center with a radius of \a radius meters.
167*/
168QGeoCircle LocationSingleton::circle(const QGeoCoordinate &center, qreal radius) const
169{
170 return QGeoCircle(center, radius);
171}
172
173/*!
174 \qmlmethod geoPath QtPositioning::path() const
175
176 Constructs an empty geoPath.
177
178 \sa {geoPath}
179 \since 5.9
180*/
181QGeoPath LocationSingleton::path() const
182{
183 return QGeoPath();
184}
185
186/*!
187 \qmlmethod geoPath QtPositioning::path(list<coordinate> coordinates, real width) const
188
189 Constructs a geoPath from coordinates and width.
190
191 \sa {geoPath}
192 \since 5.9
193*/
194QGeoPath LocationSingleton::path(const QJSValue &value, qreal width) const
195{
196 QList<QGeoCoordinate> pathList;
197
198 if (value.isArray()) {
199 quint32 length = value.property(QStringLiteral("length")).toUInt();
200 for (quint32 i = 0; i < length; ++i) {
201 bool ok = false;
202 QGeoCoordinate c = parseCoordinate(value.property(i), &ok);
203
204 if (!ok || !c.isValid()) {
205 pathList.clear(); // aborting
206 break;
207 }
208
209 pathList.append(c);
210 }
211 }
212
213 return QGeoPath(pathList, width);
214}
215
216/*!
217 \qmlmethod geoPolygon QtPositioning::polygon() const
218
219 Constructs an empty polygon.
220
221 \sa {geoPolygon}
222 \since 5.10
223*/
224QGeoPolygon LocationSingleton::polygon() const
225{
226 return QGeoPolygon();
227}
228
229/*!
230 \qmlmethod geoPolygon QtPositioning::polygon(list<coordinate> coordinates) const
231
232 Constructs a polygon from coordinates.
233
234 \sa {geoPolygon}
235 \since 5.10
236*/
237QGeoPolygon LocationSingleton::polygon(const QVariantList &coordinates) const
238{
239 QList<QGeoCoordinate> internalCoordinates;
240 for (const auto &coordinate : coordinates) {
241 if (coordinate.canConvert<QGeoCoordinate>())
242 internalCoordinates << coordinate.value<QGeoCoordinate>();
243 }
244
245 return QGeoPolygon(internalCoordinates);
246}
247
248/*!
249 \qmlmethod geoPolygon QtPositioning::polygon(list<coordinate> perimeter, list<list<coordinate>> holes) const
250
251 Constructs a polygon from coordinates for perimeter and inner holes.
252
253 \sa {geoPolygon}
254 \since 5.12
255*/
256QGeoPolygon LocationSingleton::polygon(const QVariantList &perimeter, const QVariantList &holes) const
257{
258 QList<QGeoCoordinate> internalCoordinates;
259 for (const auto &coordinate : perimeter) {
260 if (coordinate.canConvert<QGeoCoordinate>())
261 internalCoordinates << coordinate.value<QGeoCoordinate>();
262 }
263 QGeoPolygon poly(internalCoordinates);
264
265 for (const auto &h : holes) {
266 if (h.metaType().id() == QMetaType::QVariantList) {
267 QList<QGeoCoordinate> hole;
268 const QVariantList &holeData = h.toList();
269 for (const auto &coord : holeData) {
270 if (coord.canConvert<QGeoCoordinate>())
271 hole << coord.value<QGeoCoordinate>();
272 }
273 if (!hole.isEmpty())
274 poly.addHole(hole);
275 }
276 }
277
278 return poly;
279}
280
281/*!
282 \qmlmethod geoCircle QtPositioning::shapeToCircle(geoShape shape) const
283
284 Converts \a shape to a geoCircle.
285
286 \sa {geoCircle}
287 \since 5.5
288*/
289QGeoCircle LocationSingleton::shapeToCircle(const QGeoShape &shape) const
290{
291 return QGeoCircle(shape);
292}
293
294/*!
295 \qmlmethod geoRectangle QtPositioning::shapeToRectangle(geoShape shape) const
296
297 Converts \a shape to a geoRectangle.
298
299 \sa {geoRectangle}
300 \since 5.5
301*/
302QGeoRectangle LocationSingleton::shapeToRectangle(const QGeoShape &shape) const
303{
304 return QGeoRectangle(shape);
305}
306
307/*!
308 \qmlmethod geoPath QtPositioning::shapeToPath(geoShape shape) const
309
310 Converts \a shape to a geoPath.
311
312 \sa {geoPath}
313 \since 5.9
314*/
315QGeoPath LocationSingleton::shapeToPath(const QGeoShape &shape) const
316{
317 return QGeoPath(shape);
318}
319
320/*!
321 \qmlmethod geoPolygon QtPositioning::shapeToPolygon(geoShape shape) const
322
323 Converts \a shape to a polygon.
324
325 \sa {geoPolygon}
326 \since 5.10
327*/
328QGeoPolygon LocationSingleton::shapeToPolygon(const QGeoShape &shape) const
329{
330 return QGeoPolygon(shape);
331}
332
333/*!
334 \qmlmethod coordinate QtPositioning::mercatorToCoord(point mercator) const
335
336 Converts a \a mercator coordinate into a latitude-longitude coordinate.
337
338 \sa {coordToMercator}
339 \since 5.12
340*/
341QGeoCoordinate LocationSingleton::mercatorToCoord(const QPointF &mercator) const
342{
343 return QWebMercator::mercatorToCoord(QDoubleVector2D(mercator.x(), mercator.y()));
344}
345
346/*!
347 \qmlmethod point QtPositioning::coordToMercator(coordinate coord) const
348
349 Converts a coordinate \a coord into a mercator coordinate and returns it.
350 \sa {mercatorToCoord}
351 \since 5.12
352*/
353QPointF LocationSingleton::coordToMercator(const QGeoCoordinate &coord) const
354{
355 return QWebMercator::coordToMercator(coord).toPointF();
356}
357
358QT_END_NAMESPACE
359
360#include "moc_locationsingleton_p.cpp"
static QT_BEGIN_NAMESPACE QGeoCoordinate parseCoordinate(const QJSValue &value, bool *ok)
Combined button and popup list for selecting options.