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 "qgeomap_p.h"
38#include "qgeomap_p_p.h"
39#include "qgeocameracapabilities_p.h"
40#include "qgeomappingmanagerengine_p.h"
41#include "qdeclarativegeomapitembase_p.h"
42#include "qgeomapobject_p.h"
43#include "qgeomapobject_p_p.h"
44#include <QtQuick/private/qquickitem_p.h>
45#include <QDebug>
46#include <QRectF>
47
48QT_BEGIN_NAMESPACE
49
50QGeoMap::QGeoMap(QGeoMapPrivate &dd, QObject *parent)
51 : QObject(dd,parent)
52{
53}
54
55QGeoMap::~QGeoMap()
56{
57 Q_D(QGeoMap);
58 clearParameters();
59 for (QGeoMapObject *p : d->mapObjects())
60 p->setMap(nullptr); // forces replacing pimpls with the default ones.
61}
62
63void QGeoMap::setViewportSize(const QSize& size)
64{
65 Q_D(QGeoMap);
66 if (size == d->m_viewportSize)
67 return;
68 d->m_viewportSize = size;
69 d->m_geoProjection->setViewportSize(size);
70 d->changeViewportSize(size);
71}
72
73QSize QGeoMap::viewportSize() const
74{
75 Q_D(const QGeoMap);
76 return d->m_viewportSize;
77}
78
79int QGeoMap::viewportWidth() const
80{
81 Q_D(const QGeoMap);
82 return d->m_viewportSize.width();
83}
84
85int QGeoMap::viewportHeight() const
86{
87 Q_D(const QGeoMap);
88 return d->m_viewportSize.height();
89}
90
91void QGeoMap::setCameraData(const QGeoCameraData &cameraData)
92{
93 Q_D(QGeoMap);
94 if (cameraData == d->m_cameraData)
95 return;
96 d->m_cameraData = cameraData;
97 d->m_geoProjection->setCameraData(cameraData, force: false);
98 d->changeCameraData(oldCameraData: cameraData);
99 emit cameraDataChanged(cameraData: d->m_cameraData);
100}
101
102void QGeoMap::setCameraCapabilities(const QGeoCameraCapabilities &cameraCapabilities)
103{
104 Q_D(QGeoMap);
105 d->setCameraCapabilities(cameraCapabilities);
106}
107
108bool QGeoMap::handleEvent(QEvent *event)
109{
110 Q_UNUSED(event);
111 return false;
112}
113
114bool QGeoMap::setBearing(qreal bearing, const QGeoCoordinate &coordinate) //FIXME visibleArea
115{
116 Q_D(QGeoMap);
117 bool res = d->m_geoProjection->setBearing(bearing, coordinate);
118 if (!res)
119 return false;
120
121 setCameraData(geoProjection().cameraData());
122 return true;
123}
124
125bool QGeoMap::anchorCoordinateToPoint(const QGeoCoordinate &coordinate, const QPointF &anchorPoint)
126{
127 Q_D(QGeoMap);
128 QGeoCoordinate newCenter = geoProjection().anchorCoordinateToPoint(coordinate, anchorPoint);
129 newCenter.setLatitude(qBound(min: d->m_minimumViewportLatitude, val: newCenter.latitude(), max: d->m_maximumViewportLatitude));
130 QGeoCameraData data = cameraData();
131 if (data.center() != newCenter) {
132 data.setCenter(newCenter);
133 setCameraData(data);
134 return true;
135 }
136 return false;
137}
138
139bool QGeoMap::fitViewportToGeoRectangle(const QGeoRectangle &rectangle, const QMargins &borders)
140{
141 Q_UNUSED(rectangle);
142 Q_UNUSED(borders);
143 return false;
144}
145
146QGeoShape QGeoMap::visibleRegion() const
147{
148 return geoProjection().visibleRegion();
149}
150
151const QGeoCameraData &QGeoMap::cameraData() const
152{
153 Q_D(const QGeoMap);
154 return d->m_cameraData;
155}
156
157void QGeoMap::setActiveMapType(const QGeoMapType type)
158{
159 Q_D(QGeoMap);
160 if (type == d->m_activeMapType)
161 return;
162 d->m_activeMapType = type;
163 d->setCameraCapabilities(d->m_engine->cameraCapabilities(mapId: type.mapId())); // emits
164 d->changeActiveMapType(mapType: type);
165 emit activeMapTypeChanged();
166}
167
168const QGeoMapType QGeoMap::activeMapType() const
169{
170 Q_D(const QGeoMap);
171 return d->m_activeMapType;
172}
173
174double QGeoMap::minimumZoom() const
175{
176 Q_D(const QGeoMap);
177 return d->m_geoProjection->minimumZoom();
178}
179
180double QGeoMap::maximumCenterLatitudeAtZoom(const QGeoCameraData &cameraData) const
181{
182 Q_D(const QGeoMap);
183 return d->maximumCenterLatitudeAtZoom(cameraData);
184}
185
186double QGeoMap::minimumCenterLatitudeAtZoom(const QGeoCameraData &cameraData) const
187{
188 Q_D(const QGeoMap);
189 return d->minimumCenterLatitudeAtZoom(cameraData);
190}
191
192double QGeoMap::mapWidth() const
193{
194 Q_D(const QGeoMap);
195 return d->mapWidth();
196}
197
198double QGeoMap::mapHeight() const
199{
200 Q_D(const QGeoMap);
201 return d->mapHeight();
202}
203
204const QGeoProjection &QGeoMap::geoProjection() const
205{
206 Q_D(const QGeoMap);
207 return *(d->m_geoProjection);
208}
209
210QGeoCameraCapabilities QGeoMap::cameraCapabilities() const
211{
212 Q_D(const QGeoMap);
213 return d->m_cameraCapabilities;
214}
215
216QGeoMap::Capabilities QGeoMap::capabilities() const
217{
218 return Capabilities(QGeoMap::SupportsNothing);
219}
220
221void QGeoMap::prefetchData()
222{
223
224}
225
226void QGeoMap::clearData()
227{
228
229}
230
231void QGeoMap::addParameter(QGeoMapParameter *param)
232{
233 Q_D(QGeoMap);
234 if (param && !d->m_mapParameters.contains(t: param)) {
235 d->m_mapParameters.append(t: param);
236 d->addParameter(param);
237 }
238}
239
240void QGeoMap::removeParameter(QGeoMapParameter *param)
241{
242 Q_D(QGeoMap);
243 if (param && d->m_mapParameters.contains(t: param)) {
244 d->removeParameter(param);
245 d->m_mapParameters.removeOne(t: param);
246 }
247}
248
249void QGeoMap::clearParameters()
250{
251 Q_D(QGeoMap);
252 for (QGeoMapParameter *p : qAsConst(t&: d->m_mapParameters))
253 d->removeParameter(param: p);
254 d->m_mapParameters.clear();
255}
256
257QGeoMap::ItemTypes QGeoMap::supportedMapItemTypes() const
258{
259 Q_D(const QGeoMap);
260 return d->supportedMapItemTypes();
261}
262
263void QGeoMap::addMapItem(QDeclarativeGeoMapItemBase *item)
264{
265 Q_D(QGeoMap);
266 if (item && !d->m_mapItems.contains(t: item) && d->supportedMapItemTypes() & item->itemType()) {
267 d->m_mapItems.append(t: item);
268 d->addMapItem(item);
269 }
270}
271
272void QGeoMap::removeMapItem(QDeclarativeGeoMapItemBase *item)
273{
274 Q_D(QGeoMap);
275 if (item && d->m_mapItems.contains(t: item)) {
276 d->removeMapItem(item);
277 d->m_mapItems.removeOne(t: item);
278 }
279}
280
281void QGeoMap::clearMapItems()
282{
283 Q_D(QGeoMap);
284 for (QDeclarativeGeoMapItemBase *p : d->m_mapItems)
285 d->removeMapItem(item: p);
286 d->m_mapItems.clear();
287}
288
289/*!
290 Fills obj with a backend-specific pimpl.
291*/
292bool QGeoMap::createMapObjectImplementation(QGeoMapObject *obj)
293{
294 Q_D(QGeoMap);
295 QExplicitlySharedDataPointer<QGeoMapObjectPrivate> pimpl =
296 QExplicitlySharedDataPointer<QGeoMapObjectPrivate>(d->createMapObjectImplementation(obj));
297 if (pimpl.constData())
298 return obj->setImplementation(pimpl);
299 return false;
300}
301
302/*!
303 To be called in ~QGeoMapObjectPrivate overrides, if needed
304*/
305void QGeoMap::removeMapObject(QGeoMapObject * /*obj*/)
306{
307}
308
309QList<QObject *> QGeoMap::mapObjectsAt(const QGeoCoordinate &/*coordinate*/) const
310{
311 return QList<QObject *>();
312}
313
314void QGeoMap::setItemToWindowTransform(const QTransform &itemToWindowTransform)
315{
316 Q_D(QGeoMap);
317 d->m_geoProjection->setItemToWindowTransform(itemToWindowTransform);
318}
319
320void QGeoMap::setVisibleArea(const QRectF &visibleArea)
321{
322 Q_D(QGeoMap);
323 const QRectF &va = d->visibleArea();
324 d->setVisibleArea(visibleArea);
325 if (va != d->visibleArea())
326 emit visibleAreaChanged();
327}
328
329QRectF QGeoMap::visibleArea() const
330{
331 Q_D(const QGeoMap);
332 return d->visibleArea();
333}
334
335QList<QGeoMapObject *> QGeoMap::mapObjects() const
336{
337 Q_D(const QGeoMap);
338 return d->mapObjects();
339}
340
341QString QGeoMap::copyrightsStyleSheet() const
342{
343 return QStringLiteral("#copyright-root { background: rgba(255, 255, 255, 128) }");
344}
345
346void QGeoMap::setAcceptedGestures(bool pan, bool flick, bool pinch, bool rotate, bool tilt)
347{
348 Q_UNUSED(pan);
349 Q_UNUSED(flick);
350 Q_UNUSED(pinch);
351 Q_UNUSED(rotate);
352 Q_UNUSED(tilt);
353}
354
355void QGeoMap::setCopyrightVisible(bool visible)
356{
357 Q_D(QGeoMap);
358 if (d->m_copyrightVisible == visible)
359 return;
360
361 d->m_copyrightVisible = visible;
362}
363
364QGeoMapPrivate::QGeoMapPrivate(QGeoMappingManagerEngine *engine, QGeoProjection *geoProjection)
365 : QObjectPrivate(),
366 m_geoProjection(geoProjection),
367 m_engine(engine),
368 m_activeMapType(QGeoMapType())
369{
370 // Setting the default camera caps without emitting anything
371 if (engine)
372 m_cameraCapabilities = m_engine->cameraCapabilities(mapId: m_activeMapType.mapId());
373}
374
375QGeoMapPrivate::~QGeoMapPrivate()
376{
377 if (m_geoProjection)
378 delete m_geoProjection;
379}
380
381void QGeoMapPrivate::setCameraCapabilities(const QGeoCameraCapabilities &cameraCapabilities)
382{
383 Q_Q(QGeoMap);
384 if (m_cameraCapabilities == cameraCapabilities)
385 return;
386 QGeoCameraCapabilities oldCaps = m_cameraCapabilities;
387 m_cameraCapabilities = cameraCapabilities;
388 emit q->cameraCapabilitiesChanged(oldCameraCapabilities: oldCaps);
389}
390
391const QGeoCameraCapabilities &QGeoMapPrivate::cameraCapabilities() const
392{
393 return m_cameraCapabilities;
394}
395
396const QGeoMapPrivate *QGeoMapPrivate::get(const QGeoMap &map)
397{
398 return map.d_func();
399}
400
401void QGeoMapPrivate::addParameter(QGeoMapParameter *param)
402{
403 Q_UNUSED(param);
404}
405
406void QGeoMapPrivate::removeParameter(QGeoMapParameter *param)
407{
408 Q_UNUSED(param);
409}
410
411QGeoMap::ItemTypes QGeoMapPrivate::supportedMapItemTypes() const
412{
413 return QGeoMap::NoItem;
414}
415
416void QGeoMapPrivate::addMapItem(QDeclarativeGeoMapItemBase *item)
417{
418 Q_UNUSED(item);
419}
420
421void QGeoMapPrivate::removeMapItem(QDeclarativeGeoMapItemBase *item)
422{
423 Q_UNUSED(item);
424}
425
426QGeoMapObjectPrivate *QGeoMapPrivate::createMapObjectImplementation(QGeoMapObject *obj)
427{
428 Q_UNUSED(obj);
429 return nullptr;
430}
431
432QList<QGeoMapObject *> QGeoMapPrivate::mapObjects() const
433{
434 return QList<QGeoMapObject *>();
435}
436
437double QGeoMapPrivate::mapWidth() const
438{
439 if (m_geoProjection->projectionType() == QGeoProjection::ProjectionWebMercator)
440 return static_cast<const QGeoProjectionWebMercator *>(m_geoProjection)->mapWidth();
441 return 0; // override this for maps supporting other projections
442}
443
444double QGeoMapPrivate::mapHeight() const
445{
446 if (m_geoProjection->projectionType() == QGeoProjection::ProjectionWebMercator)
447 return static_cast<const QGeoProjectionWebMercator *>(m_geoProjection)->mapHeight();
448 return 0; // override this for maps supporting other projections
449}
450
451void QGeoMapPrivate::setCopyrightVisible(bool visible)
452{
453 m_copyrightVisible = visible;
454}
455
456bool QGeoMapPrivate::copyrightVisible() const
457{
458 return m_copyrightVisible;
459}
460
461double QGeoMapPrivate::maximumCenterLatitudeAtZoom(const QGeoCameraData &cameraData) const
462{
463 m_maximumViewportLatitude = m_geoProjection->maximumCenterLatitudeAtZoom(cameraData);
464 return m_maximumViewportLatitude;
465}
466
467double QGeoMapPrivate::minimumCenterLatitudeAtZoom(const QGeoCameraData &cameraData) const
468{
469 m_minimumViewportLatitude = m_geoProjection->minimumCenterLatitudeAtZoom(cameraData);
470 return m_minimumViewportLatitude;
471}
472
473void QGeoMapPrivate::setVisibleArea(const QRectF &/*visibleArea*/)
474{
475
476}
477
478QRectF QGeoMapPrivate::visibleArea() const
479{
480 return QRectF();
481}
482
483QRectF QGeoMapPrivate::clampVisibleArea(const QRectF &visibleArea) const
484{
485 qreal xp = qMin<qreal>(a: visibleArea.x(), b: qMax(a: m_viewportSize.width() - 1, b: 0));
486 qreal yp = qMin<qreal>(a: visibleArea.y(), b: qMax(a: m_viewportSize.height() - 1, b: 0));
487 qreal w = qMin<qreal>(a: visibleArea.width(), b: qMax<qreal>(a: m_viewportSize.width() - xp, b: 0));
488 qreal h = qMin<qreal>(a: visibleArea.height(), b: qMax<qreal>(a: m_viewportSize.height() - yp, b: 0));
489 return QRectF(xp, yp, w, h);
490}
491
492QT_END_NAMESPACE
493

source code of qtlocation/src/location/maps/qgeomap.cpp