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
qplacesearchreplyosm.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Aaron McCarthy <mccarthy.aaron@gmail.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#include <QtCore/QJsonDocument>
8#include <QtCore/QJsonArray>
9#include <QtCore/QJsonObject>
10#include <QtNetwork/QNetworkReply>
11#include <QtPositioning/QGeoCircle>
12#include <QtPositioning/QGeoRectangle>
13#include <QtPositioning/QGeoLocation>
14#include <QtPositioning/QGeoAddress>
15#include <QtLocation/QPlace>
16#include <QtLocation/QPlaceAttribute>
17#include <QtLocation/QPlaceIcon>
18#include <QtLocation/QPlaceResult>
19#include <QtLocation/QPlaceCategory>
20#include <QtLocation/QPlaceSearchRequest>
21#include <QtLocation/private/qplacesearchrequest_p.h>
22
24
27: QPlaceSearchReply(parent)
28{
30 if (!reply) {
31 setError(UnknownError, QStringLiteral("Null reply"));
32 return;
33 }
35
37 this, &QPlaceSearchReplyOsm::replyFinished);
39 this, &QPlaceSearchReplyOsm::networkError);
42}
43
47
48void QPlaceSearchReplyOsm::setError(QPlaceReply::Error errorCode, const QString &errorString)
49{
51 emit errorOccurred(errorCode, errorString);
52 setFinished(true);
53 emit finished();
54}
55
56static QGeoRectangle parseBoundingBox(const QJsonArray &coordinates)
57{
58 if (coordinates.count() != 4)
59 return QGeoRectangle();
60
61 double bottom = coordinates.at(0).toString().toDouble();
62 double top = coordinates.at(1).toString().toDouble();
63 double left = coordinates.at(2).toString().toDouble();
64 double right = coordinates.at(3).toString().toDouble();
65
67}
68
69void QPlaceSearchReplyOsm::replyFinished()
70{
71 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
73
75 return;
76
78 if (!document.isArray()) {
79 setError(ParseError, tr("Response parse error"));
80 return;
81 }
82
83 QJsonArray resultsArray = document.array();
84
85 QGeoCoordinate searchCenter = request().searchArea().center();
86
87 QStringList placeIds;
88
89 QList<QPlaceSearchResult> results;
90 for (int i = 0; i < resultsArray.count(); ++i) {
91 QJsonObject item = resultsArray.at(i).toObject();
92 QPlaceResult pr = parsePlaceResult(item);
93 pr.setDistance(searchCenter.distanceTo(pr.place().location().coordinate()));
94 placeIds.append(pr.place().placeId());
95 results.append(pr);
96 }
97
98 QVariantMap searchContext = request().searchContext().toMap();
99 QStringList excludePlaceIds =
100 searchContext.value(QStringLiteral("ExcludePlaceIds")).toStringList();
101
102 if (!excludePlaceIds.isEmpty()) {
104 QVariantMap parameters = searchContext;
105
106 QStringList epi = excludePlaceIds;
107 epi.removeLast();
108
109 parameters.insert(QStringLiteral("ExcludePlaceIds"), epi);
110 r.setSearchContext(parameters);
112 rpimpl->related = true;
113 rpimpl->page--;
115 }
116
117 if (!placeIds.isEmpty()) {
119 QVariantMap parameters = searchContext;
120
121 QStringList epi = excludePlaceIds;
122 epi.append(placeIds.join(QLatin1Char(',')));
123
124 parameters.insert(QStringLiteral("ExcludePlaceIds"), epi);
125 r.setSearchContext(parameters);
127 rpimpl->related = true;
128 rpimpl->page++;
130 }
131
133
134 setFinished(true);
135 emit finished();
136}
137
138void QPlaceSearchReplyOsm::networkError(QNetworkReply::NetworkError error)
139{
141 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
144}
145
146QPlaceResult QPlaceSearchReplyOsm::parsePlaceResult(const QJsonObject &item) const
147{
148 QPlace place;
149
150 QGeoCoordinate coordinate = QGeoCoordinate(item.value(QStringLiteral("lat")).toString().toDouble(),
151 item.value(QStringLiteral("lon")).toString().toDouble());
152
153 //const QString placeRank = item.value(QStringLiteral("place_rank")).toString();
154 const QString categoryName = item.value(QStringLiteral("category")).toString();
155 const QString type = item.value(QStringLiteral("type")).toString();
156 //double importance = item.value(QStringLiteral("importance")).toDouble();
157
158 place.setAttribution(item.value(QStringLiteral("licence")).toString());
159 place.setPlaceId(QString::number(item.value(QStringLiteral("place_id")).toInt()));
160
161 QVariantMap iconParameters;
162 iconParameters.insert(QPlaceIcon::SingleUrl,
163 QUrl(item.value(QStringLiteral("icon")).toString()));
165 icon.setParameters(iconParameters);
166 place.setIcon(icon);
167
168 QJsonObject addressDetails = item.value(QStringLiteral("address")).toObject();
169 const QString title = addressDetails.value(categoryName).toString();
170
171 place.setName(title);
172
173 if (!requestUrl.isEmpty()) {
175 attribute.setLabel("requestUrl");
176 attribute.setText(requestUrl);
177 place.setExtendedAttribute("requestUrl", attribute);
178 }
179
181 address.setCity(addressDetails.value(QStringLiteral("city")).toString());
182 address.setCountry(addressDetails.value(QStringLiteral("country")).toString());
183 // FIXME: country_code is alpha-2 setCountryCode takes alpha-3
184 //address.setCountryCode(addressDetails.value(QStringLiteral("country_code")).toString());
185 address.setPostalCode(addressDetails.value(QStringLiteral("postcode")).toString());
186 address.setStreet(addressDetails.value(QStringLiteral("road")).toString());
187 address.setStreetNumber(addressDetails.value(QStringLiteral("house_number")).toString());
188 address.setState(addressDetails.value(QStringLiteral("state")).toString());
189 address.setDistrict(addressDetails.value(QStringLiteral("suburb")).toString());
190
192 location.setCoordinate(coordinate);
193 location.setAddress(address);
194 location.setBoundingShape(parseBoundingBox(item.value(QStringLiteral("boundingbox")).toArray()));
195 place.setLocation(location);
196
198 category.setName(categoryName + "=" + type);
199 category.setCategoryId(categoryName + "=" + type);
200 place.setCategory(category);
201
204 result.setPlace(place);
205 result.setTitle(title);
206
207 return result;
208}
209
\inmodule QtPositioning
Definition qgeoaddress.h:18
void setCity(const QString &city)
Sets the city.
\inmodule QtPositioning
\inmodule QtPositioning
void setCoordinate(const QGeoCoordinate &position)
Sets the coordinate of the location.
\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
void append(parameter_type t)
Definition qlist.h:458
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:357
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
\inmodule 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
QPlaceSearchReplyOsm(const QPlaceSearchRequest &request, QNetworkReply *reply, QPlaceManagerEngineOsm *parent)
\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.
void setNextPageRequest(const QPlaceSearchRequest &next)
Sets the next page of search results request to next.
QList< QPlaceSearchResult > results() const
Returns a list of search results;.
void setPreviousPageRequest(const QPlaceSearchRequest &previous)
Sets the previous page of search results request to previous.
void setResults(const QList< QPlaceSearchResult > &results)
Sets the list of search results.
static const QPlaceSearchRequestPrivate * get(const QPlaceSearchRequest &request)
\inmodule QtLocation
QGeoShape searchArea() const
Returns the search area which will be used to limit search results.
QVariant searchContext() const
Returns backend specific additional search context associated with this place search request.
void setIcon(const QPlaceIcon &icon)
Sets the icon of the search result to icon.
\inmodule QtLocation
Definition qplace.h:25
void setIcon(const QPlaceIcon &icon)
Sets the icon of the place.
Definition qplace.cpp:347
void setCategory(const QPlaceCategory &category)
Sets a single category that this place belongs to.
Definition qplace.cpp:172
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 setExtendedAttribute(const QString &attributeType, const QPlaceAttribute &attribute)
Assigns an attribute of the given attributeType to a place.
Definition qplace.cpp:448
void setName(const QString &name)
Sets the name of the place.
Definition qplace.cpp:296
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
\inmodule QtCore
Definition qurl.h:94
QMap< QString, QVariant > toMap() const
Returns the variant as a QVariantMap if the variant has type() \l QMetaType::QVariantMap.
QStringList toStringList() const
Returns the variant as a QStringList if the variant has userType() \l QMetaType::QStringList,...
const QLoggingCategory & category()
[1]
Combined button and popup list for selecting options.
emscripten::val document()
Definition qwasmdom.h:49
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint attribute
GLint location
GLboolean r
[2]
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLint left
GLenum type
GLint GLint bottom
GLuint GLuint64EXT address
GLuint64EXT * result
[6]
static QGeoRectangle parseBoundingBox(const QJsonArray &coordinates)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QStringLiteral(str)
#define tr(X)
#define emit
#define Q_UNUSED(x)
static int toInt(const QChar &qc, int R)
static double toDouble(Value v)
QString title
[35]
QGraphicsItem * item
QNetworkRequest request(url)
QNetworkReply * reply
char * toString(const MyType &t)
[31]
\inmodule QtCore \reentrant
Definition qchar.h:18