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
qplacesearchsuggestionreplymapbox.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
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 <QtLocation/QPlaceResult>
14#include <QtLocation/QPlaceSearchRequest>
15
17
20{
21 Q_ASSERT(parent);
22 if (!reply) {
23 setError(UnknownError, QStringLiteral("Null reply"));
24 return;
25 }
26
27 connect(reply, &QNetworkReply::finished, this, &QPlaceSearchSuggestionReplyMapbox::onReplyFinished);
28 connect(reply, &QNetworkReply::errorOccurred, this, &QPlaceSearchSuggestionReplyMapbox::onNetworkError);
29
30 connect(this, &QPlaceReply::aborted, reply, &QNetworkReply::abort);
31 connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
32}
33
37
38void QPlaceSearchSuggestionReplyMapbox::setError(QPlaceReply::Error errorCode, const QString &errorString)
39{
40 QPlaceReply::setError(errorCode, errorString);
41 emit errorOccurred(errorCode, errorString);
42
43 setFinished(true);
44 emit finished();
45}
46
47void QPlaceSearchSuggestionReplyMapbox::onReplyFinished()
48{
49 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
50 reply->deleteLater();
51
52 if (reply->error() != QNetworkReply::NoError)
53 return;
54
55 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
56 if (!document.isObject()) {
57 setError(ParseError, tr("Response parse error"));
58 return;
59 }
60
61 const QJsonArray features = document.object().value(QStringLiteral("features")).toArray();
62
63 QStringList suggestions;
64 for (const QJsonValueConstRef feature : features) {
65 if (feature.isObject())
66 suggestions.append(feature.toObject().value(QStringLiteral("text")).toString());
67 }
68
69 setSuggestions(suggestions);
70
71 setFinished(true);
72 emit finished();
73}
74
75void QPlaceSearchSuggestionReplyMapbox::onNetworkError(QNetworkReply::NetworkError error)
76{
77 Q_UNUSED(error);
78 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
79 reply->deleteLater();
80 setError(CommunicationError, reply->errorString());
81}
82
83QT_END_NAMESPACE
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
QObject * parent
Definition qobject.h:73
Combined button and popup list for selecting options.
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)