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
qplacesearchreplyhere.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 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
5#include "jsonparserhelpers.h"
8
9#include <QCoreApplication>
10#include <QtCore/QJsonDocument>
11#include <QtCore/QJsonObject>
12#include <QtCore/QJsonArray>
13#include <QtPositioning/QGeoAddress>
14#include <QtPositioning/QGeoLocation>
15#include <QtPositioning/QGeoRectangle>
16#include <QtLocation/QPlace>
17#include <QtLocation/QPlaceAttribute>
18#include <QtLocation/QPlaceIcon>
19#include <QtLocation/QPlaceResult>
20#include <QtLocation/QPlaceRatings>
21#include <QtLocation/QPlaceProposedSearchResult>
22#include <QtLocation/private/qplacesearchrequest_p.h>
23
24#include <QtCore/QDebug>
25
27
31 : QPlaceSearchReply(parent), m_engine(parent)
32{
33 if (!reply) {
34 setError(UnknownError, QStringLiteral("Null reply"));
35 return;
36 }
38
40 this, &QPlaceSearchReplyHere::replyFinished);
42 this, &QPlaceSearchReplyHere::replyError);
45}
46
50
51void QPlaceSearchReplyHere::setError(QPlaceReply::Error error_, const QString &errorString)
52{
55 setFinished(true);
56 emit finished();
57}
58
59void QPlaceSearchReplyHere::replyFinished()
60{
61 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
63
65 return;
66
68 if (!document.isObject()) {
70 return;
71 }
72
73 QJsonObject resultsObject = document.object();
74
75 if (resultsObject.contains(QStringLiteral("results")))
76 resultsObject = resultsObject.value(QStringLiteral("results")).toObject();
77
78 QJsonArray items = resultsObject.value(QStringLiteral("items")).toArray();
79
80 QList<QPlaceSearchResult> results;
81 for (int i = 0; i < items.count(); ++i) {
82 QJsonObject item = items.at(i).toObject();
83
84 const QString type = item.value(QStringLiteral("type")).toString();
85 if (type == QStringLiteral("urn:nlp-types:place"))
86 results.append(parsePlaceResult(item));
87 else if (type == QStringLiteral("urn:nlp-types:search"))
88 results.append(parseSearchResult(item));
89 }
90
93
94 if (resultsObject.contains(QStringLiteral("next"))) {
96 request.setSearchContext(QUrl(resultsObject.value(QStringLiteral("next")).toString()));
98 rpimpl->related = true;
99 rpimpl->page = rpimpl_orig->page + 1;
101 }
102
103 if (resultsObject.contains(QStringLiteral("previous"))) {
105 request.setSearchContext(QUrl(resultsObject.value(QStringLiteral("previous")).toString()));
107 rpimpl->related = true;
108 rpimpl->page = rpimpl_orig->page - 1;
110 }
111
113
114 setFinished(true);
115 emit finished();
116}
117
118QPlaceResult QPlaceSearchReplyHere::parsePlaceResult(const QJsonObject &item) const
119{
121
122 if (item.contains(QStringLiteral("distance")))
123 result.setDistance(item.value(QStringLiteral("distance")).toDouble());
124
125 QPlace place;
126
128
129 location.setCoordinate(parseCoordinate(item.value(QStringLiteral("position")).toArray()));
130
131 const QString vicinity = item.value(QStringLiteral("vicinity")).toString();
133 address.setText(vicinity);
134 location.setAddress(address);
135
136 if (item.contains(QStringLiteral("bbox"))) {
137 QJsonArray bbox = item.value(QStringLiteral("bbox")).toArray();
138 QGeoRectangle box(QGeoCoordinate(bbox.at(3).toDouble(), bbox.at(0).toDouble()),
139 QGeoCoordinate(bbox.at(1).toDouble(), bbox.at(2).toDouble()));
140 location.setBoundingShape(box);
141 }
142
143 place.setLocation(location);
144
145 QPlaceRatings ratings;
146 ratings.setAverage(item.value(QStringLiteral("averageRating")).toDouble());
147 ratings.setMaximum(5.0);
148 place.setRatings(ratings);
149
150 const QString title = item.value(QStringLiteral("title")).toString();
151 place.setName(title);
152 result.setTitle(title);
153
154 QPlaceIcon icon = m_engine->icon(item.value(QStringLiteral("icon")).toString());
155 place.setIcon(icon);
156 result.setIcon(icon);
157
158 place.setCategory(parseCategory(item.value(QStringLiteral("category")).toObject(),
159 m_engine));
160
161 //QJsonArray having = item.value(QStringLiteral("having")).toArray();
162
163 result.setSponsored(item.value(QStringLiteral("sponsored")).toBool());
164
165 QUrl href = item.value(QStringLiteral("href")).toString();
166 //QUrl type = item.value(QStringLiteral("type")).toString();
167
168 place.setPlaceId(href.path().mid(18, 41));
169
170 QPlaceAttribute provider;
171 provider.setText(QStringLiteral("here"));
174
175 result.setPlace(place);
176
177 return result;
178}
179
180QPlaceProposedSearchResult QPlaceSearchReplyHere::parseSearchResult(const QJsonObject &item) const
181{
183
184 result.setTitle(item.value(QStringLiteral("title")).toString());
185
186 QPlaceIcon icon = m_engine->icon(item.value(QStringLiteral("icon")).toString());
187 result.setIcon(icon);
188
190 request.setSearchContext(QUrl(item.value("href").toString()));
191
192 result.setSearchRequest(request);
193
194 return result;
195}
196
197void QPlaceSearchReplyHere::replyError(QNetworkReply::NetworkError error)
198{
199 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
205 QString::fromLatin1("The id, %1, does not reference an existing place")
206 .arg(request().recommendationId()));
207 } else {
209 }
210}
211
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
\inmodule QtPositioning
Definition qgeoaddress.h:18
void setText(const QString &text)
If text is not empty, explicitly assigns text as the string to be returned by text().
\inmodule QtPositioning
\inmodule QtPositioning
void setCoordinate(const QGeoCoordinate &position)
Sets the coordinate of the location.
\inmodule QtPositioning
virtual bool contains(const QPointF &point) const
Returns true if this item contains point, which is in local coordinates; otherwise,...
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
\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
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
T value(qsizetype i) const
Definition qlist.h:664
qsizetype count() const noexcept
Definition qlist.h:398
void append(parameter_type t)
Definition qlist.h:458
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.
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
void setText(const QString &text)
static const QString Provider
\variable QPlaceAttribute::Provider The constant to specify an attribute that defines which provider ...
\inmodule QtLocation
Definition qplaceicon.h:23
QPlaceIcon icon(const QString &remotePath, const QList< QPlaceCategory > &categories=QList< QPlaceCategory >()) const
\inmodule QtLocation
void setMaximum(qreal max)
void setAverage(qreal average)
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
@ PlaceDoesNotExistError
Definition qplacereply.h:20
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 setDistance(qreal distance)
Set the distance of the search result's place from a search center.
QPlaceSearchReplyHere(const QPlaceSearchRequest &request, QNetworkReply *reply, QPlaceManagerEngineNokiaV2 *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
void setSearchContext(const QVariant &context)
Sets the search context to context.
void setTitle(const QString &title)
Sets the title of the search result to title.
\inmodule QtLocation
Definition qplace.h:25
void setVisibility(QLocation::Visibility visibility)
Sets the visibility of the place to visibility.
Definition qplace.cpp:528
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 setRatings(const QPlaceRatings &ratings)
Sets the aggregated rating of the place.
Definition qplace.cpp:213
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
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
\inmodule QtCore
Definition qurl.h:94
QString path(ComponentFormattingOptions options=FullyDecoded) const
Returns the path of the URL.
Definition qurl.cpp:2468
QT_BEGIN_NAMESPACE QGeoCoordinate parseCoordinate(const QJsonArray &coordinateArray)
QPlaceCategory parseCategory(const QJsonObject &categoryObject, const QPlaceManagerEngineNokiaV2 *engine)
@ PublicVisibility
Definition qlocation.h:21
Combined button and popup list for selecting options.
emscripten::val document()
Definition qwasmdom.h:49
DBusConnection const char DBusError * error
QT_BEGIN_NAMESPACE const char NOKIA_PLUGIN_CONTEXT_NAME[]
const char PARSE_ERROR[]
const char CANCEL_ERROR[]
const char NETWORK_ERROR[]
GLint location
GLenum type
GLsizei const GLint * box
GLuint GLuint64EXT address
GLuint64EXT * result
[6]
SSL_CTX int void * arg
#define QStringLiteral(str)
#define emit
static double toDouble(Value v)
QString title
[35]
QGraphicsItem * item
QList< QTreeWidgetItem * > items
QNetworkRequest request(url)
QNetworkReply * reply
char * toString(const MyType &t)
[31]
QT_BEGIN_NAMESPACE bool toBool(const QString &str)
Definition utils.h:14