1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdeclarativesearchsuggestionmodel_p.h"
#include "qdeclarativegeoserviceprovider_p.h"
#include <QtDeclarative/QDeclarativeInfo>
#include <QtLocation/QGeoServiceProvider>
#include <qplacemanager.h>
#include <qplacesearchrequest.h>
QT_USE_NAMESPACE
/*!
\qmlclass PlaceSearchSuggestionModel QDeclarativeSearchSuggestionModel
\inqmlmodule QtLocation 5
\ingroup qml-QtLocation5-places
\since QtLocation 5.0
\brief The PlaceSearchSuggestionModel element provides access to search term suggestions.
The PlaceSearchSuggestionModel can be used to provide search term suggestions as the user enters their
search term. The properties of this model should match that of the \l PlaceSearchModel, which
will be used to perform the actual search query, to ensure that the search suggestion results are
relevant.
There are two ways of accessing the data provided by this model, either through the
\l suggestions property or through views and delegates. The latter is the preferred
method.
The \l offset and \l limit properties can be used to access paged suggestions. When the
\l offset and \l limit properties are set the suggestions between \l offset and
(\l offset + \l limit - 1) will be returned. Support for paging may vary
from plugin to plugin.
The model returns data for the following roles:
\table
\header
\o Role
\o Type
\o Description
\row
\o suggestion
\o string
\o Suggested search term.
\endtable
The following example shows how to use the PlaceSearchSuggestionModel to get suggested search terms
from a partial search term. The \l searchArea is set to match what would be used to perform the
actual place search with \l PlaceSearchModel.
\snippet snippets/declarative/places.qml QtQuick import
\snippet snippets/declarative/places.qml QtLocation import
\codeline
\snippet snippets/declarative/places.qml SearchSuggestionModel
\sa PlaceSearchModel, {QPlaceManager}
*/
/*!
\qmlproperty Plugin PlaceSearchSuggestionModel::plugin
This property holds the provider \l Plugin which will be used to perform the search.
*/
/*!
\qmlproperty BoundingArea PlaceSearchSuggestionModel::searchArea
This property holds the search area. Search suggestion results returned by the model will be
relevant to the given search area.
If this property is set to a \l BoundingCircle its \l {BoundingCircle::radius}{radius} property
may be left unset, in which case the \l Plugin will choose an appropriate radius for the
search.
*/
/*!
\qmlproperty int PlaceSearchSuggestionModel::offset
This property holds the index of the first item in the model.
\sa limit
*/
/*!
\qmlproperty int PlaceSearchSuggestionModel::limit
This property holds the limit of the number of items that will be returned.
\sa offset
*/
/*!
\qmlproperty enum PlaceSearchSuggestionModel::status
This property holds the status of the model. It can be one of:
\table
\row
\o SearchSuggestionModel.Ready
\o The search query has completed and the result are available.
\row
\o SearchSuggestionModel.Executing
\o A search query is currently being executed.
\row
\o SearchSuggestionModel.Error
\o An error occurred when executing the previous search query.
\endtable
*/
/*!
\qmlmethod PlaceSearchSuggestionModel::execute()
Executes a search suggestion query for the partial \l searchTerm and \l searchArea. Once the
query completes the model items are updated with the suggestions.
\sa cancel(), status
*/
/*!
\qmlmethod PlaceSearchSuggestionModel::cancel()
Cancels an ongoing search suggestion operation.
\sa execute(), status
*/
QDeclarativeSearchSuggestionModel::QDeclarativeSearchSuggestionModel(QObject *parent)
: QDeclarativeSearchModelBase(parent)
{
QHash<int, QByteArray> roleNames;
roleNames = QAbstractItemModel::roleNames();
roleNames.insert(SearchSuggestionRole, "suggestion");
setRoleNames(roleNames);
}
QDeclarativeSearchSuggestionModel::~QDeclarativeSearchSuggestionModel()
{
}
/*!
\qmlproperty string PlaceSearchSuggestionModel::searchTerm
This property holds the partial search term used in query.
*/
QString QDeclarativeSearchSuggestionModel::searchTerm() const
{
return m_request.searchTerm();
}
void QDeclarativeSearchSuggestionModel::setSearchTerm(const QString &searchTerm)
{
if (m_request.searchTerm() == searchTerm)
return;
m_request.setSearchTerm(searchTerm);
emit searchTermChanged();
}
/*!
\qmlproperty QStringList PlaceSearchSuggestionModel::suggestions
This property holds the list of predicted search terms that the model currently has.
*/
QStringList QDeclarativeSearchSuggestionModel::suggestions() const
{
return m_suggestions;
}
void QDeclarativeSearchSuggestionModel::clearData()
{
m_suggestions.clear();
}
void QDeclarativeSearchSuggestionModel::updateSearchRequest()
{
QDeclarativeSearchModelBase::updateSearchRequest();
}
int QDeclarativeSearchSuggestionModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent)
return m_suggestions.count();
}
QVariant QDeclarativeSearchSuggestionModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= rowCount(index.parent()) || index.row() < 0)
return QVariant();
switch (role) {
case Qt::DisplayRole:
case SearchSuggestionRole:
return m_suggestions.at(index.row());
}
return QVariant();
}
void QDeclarativeSearchSuggestionModel::queryFinished()
{
if (!m_reply)
return;
QPlaceReply *reply = m_reply;
m_reply = 0;
beginResetModel();
clearData();
QPlaceSearchSuggestionReply *suggestionReply = qobject_cast<QPlaceSearchSuggestionReply *>(reply);
m_suggestions = suggestionReply->suggestions();
emit suggestionsChanged();
endResetModel();
if (suggestionReply->error() != QPlaceReply::NoError)
setStatus(Error, suggestionReply->errorString());
else
setStatus(Ready);
reply->deleteLater();
}
QPlaceReply *QDeclarativeSearchSuggestionModel::sendQuery(QPlaceManager *manager,
const QPlaceSearchRequest &request)
{
return manager->searchSuggestions(request);
}
|