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