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
qdeclarativegeomapitemgroup.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
8
9QT_BEGIN_NAMESPACE
10
11/*!
12 \qmltype MapItemGroup
13 \nativetype QDeclarativeGeoMapItemGroup
14 \inqmlmodule QtLocation
15 \ingroup qml-QtLocation5-maps
16 \since QtLocation 5.9
17
18 \brief The MapItemGroup type is a container for map items.
19
20 Its purpose is to enable code modularization by allowing the usage
21 of qml files containing map elements related to each other, and
22 the associated bindings.
23
24 \note The release of this API with Qt 5.9 is a Technology Preview.
25
26 \section2 Example Usage
27
28 The following snippet shows how to use a MapItemGroup to create a MapCircle, centered at
29 the coordinate (63, -18) with a radius of 100km, filled in red, surrounded by an ondulated green border,
30 both contained in a semitransparent blue circle with a MouseArea that moves the whole group.
31 This group is defined in a separate file named PolygonGroup.qml:
32
33 \code
34 import QtQuick
35 import QtPositioning
36 import QtLocation
37
38 MapItemGroup {
39 id: itemGroup
40 property alias position: mainCircle.center
41 property var radius: 100 * 1000
42 property var borderHeightPct : 0.3
43
44 MapCircle {
45 id: mainCircle
46 center : QtPositioning.coordinate(40, 0)
47 radius: itemGroup.radius * (1.0 + borderHeightPct)
48 opacity: 0.05
49 visible: true
50 color: 'blue'
51
52 MouseArea{
53 anchors.fill: parent
54 drag.target: parent
55 id: maItemGroup
56 }
57 }
58
59 MapCircle {
60 id: groupCircle
61 center: itemGroup.position
62 radius: itemGroup.radius
63 color: 'crimson'
64
65 onCenterChanged: {
66 groupPolyline.populateBorder();
67 }
68 }
69
70 MapPolyline {
71 id: groupPolyline
72 line.color: 'green'
73 line.width: 3
74
75 function populateBorder() {
76 groupPolyline.path = [] // clearing the path
77 var waveLength = 8.0;
78 var waveAmplitude = groupCircle.radius * borderHeightPct;
79 for (var i=0; i <= 360; i++) {
80 var wavePhase = (i/360.0 * 2.0 * Math.PI )* waveLength
81 var waveHeight = (Math.cos(wavePhase) + 1.0) / 2.0
82 groupPolyline.addCoordinate(groupCircle.center.atDistanceAndAzimuth(groupCircle.radius + waveAmplitude * waveHeight , i))
83 }
84 }
85
86 Component.onCompleted: {
87 populateBorder()
88 }
89 }
90 }
91 \endcode
92
93 PolygonGroup.qml is now a reusable component that can then be used in a Map as:
94
95 \code
96 Map {
97 id: map
98 PolygonGroup {
99 id: polygonGroup
100 position: QtPositioning.coordinate(63,-18)
101 }
102 }
103 \endcode
104
105 \image api-mapitemgroup.png
106*/
107
108QDeclarativeGeoMapItemGroup::QDeclarativeGeoMapItemGroup(QQuickItem *parent)
109 : QQuickItem(parent)
110{
111 connect(this, &QQuickItem::opacityChanged,
112 this, &QDeclarativeGeoMapItemGroup::mapItemOpacityChanged);
113}
114
115QDeclarativeGeoMapItemGroup::~QDeclarativeGeoMapItemGroup()
116{
117
118}
119
120void QDeclarativeGeoMapItemGroup::setParentGroup(QDeclarativeGeoMapItemGroup &parentGroup)
121{
122 m_parentGroup = &parentGroup;
123 connect(m_parentGroup, &QDeclarativeGeoMapItemGroup::mapItemOpacityChanged,
124 this, &QDeclarativeGeoMapItemGroup::mapItemOpacityChanged);
125}
126
127void QDeclarativeGeoMapItemGroup::setQuickMap(QDeclarativeGeoMap *quickMap)
128{
129 if (!quickMap && m_quickMap)
130 m_quickMap->disconnect(this);
131 m_quickMap = quickMap;
132 if (m_quickMap) {
133 onMapSizeChanged();
134 connect(m_quickMap, &QQuickItem::widthChanged, this, &QDeclarativeGeoMapItemGroup::onMapSizeChanged);
135 connect(m_quickMap, &QQuickItem::heightChanged, this, &QDeclarativeGeoMapItemGroup::onMapSizeChanged);
136 }
137}
138
139QDeclarativeGeoMap *QDeclarativeGeoMapItemGroup::quickMap() const
140{
141 return m_quickMap;
142}
143
144qreal QDeclarativeGeoMapItemGroup::mapItemOpacity() const
145{
146 return ((m_parentGroup) ? m_parentGroup->mapItemOpacity() : 1.0) * opacity();
147}
148
149void QDeclarativeGeoMapItemGroup::classBegin()
150{
151 QQuickItem::classBegin();
152}
153
154void QDeclarativeGeoMapItemGroup::componentComplete()
155{
156 QQuickItem::componentComplete();
157
158 // In certain cases the parent won't be set via the constructor, but rather later on
159 // during the instantiation/incubation process.
160 // Therefore calling setParentGroup here, when the parent is known.
161 // The childrenChanged use case to handle dynamically-added items is currently unsupported.
162 const QList<QQuickItem *> &quickKids = childItems();
163 for (QQuickItem *k : quickKids) {
164 QDeclarativeGeoMapItemGroup *childGroup
165 = qobject_cast<QDeclarativeGeoMapItemGroup *>(k);
166 if (childGroup) {
167 childGroup->setParentGroup(*this);
168 continue;
169 }
170 QDeclarativeGeoMapItemBase *childItem
171 = qobject_cast<QDeclarativeGeoMapItemBase *>(k);
172 if (childItem) {
173 childItem->setParentGroup(*this);
174 continue;
175 }
176 }
177}
178
179void QDeclarativeGeoMapItemGroup::onMapSizeChanged()
180{
181 setWidth(m_quickMap->width());
182 setHeight(m_quickMap->height());
183}
184
185QT_END_NAMESPACE