Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgeoaddress.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
4#include "qgeoaddress.h"
5#include "qgeoaddress_p.h"
6
7#include <QtCore/QStringList>
8
9#ifdef QGEOADDRESS_DEBUG
10#include <QDebug>
11#endif
12
14
16
17/*
18 Combines a list of address parts into a single line.
19
20 The parts parameter contains both address elements such as city, state and so on
21 as well as separators such as spaces and commas.
22
23 It is expected that an element is always followed by a separator and the last
24 sepator is usually a new line delimeter.
25
26 For example: Springfield, 8900
27 would have four parts
28 ["Springfield", ", ", "8900", "<br>"]
29
30 The addressLine takes care of putting in separators appropriately or leaving
31 them out depending on whether the adjacent elements are present or not.
32 For example if city were empty in the above scenario the returned string is "8900<br>"
33 If the postal code was empty, returned string is "Springfield<br>"
34 If both city and postal code were empty, the returned string is "".
35*/
36static QString addressLine(const QStringList &parts)
37{
39 Q_ASSERT(parts.size() % 2 == 0);
40
41 //iterate until just before the last pair
42 QString penultimateSeparator;
43 for (qsizetype i = 0; i < parts.size() - 2; i += 2) {
44 if (!parts.at(i).isEmpty()) {
45 line.append(parts.at(i) + parts.at(i + 1));
46 penultimateSeparator = parts.at(i + 1);
47 }
48 }
49
50 if (parts.at(parts.size() - 2).isEmpty()) {
51 line.chop(penultimateSeparator.size());
52
53 if (!line.isEmpty())
54 line.append(parts.at(parts.size() - 1));
55 } else {
56 line.append(parts.at(parts.size() - 2));
57 line.append(parts.at(parts.size() - 1));
58 }
59
60 return line;
61}
62
63/*
64 Returns a single formatted string representing the \a address. Lines of the address
65 are delimited by \a newLine. By default lines are delimited by <br/>. The \l
66 {QGeoAddress::countryCode} {countryCode} of the \a address determines the format of
67 the resultant string.
68*/
70 const QString &newLine = QLatin1String("<br/>"))
71{
72 const QString Comma(QStringLiteral(", "));
73 const QString Dash(QStringLiteral("-"));
74 const QString Space(QStringLiteral(" "));
75
77
78 // We have 2 main approaches here:
79 // 1. street number goes after street name
80 // 2. street number goes before street name
81 // We need to take it into account while generating the formatted address.
82 // Currently these pages were used to check the formats:
83 // https://en.wikipedia.org/wiki/Address
84 // https://www.grcdi.nl/gsb/world%20address%20formats.html
85 // We do not take into account such address extensions as apartment number,
86 // because this is not what you can usually get from map providers (like
87 // openstreetmap).
88
89 if (address.countryCode() == QLatin1String("ALB")
90 || address.countryCode() == QLatin1String("MTQ")) {
91 text += addressLine(QStringList() << address.street() << Space
92 << address.streetNumber() << newLine);
93 text += addressLine(QStringList() << address.postalCode() << Comma
94 << address.city() << newLine);
95 text += addressLine(QStringList() << address.country() << newLine);
96 } else if (address.countryCode() == QLatin1String("AND")
97 || address.countryCode() == QLatin1String("AUT")
98 || address.countryCode() == QLatin1String("GLP")
99 || address.countryCode() == QLatin1String("ITA")
100 || address.countryCode() == QLatin1String("RUS")
101 || address.countryCode() == QLatin1String("SMR")
102 || address.countryCode() == QLatin1String("VAT")) {
103 text += addressLine(QStringList() << address.street() << Space
104 << address.streetNumber() << newLine);
105 text += addressLine(QStringList() << address.postalCode() << Space
106 << address.city() << newLine);
107 text += addressLine(QStringList() << address.country() << newLine);
108 }else if (address.countryCode() == QLatin1String("FRA")
109 || address.countryCode() == QLatin1String("GUF")
110 || address.countryCode() == QLatin1String("LUX")
111 || address.countryCode() == QLatin1String("MCO")
112 || address.countryCode() == QLatin1String("REU")) {
113 text += addressLine(QStringList() << address.streetNumber() << Space
114 << address.street() << newLine);
115 text += addressLine(QStringList() << address.postalCode() << Space
116 << address.city() << newLine);
117 text += addressLine(QStringList() << address.country() << newLine);
118
119 } else if (address.countryCode() == QLatin1String("ARE")
120 || address.countryCode() == QLatin1String("BHS")) {
121 text += addressLine(QStringList() << address.street() << Space
122 << address.streetNumber() << newLine);
123 text += addressLine(QStringList() << address.district() << Space
124 << address.city() << newLine);
125 text += addressLine(QStringList() << address.country() << newLine);
126 } else if (address.countryCode() == QLatin1String("AUS")) {
127 text += addressLine(QStringList() << address.streetNumber() << Space
128 << address.street() << newLine);
129 text += addressLine(QStringList() << (address.district().isEmpty() ? address.city() : address.district())
130 << Space << address.state() << Space << address.postalCode() << newLine);
131 text += addressLine(QStringList() << address.country() << newLine);
132 } else if (address.countryCode() == QLatin1String("BHR")) {
133 text += addressLine(QStringList() << address.street() << Space
134 << address.streetNumber() << newLine);
135 text += addressLine(QStringList() << address.district() << Comma
136 << address.city() << Comma << address.state() << newLine);
137 text += addressLine(QStringList() << address.country() << newLine);
138 } else if (address.countryCode() == QLatin1String("BRA")) {
139 text += addressLine(QStringList() << address.street() << Space
140 << address.streetNumber() << newLine);
141 text += addressLine(QStringList() << address.district() << Space
142 << address.city() << Dash << address.state() << Space << address.postalCode() << newLine);
143 text += addressLine(QStringList() << address.country() << newLine);
144 } else if (address.countryCode() == QLatin1String("BRN")
145 || address.countryCode() == QLatin1String("JOR")
146 || address.countryCode() == QLatin1String("LBN")
147 || address.countryCode() == QLatin1String("NZL")) {
148 text += addressLine(QStringList() << address.streetNumber() << Space
149 << address.street() << newLine);
150 text += addressLine(QStringList() << address.district() << Space
151 << address.city() << Space << address.postalCode() << newLine);
152 text += addressLine(QStringList() << address.country() << newLine);
153 } else if (address.countryCode() == QLatin1String("CAN")
154 || address.countryCode() == QLatin1String("USA")
155 || address.countryCode() == QLatin1String("VIR")) {
156 text += addressLine(QStringList() << address.streetNumber() << Space
157 << address.street() << newLine);
158 text += addressLine(QStringList() << address.city() << Comma << address.state() << Space
159 << address.postalCode() << newLine);
160 text += addressLine(QStringList() << address.country() << newLine);
161 } else if (address.countryCode() == QLatin1String("CHN")) {
162 text += addressLine(QStringList() << address.street() << Space
163 << address.streetNumber() << Comma
164 << address.city() << newLine);
165 text += addressLine(QStringList() << address.postalCode() << Space << address.state() << newLine);
166 text += addressLine(QStringList() << address.country() << newLine);
167 } else if (address.countryCode() == QLatin1String("CHL")) {
168 text += addressLine(QStringList() << address.street() << Space
169 << address.streetNumber() << newLine);
170 text += addressLine(QStringList() << address.postalCode() << Space
171 << address.district() << Comma << address.city() << Comma
172 << address.state() << newLine);
173 text += addressLine(QStringList() << address.country() << newLine);
174 } else if (address.countryCode() == QLatin1String("CYM")) {
175 text += addressLine(QStringList() << address.streetNumber() << Space
176 << address.street() << newLine);
177 text += addressLine(QStringList() << address.state() << Space
178 << address.postalCode() << newLine);
179 text += addressLine(QStringList() << address.country() << newLine);
180 } else if (address.countryCode() == QLatin1String("GBR")) {
181 text += addressLine(QStringList() << address.streetNumber() << Space
182 << address.street() << newLine);
183 text += addressLine(QStringList() << address.district() << Comma
184 << address.city() << Comma << address.postalCode() << newLine);
185 text += addressLine(QStringList() << address.country() << newLine);
186 } else if (address.countryCode() == QLatin1String("GIB")) {
187 text += addressLine(QStringList() << address.streetNumber() << Space
188 << address.street() << newLine);
189 text += addressLine(QStringList() << address.city() << newLine);
190 text += addressLine(QStringList() << address.country() << newLine);
191 } else if (address.countryCode() == QLatin1String("HKG")) {
192 text += addressLine(QStringList() << address.streetNumber() << Space
193 << address.street() << newLine);
194 text += addressLine(QStringList() << address.district() << newLine);
195 text += addressLine(QStringList() << address.city() << newLine);
196 } else if (address.countryCode() == QLatin1String("IND")) {
197 text += addressLine(QStringList() << address.streetNumber() << Space
198 << address.street() << newLine);
199 text += addressLine(QStringList() << address.city() << Space << address.postalCode() << Space
200 << address.state() << newLine);
201 text += addressLine(QStringList() << address.country() << newLine);
202 } else if (address.countryCode() == QLatin1String("IDN")
203 || address.countryCode() == QLatin1String("JEY")
204 || address.countryCode() == QLatin1String("LVA")) {
205 text += addressLine(QStringList() << address.street() << Space
206 << address.streetNumber() << newLine);
207 text += addressLine(QStringList() << address.city() << Comma << address.postalCode() << newLine);
208 text += addressLine(QStringList() << address.country() << newLine);
209 } else if (address.countryCode() == QLatin1String("IRL")) {
210 text += addressLine(QStringList() << address.streetNumber() << Space
211 << address.street() << newLine);
212 text += addressLine(QStringList() << address.district() << Comma << address.state() << newLine);
213 text += addressLine(QStringList() << address.country() << newLine);
214 } else if (address.countryCode() == QLatin1String("KWT")) {
215 text += addressLine(QStringList() << address.street() << Space
216 << address.streetNumber() << newLine);
217 text += addressLine(QStringList() << address.postalCode() << Comma
218 << address.district() << Comma << address.city() << newLine);
219 text += addressLine(QStringList() << address.country() << newLine);
220 } else if (address.countryCode() == QLatin1String("MLT")
221 || address.countryCode() == QLatin1String("SGP")) {
222 text += addressLine(QStringList() << address.streetNumber() << Space
223 << address.street() << newLine);
224 text += addressLine(QStringList() << address.city() << Space << address.postalCode() << newLine);
225 text += addressLine(QStringList() << address.country() << newLine);
226 } else if (address.countryCode() == QLatin1String("UKR")) {
227 text += addressLine(QStringList() << address.street() << Space
228 << address.streetNumber() << newLine);
229 text += addressLine(QStringList() << address.city() << Space << address.postalCode() << newLine);
230 text += addressLine(QStringList() << address.country() << newLine);
231 } else if (address.countryCode() == QLatin1String("MEX")) {
232 text += addressLine(QStringList() << address.street() << Space
233 << address.streetNumber() << newLine);
234 text += addressLine(QStringList() << address.district() << newLine);
235 text += addressLine(QStringList() << address.postalCode() << Space << address.city() << Comma
236 << address.state() << newLine);
237 text += addressLine(QStringList() << address.country() << newLine);
238 } else if (address.countryCode() == QLatin1String("MYS")) {
239 text += addressLine(QStringList() << address.streetNumber() << Space
240 << address.street() << newLine);
241 text += addressLine(QStringList() << address.postalCode() << Space << address.city() << newLine);
242 text += addressLine(QStringList() << address.state() << newLine);
243 text += addressLine(QStringList() << address.country() << newLine);
244 } else if (address.countryCode() == QLatin1String("OMN")) {
245 text += addressLine(QStringList() << address.streetNumber() << Space
246 << address.street() << newLine);
247 text += addressLine(QStringList() << address.district() << Comma
248 << address.postalCode() << Comma
249 << address.city() << Comma
250 << address.country() << newLine);
251 } else if (address.countryCode() == QLatin1String("PRI")) {
252 text += addressLine(QStringList() << address.street() << Space
253 << address.streetNumber() << newLine);
254 text += addressLine(QStringList() << address.district() << Comma << address.city() << Comma
255 << address.state() << Comma << address.postalCode() << newLine);
256 text += addressLine(QStringList() << address.country() << newLine);
257 } else if (address.countryCode() == QLatin1String("QAT")) {
258 text += addressLine(QStringList() << address.street() << Space
259 << address.streetNumber() << newLine);
260 text += addressLine(QStringList() << address.district() << Space << address.city() << Comma
261 << address.country() << newLine);
262 } else if (address.countryCode() == QLatin1String("SAU")) {
263 text += addressLine(QStringList() << address.streetNumber() << Space
264 << address.street() << Space
265 << address.district() << newLine);
266 text += addressLine(QStringList() << address.city() << Space << address.postalCode() << newLine);
267 text += addressLine(QStringList() << address.country() << newLine);
268 } else if (address.countryCode() == QLatin1String("TWN")) {
269 text += addressLine(QStringList() << address.street() << Space
270 << address.streetNumber() << Comma
271 << address.district() << Comma
272 << address.city() << newLine);
273 text += addressLine(QStringList() << address.country() << newLine);
274 } else if (address.countryCode() == QLatin1String("THA")) {
275 text += addressLine(QStringList() << address.street() << Space
276 << address.streetNumber() << newLine);
277 text += addressLine(QStringList() << address.district() << Comma << address.city() << Space
278 << address.postalCode() << newLine);
279 text += addressLine(QStringList() << address.country() << newLine);
280 } else if (address.countryCode() == QLatin1String("TUR")) {
281 text += addressLine(QStringList() << address.street() << Space
282 << address.streetNumber() << newLine);
283 text += addressLine(QStringList() << address.postalCode() << Space << address.district() << Comma
284 << address.city() << newLine);
285 text += addressLine(QStringList() << address.country() << newLine);
286 } else if (address.countryCode() == QLatin1String("VEN")) {
287 text += addressLine(QStringList() << address.street() << Space
288 << address.streetNumber() << newLine);
289 text += addressLine(QStringList() << address.city() << Space << address.postalCode() << Comma
290 << address.state() << newLine);
291 text += addressLine(QStringList() << address.country() << newLine);
292 } else if (address.countryCode() == QLatin1String("ZAF")) {
293 text += addressLine(QStringList() << address.street() << Space
294 << address.streetNumber() << newLine);
295 text += addressLine(QStringList() << address.district() << Comma << address.city() << newLine);
296 text += addressLine(QStringList() << address.country() << newLine);
297 } else {
298 text += addressLine(QStringList() << address.street() << Space
299 << address.streetNumber() << newLine);
300 text += addressLine(QStringList() << address.postalCode() << Space << address.city() << newLine);
301 text += addressLine(QStringList() << address.country() << newLine);
302 }
303
304 text.chop(newLine.size());
305 return text;
306}
307
309 : QSharedData(),
310 m_autoGeneratedText(false)
311{
312}
313
316 sCountry(other.sCountry),
317 sCountryCode(other.sCountryCode),
318 sState(other.sState),
319 sCounty(other.sCounty),
320 sCity(other.sCity),
321 sDistrict(other.sDistrict),
322 sStreet(other.sStreet),
323 sStreetNumber(other.sStreetNumber),
324 sPostalCode(other.sPostalCode),
325 sText(other.sText),
326 m_autoGeneratedText(false)
327{
328}
329
333
368
376
406
408
409
414{
415 if (this == &address)
416 return *this;
417
418 d = address.d;
419 return *this;
420}
421
457{
458 if (d->sText.isEmpty())
459 return formattedAddress(*this);
460 else
461 return d->sText;
462}
463
472{
473 d->sText = text;
474}
475
480{
481 return d->sCountry;
482}
483
488{
489 d->sCountry = country;
490}
491
496{
497 return d->sCountryCode;
498}
499
503void QGeoAddress::setCountryCode(const QString &countryCode)
504{
506}
507
512{
513 return d->sState;
514}
515
520{
521 d->sState = state;
522}
523
528{
529 return d->sCounty;
530}
531
536{
537 d->sCounty = county;
538}
539
544{
545 return d->sCity;
546}
547
552{
553 d->sCity = city;
554}
555
560{
561 return d->sDistrict;
562}
563
568{
569 d->sDistrict = district;
570}
571
582{
583 return d->sStreet;
584}
585
596{
597 d->sStreet = street;
598}
599
612{
613 return d->sStreetNumber;
614}
615
627void QGeoAddress::setStreetNumber(const QString &streetNumber)
628{
630}
631
636{
637 return d->sPostalCode;
638}
639
643void QGeoAddress::setPostalCode(const QString &postalCode)
644{
646}
647
653{
654 return d->sCountry.isEmpty() &&
655 d->sCountryCode.isEmpty() &&
656 d->sState.isEmpty() &&
657 d->sCounty.isEmpty() &&
658 d->sCity.isEmpty() &&
659 d->sDistrict.isEmpty() &&
660 d->sStreet.isEmpty() &&
661 d->sStreetNumber.isEmpty() &&
662 d->sPostalCode.isEmpty() &&
663 d->sText.isEmpty();
664
665}
666
671{
672 d->sCountry.clear();
673 d->sCountryCode.clear();
674 d->sState.clear();
675 d->sCounty.clear();
676 d->sCity.clear();
677 d->sDistrict.clear();
678 d->sStreet.clear();
679 d->sStreetNumber.clear();
680 d->sPostalCode.clear();
681 d->sText.clear();
682}
683
691{
692 return d->sText.isEmpty();
693}
694
695bool QGeoAddress::equals(const QGeoAddress &lhs, const QGeoAddress &rhs)
696{
697#ifdef QGEOADDRESS_DEBUG
698 qDebug() << "country" << (lhs.d->sCountry == rhs.d->sCountry);
699 qDebug() << "countryCode" << (lhs.d->sCountryCode == rhs.d->sCountryCode);
700 qDebug() << "state:" << (lhs.d->sState == rhs.d->sState);
701 qDebug() << "county:" << (lhs.d->sCounty == rhs.d->sCounty);
702 qDebug() << "city:" << (lhs.d->sCity == rhs.d->sCity);
703 qDebug() << "district:" << (lhs.d->sDistrict == rhs.d->sDistrict);
704 qDebug() << "street:" << (lhs.d->sStreet == rhs.d->sStreet);
705 qDebug() << "street number:" << (lhs.d->sStreetNumber == rhs.d->sStreetNumber);
706 qDebug() << "postalCode:" << (lhs.d->sPostalCode == rhs.d->sPostalCode);
707 qDebug() << "text:" << (lhs.text() == rhs.text());
708#endif
709
710 return lhs.d->sCountry == rhs.d->sCountry &&
711 lhs.d->sCountryCode == rhs.d->sCountryCode &&
712 lhs.d->sState == rhs.d->sState &&
713 lhs.d->sCounty == rhs.d->sCounty &&
714 lhs.d->sCity == rhs.d->sCity &&
715 lhs.d->sDistrict == rhs.d->sDistrict &&
716 lhs.d->sStreet == rhs.d->sStreet &&
717 lhs.d->sStreetNumber == rhs.d->sStreetNumber &&
718 lhs.d->sPostalCode == rhs.d->sPostalCode &&
719 lhs.text() == rhs.text();
720}
721
728size_t qHash(const QGeoAddress &address, size_t seed) noexcept
729{
730 size_t hash = qHashMulti(seed, address.country(), address.countryCode(), address.state(),
731 address.county(), address.city(), address.district(),
732 address.street(), address.streetNumber(), address.postalCode());
733
734 // If the text is generated from all fields, there is no need to use the
735 // resulting string in the hash. However, when the text is specified
736 // explicitly, we need to use it as well.
737 if (!address.isTextGenerated())
738 hash = qHashMulti(seed, hash, address.text());
739 return hash;
740}
741
QString sState
state field
QString sStreetNumber
street number field
QString sPostalCode
postal code field
QString sStreet
street name field
QString sCountry
country field
QString sCity
city field
QString sCountryCode
country code field
QString sCounty
county field
QString sDistrict
district field
\inmodule QtPositioning
Definition qgeoaddress.h:18
QGeoAddress()
Default constructor.
bool isTextGenerated() const
Returns true if QGeoAddress::text() is automatically generated from address elements,...
QString district() const
Returns the district.
size_t qHash(const QGeoAddress &address, size_t seed) noexcept
Returns the hash value for the address, using seed for the calculation.
QString state() const
Returns the state.
void clear()
Clears all of the address' data fields.
void setCountryCode(const QString &countryCode)
Sets the countryCode according to ISO 3166-1 alpha-3.
void setCountry(const QString &country)
Sets the country name.
void setStreet(const QString &street)
Sets the street name to street.
QString street() const
Returns the street name.
void setCounty(const QString &county)
Sets the county.
void setText(const QString &text)
If text is not empty, explicitly assigns text as the string to be returned by text().
QString streetNumber() const
~QGeoAddress()
Destroys this address.
QString city() const
Returns the city.
void setStreetNumber(const QString &streetNumber)
void setPostalCode(const QString &postalCode)
Sets the postalCode.
QString countryCode() const
Returns the country code according to ISO 3166-1 alpha-3.
QString county() const
Returns the county.
QString postalCode() const
Returns the postal code.
QString country() const
Returns the country name.
QString text() const
Returns the address as a single formatted string.
void setDistrict(const QString &district)
Sets the district.
void setCity(const QString &city)
Sets the city.
bool isEmpty() const
Returns whether this address is empty.
void setState(const QString &state)
Sets the state.
\inmodule QtCore
Definition qshareddata.h:19
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void chop(qsizetype n)
Removes n characters from the end of the string.
Definition qstring.cpp:6340
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
QString & append(QChar c)
Definition qstring.cpp:3252
QHash< int, QWidget * > hash
[35multi]
QString text
else opt state
[0]
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.
static QString formattedAddress(const QGeoAddress &address, const QString &newLine=QLatin1String("<br/>"))
static QT_BEGIN_NAMESPACE QString addressLine(const QStringList &parts)
constexpr QtPrivate::QHashMultiReturnType< T... > qHashMulti(size_t seed, const T &... args) noexcept(std::conjunction_v< QtPrivate::QNothrowHashable< T >... >)
@ Space
#define qDebug
[1]
Definition qlogging.h:164
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1390
GLuint GLuint64EXT address
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QT_DEFINE_QSDP_SPECIALIZATION_DTOR(Class)
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
ptrdiff_t qsizetype
Definition qtypes.h:165
QSharedPointer< T > other(t)
[5]