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
qdeclarativesearchmodelbase.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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
7
8#include <QtCore/QCoreApplication>
9#include <QtQml/QQmlInfo>
10#include <QtLocation/QGeoServiceProvider>
11#include <QtLocation/QPlaceIcon>
12#include <QtLocation/QPlaceManager>
13#include <QtLocation/QPlaceSearchRequest>
14#include <QtLocation/QPlaceSearchReply>
15#include <QtPositioning/QGeoCircle>
16#include <QtPositioning/QGeoPolygon>
17#include <QtLocation/private/qplacesearchrequest_p.h>
18
20
21QDeclarativeSearchModelBase::QDeclarativeSearchModelBase(QObject *parent)
22 : QAbstractListModel(parent)
23{
24}
25
26QDeclarativeSearchModelBase::~QDeclarativeSearchModelBase()
27{
28}
29
30/*!
31 \internal
32*/
33QDeclarativeGeoServiceProvider *QDeclarativeSearchModelBase::plugin() const
34{
35 return m_plugin;
36}
37
38/*!
39 \internal
40*/
41void QDeclarativeSearchModelBase::setPlugin(QDeclarativeGeoServiceProvider *plugin)
42{
43 if (m_plugin == plugin)
44 return;
45
46 initializePlugin(plugin);
47
48 if (m_complete)
49 emit pluginChanged();
50}
51
52/*!
53 \internal
54*/
55QVariant QDeclarativeSearchModelBase::searchArea() const
56{
57 QGeoShape s = m_request.searchArea();
58 if (s.type() == QGeoShape::RectangleType)
59 return QVariant::fromValue(QGeoRectangle(s));
60 else if (s.type() == QGeoShape::CircleType)
61 return QVariant::fromValue(QGeoCircle(s));
62 else if (s.type() == QGeoShape::PolygonType)
63 return QVariant::fromValue(QGeoPolygon(s));
64 else
65 return QVariant::fromValue(s);
66}
67
68/*!
69 \internal
70*/
71void QDeclarativeSearchModelBase::setSearchArea(const QVariant &searchArea)
72{
73 QGeoShape s;
74 QGeoRoute route;
75 bool routeSearchArea = false;
76 if (searchArea.userType() == qMetaTypeId<QGeoRectangle>()) {
77 s = searchArea.value<QGeoRectangle>();
78 } else if (searchArea.userType() == qMetaTypeId<QGeoCircle>()) {
79 s = searchArea.value<QGeoCircle>();
80 } else if (searchArea.userType() == qMetaTypeId<QGeoShape>()) {
81 s = searchArea.value<QGeoShape>();
82 } else if (searchArea.typeId() == qMetaTypeId<QGeoRoute>()) {
83 route = searchArea.value<QGeoRoute>();
84 if (route == QGeoRoute())
85 return;
86 routeSearchArea = true;
87 }
88
89 QPlaceSearchRequestPrivate *rp = QPlaceSearchRequestPrivate::get(m_request);
90 // Invalidating the other thing
91 if (routeSearchArea)
92 m_request.setSearchArea(QGeoShape());
93 else
94 rp->routeSearchArea = QGeoRoute();
95
96 if (m_request.searchArea() == s
97 && (route == QGeoRoute() || rp->routeSearchArea == route)) {
98 return;
99 }
100
101 if (routeSearchArea)
102 rp->routeSearchArea = route;
103 else
104 m_request.setSearchArea(s);
105 emit searchAreaChanged();
106}
107
108/*!
109 \internal
110*/
111int QDeclarativeSearchModelBase::limit() const
112{
113 return m_request.limit();
114}
115
116/*!
117 \internal
118*/
119void QDeclarativeSearchModelBase::setLimit(int limit)
120{
121 if (m_request.limit() == limit)
122 return;
123
124 m_request.setLimit(limit);
125 emit limitChanged();
126}
127
128/*!
129 \internal
130*/
131bool QDeclarativeSearchModelBase::previousPagesAvailable() const
132{
133 return m_previousPageRequest != QPlaceSearchRequest();
134}
135
136/*!
137 \internal
138*/
139bool QDeclarativeSearchModelBase::nextPagesAvailable() const
140{
141 return m_nextPageRequest != QPlaceSearchRequest();
142}
143
144/*!
145 \internal
146*/
147QDeclarativeSearchModelBase::Status QDeclarativeSearchModelBase::status() const
148{
149 return m_status;
150}
151
152/*!
153 \internal
154*/
155void QDeclarativeSearchModelBase::setStatus(Status status, const QString &errorString)
156{
157 Status prevStatus = m_status;
158
159 m_status = status;
160 m_errorString = errorString;
161
162 if (prevStatus != m_status)
163 emit statusChanged();
164}
165
166/*!
167 \internal
168*/
169void QDeclarativeSearchModelBase::update()
170{
171 if (m_reply)
172 return;
173
174 setStatus(Loading);
175
176 if (!m_plugin) {
177 clearData();
178 setStatus(Error, QCoreApplication::translate(CONTEXT_NAME, PLUGIN_PROPERTY_NOT_SET));
179 return;
180 }
181
182 QGeoServiceProvider *serviceProvider = m_plugin->sharedGeoServiceProvider();
183 if (!serviceProvider) {
184 clearData();
185 setStatus(Error, QCoreApplication::translate(CONTEXT_NAME, PLUGIN_PROVIDER_ERROR)
186 .arg(m_plugin->name()));
187 return;
188 }
189
190 QPlaceManager *placeManager = serviceProvider->placeManager();
191 if (!placeManager) {
192 clearData();
193 setStatus(Error, QCoreApplication::translate(CONTEXT_NAME, PLUGIN_ERROR)
194 .arg(m_plugin->name()).arg(serviceProvider->errorString()));
195 return;
196 }
197
198 m_reply = sendQuery(placeManager, m_request);
199 if (!m_reply) {
200 clearData();
201 setStatus(Error, QCoreApplication::translate(CONTEXT_NAME, UNABLE_TO_MAKE_REQUEST));
202 return;
203 }
204
205 m_reply->setParent(this);
206 connect(m_reply, &QPlaceReply::finished,
207 this, &QDeclarativeSearchModelBase::queryFinished);
208 connect(m_reply, &QPlaceReply::contentUpdated,
209 this, &QDeclarativeSearchModelBase::onContentUpdated);
210}
211
212/*!
213 \internal
214*/
215void QDeclarativeSearchModelBase::cancel()
216{
217 if (!m_reply)
218 return;
219
220 if (!m_reply->isFinished())
221 m_reply->abort();
222
223 if (m_reply) {
224 m_reply->deleteLater();
225 m_reply = nullptr;
226 }
227
228 setStatus(Ready);
229}
230
231/*!
232 \internal
233*/
234void QDeclarativeSearchModelBase::reset()
235{
236 beginResetModel();
237 clearData();
238 setStatus(Null);
239 endResetModel();
240}
241
242/*!
243 \internal
244*/
245QString QDeclarativeSearchModelBase::errorString() const
246{
247 return m_errorString;
248}
249
250/*!
251 \internal
252*/
253void QDeclarativeSearchModelBase::previousPage()
254{
255 if (m_previousPageRequest == QPlaceSearchRequest())
256 return;
257
258 m_request = m_previousPageRequest;
259 update();
260}
261
262/*!
263 \internal
264*/
265void QDeclarativeSearchModelBase::nextPage()
266{
267 if (m_nextPageRequest == QPlaceSearchRequest())
268 return;
269
270 m_request = m_nextPageRequest;
271 update();
272}
273
274/*!
275 \internal
276*/
277void QDeclarativeSearchModelBase::clearData(bool suppressSignal)
278{
279 Q_UNUSED(suppressSignal);
280}
281
282/*!
283 \internal
284*/
285void QDeclarativeSearchModelBase::classBegin()
286{
287}
288
289/*!
290 \internal
291*/
292void QDeclarativeSearchModelBase::componentComplete()
293{
294 m_complete = true;
295}
296
297/*!
298 \internal
299*/
300void QDeclarativeSearchModelBase::initializePlugin(QDeclarativeGeoServiceProvider *plugin)
301{
302 beginResetModel();
303 if (plugin != m_plugin) {
304 if (m_plugin) {
305 disconnect(m_plugin, &QDeclarativeGeoServiceProvider::nameChanged,
306 this, &QDeclarativeSearchModelBase::pluginNameChanged);
307 }
308 if (plugin) {
309 connect(plugin, &QDeclarativeGeoServiceProvider::nameChanged,
310 this, &QDeclarativeSearchModelBase::pluginNameChanged);
311 }
312 m_plugin = plugin;
313 }
314
315 if (m_plugin) {
316 QGeoServiceProvider *serviceProvider = m_plugin->sharedGeoServiceProvider();
317 if (serviceProvider) {
318 QPlaceManager *placeManager = serviceProvider->placeManager();
319 if (placeManager) {
320 if (placeManager->childCategoryIds().isEmpty()) {
321 QPlaceReply *reply = placeManager->initializeCategories();
322 connect(reply, &QPlaceReply::finished, reply, &QObject::deleteLater);
323 }
324 }
325 }
326 }
327
328 endResetModel();
329}
330
331void QDeclarativeSearchModelBase::onContentUpdated()
332{
333
334}
335
336/*!
337 \internal
338*/
339void QDeclarativeSearchModelBase::pluginNameChanged()
340{
341 initializePlugin(m_plugin);
342}
343
344/*!
345 \internal
346*/
347void QDeclarativeSearchModelBase::setPreviousPageRequest(const QPlaceSearchRequest &previous)
348{
349 if (m_previousPageRequest == previous)
350 return;
351
352 m_previousPageRequest = previous;
353 emit previousPagesAvailableChanged();
354}
355
356void QDeclarativeSearchModelBase::setNextPageRequest(const QPlaceSearchRequest &next)
357{
358 if (m_nextPageRequest == next)
359 return;
360
361 m_nextPageRequest = next;
362 emit nextPagesAvailableChanged();
363}
364
365QT_END_NAMESPACE