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 {Overlapping red and green polygons
106 drawn on a map of Iceland}
107*/
108
109QDeclarativeGeoMapItemGroup::QDeclarativeGeoMapItemGroup(QQuickItem *parent)
110 : QQuickItem(parent)
111{
112 connect(this, &QQuickItem::opacityChanged,
113 this, &QDeclarativeGeoMapItemGroup::mapItemOpacityChanged);
114}
115
116QDeclarativeGeoMapItemGroup::~QDeclarativeGeoMapItemGroup()
117{
118
119}
120
121void QDeclarativeGeoMapItemGroup::setParentGroup(QDeclarativeGeoMapItemGroup &parentGroup)
122{
123 m_parentGroup = &parentGroup;
124 connect(m_parentGroup, &QDeclarativeGeoMapItemGroup::mapItemOpacityChanged,
125 this, &QDeclarativeGeoMapItemGroup::mapItemOpacityChanged);
126}
127
128void QDeclarativeGeoMapItemGroup::setQuickMap(QDeclarativeGeoMap *quickMap)
129{
130 if (!quickMap && m_quickMap)
131 m_quickMap->disconnect(this);
132 m_quickMap = quickMap;
133 if (m_quickMap) {
134 onMapSizeChanged();
135 connect(m_quickMap, &QQuickItem::widthChanged, this, &QDeclarativeGeoMapItemGroup::onMapSizeChanged);
136 connect(m_quickMap, &QQuickItem::heightChanged, this, &QDeclarativeGeoMapItemGroup::onMapSizeChanged);
137 }
138}
139
140QDeclarativeGeoMap *QDeclarativeGeoMapItemGroup::quickMap() const
141{
142 return m_quickMap;
143}
144
145qreal QDeclarativeGeoMapItemGroup::mapItemOpacity() const
146{
147 return ((m_parentGroup) ? m_parentGroup->mapItemOpacity() : 1.0) * opacity();
148}
149
150void QDeclarativeGeoMapItemGroup::classBegin()
151{
152 QQuickItem::classBegin();
153}
154
155void QDeclarativeGeoMapItemGroup::componentComplete()
156{
157 QQuickItem::componentComplete();
158
159 // In certain cases the parent won't be set via the constructor, but rather later on
160 // during the instantiation/incubation process.
161 // Therefore calling setParentGroup here, when the parent is known.
162 // The childrenChanged use case to handle dynamically-added items is currently unsupported.
163 const QList<QQuickItem *> &quickKids = childItems();
164 for (QQuickItem *k : quickKids) {
165 QDeclarativeGeoMapItemGroup *childGroup
166 = qobject_cast<QDeclarativeGeoMapItemGroup *>(k);
167 if (childGroup) {
168 childGroup->setParentGroup(*this);
169 continue;
170 }
171 QDeclarativeGeoMapItemBase *childItem
172 = qobject_cast<QDeclarativeGeoMapItemBase *>(k);
173 if (childItem) {
174 childItem->setParentGroup(*this);
175 continue;
176 }
177 }
178}
179
180void QDeclarativeGeoMapItemGroup::onMapSizeChanged()
181{
182 setWidth(m_quickMap->width());
183 setHeight(m_quickMap->height());
184}
185
186QT_END_NAMESPACE