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
qdeclarativegeomapquickitem.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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
7#include <QtCore/QScopedValueRollback>
8#include <QtQml/qqmlinfo.h>
9#include <QtQuick/QSGOpacityNode>
10#include <QtPositioning/private/qdoublevector2d_p.h>
11#include <QtQuick/private/qquickitem_p.h>
12#include <QtQuick/private/qquickmousearea_p.h>
13#include <QtLocation/private/qgeomap_p.h>
14#include <QtLocation/private/qgeoprojection_p.h>
15#include <QDebug>
16#include <cmath>
17
18QT_BEGIN_NAMESPACE
19
20/*!
21 \qmltype MapQuickItem
22 \nativetype QDeclarativeGeoMapQuickItem
23 \inqmlmodule QtLocation
24 \ingroup qml-QtLocation5-maps
25 \since QtLocation 5.5
26
27 \brief The MapQuickItem type displays an arbitrary Qt Quick object
28 on a Map.
29
30 The MapQuickItem type is used to place an arbitrary Qt Quick object
31 on a Map at a specified location and size. Compared to floating an item
32 above the Map, a MapQuickItem will follow the panning (and optionally, the
33 zooming) of the Map as if it is on the Map surface.
34
35 The \l{sourceItem} property contains the Qt Quick item to be drawn, which
36 can be any kind of visible type.
37
38 \section2 Positioning and Sizing
39
40 The positioning of the MapQuickItem on the Map is controlled by two
41 properties: \l coordinate and \l anchorPoint. If only \l coordinate is set,
42 it specifies a longitude/latitude coordinate for the item to be placed at.
43 The set coordinate will line up with the top-left corner of the contained
44 item when shown on the screen.
45
46 The \l anchorPoint property provides a way to line up the coordinate with
47 other parts of the item than just the top-left corner, by setting a number
48 of pixels the item will be offset by. A simple way to think about it is
49 to note that the point given by \l anchorPoint on the item itself is the
50 point that will line up with the given \l coordinate when displayed.
51
52 In addition to being anchored to the map, the MapQuickItem can optionally
53 follow the scale of the map, and change size when the Map is zoomed in or
54 zoomed out. This behaviour is controlled by the \l zoomLevel property. The
55 default behaviour if \l zoomLevel is not set is for the item to be drawn
56 "on the screen" rather than "on the map", so that its size remains the same
57 regardless of the zoom level of the Map.
58
59 \section2 Performance
60
61 Performance of a MapQuickItem is normally in the same ballpark as the
62 contained Qt Quick item alone. Overheads added amount to a translation
63 and (possibly) scaling of the original item, as well as a transformation
64 from longitude and latitude to screen position.
65
66 \section2 Limitations
67
68 \note Due to an implementation detail, items placed inside a
69 MapQuickItem will have a \c{parent} item which is not the MapQuickItem.
70 Refer to the MapQuickItem by its \c{id}, and avoid the use of \c{anchor}
71 in the \c{sourceItem}.
72
73 \section2 Example Usage
74
75 The following snippet shows a MapQuickItem containing an Image object,
76 to display a Marker on the Map. This strategy is used to show the map
77 markers in the MapViewer example.
78
79 \snippet mapviewer/map/Marker.qml mqi-top
80 \snippet mapviewer/map/Marker.qml mqi-anchor
81 \snippet mapviewer/map/Marker.qml mqi-closeimage
82 \snippet mapviewer/map/Marker.qml mqi-close
83
84 \image api-mapquickitem.png {Marker rendered as a teardrop pin on a map}
85*/
86
87/*!
88 \qmlproperty bool QtLocation::MapQuickItem::autoFadeIn
89
90 This property holds whether the item automatically fades in when zooming into the map
91 starting from very low zoom levels. By default this is \c true.
92 Setting this property to \c false causes the map item to always have the opacity specified
93 with the \l QtQuick::Item::opacity property, which is 1.0 by default.
94
95 \since 5.14
96*/
97
98QMapQuickItemMatrix4x4::QMapQuickItemMatrix4x4(QObject *parent) : QQuickTransform(parent) { }
99
100void QMapQuickItemMatrix4x4::setMatrix(const QMatrix4x4 &matrix)
101{
102 if (m_matrix == matrix)
103 return;
104 m_matrix = matrix;
105 update();
106}
107
108void QMapQuickItemMatrix4x4::applyTo(QMatrix4x4 *matrix) const
109{
110 *matrix *= m_matrix;
111}
112
113
114QDeclarativeGeoMapQuickItem::QDeclarativeGeoMapQuickItem(QQuickItem *parent)
115 : QDeclarativeGeoMapItemBase(parent)
116{
117 m_itemType = QGeoMap::MapQuickItem;
118 setFlag(ItemHasContents, true);
119 opacityContainer_ = new QQuickItem(this);
120 opacityContainer_->setParentItem(this);
121 opacityContainer_->setFlag(ItemHasContents, true);
122}
123
124QDeclarativeGeoMapQuickItem::~QDeclarativeGeoMapQuickItem() {}
125
126/*!
127 \qmlproperty coordinate MapQuickItem::coordinate
128
129 This property holds the anchor coordinate of the MapQuickItem. The point
130 on the sourceItem that is specified by anchorPoint is kept aligned with
131 this coordinate when drawn on the map.
132
133 In the image below, there are 3 MapQuickItems that are identical except
134 for the value of their anchorPoint properties. The values of anchorPoint
135 for each are written on top of the item.
136
137 \image api-mapquickitem-anchor.png {Three markers on a map showing
138 the effect of different anchor points}
139*/
140void QDeclarativeGeoMapQuickItem::setCoordinate(const QGeoCoordinate &coordinate)
141{
142 if (coordinate_ == coordinate)
143 return;
144
145 coordinate_ = coordinate;
146 geoshape_.setTopLeft(coordinate_);
147 geoshape_.setBottomRight(coordinate_);
148 // TODO: Handle zoomLevel != 0.0
149 polishAndUpdate();
150 emit coordinateChanged();
151}
152
153/*!
154 \internal
155*/
156void QDeclarativeGeoMapQuickItem::setMap(QDeclarativeGeoMap *quickMap, QGeoMap *map)
157{
158 QDeclarativeGeoMapItemBase::setMap(quickMap,map);
159 if (map && quickMap) {
160 connect(map, &QGeoMap::cameraDataChanged,
161 this, &QDeclarativeGeoMapQuickItem::polishAndUpdate);
162 polishAndUpdate();
163 }
164}
165
166/*!
167 \internal
168*/
169void QDeclarativeGeoMapQuickItem::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
170{
171 if (!mapAndSourceItemSet_ || updatingGeometry_ ||
172 newGeometry.topLeft() == oldGeometry.topLeft()) {
173 QDeclarativeGeoMapItemBase::geometryChange(newGeometry, oldGeometry);
174 return;
175 }
176
177 QGeoCoordinate newCoordinate = map()->geoProjection().
178 itemPositionToCoordinate(QDoubleVector2D(x(), y()) + QDoubleVector2D(anchorPoint_), false);
179
180 if (newCoordinate.isValid())
181 setCoordinate(newCoordinate);
182
183 // Not calling QDeclarativeGeoMapItemBase::geometryChange() as it will be called from a nested
184 // call to this function.
185}
186
187/*!
188 \internal
189*/
190QGeoCoordinate QDeclarativeGeoMapQuickItem::coordinate()
191{
192 return coordinate_;
193}
194
195/*!
196 \qmlproperty object MapQuickItem::sourceItem
197
198 This property holds the source item that will be drawn on the map.
199*/
200void QDeclarativeGeoMapQuickItem::setSourceItem(QQuickItem *sourceItem)
201{
202 QQuickItem *item = qobject_cast<QQuickItem *>(sourceItem); // Workaround for QTBUG-72930
203 if (sourceItem_.data() == item)
204 return;
205 sourceItem_ = item;
206 polishAndUpdate();
207 emit sourceItemChanged();
208}
209
210QQuickItem *QDeclarativeGeoMapQuickItem::sourceItem()
211{
212 return sourceItem_.data();
213}
214
215/*!
216 \internal
217*/
218void QDeclarativeGeoMapQuickItem::afterChildrenChanged()
219{
220 const QList<QQuickItem *> kids = childItems();
221 if (kids.size() > 0) {
222 bool printedWarning = false;
223 for (auto *i : kids) {
224 if (i->flags() & QQuickItem::ItemHasContents
225 && !qobject_cast<QQuickMouseArea *>(i)
226 && sourceItem_.data() != i
227 && opacityContainer_ != i) {
228 if (!printedWarning) {
229 qmlWarning(this) << "Use the sourceItem property for the contained item, direct children are not supported";
230 printedWarning = true;
231 }
232
233 qmlWarning(i) << "deleting this child";
234 i->deleteLater();
235 }
236 }
237 }
238}
239
240/*!
241 \qmlproperty point MapQuickItem::anchorPoint
242
243 This property determines which point on the sourceItem that will be lined
244 up with the coordinate on the map.
245*/
246void QDeclarativeGeoMapQuickItem::setAnchorPoint(const QPointF &anchorPoint)
247{
248 if (anchorPoint == anchorPoint_)
249 return;
250 anchorPoint_ = anchorPoint;
251 polishAndUpdate();
252 emit anchorPointChanged();
253}
254
255QPointF QDeclarativeGeoMapQuickItem::anchorPoint() const
256{
257 return anchorPoint_;
258}
259
260/*!
261 \qmlproperty real MapQuickItem::zoomLevel
262
263 This property controls the scaling behaviour of the contents of the
264 MapQuickItem. In particular, by setting this property it is possible
265 to choose between objects that are drawn on the screen (and sized in
266 screen pixels), and those drawn on the map surface (which change size
267 with the zoom level of the map).
268
269 The default value for this property is 0.0, which corresponds to drawing
270 the object on the screen surface. If set to another value, the object will
271 be drawn on the map surface instead. The value (if not zero) specifies the
272 zoomLevel at which the object will be visible at a scale of 1:1 (ie, where
273 object pixels and screen pixels are the same). At zoom levels lower than
274 this, the object will appear smaller, and at higher zoom levels, appear
275 larger. This is in contrast to when this property is set to zero, where
276 the object remains the same size on the screen at all zoom levels.
277*/
278void QDeclarativeGeoMapQuickItem::setZoomLevel(qreal zoomLevel)
279{
280 if (zoomLevel == zoomLevel_)
281 return;
282 zoomLevel_ = zoomLevel;
283 // TODO: update geoshape_!
284 polishAndUpdate();
285 emit zoomLevelChanged();
286}
287
288qreal QDeclarativeGeoMapQuickItem::zoomLevel() const
289{
290 return zoomLevel_;
291}
292
293const QGeoShape &QDeclarativeGeoMapQuickItem::geoShape() const
294{
295 // TODO: return a QGeoRectangle representing the bounding geo rectangle of the quick item
296 // when zoomLevel_ is != 0.0
297 return geoshape_;
298}
299
300void QDeclarativeGeoMapQuickItem::setGeoShape(const QGeoShape &shape)
301{
302 if (shape == geoshape_)
303 return;
304
305 const QGeoRectangle rect = shape.boundingGeoRectangle();
306 geoshape_ = rect;
307 coordinate_ = rect.center();
308
309 // TODO: Handle zoomLevel != 0.0
310 polishAndUpdate();
311 emit coordinateChanged();
312
313}
314
315bool QDeclarativeGeoMapQuickItem::contains(const QPointF &point) const
316{
317 if (zoomLevel_ != 0) {
318 // This follows the mathematical principle of
319 // QDeclarativeGeoMap::fitViewportToMapItemsRefine but in a shorter way:
320 const QTransform t = matrix_->m_matrix.toTransform();
321 const QPointF transformedPoint = t.inverted().map(point);
322 return QQuickItem::contains(transformedPoint);
323 }
324 return QQuickItem::contains(point);
325}
326
327/*!
328 \internal
329*/
330void QDeclarativeGeoMapQuickItem::updatePolish()
331{
332 if (!quickMap() && sourceItem_) {
333 mapAndSourceItemSet_ = false;
334 sourceItem_.data()->setParentItem(0);
335 return;
336 }
337
338 if (!quickMap() || !map() || !sourceItem_) {
339 mapAndSourceItemSet_ = false;
340 return;
341 }
342
343 if (!mapAndSourceItemSet_ && quickMap() && map() && sourceItem_) {
344 mapAndSourceItemSet_ = true;
345 sourceItem_.data()->setParentItem(opacityContainer_);
346 sourceItem_.data()->setTransformOrigin(QQuickItem::TopLeft);
347 connect(sourceItem_.data(), &QQuickItem::xChanged,
348 this, &QDeclarativeGeoMapQuickItem::polishAndUpdate);
349 connect(sourceItem_.data(), &QQuickItem::yChanged,
350 this, &QDeclarativeGeoMapQuickItem::polishAndUpdate);
351 connect(sourceItem_.data(), &QQuickItem::widthChanged,
352 this, &QDeclarativeGeoMapQuickItem::polishAndUpdate);
353 connect(sourceItem_.data(), &QQuickItem::heightChanged,
354 this, &QDeclarativeGeoMapQuickItem::polishAndUpdate);
355 }
356
357 if (!coordinate_.isValid()) {
358 opacityContainer_->setVisible(false);
359 return;
360 } else {
361 opacityContainer_->setVisible(true);
362 }
363
364 QScopedValueRollback<bool> rollback(updatingGeometry_);
365 updatingGeometry_ = true;
366
367 opacityContainer_->setOpacity(zoomLevelOpacity());
368
369 setWidth(sourceItem_.data()->width());
370 setHeight(sourceItem_.data()->height());
371 if (zoomLevel_ != 0.0 // zoom level initialized to 0.0. If it's different, it has been set explicitly.
372 && map()->geoProjection().projectionType() == QGeoProjection::ProjectionWebMercator) { // Currently unsupported on any other projection
373 const QGeoProjectionWebMercator &p = static_cast<const QGeoProjectionWebMercator&>(map()->geoProjection());
374
375 if (!matrix_) {
376 matrix_ = new QMapQuickItemMatrix4x4(this);
377 matrix_->appendToItem(opacityContainer_);
378 }
379 matrix_->setMatrix(p.quickItemTransformation(coordinate(), anchorPoint_, zoomLevel_));
380 setPosition(QPointF(0,0));
381 } else {
382 if (map()->geoProjection().projectionType() == QGeoProjection::ProjectionWebMercator) {
383 const QGeoProjectionWebMercator &p = static_cast<const QGeoProjectionWebMercator&>(map()->geoProjection());
384 if (map()->cameraData().tilt() > 0.0
385 && !p.isProjectable(p.geoToWrappedMapProjection(coordinate()))) {
386 // if the coordinate is behind the camera, we use the transformation to get the item out of the way
387 if (!matrix_) {
388 matrix_ = new QMapQuickItemMatrix4x4(this);
389 matrix_->appendToItem(opacityContainer_);
390 }
391 matrix_->setMatrix(p.quickItemTransformation(coordinate(), anchorPoint_, map()->cameraData().zoomLevel()));
392 setPosition(QPointF(0,0));
393 } else { // All good, rendering screen-aligned
394 if (matrix_)
395 matrix_->setMatrix(QMatrix4x4());
396 setPositionOnMap(coordinate(), anchorPoint_);
397 }
398 } else { // On other projections we can only currently test if coordinateToItemPosition returns a valid position
399 if (map()->cameraData().tilt() > 0.0
400 && qIsNaN(map()->geoProjection().coordinateToItemPosition(coordinate(), false).x())) {
401 opacityContainer_->setVisible(false);
402 } else {
403 if (matrix_)
404 matrix_->setMatrix(QMatrix4x4());
405 setPositionOnMap(coordinate(), anchorPoint_);
406 }
407 }
408 }
409}
410
411/*!
412 \internal
413*/
414void QDeclarativeGeoMapQuickItem::afterViewportChanged(const QGeoMapViewportChangeEvent &event)
415{
416 Q_UNUSED(event);
417 if (event.mapSize.width() <= 0 || event.mapSize.height() <= 0)
418 return;
419
420 polishAndUpdate();
421}
422
423/*!
424 \internal
425*/
426qreal QDeclarativeGeoMapQuickItem::scaleFactor()
427{
428 qreal scale = 1.0;
429 // use 1+x to avoid fuzzy compare against zero
430 if (!qFuzzyCompare(1.0 + zoomLevel_, 1.0))
431 scale = std::pow(0.5, zoomLevel_ - map()->cameraData().zoomLevel());
432 return scale;
433}
434
435QT_END_NAMESPACE