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
qdeclarativesearchsuggestionmodel.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// Qt-Security score:significant reason:default
4
7
8#include <QtQml/QQmlInfo>
9#include <QtLocation/QGeoServiceProvider>
10
11#include <qplacemanager.h>
12#include <qplacesearchrequest.h>
13#include <qplacesearchsuggestionreply.h>
14
16
17/*!
18 \qmltype PlaceSearchSuggestionModel
19 \nativetype QDeclarativeSearchSuggestionModel
20 \inqmlmodule QtLocation
21 \ingroup qml-QtLocation5-places
22 \ingroup qml-QtLocation5-places-models
23 \since QtLocation 5.5
24
25 \brief Provides access to search term suggestions.
26
27 The PlaceSearchSuggestionModel can be used to provide search term suggestions as the user enters their
28 search term. The properties of this model should match that of the \l PlaceSearchModel, which
29 will be used to perform the actual search query, to ensure that the search suggestion results are
30 relevant.
31
32 There are two ways of accessing the data provided by this model, either through the
33 \l suggestions property or through views and delegates. The latter is the preferred
34 method.
35
36 The \l offset and \l limit properties can be used to access paged suggestions. When the
37 \l offset and \l limit properties are set the suggestions between \l offset and
38 (\l offset + \l limit - 1) will be returned. Support for paging may vary
39 from plugin to plugin.
40
41 The model returns data for the following roles:
42
43 \table
44 \header
45 \li Role
46 \li Type
47 \li Description
48 \row
49 \li suggestion
50 \li string
51 \li Suggested search term.
52 \endtable
53
54 The following example shows how to use the PlaceSearchSuggestionModel to get suggested search terms
55 from a partial search term. The \l searchArea is set to match what would be used to perform the
56 actual place search with \l PlaceSearchModel.
57
58 \snippet declarative/places.qml QtQuick import
59 \snippet declarative/maps.qml QtLocation import
60 \codeline
61 \snippet declarative/places.qml SearchSuggestionModel
62
63 \sa PlaceSearchModel, {QPlaceManager}
64*/
65
66/*!
67 \qmlproperty Plugin PlaceSearchSuggestionModel::plugin
68
69 This property holds the provider \l Plugin which will be used to perform the search.
70*/
71
72/*!
73 \qmlproperty geoShape PlaceSearchSuggestionModel::searchArea
74
75 This property holds the search area. Search suggestion results returned by the model will be
76 relevant to the given search area.
77
78 If this property is set to a \l {geoCircle} its
79 \l {geoCircle}{radius} property may be left unset, in which case the \l Plugin
80 will choose an appropriate radius for the search.
81*/
82
83/*!
84 \qmlproperty int PlaceSearchSuggestionModel::offset
85
86 This property holds the index of the first item in the model.
87
88 \sa limit
89*/
90
91/*!
92 \qmlproperty int PlaceSearchSuggestionModel::limit
93
94 This property holds the limit of the number of items that will be returned.
95
96 \sa offset
97*/
98
99/*!
100 \qmlproperty enum PlaceSearchSuggestionModel::status
101
102 This property holds the status of the model. It can be one of:
103
104 \table
105 \row
106 \li PlaceSearchSuggestionModel.Null
107 \li No search query has been executed. The model is empty.
108 \row
109 \li PlaceSearchSuggestionModel.Ready
110 \li The search query has completed, and the results are available.
111 \row
112 \li PlaceSearchSuggestionModel.Loading
113 \li A search query is currently being executed.
114 \row
115 \li PlaceSearchSuggestionModel.Error
116 \li An error occurred when executing the previous search query.
117 \endtable
118*/
119
120/*!
121 \qmlmethod void PlaceSearchSuggestionModel::update()
122
123 Updates the model based on the provided query parameters. The model will be populated with a
124 list of search suggestions for the partial \l searchTerm and \l searchArea. If the \l plugin
125 supports it, other parameters such as \l limit and \l offset may be specified. \c update()
126 submits the set of parameters to the \l plugin to process.
127
128
129 While the model is updating the \l status of the model is set to
130 \c PlaceSearchSuggestionModel.Loading. If the model is successfully updated, the \l status is
131 set to \c PlaceSearchSuggestionModel.Ready, while if it unsuccessfully completes, the \l status
132 is set to \c PlaceSearchSuggestionModel.Error and the model cleared.
133
134 This example shows use of the model
135 \code
136 PlaceSeachSuggestionModel {
137 id: model
138 plugin: backendPlugin
139 searchArea: QtPositioning.circle(QtPositioning.coordinate(10, 10))
140 ...
141 }
142
143 MouseArea {
144 ...
145 onClicked: {
146 model.searchTerm = "piz"
147 model.searchArea.center.latitude = -27.5;
148 model.searchArea.cetner.longitude = 153;
149 model.update();
150 }
151 }
152 \endcode
153
154 A more detailed example can be found in the in
155 \l {Places (QML)#Presenting-Search-Suggestions}{Places (QML)} example.
156
157 \sa cancel(), status
158*/
159
160/*!
161 \qmlmethod void PlaceSearchSuggestionModel::cancel()
162
163 Cancels an ongoing search suggestion operation immediately and sets the model
164 status to PlaceSearchSuggestionModel.Ready. The model retains any search
165 suggestions it had before the operation was started.
166
167 If an operation is not ongoing, invoking cancel() has no effect.
168
169 \sa update(), status
170*/
171
172/*!
173 \qmlmethod void PlaceSearchSuggestionModel::reset()
174
175 Resets the model. All search suggestions are cleared, any outstanding requests are aborted and
176 possible errors are cleared. Model status will be set to PlaceSearchSuggestionModel.Null.
177*/
178
179
180/*!
181 \qmlmethod string QtLocation::PlaceSearchSuggestionModel::errorString()
182
183 This read-only property holds the textual presentation of the latest search suggestion model error.
184 If no error has occurred, or if the model was cleared, an empty string is returned.
185
186 An empty string may also be returned if an error occurred which has no associated
187 textual representation.
188*/
189
190QDeclarativeSearchSuggestionModel::QDeclarativeSearchSuggestionModel(QObject *parent)
191: QDeclarativeSearchModelBase(parent)
192{
193}
194
195QDeclarativeSearchSuggestionModel::~QDeclarativeSearchSuggestionModel()
196{
197}
198
199/*!
200 \qmlproperty string PlaceSearchSuggestionModel::searchTerm
201
202 This property holds the partial search term used in query.
203*/
204QString QDeclarativeSearchSuggestionModel::searchTerm() const
205{
206 return m_request.searchTerm();
207}
208
209void QDeclarativeSearchSuggestionModel::setSearchTerm(const QString &searchTerm)
210{
211 if (m_request.searchTerm() == searchTerm)
212 return;
213
214 m_request.setSearchTerm(searchTerm);
215 emit searchTermChanged();
216}
217
218/*!
219 \qmlproperty stringlist PlaceSearchSuggestionModel::suggestions
220
221 This property holds the list of predicted search terms that the model currently has.
222*/
223QStringList QDeclarativeSearchSuggestionModel::suggestions() const
224{
225 return m_suggestions;
226}
227
228/*!
229 \internal
230*/
231void QDeclarativeSearchSuggestionModel::clearData(bool suppressSignal)
232{
233 QDeclarativeSearchModelBase::clearData(suppressSignal);
234
235 if (!m_suggestions.isEmpty()) {
236 m_suggestions.clear();
237
238 if (!suppressSignal)
239 emit suggestionsChanged();
240 }
241}
242
243/*!
244 \internal
245*/
246int QDeclarativeSearchSuggestionModel::rowCount(const QModelIndex &parent) const
247{
248 Q_UNUSED(parent);
249
250 return m_suggestions.count();
251}
252
253/*!
254 \internal
255*/
256QVariant QDeclarativeSearchSuggestionModel::data(const QModelIndex &index, int role) const
257{
258 if (!index.isValid())
259 return QVariant();
260
261 if (index.row() >= rowCount(index.parent()) || index.row() < 0)
262 return QVariant();
263
264 switch (role) {
265 case Qt::DisplayRole:
266 case SearchSuggestionRole:
267 return m_suggestions.at(index.row());
268 }
269
270 return QVariant();
271}
272
273QHash<int, QByteArray> QDeclarativeSearchSuggestionModel::roleNames() const
274{
275 QHash<int, QByteArray> roleNames = QDeclarativeSearchModelBase::roleNames();
276 roleNames.insert(SearchSuggestionRole, "suggestion");
277 return roleNames;
278}
279
280/*!
281 \internal
282*/
283void QDeclarativeSearchSuggestionModel::queryFinished()
284{
285 if (!m_reply)
286 return;
287
288 QPlaceReply *reply = m_reply;
289 m_reply = nullptr;
290
291 int initialCount = m_suggestions.count();
292 beginResetModel();
293
294 clearData(true);
295
296 QPlaceSearchSuggestionReply *suggestionReply = qobject_cast<QPlaceSearchSuggestionReply *>(reply);
297 m_suggestions = suggestionReply->suggestions();
298
299 if (initialCount != m_suggestions.count())
300 emit suggestionsChanged();
301
302 endResetModel();
303
304 if (suggestionReply->error() != QPlaceReply::NoError)
305 setStatus(Error, suggestionReply->errorString());
306 else
307 setStatus(Ready);
308
309
310 reply->deleteLater();
311}
312
313/*!
314 \internal
315*/
316QPlaceReply *QDeclarativeSearchSuggestionModel::sendQuery(QPlaceManager *manager,
317 const QPlaceSearchRequest &request)
318{
319 return manager->searchSuggestions(request);
320}
321
322QT_END_NAMESPACE
Combined button and popup list for selecting options.