Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5
6#include <QtCore/QScopedValueRollback>
7#include <QtQml/qqmlinfo.h>
8#include <QtQuick/QSGOpacityNode>
9#include <QtPositioning/private/qdoublevector2d_p.h>
10#include <QtQuick/private/qquickmousearea_p.h>
11#include <QtLocation/private/qgeomap_p.h>
12#include <QtLocation/private/qgeoprojection_p.h>
13#include <QDebug>
14#include <cmath>
15
17
97
99{
100 if (m_matrix == matrix)
101 return;
103 update();
104}
105
110
111
114{
117 opacityContainer_ = new QQuickItem(this);
118 opacityContainer_->setParentItem(this);
119 opacityContainer_->setFlag(ItemHasContents, true);
120}
121
123
138{
139 if (coordinate_ == coordinate)
140 return;
141
142 coordinate_ = coordinate;
143 geoshape_.setTopLeft(coordinate_);
144 geoshape_.setBottomRight(coordinate_);
145 // TODO: Handle zoomLevel != 0.0
148}
149
162
166void QDeclarativeGeoMapQuickItem::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
167{
168 if (!mapAndSourceItemSet_ || updatingGeometry_ ||
169 newGeometry.topLeft() == oldGeometry.topLeft()) {
170 QDeclarativeGeoMapItemBase::geometryChange(newGeometry, oldGeometry);
171 return;
172 }
173
174 QGeoCoordinate newCoordinate = map()->geoProjection().
175 itemPositionToCoordinate(QDoubleVector2D(x(), y()) + QDoubleVector2D(anchorPoint_), false);
176
177 if (newCoordinate.isValid())
178 setCoordinate(newCoordinate);
179
180 // Not calling QDeclarativeGeoMapItemBase::geometryChange() as it will be called from a nested
181 // call to this function.
182}
183
188{
189 return coordinate_;
190}
191
198{
199 QQuickItem *item = qobject_cast<QQuickItem *>(sourceItem); // Workaround for QTBUG-72930
200 if (sourceItem_.data() == item)
201 return;
202 sourceItem_ = item;
205}
206
208{
209 return sourceItem_.data();
210}
211
216{
217 const QList<QQuickItem *> kids = childItems();
218 if (kids.size() > 0) {
219 bool printedWarning = false;
220 for (auto *i : kids) {
221 if (i->flags() & QQuickItem::ItemHasContents
222 && !qobject_cast<QQuickMouseArea *>(i)
223 && sourceItem_.data() != i
224 && opacityContainer_ != i) {
225 if (!printedWarning) {
226 qmlWarning(this) << "Use the sourceItem property for the contained item, direct children are not supported";
227 printedWarning = true;
228 }
229
230 qmlWarning(i) << "deleting this child";
231 i->deleteLater();
232 }
233 }
234 }
235}
236
244{
245 if (anchorPoint == anchorPoint_)
246 return;
247 anchorPoint_ = anchorPoint;
250}
251
253{
254 return anchorPoint_;
255}
256
276{
277 if (zoomLevel == zoomLevel_)
278 return;
279 zoomLevel_ = zoomLevel;
280 // TODO: update geoshape_!
283}
284
286{
287 return zoomLevel_;
288}
289
291{
292 // TODO: return a QGeoRectangle representing the bounding geo rectangle of the quick item
293 // when zoomLevel_ is != 0.0
294 return geoshape_;
295}
296
298{
299 if (shape == geoshape_)
300 return;
301
303 geoshape_ = rect;
304 coordinate_ = rect.center();
305
306 // TODO: Handle zoomLevel != 0.0
309
310}
311
316{
317 if (!quickMap() && sourceItem_) {
318 mapAndSourceItemSet_ = false;
319 sourceItem_.data()->setParentItem(0);
320 return;
321 }
322
323 if (!quickMap() || !map() || !sourceItem_) {
324 mapAndSourceItemSet_ = false;
325 return;
326 }
327
328 if (!mapAndSourceItemSet_ && quickMap() && map() && sourceItem_) {
329 mapAndSourceItemSet_ = true;
330 sourceItem_.data()->setParentItem(opacityContainer_);
332 connect(sourceItem_.data(), &QQuickItem::xChanged,
334 connect(sourceItem_.data(), &QQuickItem::yChanged,
336 connect(sourceItem_.data(), &QQuickItem::widthChanged,
338 connect(sourceItem_.data(), &QQuickItem::heightChanged,
340 }
341
342 if (!coordinate_.isValid()) {
343 opacityContainer_->setVisible(false);
344 return;
345 } else {
346 opacityContainer_->setVisible(true);
347 }
348
349 QScopedValueRollback<bool> rollback(updatingGeometry_);
350 updatingGeometry_ = true;
351
352 opacityContainer_->setOpacity(zoomLevelOpacity());
353
354 setWidth(sourceItem_.data()->width());
355 setHeight(sourceItem_.data()->height());
356 if (zoomLevel_ != 0.0 // zoom level initialized to 0.0. If it's different, it has been set explicitly.
357 && map()->geoProjection().projectionType() == QGeoProjection::ProjectionWebMercator) { // Currently unsupported on any other projection
358 const QGeoProjectionWebMercator &p = static_cast<const QGeoProjectionWebMercator&>(map()->geoProjection());
359
360 if (!matrix_) {
361 matrix_ = new QMapQuickItemMatrix4x4(this);
362 matrix_->appendToItem(opacityContainer_);
363 }
364 matrix_->setMatrix(p.quickItemTransformation(coordinate(), anchorPoint_, zoomLevel_));
365 setPosition(QPointF(0,0));
366 } else {
367 if (map()->geoProjection().projectionType() == QGeoProjection::ProjectionWebMercator) {
368 const QGeoProjectionWebMercator &p = static_cast<const QGeoProjectionWebMercator&>(map()->geoProjection());
369 if (map()->cameraData().tilt() > 0.0
370 && !p.isProjectable(p.geoToWrappedMapProjection(coordinate()))) {
371 // if the coordinate is behind the camera, we use the transformation to get the item out of the way
372 if (!matrix_) {
373 matrix_ = new QMapQuickItemMatrix4x4(this);
374 matrix_->appendToItem(opacityContainer_);
375 }
376 matrix_->setMatrix(p.quickItemTransformation(coordinate(), anchorPoint_, map()->cameraData().zoomLevel()));
377 setPosition(QPointF(0,0));
378 } else { // All good, rendering screen-aligned
379 if (matrix_)
380 matrix_->setMatrix(QMatrix4x4());
381 setPositionOnMap(coordinate(), anchorPoint_);
382 }
383 } else { // On other projections we can only currently test if coordinateToItemPosition returns a valid position
384 if (map()->cameraData().tilt() > 0.0
385 && qIsNaN(map()->geoProjection().coordinateToItemPosition(coordinate(), false).x())) {
386 opacityContainer_->setVisible(false);
387 } else {
388 if (matrix_)
389 matrix_->setMatrix(QMatrix4x4());
390 setPositionOnMap(coordinate(), anchorPoint_);
391 }
392 }
393 }
394}
395
400{
402 if (event.mapSize.width() <= 0 || event.mapSize.height() <= 0)
403 return;
404
406}
407
411qreal QDeclarativeGeoMapQuickItem::scaleFactor()
412{
413 qreal scale = 1.0;
414 // use 1+x to avoid fuzzy compare against zero
415 if (!qFuzzyCompare(1.0 + zoomLevel_, 1.0))
416 scale = std::pow(0.5, zoomLevel_ - map()->cameraData().zoomLevel());
417 return scale;
418}
419
virtual void setPositionOnMap(const QGeoCoordinate &coordinate, const QPointF &offset)
virtual void setMap(QDeclarativeGeoMap *quickMap, QGeoMap *map)
QDeclarativeGeoMap * quickMap() const
const QGeoShape & geoShape() const override
void setMap(QDeclarativeGeoMap *quickMap, QGeoMap *map) override
void setCoordinate(const QGeoCoordinate &coordinate)
\qmlproperty coordinate MapQuickItem::coordinate
void setGeoShape(const QGeoShape &shape) override
void setAnchorPoint(const QPointF &anchorPoint)
\qmlproperty QPointF MapQuickItem::anchorPoint
void afterViewportChanged(const QGeoMapViewportChangeEvent &event) override
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override
void setZoomLevel(qreal zoomLevel)
\qmlproperty real MapQuickItem::zoomLevel
void setSourceItem(QQuickItem *sourceItem)
\qmlproperty object MapQuickItem::sourceItem
QDeclarativeGeoMapQuickItem(QQuickItem *parent=nullptr)
double tilt() const
\inmodule QtPositioning
bool isValid
This property holds the validity of this geo coordinate.
void cameraDataChanged(const QGeoCameraData &cameraData)
const QGeoProjection & geoProjection() const
Definition qgeomap.cpp:164
const QGeoCameraData & cameraData() const
Definition qgeomap.cpp:111
@ MapQuickItem
Definition qgeomap_p.h:52
\inmodule QtPositioning
void setTopLeft(const QGeoCoordinate &topLeft)
Sets the top left coordinate of this geo rectangle to topLeft.
void setBottomRight(const QGeoCoordinate &bottomRight)
Sets the bottom right coordinate of this geo rectangle to bottomRight.
\inmodule QtPositioning
Definition qgeoshape.h:17
Q_INVOKABLE QGeoRectangle boundingGeoRectangle() const
Returns a QGeoRectangle representing the geographical bounding rectangle of the geo shape,...
QMapQuickItemMatrix4x4(QObject *parent=nullptr)
\qmltype MapQuickItem \instantiates QDeclarativeGeoMapQuickItem \inqmlmodule QtLocation
void applyTo(QMatrix4x4 *matrix) const override
void setMatrix(const QMatrix4x4 &matrix)
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore\reentrant
Definition qpoint.h:217
T * data() const noexcept
Definition qpointer.h:73
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
void xChanged()
void heightChanged()
QList< QQuickItem * > childItems() const
Returns the children of this item.
void setOpacity(qreal)
void setFlag(Flag flag, bool enabled=true)
Enables the specified flag for this item if enabled is true; if enabled is false, the flag is disable...
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
void widthChanged()
qreal x
\qmlproperty real QtQuick::Item::x \qmlproperty real QtQuick::Item::y \qmlproperty real QtQuick::Item...
Definition qquickitem.h:72
void setParentItem(QQuickItem *parent)
qreal y
Defines the item's y position relative to its parent.
Definition qquickitem.h:73
void setHeight(qreal)
void yChanged()
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
void setVisible(bool)
qreal height
This property holds the height of this item.
Definition qquickitem.h:76
void setWidth(qreal)
QQuickItem(QQuickItem *parent=nullptr)
Constructs a QQuickItem with the given parent.
qreal scale
\qmlproperty real QtQuick::Item::scale This property holds the scale factor for this item.
Definition qquickitem.h:107
void setTransformOrigin(TransformOrigin)
void appendToItem(QQuickItem *)
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr QPointF topLeft() const noexcept
Returns the position of the rectangle's top-left corner.
Definition qrect.h:511
QMap< QString, QString > map
[6]
rect
[4]
Combined button and popup list for selecting options.
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
bool qIsNaN(qfloat16 f) noexcept
Definition qfloat16.h:284
n void setPosition(void) \n\
struct _cl_event * event
GLuint GLenum matrix
GLfloat GLfloat p
[1]
GLenum GLenum GLenum GLenum GLenum scale
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:492
#define emit
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:187
QGraphicsItem * item