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
qdeclarativegeoaddress.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 Address
11 \inqmlmodule QtPositioning
12 \since 5.2
13
14 \brief The Address QML type represents a specific location as a street address.
15
16 An Address is used as a unit of data for queries such as (Reverse) Geocoding
17 or Places searches -- many of these operations either accept an Address
18 or return one.
19
20 Not all properties of an Address are necessarily available or relevant
21 in all parts of the world and all locales. The \l district, \l state and
22 \l county properties are particularly area-specific for many data sources,
23 and often only one or two of these are available or useful.
24
25 The Address has a \l text property which holds a formatted string. It
26 is the recommended way to display an address to the user and typically
27 takes the format of an address as found on an envelope, but this is not always
28 the case. The \l text may be automatically generated from constituent
29 address properties such as \l street, \l city and and so on, but can also
30 be explicitly assigned. See \l text for details.
31
32 \section2 Example Usage
33
34 The following code snippet shows the declaration of an Address object.
35
36 \code
37 Address {
38 id: address
39 street: "53 Brandl St"
40 city: "Eight Mile Plains"
41 country: "Australia"
42 countryCode: "AUS"
43 }
44 \endcode
45
46 This could then be used, for example, as the value of a geocoding query,
47 to get an exact longitude and latitude for the address.
48
49 \sa {QGeoAddress}
50*/
51
52QDeclarativeGeoAddress::QDeclarativeGeoAddress(QObject *parent) :
53 QObject(parent)
54{
55}
56
57QDeclarativeGeoAddress::QDeclarativeGeoAddress(const QGeoAddress &address, QObject *parent) :
58 QObject(parent), m_address(address)
59{
60}
61
62/*!
63 \qmlproperty QGeoAddress QtPositioning::Address::address
64
65 For details on how to use this property to interface between C++ and QML see
66 "\l {Address - QGeoAddress} {Interfaces between C++ and QML Code}".
67*/
68QGeoAddress QDeclarativeGeoAddress::address() const
69{
70 return m_address;
71}
72
73void QDeclarativeGeoAddress::setAddress(const QGeoAddress &address)
74{
75 // Elaborate but takes care of emiting needed signals
76 setText(address.text());
77 setCountry(address.country());
78 setCountryCode(address.countryCode());
79 setState(address.state());
80 setCounty(address.county());
81 setCity(address.city());
82 setDistrict(address.district());
83 setStreet(address.street());
84 setStreetNumber(address.streetNumber());
85 setPostalCode(address.postalCode());
86 m_address = address;
87}
88
89/*!
90 \qmlproperty string QtPositioning::Address::text
91
92 This property holds the address as a single formatted string. It is the recommended
93 string to use to display the address to the user. It typically takes the format of
94 an address as found on an envelope, but this is not always necessarily the case.
95
96 The address \c text is either automatically generated or explicitly assigned,
97 this can be determined by checking \l isTextGenerated.
98
99 If an empty string is assigned to \c text, then \l isTextGenerated will be set
100 to true and \c text will return a string which is locally formatted according to
101 \l countryCode and based on the properties of the address. Modifying the address
102 properties such as \l street, \l city and so on may cause the contents of \c text to
103 change.
104
105 If a non-empty string is assigned to \c text, then \l isTextGenerated will be
106 set to false and \c text will always return the explicitly assigned string.
107 Modifying address properties will not affect the \c text property.
108*/
109QString QDeclarativeGeoAddress::text() const
110{
111 return m_address.text();
112}
113
114void QDeclarativeGeoAddress::setText(const QString &address)
115{
116 QString oldText = m_address.text();
117 bool oldIsTextGenerated = m_address.isTextGenerated();
118 m_address.setText(address);
119
120 if (oldText != m_address.text())
121 emit textChanged();
122 if (oldIsTextGenerated != m_address.isTextGenerated())
123 emit isTextGeneratedChanged();
124}
125
126/*!
127 \qmlproperty string QtPositioning::Address::country
128
129 This property holds the country of the address as a single formatted string.
130*/
131QString QDeclarativeGeoAddress::country() const
132{
133 return m_address.country();
134}
135
136void QDeclarativeGeoAddress::setCountry(const QString &country)
137{
138 if (m_address.country() == country)
139 return;
140 QString oldText = m_address.text();
141 m_address.setCountry(country);
142 emit countryChanged();
143
144 if (m_address.isTextGenerated() && oldText != m_address.text())
145 emit textChanged();
146}
147
148/*!
149 \qmlproperty string QtPositioning::Address::countryCode
150
151 This property holds the country code of the address as a single formatted string.
152*/
153QString QDeclarativeGeoAddress::countryCode() const
154{
155 return m_address.countryCode();
156}
157
158void QDeclarativeGeoAddress::setCountryCode(const QString &countryCode)
159{
160 if (m_address.countryCode() == countryCode)
161 return;
162 QString oldText = m_address.text();
163 m_address.setCountryCode(countryCode);
164 emit countryCodeChanged();
165
166 if (m_address.isTextGenerated() && oldText != m_address.text())
167 emit textChanged();
168}
169
170/*!
171 \qmlproperty string QtPositioning::Address::state
172
173 This property holds the state of the address as a single formatted string.
174*/
175QString QDeclarativeGeoAddress::state() const
176{
177 return m_address.state();
178}
179
180void QDeclarativeGeoAddress::setState(const QString &state)
181{
182 if (m_address.state() == state)
183 return;
184 QString oldText = m_address.text();
185 m_address.setState(state);
186 emit stateChanged();
187
188 if (m_address.isTextGenerated() && oldText != m_address.text())
189 emit textChanged();
190}
191
192/*!
193 \qmlproperty string QtPositioning::Address::county
194
195 This property holds the county of the address as a single formatted string.
196*/
197QString QDeclarativeGeoAddress::county() const
198{
199 return m_address.county();
200}
201
202void QDeclarativeGeoAddress::setCounty(const QString &county)
203{
204 if (m_address.county() == county)
205 return;
206 QString oldText = m_address.text();
207 m_address.setCounty(county);
208 emit countyChanged();
209
210 if (m_address.isTextGenerated() && oldText != m_address.text())
211 emit textChanged();
212}
213
214/*!
215 \qmlproperty string QtPositioning::Address::city
216
217 This property holds the city of the address as a single formatted string.
218*/
219QString QDeclarativeGeoAddress::city() const
220{
221 return m_address.city();
222}
223
224void QDeclarativeGeoAddress::setCity(const QString &city)
225{
226 if (m_address.city() == city)
227 return;
228 QString oldText = m_address.text();
229 m_address.setCity(city);
230 emit cityChanged();
231
232 if (m_address.isTextGenerated() && oldText != m_address.text())
233 emit textChanged();
234}
235
236/*!
237 \qmlproperty string QtPositioning::Address::district
238
239 This property holds the district of the address as a single formatted string.
240*/
241QString QDeclarativeGeoAddress::district() const
242{
243 return m_address.district();
244}
245
246void QDeclarativeGeoAddress::setDistrict(const QString &district)
247{
248 if (m_address.district() == district)
249 return;
250 QString oldText = m_address.text();
251 m_address.setDistrict(district);
252 emit districtChanged();
253
254 if (m_address.isTextGenerated() && oldText != m_address.text())
255 emit textChanged();
256}
257
258/*!
259 \qmlproperty string QtPositioning::Address::street
260
261 This property holds the street of the address.
262
263 \note Before Qt6 this property could also contain things like a unit number,
264 a building name, or anything else that might be used to distinguish one
265 address from another. Since Qt6 use \l{QtPositioning::Address::}{streetNumber}
266 property for such information.
267*/
268QString QDeclarativeGeoAddress::street() const
269{
270 return m_address.street();
271}
272
273void QDeclarativeGeoAddress::setStreet(const QString &street)
274{
275 if (m_address.street() == street)
276 return;
277 QString oldText = m_address.text();
278 m_address.setStreet(street);
279 emit streetChanged();
280
281 if (m_address.isTextGenerated() && oldText != m_address.text())
282 emit textChanged();
283}
284
285/*!
286 \qmlproperty string QtPositioning::Address::streetNumber
287 \since QtPositioning 6.2
288
289 This property holds the street number of the address like a unit number,
290 a building name, or anything else that might be used to distinguish one
291 address from another.
292*/
293QString QDeclarativeGeoAddress::streetNumber() const
294{
295 return m_address.streetNumber();
296}
297
298void QDeclarativeGeoAddress::setStreetNumber(const QString &streetNumber)
299{
300 if (m_address.streetNumber() == streetNumber)
301 return;
302 QString oldText = m_address.text();
303 m_address.setStreetNumber(streetNumber);
304 emit streetNumberChanged();
305
306 if (m_address.isTextGenerated() && oldText != m_address.text())
307 emit textChanged();
308}
309
310/*!
311 \qmlproperty string QtPositioning::Address::postalCode
312
313 This property holds the postal code of the address as a single formatted string.
314*/
315QString QDeclarativeGeoAddress::postalCode() const
316{
317 return m_address.postalCode();
318}
319
320void QDeclarativeGeoAddress::setPostalCode(const QString &postalCode)
321{
322 if (m_address.postalCode() == postalCode)
323 return;
324 QString oldText = m_address.text();
325 m_address.setPostalCode(postalCode);
326 emit postalCodeChanged();
327
328 if (m_address.isTextGenerated() && oldText != m_address.text())
329 emit textChanged();
330}
331
332/*!
333 \qmlproperty bool QtPositioning::Address::isTextGenerated
334
335 This property holds a boolean that if true, indicates that \l text is automatically
336 generated from address properties. If false, it indicates that the \l text has been
337 explicitly assigned.
338
339*/
340bool QDeclarativeGeoAddress::isTextGenerated() const
341{
342 return m_address.isTextGenerated();
343}
344
345QT_END_NAMESPACE
346
347#include "moc_qdeclarativegeoaddress_p.cpp"
Combined button and popup list for selecting options.