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
requesthandler.h
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QDebug>
5#include <QGeoCircle>
6#include <QGeoServiceProvider>
7#include <QPlaceDetailsReply>
8#include <QPlaceIdReply>
9#include <QPlaceManager>
10#include <QPlaceSearchReply>
11#include <QPlaceResult>
12#include <QPlaceImage>
13#include <QtCore/QMetaObject>
14
15class RequestHandler : public QObject
16{
17public:
19 //! [Initialize Manager]
20 //The "provider name" is used to select a particular provider
21 QGeoServiceProvider *provider = new QGeoServiceProvider("provider name");
22 QPlaceManager *manager = provider->placeManager();
23 //! [Initialize Manager]
24 Q_UNUSED(provider);
25 Q_UNUSED(manager);
26 }
27
29 {
30 //! [Simple search]
31 //1) Make an appropriate request
32 QPlaceSearchRequest searchRequest;
33 searchRequest.setSearchTerm("ice cream");
34 searchRequest.setSearchArea(QGeoCircle(QGeoCoordinate(12.34, 56.78)));
35
36 //2) Use the manager to initiate a request and retrieve a reply object
37 QPlaceSearchReply * searchReply = manager->search(searchRequest);
38
39 //3) Connect the reply object to a slot which is invoked upon operation completion
40 connect(searchReply, &QPlaceSearchReply::finished,
41 this, &RequestHandler::processSearchReply);
42 //! [Simple search]
43 }
44
45 void search()
46 {
47 //! [Search for places cpp]
48
49 //instantiate request and set parameters
50 QPlaceSearchRequest searchRequest;
51 searchRequest.setSearchTerm("ice cream");
52 searchRequest.setSearchArea(QGeoCircle(QGeoCoordinate(12.34, 56.78)));
53
54 //send off a search request
55 /*QPlaceSearchReply * */ searchReply = manager->search(searchRequest);
56
57 //connect a slot to handle the reply
58 connect(searchReply, &QPlaceSearchReply::finished, this, &RequestHandler::handleSearchReply);
59
60 //! [Search for places cpp]
61 }
62
64 {
65 //! [Search paging]
66 QPlaceSearchRequest searchRequest;
67 searchRequest.setLimit(15); //specify how many results are to be retrieved.
68 //! [Search paging]
69 }
70
71 void details()
72 {
73 QPlace place;
74 //! [Details check]
75 if (!place.detailsFetched()) {
76 /*QPlaceDetailsReply * */ detailsReply = manager->getPlaceDetails(place.placeId());
77 connect(detailsReply, &QPlaceDetailsReply::finished, this, &RequestHandler::handleDetailsReply);
78 }
79 //! [Details check]
80 }
81
82 void images()
83 {
84 QPlace place;
85
86 //! [Image request]
87 QPlaceContentRequest request;
88 request.setContentType(QPlaceContent::ImageType);
89 request.setPlaceId(place.placeId());
90 request.setLimit(5);
91 /*QPlaceContentReply * */ contentReply = manager->getPlaceContent(request);
92 connect(contentReply, &QPlaceContentReply::finished, this, &RequestHandler::handleImagesReply);
93 //! [Image request]
94 }
95
96
98 {
99 //! [Suggestion request]
100 QPlaceSearchRequest request;
101 request.setSearchTerm("piz");
102 request.setSearchArea(QGeoCircle(QGeoCoordinate(12.34, 56.78)));
103 /* QPlaceSearchSuggestion * */suggestionReply = manager->searchSuggestions(request);
104 connect(suggestionReply, &QPlaceSearchSuggestion::finished, this, &RequestHandler::handleSuggestionReply);
105 //! [Suggestion request]
106 }
107
109 {
110 //! [Save place pt1]
111 QPlace place;
112 place.setName( "Fred's Ice Cream Parlor" );
113
114 QGeoLocation location;
115 location.setCoordinate(QGeoCoordinate(12.34, 56.78));
116
117 QGeoAddress address;
118 address.setStreet("111 Nother Street");
119 //! [Save place pt1]
120
121 //! [Save place pt2]
122 location.setAddress(address);
123 place.setLocation(location);
124
125 /* QPlaceIdReply * */savePlaceReply = manager->savePlace(place);
126 connect(savePlaceReply, &QPlaceIdReply::finished, this, &RequestHandler::handleSavePlaceReply);
127 //! [Save place pt2]
128 }
129
131 {
132 QPlace place;
133 //! [Remove place]
134 /* QPlaceIdReply * */removePlaceReply = manager->removePlace(place.placeId());
135 connect(removePlaceReply, &QPlaceIdReply::finished, this, &RequestHandler::handleRemovePlaceReply);
136 //! [Remove place]
137 }
138
140 {
141 //! [Initialize categories]
142 /* QPlaceReply * */initCatReply = manager->initializeCategories();
143 connect(initCatReply, &QPlaceReply::finished, this, &RequestHandler::handleInitCatReply);
144 //! [Initialize categories]
145 }
146
148 {
149 //! [Save category]
150 QPlaceCategory fastFood;
151
152 QPlaceCategory category;
153 category.setName("pizza");
154 /*QPlaceIdReply */ saveCategoryReply = manager->saveCategory(category);
155 connect(saveCategoryReply, &QPlaceIdReply::finished, this, &RequestHandler::handleSaveCategoryReply);
156
157 //we could have saved a category as a child by supplying a parent identifier.
158 saveCategoryReply = manager->saveCategory(category, fastFood.categoryId());
159 //! [Save category]
160 }
161
163 {
164 QPlaceCategory category;
165 //! [Remove category]
166 /* QPlaceIdReply * */removeCategoryReply = manager->removeCategory(place.placeId());
167 connect(removeCategoryReply, &QPlaceIdReply::finished, this, &RequestHandler::handleRemoveCategoryReply);
168 //! [Remove category]
169 }
170
172 QPlaceCategory diner;
173 QPlaceCategory restaurant;
174
175 //! [Search request]
176 QPlaceSearchRequest searchRequest;
177 searchRequest.setSearchTerm("Fast food"); //search term for what we are interested in
178
179 //set a search center
180 searchRequest.setSearchArea(QGeoCircle(QGeoCoordinate(2.3, 48.87)));
181
182 //set a distance hint as a relevancy hint.
183 //closer places have greater weighting in the ranking of results.
184 searchRequest.setRelevanceHint(QPlaceSearchRequest::DistanceHint);
185
186 //use limit to adjust pagination.
187 //this limits the number of place results to 5 per page.
188 searchRequest.setLimit(5);
189
190 //provide some categories to narrow down search
191 QList<QPlaceCategory> categories;
192 categories << diner << restaurant;
193 searchRequest.setCategories(categories);
194 //! [Search request]
195 }
196
197 void content() {
198 QPlace place;
199 //! [Content request]
200 QPlaceContentRequest request;
201 request.setContentType(QPlaceContent::ImageType);
202 request.setPlaceId(place.placeId());
203 request.setLimit(5);
204
205 QPlaceContentReply *contentReply = manager->getPlaceContent(request);
206 //..connect signals..//
207
208 //! [Content request]
209 Q_UNUSED(contentReply);
210 }
211
213 {
214 //! [Content conversion]
215 QPlaceImage image;
216 image.setUrl(QUrl("www.example.com"));
217
218 QPlaceContent content = image;
219
220 QPlaceImage image2;
221 image2 = content;
222 qDebug() << image2.url(); //image2.url() == "www.example.com"
223 //! [Content conversion]
224 }
225
226 void icon() {
227 QPlace place;
228 //! [icon]
229 QUrl iconSourceUrl = place.icon().url(QSize(32,32));
230
231 //A default icon may also be requested like so
232 iconSourceUrl = place.icon().url();
233 //! [icon]
234 }
235
237 QPlaceResult result;
238 QPlaceIdReply *saveReply;
239 //! [ Save to different manager]
240 //result retrieved from a different manager)
241 QPlace place = manager->compatiblePlace(result.place());
242 saveReply = manager->savePlace(place);
243 //! [ Save to different manager]
244 saveReply->abort();//needed to avoid warnings
245 }
246
247 void ratings() {
248 //! [Ratings]
249 qDebug() << QString("This place rated ") + place.ratings().average()
250 + "out of " + place.ratings().maximum() + "stars";
251 //! [Ratings]
252 }
253
254 void matchPlaces() {
255 QList<QPlaceSearchResult> results;
256 //! [Match places]
257 QPlaceMatchRequest request;
258 request.setResults(results);
259 QVariantMap parameters;
260 parameters.insert(QPlaceMatchRequest::AlternativeId, "x_id_here");
261 request.setParameters(parameters);
262 matchReply = manager->matchingPlaces(request);
263 //! [Match places]
264 }
265
266public slots:
267 // ![Simple search handler]
268 //4) Have the slot appropriately process the results of the operation
271 for (const QPlaceSearchResult &result : searchReply->results()) {
273 qDebug() << "Title:" << result.title();
274 }
275 }
276
277 //5) Discard the rely object when done.
279 searchReply = nullptr;
280 }
281 // ![Simple search handler]
282
283 //! [Search for places handler cpp]
286 for (const QPlaceSearchResult &result : searchReply->results()) {
289 qDebug() << "Name: " << placeResult.place().name();
290 qDebug() << "Coordinate " << placeResult.place().location().coordinate().toString();
291 qDebug() << "Street: " << placeResult.place().location().address().street();
292 qDebug() << "Distance: " << placeResult.distance();
293 }
294 }
295 }
296 searchReply->deleteLater(); //discard reply
297 searchReply = nullptr;
298 }
299 //! [Search for places handler cpp]
300
301 //! [Details handler cpp]
306
307 detailsReply->deleteLater(); //discard reply
308 detailsReply = nullptr;
309 }
310 //! [Details handler cpp]
311
312 //! [Image handler]
315 const auto content = contentReply->content();
316 for (auto iter = content.cbegin(), end = content.cend(); iter != end; ++iter) {
317 qDebug() << "Index: " << iter.key();
319 qDebug() << image.url();
320 qDebug() << image.mimeType();
321 }
322
323 //alternatively if indexes are irrelevant
324 for (const QPlaceImage &image : contentReply->content()) {
325 qDebug() << image.url();
326 qDebug() << image.mimeType();
327 }
328
329 //we can assign content to the place that it belongs to.
330 //the place object serves as a container where we can retrieve
331 //content that has already been fetched
334 }
335
337 contentReply = nullptr;
338 }
339 //! [Image handler]
340
341 //! [Suggestion handler]
345 qDebug() << suggestion;
346 }
347
348 suggestionReply->deleteLater(); //discard reply
349 suggestionReply = nullptr;
350 }
351
352 //! [Suggestion handler]
353
354 //! [Save place handler]
357 qDebug() << savePlaceReply->id();
358
359 savePlaceReply->deleteLater(); //discard reply
360 savePlaceReply = nullptr;
361 }
362 //! [Save place handler]
363
364 //! [Remove place handler]
367 qDebug() << "Removal of place identified by"
368 << removePlaceReply->id() << "was successful";
369
370 removePlaceReply->deleteLater(); //discard reply
371 removePlaceReply = nullptr;
372 }
373 //! [Remove place handler]
374
375 //! [Initialize categories reply]
378 qDebug() << "Categories initialized";
379 else
380 qDebug() << "Failed to initialize categories";
381
383 initCatReply = nullptr;
384 }
385 //! [Initialize categories reply]
386
387 void categories() {
389 //! [Top level categories]
392 qDebug() << category.name();
393 //! [Top level categories]
394
395 //! [Child categories]
397 //! [Child categories]
398 }
399
400 //! [Save category handler]
403 qDebug() << "Saved category id =" << saveCategoryReply->id();
404 }
405
407 saveCategoryReply = nullptr;
408 }
409 //! [Save category handler]
410
411 //! [Remove category handler]
414 qDebug() << "Removal of category identified by"
415 << removeCategoryReply->id() << "was successful";
416
417 removeCategoryReply->deleteLater(); //discard reply
418 removeCategoryReply = nullptr;
419 }
420 //! [Remove category handler]
421
422 //! [Content handler]
429 //! [Content handler]
430
432 //! [Phone numbers]
435 qDebug() << number.label() << ":" << number.value();
436 }
437 //! [Phone numbers]
438 }
439
440
442 //! [Opening hours]
445 //! [Opening hours]
446 }
447
448 //! [Match places handler]
450 if (matchReply->error() == QPlaceReply::NoError) {
451 const auto places = matchReply->places();
452 for (const QPlace &place : places) {
453 if (place != QPlace())
454 qDebug() << "Place is a favorite with name" << place.name();
455 else
456 qDebug() << "Place is not a favorite";
457 }
458 }
459
461 matchReply = nullptr;
462 }
463 //! [Match places handler]
464
467 //! [Convert search result]
470 qDebug() << placeResult.place().name();
473 }
474 //! [Convert search result]
475 }
476
489};
490
491class ManagerEngine : public QObject
492{
493};
494
495//! [Implement reply pt1]
497{
498public:
499 explicit SearchReply(ManagerEngine *engine)
500 : QPlaceSearchReply(engine), m_engine(engine){}
501
503 void setResults(const QList<QPlaceSearchResult> &results);
504 void setRequest(const QPlaceSearchRequest &request);
505//! [Implement reply pt1]
506
507//! [Implement reply pt2]
508 void triggerDone(QPlaceReply::Error error = QPlaceReply::NoError,
509 const QString &errorString = QString());
510
512};
513//! [Implement reply pt2]
514
516{
517public:
518 void triggerDone(QPlaceReply::Error error = QPlaceReply::NoError,
519 const QString &errorString = QString());
520
522
523};
524
525//! [Trigger done]
526void SearchSuggestionReply::triggerDone(QPlaceReply::Error error,
527 const QString &errorString)
528{
529 if (error != QPlaceReply::NoError) {
530 this->setError(error,errorString);
531 QMetaObject::invokeMethod(m_engine, "errorOccurred", Qt::QueuedConnection,
532 Q_ARG(QPlaceReply *,this),
533 Q_ARG(QPlaceReply::Error, error),
534 Q_ARG(QString, errorString));
535 QMetaObject::invokeMethod(this, "errorOccurred", Qt::QueuedConnection,
536 Q_ARG(QPlaceReply::Error, error),
537 Q_ARG(QString, errorString));
538 }
539
540 this->setFinished(true);
541 QMetaObject::invokeMethod(m_engine, "finished", Qt::QueuedConnection,
542 Q_ARG(QPlaceReply *,this));
543 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
544}
545//! [Trigger done]
int main(int argc, char *argv[])
[2]
Definition buffer.cpp:77
QPlaceSearchSuggestionReply * suggestionReply
QPlaceMatchReply * matchReply
QPlaceIdReply * removePlaceReply
QPlaceContentReply * contentReply
QPlaceManager * manager
void initializeManager()
QPlaceDetailsReply * detailsReply
void saveBetweenManagers()
QPlaceIdReply * saveCategoryReply
QPlaceIdReply * savePlaceReply
void initializeCategories()
QPlaceIdReply * removeCategoryReply
QPlaceReply * initCatReply
[Implement reply pt1]
ManagerEngine * m_engine
void triggerDone(QPlaceReply::Error error=QPlaceReply::NoError, const QString &errorString=QString())
[Implement reply pt1]
void setRequest(const QPlaceSearchRequest &request)
SearchReply(ManagerEngine *engine)
void setResults(const QList< QPlaceSearchResult > &results)
[Implement reply pt2]
ManagerEngine * m_engine
void triggerDone(QPlaceReply::Error error=QPlaceReply::NoError, const QString &errorString=QString())
[Trigger done]