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
qplacesearchreplymapbox.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 Mapbox, Inc.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6#include "qmapboxcommon.h"
7
8#include <QtCore/QJsonDocument>
9#include <QtCore/QJsonArray>
10#include <QtCore/QJsonObject>
11#include <QtNetwork/QNetworkReply>
12#include <QtPositioning/QGeoCircle>
13#include <QtPositioning/QGeoRectangle>
14#include <QtLocation/QPlace>
15#include <QtLocation/QPlaceCategory>
16#include <QtLocation/QPlaceIcon>
17#include <QtLocation/QPlaceResult>
18#include <QtLocation/QPlaceSearchRequest>
19#include <QtLocation/QPlaceContactDetail>
20
21#include <algorithm>
22
24
25namespace {
26
27// https://www.mapbox.com/api-documentation/#response-object
28QPlaceResult parsePlaceResult(const QJsonObject &response, const QString &attribution)
29{
30 QPlace place;
31
32 place.setAttribution(attribution);
33 place.setPlaceId(response.value(QStringLiteral("id")).toString());
35
36 QString placeName = response.value(QStringLiteral("text")).toString();
37 if (placeName.isEmpty())
38 placeName = response.value(QStringLiteral("place_name")).toString();
39
40 place.setName(placeName);
41 place.setDetailsFetched(true);
42
43 // Unused data: type, place_type, relevance, properties.short_code,
44 // properties.landmark, properties.wikidata
45
46 // The property object is unstable and only Carmen GeoJSON properties are
47 // guaranteed. This implementation should check for the presence of these
48 // values in a response before it attempts to use them.
49 if (response.value(QStringLiteral("properties")).isObject()) {
50 const QJsonObject properties = response.value(QStringLiteral("properties")).toObject();
51
52 const QString makiString = properties.value(QStringLiteral("maki")).toString();
53 if (!makiString.isEmpty()) {
54 QVariantMap iconParameters;
55 iconParameters.insert(QPlaceIcon::SingleUrl,
56 QUrl::fromLocalFile(QStringLiteral(":/mapbox/") + makiString + QStringLiteral(".svg")));
57
59 icon.setParameters(iconParameters);
60 place.setIcon(icon);
61 }
62
63 const QString phoneString = properties.value(QStringLiteral("tel")).toString();
64 if (!phoneString.isEmpty()) {
65 QPlaceContactDetail phoneDetail;
67 phoneDetail.setValue(phoneString);
68 place.setContactDetails(QPlaceContactDetail::Phone, QList<QPlaceContactDetail>() << phoneDetail);
69 }
70
71 const QString categoryString = properties.value(QStringLiteral("category")).toString();
72 if (!categoryString.isEmpty()) {
73 QList<QPlaceCategory> categories;
74 for (const QString &categoryId : categoryString.split(QStringLiteral(", "), Qt::SkipEmptyParts)) {
77 category.setCategoryId(categoryId);
78 categories.append(category);
79 }
81 }
82 }
83
84 // XXX: matching_text, matching_place_name
85 // XXX: text_{language}, place_name_{language}
86 // XXX: language, language_{language}
87
89
90 // XXX: geometry, geometry.type, geometry.coordinates, geometry.interpolated
91
93 result.setPlace(place);
94 result.setTitle(place.name());
95
96 return result;
97}
98
99} // namespace
100
102: QPlaceSearchReply(parent)
103{
105 if (!reply) {
106 setError(UnknownError, QStringLiteral("Null reply"));
107 return;
108 }
110
111 connect(reply, &QNetworkReply::finished, this, &QPlaceSearchReplyMapbox::onReplyFinished);
112 connect(reply, &QNetworkReply::errorOccurred, this, &QPlaceSearchReplyMapbox::onNetworkError);
113
116}
117
121
123{
125 emit errorOccurred(errorCode, errorString);
126
127 setFinished(true);
128 emit finished();
129}
130
131void QPlaceSearchReplyMapbox::onReplyFinished()
132{
133 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
135
137 return;
138
140 if (!document.isObject()) {
141 setError(ParseError, tr("Response parse error"));
142 return;
143 }
144
145 const QJsonArray features = document.object().value(QStringLiteral("features")).toArray();
146 const QString attribution = document.object().value(QStringLiteral("attribution")).toString();
147
148 const QGeoCoordinate searchCenter = request().searchArea().center();
149 const QList<QPlaceCategory> categories = request().categories();
150
151 QList<QPlaceSearchResult> results;
152 for (const QJsonValue feature : features) {
153 QPlaceResult placeResult = parsePlaceResult(feature.toObject(), attribution);
154
155 if (!categories.isEmpty()) {
156 const QList<QPlaceCategory> placeCategories = placeResult.place().categories();
157 bool categoryMatch = false;
158 if (!placeCategories.isEmpty()) {
159 for (const QPlaceCategory &placeCategory : placeCategories) {
160 if (categories.contains(placeCategory)) {
161 categoryMatch = true;
162 break;
163 }
164 }
165 }
166 if (!categoryMatch)
167 continue;
168 }
169 placeResult.setDistance(searchCenter.distanceTo(placeResult.place().location().coordinate()));
170 results.append(placeResult);
171 }
172
173 if (request().relevanceHint() == QPlaceSearchRequest::DistanceHint) {
174 std::sort(results.begin(), results.end(), [](const QPlaceResult &a, const QPlaceResult &b) -> bool {
175 return a.distance() < b.distance();
176 });
177 } else if (request().relevanceHint() == QPlaceSearchRequest::LexicalPlaceNameHint) {
178 std::sort(results.begin(), results.end(), [](const QPlaceResult &a, const QPlaceResult &b) -> bool {
179 return a.place().name() < b.place().name();
180 });
181 }
182
184
185 setFinished(true);
186 emit finished();
187}
188
189void QPlaceSearchReplyMapbox::onNetworkError(QNetworkReply::NetworkError error)
190{
192 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
195}
196
\inmodule QtPositioning
QGeoCoordinate center
Definition qgeoshape.h:22
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
\inmodule QtCore\reentrant
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=nullptr)
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
QJsonValue value(const QString &key) const
Returns a QJsonValue representing the value for the key key.
\inmodule QtCore\reentrant
Definition qjsonvalue.h:25
QJsonObject toObject() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QString toString() const
Converts the value to a QString and returns it.
iterator end()
Definition qlist.h:626
iterator begin()
Definition qlist.h:625
void append(parameter_type t)
Definition qlist.h:458
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
static QString mapboxNameForCategory(const QString &category)
static QGeoLocation parseGeoLocation(const QJsonObject &response)
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
void errorOccurred(QNetworkReply::NetworkError)
NetworkError error() const
Returns the error that was found during the processing of this request.
virtual void abort()=0
Aborts the operation immediately and close down any network connections still open.
NetworkError
Indicates all possible error conditions found during the processing of the request.
void finished()
This signal is emitted when the reply has finished processing.
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition qobject.cpp:2658
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
\inmodule QtLocation
The QPlaceContactDetail class represents a contact detail such as a phone number or website url.
void setLabel(const QString &label)
static const QString Phone
\qmlvaluetype contactDetail \inqmlmodule QtLocation
\inmodule QtLocation
Definition qplaceicon.h:23
static const QString SingleUrl
\qmlvaluetype icon \inqmlmodule QtLocation
Definition qplaceicon.h:32
QPlaceReply::Error error() const
Returns the error code.
void errorOccurred(QPlaceReply::Error error, const QString &errorString=QString())
This signal is emitted when an error has been detected in the processing of this reply.
void finished()
This signal is emitted when this reply has finished processing.
Error
Describes an error which occurred during an operation.
Definition qplacereply.h:18
void aborted()
QString errorString() const
Returns the error string of the reply.
void setError(QPlaceReply::Error error, const QString &errorString)
Sets the error and errorString of the reply.
void setFinished(bool finished)
Sets the status of whether the reply is finished or not.
\inmodule QtLocation
void setPlace(const QPlace &place)
Sets the place that this result refers to.
QPlaceSearchReplyMapbox(const QPlaceSearchRequest &request, QNetworkReply *reply, QPlaceManagerEngineMapbox *parent)
void setError(QPlaceReply::Error errorCode, const QString &errorString)
\inmodule QtLocation
void setRequest(const QPlaceSearchRequest &request)
Sets the search request used to generate this reply.
QPlaceSearchRequest request() const
Returns the search request that was used to generate this reply.
QList< QPlaceSearchResult > results() const
Returns a list of search results;.
void setResults(const QList< QPlaceSearchResult > &results)
Sets the list of search results.
\inmodule QtLocation
QGeoShape searchArea() const
Returns the search area which will be used to limit search results.
QList< QPlaceCategory > categories() const
Return the categories to be used in the search request.
\inmodule QtLocation
Definition qplace.h:25
void setVisibility(QLocation::Visibility visibility)
Sets the visibility of the place to visibility.
Definition qplace.cpp:528
void setDetailsFetched(bool fetched)
Sets whether the details of this place have been fetched or not.
Definition qplace.cpp:418
void setIcon(const QPlaceIcon &icon)
Sets the icon of the place.
Definition qplace.cpp:347
void setContactDetails(const QString &contactType, QList< QPlaceContactDetail > details)
Sets the contact details of a specified contactType.
Definition qplace.cpp:495
void setPlaceId(const QString &identifier)
Sets the identifier of the place.
Definition qplace.cpp:314
void setAttribution(const QString &attribution)
Sets the attribution string of the place.
Definition qplace.cpp:331
void setLocation(const QGeoLocation &location)
Sets the location of the place.
Definition qplace.cpp:197
void setCategories(const QList< QPlaceCategory > &categories)
Sets the categories that this place belongs to.
Definition qplace.cpp:181
void setName(const QString &name)
Sets the name of the place.
Definition qplace.cpp:296
QString name() const
Returns the name of the place.
Definition qplace.cpp:288
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition qurl.cpp:3368
const QLoggingCategory & category()
[1]
@ PublicVisibility
Definition qlocation.h:21
Combined button and popup list for selecting options.
QPlaceResult parsePlaceResult(const QJsonObject &response, const QString &attribution)
@ SkipEmptyParts
Definition qnamespace.h:128
emscripten::val document()
Definition qwasmdom.h:49
static const QCssKnownValue properties[NumProperties - 1]
DBusConnection const char DBusError * error
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLsizei GLenum * categories
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QStringLiteral(str)
#define tr(X)
#define emit
#define Q_UNUSED(x)
QNetworkRequest request(url)
QNetworkReply * reply
char * toString(const MyType &t)
[31]