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
qdeclarativegeolocation.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
8
9/*!
10 \qmltype Location
11 \inqmlmodule QtPositioning
12 \since 5.2
13
14 \brief The Location type holds location data.
15
16 Location types represent a geographic "location", in a human sense. This
17 consists of a specific \l {coordinate}, an \l {address} and a
18 \l {boundingShape}{bounding shape}.
19 The \l {boundingShape}{bounding shape} represents the recommended region
20 to display when viewing this location.
21
22 The Location type is most commonly seen as the contents of a search
23 model such as the GeocodeModel. When a GeocodeModel returns the list of
24 locations found for a given query, it represents these as Location objects.
25
26 \section2 Example Usage
27
28 The following example shows a simple Location object being declared:
29
30 \code
31 Location {
32 coordinate {
33 latitude: -27.3
34 longitude: 153.1
35 }
36 address: Address {
37 ...
38 }
39 }
40 \endcode
41*/
42
43/*!
44 \qmlproperty VariantMap QtPositioning::Location::extendedAttributes
45
46 This property holds the extended attributes for this Location.
47 Extended attributes are backend-dependent and can be location-dependent.
48
49 \since 5.13
50*/
51
52QDeclarativeGeoLocation::QDeclarativeGeoLocation(QObject *parent)
53: QObject(parent)
54
55{
56 setLocationInternal(QGeoLocation());
57}
58
59QDeclarativeGeoLocation::QDeclarativeGeoLocation(const QGeoLocation &src, QObject *parent)
60: QObject(parent)
61{
62 setLocationInternal(src);
63}
64
65QDeclarativeGeoLocation::~QDeclarativeGeoLocation()
66{
67}
68
69/*!
70 \internal
71
72 This method is supposed to be used in the constructors, when we know that
73 the properties have no bindings, and we do not want to introduce any.
74*/
75void QDeclarativeGeoLocation::setLocationInternal(const QGeoLocation &src)
76{
77 m_address.setValueBypassingBindings(new QDeclarativeGeoAddress(src.address(), this));
78 m_coordinate.setValueBypassingBindings(src.coordinate());
79 m_boundingShape.setValueBypassingBindings(src.boundingShape());
80 m_extendedAttributes.setValueBypassingBindings(src.extendedAttributes());
81}
82
83/*!
84 \qmlproperty QGeoLocation QtPositioning::Location::location
85
86 For details on how to use this property to interface between C++ and QML see
87 "\l {Location - QGeoLocation} {Interfaces between C++ and QML Code}".
88
89 \note This property updates the whole geo location information, so using it
90 will result in breaking of all the bindings for all other properties.
91*/
92void QDeclarativeGeoLocation::setLocation(const QGeoLocation &src)
93{
94 if (m_address && m_address->parent() == this) {
95 m_address->setAddress(src.address());
96 } else if (!m_address || m_address->parent() != this) {
97 m_address.setValue(new QDeclarativeGeoAddress(src.address(), this));
98 m_address.notify();
99 }
100
101 setCoordinate(src.coordinate());
102 setBoundingShape(src.boundingShape());
103 setExtendedAttributes(src.extendedAttributes());
104}
105
106QGeoLocation QDeclarativeGeoLocation::location() const
107{
108 QGeoLocation retValue;
109 retValue.setAddress(m_address ? m_address->address() : QGeoAddress());
110 retValue.setCoordinate(m_coordinate);
111 retValue.setBoundingShape(m_boundingShape);
112 retValue.setExtendedAttributes(m_extendedAttributes);
113 return retValue;
114}
115
116/*!
117 \qmlproperty Address QtPositioning::Location::address
118
119 This property holds the address of the location which can be use to retrieve address details of the location.
120*/
121void QDeclarativeGeoLocation::setAddress(QDeclarativeGeoAddress *address)
122{
123 m_address.removeBindingUnlessInWrapper();
124
125 const QDeclarativeGeoAddress *oldAddress = m_address.valueBypassingBindings();
126 if (oldAddress == address)
127 return;
128
129 // implicitly deleting m_address.value() will force the QML bindings to
130 // be reevaluated by the QML engine. So we defer the deletion of the old
131 // address until a new value is assigned to the m_address.
132 m_address.setValueBypassingBindings(address);
133 m_address.notify();
134 if (oldAddress && (oldAddress->parent() == this))
135 delete oldAddress;
136}
137
138QBindable<QDeclarativeGeoAddress *> QDeclarativeGeoLocation::bindableAddress()
139{
140 return QBindable<QDeclarativeGeoAddress *>(&m_address);
141}
142
143QDeclarativeGeoAddress *QDeclarativeGeoLocation::address() const
144{
145 return m_address;
146}
147
148/*!
149 \qmlproperty coordinate QtPositioning::Location::coordinate
150
151 This property holds the exact geographical coordinate of the location which can be used to retrieve the latitude, longitude and altitude of the location.
152*/
153void QDeclarativeGeoLocation::setCoordinate(const QGeoCoordinate coordinate)
154{
155 m_coordinate = coordinate;
156}
157
158QBindable<QGeoCoordinate> QDeclarativeGeoLocation::bindableCoordinate()
159{
160 return QBindable<QGeoCoordinate>(&m_coordinate);
161}
162
163QGeoCoordinate QDeclarativeGeoLocation::coordinate() const
164{
165 return m_coordinate;
166}
167
168/*!
169 \since QtPositioning 6.2
170
171 \qmlproperty geoShape QtPositioning::Location::boundingShape
172
173 This property holds the recommended region to use when displaying the location.
174 For example, a building's location may have a region centered around the building,
175 but the region is large enough to show it's immediate surrounding geographical
176 context.
177
178 \note This property was introduced in Qt6 instead of boundingBox property.
179 It returns a \l geoShape instead of a \l geoRectangle.
180 Use \l QGeoShape::boundingGeoRectangle() to obtain a bounding
181 \l geoRectangle for the shape.
182
183 If you need to convert the returned shape to a specific type, use the
184 \c type property of \l geoShape together with convenience
185 methods from \l [QML]{QtPositioning} like
186 \l {QtPositioning::shapeToRectangle}{QtPositioning.shapeToRectangle()}.
187*/
188void QDeclarativeGeoLocation::setBoundingShape(const QGeoShape &boundingShape)
189{
190 m_boundingShape = boundingShape;
191}
192
193QBindable<QGeoShape> QDeclarativeGeoLocation::bindableBoundingShape()
194{
195 return QBindable<QGeoShape>(&m_boundingShape);
196}
197
198QVariantMap QDeclarativeGeoLocation::extendedAttributes() const
199{
200 return m_extendedAttributes;
201}
202
203void QDeclarativeGeoLocation::setExtendedAttributes(const QVariantMap &attributes)
204{
205 m_extendedAttributes = attributes;
206}
207
208QBindable<QVariantMap> QDeclarativeGeoLocation::bindableExtendedAttributes()
209{
210 return QBindable<QVariantMap>(&m_extendedAttributes);
211}
212
213QGeoShape QDeclarativeGeoLocation::boundingShape() const
214{
215 return m_boundingShape;
216}
217
218QT_END_NAMESPACE
219
220#include "moc_qdeclarativegeolocation_p.cpp"
Combined button and popup list for selecting options.