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
qohosmapkitcommon.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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 <QtCore/qcoreapplication.h>
5#include <QtCore/qlist.h>
6#include <QtCore/qlocale.h>
7#include <qohosmapkitcommon.h>
8
10
11namespace OhosMapKit {
12
13namespace {
14
15const QString userAgentParameterName = QStringLiteral("ohosmapkit.useragent");
16const QString defaultUserAgent = QStringLiteral("Qt Location based application");
17
18}
19
21{
22 return QLocale::system().name().section(QLatin1Char('_'), 0, 0);
23}
24
25QString getAuthenticationKeyParameterOrEmpty(const QVariantMap &parameters)
26{
27 return parameters.value(authenticationKeyParameterName).toString();
28}
29
30QString getUserAgentParameterOrDefault(const QVariantMap &parameters)
31{
32 return parameters.contains(userAgentParameterName)
33 ? parameters.value(userAgentParameterName).toString()
34 : defaultUserAgent;
35}
36
38 const QUrl &requestUrl, const QString &userAgent, const QString &authenticationKey)
39{
40 QNetworkRequest networkRequest(requestUrl);
41 networkRequest.setHeader(QNetworkRequest::UserAgentHeader, userAgent.toLatin1());
42 networkRequest.setRawHeader(
43 "Authorization", QStringLiteral("Bearer %1").arg(authenticationKey).toLatin1());
44
45 return networkRequest;
46}
47
49 const QUrl &requestUrl, const QString &userAgent, const QString &authenticationKey)
50{
51 auto networkRequest = createOhosMapKitNetworkRequest(requestUrl, userAgent, authenticationKey);
52 networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
53 return networkRequest;
54}
55
56QString msgReplyError(const QString &errorString)
57{
58 return QCoreApplication::translate("OhosMapKit", "Network reply error: %1").arg(errorString);
59}
60
62{
63 return QCoreApplication::translate("OhosMapKit", "Null reply");
64}
65
66namespace CoordinateJson {
67
68const QString latitudeKey = QStringLiteral("lat");
69const QString longitudeKey = QStringLiteral("lng");
70
71QJsonObject tryConvertFromQGeoCoordinate(const QGeoCoordinate &coordinate)
72{
73 return coordinate.isValid()
74 ? QJsonObject(
75 {
76 {latitudeKey, coordinate.latitude()},
77 {longitudeKey, coordinate.longitude()},
78 })
79 : QJsonObject();
80}
81
82QGeoCoordinate tryConvertToQGeoCoordinate(const QJsonObject &coordinateObj)
83{
84 return coordinateObj.contains(latitudeKey) && coordinateObj.contains(longitudeKey)
85 ? QGeoCoordinate(
86 coordinateObj.value(latitudeKey).toDouble(),
87 coordinateObj.value(longitudeKey).toDouble())
88 : QGeoCoordinate();
89}
90
91}
92
93namespace CoordinateBoundsJson {
94
95const QString northeastKey = QStringLiteral("northeast");
96const QString southwestKey = QStringLiteral("southwest");
97
98QJsonObject tryConvertFromQGeoRectangle(const QGeoRectangle &boundingGeoRect)
99{
100 auto northeastCoordObj = CoordinateJson::tryConvertFromQGeoCoordinate(boundingGeoRect.topRight());
101 auto southwestCoordObj = CoordinateJson::tryConvertFromQGeoCoordinate(boundingGeoRect.bottomLeft());
102
103 return !northeastCoordObj.isEmpty() && !southwestCoordObj.isEmpty()
104 ? QJsonObject(
105 {
106 {northeastKey, northeastCoordObj},
107 {southwestKey, southwestCoordObj},
108 })
109 : QJsonObject();
110}
111
112QGeoRectangle tryConvertToQGeoRectangle(const QJsonObject &coordinateBounds)
113{
114 if (!coordinateBounds.contains(northeastKey) || !coordinateBounds.contains(southwestKey))
115 return {};
116
117 const auto northeast =
118 CoordinateJson::tryConvertToQGeoCoordinate(coordinateBounds.value(northeastKey).toObject());
119 const auto southwest =
120 CoordinateJson::tryConvertToQGeoCoordinate(coordinateBounds.value(southwestKey).toObject());
121
122 return northeast.isValid() && southwest.isValid()
123 ? QGeoRectangle(QList<QGeoCoordinate>({northeast, southwest}))
124 : QGeoRectangle();
125}
126
127}
128
129namespace SiteJson {
130
131QGeoAddress tryConvertToGeoAddress(const QJsonObject &siteJsonObj)
132{
133 QGeoAddress address;
134
135 const auto addressJsonObj = siteJsonObj.value(QStringLiteral("address")).toObject();
136 if (!addressJsonObj.isEmpty()) {
137 address.setCountry(addressJsonObj.value(QStringLiteral("country")).toVariant().toString());
138 address.setCountryCode(addressJsonObj.value(QStringLiteral("countryCode")).toVariant().toString());
139 address.setState(addressJsonObj.value(QStringLiteral("adminArea")).toVariant().toString());
140
141 auto city = addressJsonObj.value(QStringLiteral("city")).toVariant().toString();
142 address.setCity(
143 !city.isEmpty()
144 ? city
145 : addressJsonObj.value(QStringLiteral("locality")).toVariant().toString());
146 address.setPostalCode(addressJsonObj.value(QStringLiteral("postalCode")).toVariant().toString());
147
148 auto streetName = addressJsonObj.value(QStringLiteral("thoroughfare")).toVariant().toString();
149 auto streetNumber = addressJsonObj.value(QStringLiteral("streetNumber")).toVariant().toString();
150 address.setStreet(QString("%1 %2").arg(streetName).arg(streetNumber).trimmed());
151 }
152
153 address.setText(siteJsonObj.value(QStringLiteral("formatAddress")).toVariant().toString());
154
155 return address;
156}
157
158QGeoLocation tryConvertToQGeoLocation(const QJsonObject &siteJsonObj)
159{
160 QGeoLocation location;
161
162 location.setAddress(tryConvertToGeoAddress(siteJsonObj));
163 location.setCoordinate(
164 OhosMapKit::CoordinateJson::tryConvertToQGeoCoordinate(
165 siteJsonObj.value(QStringLiteral("location")).toObject()));
166 location.setBoundingShape(
167 OhosMapKit::CoordinateBoundsJson::tryConvertToQGeoRectangle(
168 siteJsonObj.value(QStringLiteral("viewport")).toObject()));
169
170 return location;
171}
172
173}
174
175}
176
177QT_END_NAMESPACE
QGeoRectangle tryConvertToQGeoRectangle(const QJsonObject &coordinateBounds)
QJsonObject tryConvertFromQGeoRectangle(const QGeoRectangle &boundingGeoRect)
QGeoCoordinate tryConvertToQGeoCoordinate(const QJsonObject &coordinateObj)
QJsonObject tryConvertFromQGeoCoordinate(const QGeoCoordinate &coordinate)
QGeoAddress tryConvertToGeoAddress(const QJsonObject &siteJsonObj)
QGeoLocation tryConvertToQGeoLocation(const QJsonObject &siteJsonObj)
QNetworkRequest createOhosMapKitNetworkRequestWithJsonBody(const QUrl &requestUrl, const QString &userAgent, const QString &authenticationKey)
QString msgNullReply()
QString getLanguageCode()
QNetworkRequest createOhosMapKitNetworkRequest(const QUrl &requestUrl, const QString &userAgent, const QString &authenticationKey)
QString getAuthenticationKeyParameterOrEmpty(const QVariantMap &parameters)
QString msgReplyError(const QString &errorString)
QString getUserAgentParameterOrDefault(const QVariantMap &parameters)
Combined button and popup list for selecting options.