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