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