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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** 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 "qdeclarativegeocodemodel_p.h"
#include "qdeclarativegeolocation_p.h"
#include <QtDeclarative/qdeclarativeinfo.h>
#include <qgeoserviceprovider.h>
#include <qgeocodingmanager.h>
QT_BEGIN_NAMESPACE
/*!
\qmlclass GeocodeModel QDeclarativeGeocodeModel
\inqmlmodule QtLocation 5
\ingroup qml-QtLocation5-geocoding
\since QtLocation 5.0
\brief The GeocodeModel element provides support for searching operations
related to geographic information.
The GeocodeModel element is used as part of a model/view grouping to
match addresses or search strings with geographic locations. How the
geographic locations generated are used or displayed is decided by any
Views attached to the GeocodeModel (eg. a \l MapItemView or \l{ListView}).
Like \l Map and \l RouteModel, all the data for a GeocodeModel to work
comes from a services plugin. This is contained in the \l{plugin} property,
and this must be set before the GeocodeModel can do any useful work.
Once the plugin is set, the \l{query} property can be used to specify the
address or search string to match. If \l{autoUpdate} is enabled, the Model
will update its output automatically. Otherwise, the \l{update} method may
be used. By default, autoUpdate is disabled.
The data stored and returned in the GeocodeModel consists of \l{Location}
elements, as a list with the role name "locationData". See the documentation
for \l{Location} for further details on its structure and contents.
\section2 Example Usage
The following snippet is two-part, showing firstly the declaration of
elements, and secondly a short piece of procedural code using it. We set
the geocodeModel's \l{autoUpdate} property to false, and call \l{update} once
the query is set up. In this case, as we use a string value in \l{query},
only one update would occur, even with autoUpdate enabled. However, if we
provided an \l{Address} object we may inadvertently trigger multiple
requests whilst setting its properties.
\code
Plugin {
id: aPlugin
}
GeocodeModel {
id: geocodeModel
plugin: aPlugin
autoUpdate: false
}
\endcode
\code
{
geocodeModel.query = "53 Brandl St, Eight Mile Plains, Australia"
geocodeModel.update()
}
\endcode
*/
QDeclarativeGeocodeModel::QDeclarativeGeocodeModel(QObject* parent)
: QAbstractListModel(parent),
autoUpdate_(false),
complete_(false),
reply_(0),
plugin_(0),
boundingArea_(0),
status_(QDeclarativeGeocodeModel::Null),
error_(QDeclarativeGeocodeModel::NoError),
coordinate_(0),
address_(0),
limit_(-1),
offset_(0)
{
QHash<int, QByteArray> roleNames;
roleNames = QAbstractItemModel::roleNames();
roleNames.insert(LocationRole, "locationData");
setRoleNames(roleNames);
}
QDeclarativeGeocodeModel::~QDeclarativeGeocodeModel()
{
qDeleteAll(declarativeLocations_);
declarativeLocations_.clear();
delete reply_;
}
// From QDeclarativeParserStatus
void QDeclarativeGeocodeModel::componentComplete()
{
complete_ = true;
if (autoUpdate_)
update();
}
QGeoBoundingArea* QDeclarativeGeocodeModel::boundingArea()
{
if (qobject_cast<QDeclarativeGeoBoundingBox*>(boundingArea_) && boundingBox_.isValid()) {
return &boundingBox_;
} else if (qobject_cast<QDeclarativeGeoBoundingCircle*>(boundingArea_) && boundingCircle_.isValid()) {
return &boundingCircle_;
}
return 0;
}
/*!
\qmlmethod QtLocation5::GeocodeModel::update()
Instructs the GeocodeModel to update its data. This is most useful
when \l autoUpdate is disabled, to force a refresh when the query
has been changed.
*/
void QDeclarativeGeocodeModel::update()
{
if (!complete_)
return;
if (!plugin_) {
qmlInfo(this) << tr("Cannot geocode, plugin not set.");
return;
}
QGeoServiceProvider *serviceProvider = plugin_->sharedGeoServiceProvider();
if (!serviceProvider)
return;
QGeocodingManager *geocodingManager = serviceProvider->geocodingManager();
if (!geocodingManager) {
qmlInfo(this) << tr("Cannot geocode, search manager (/plugin) not set.");
return;
}
if ((!coordinate_ || !coordinate_->coordinate().isValid()) &&
(!address_ || address_->address().isEmpty()) &&
(searchString_.isEmpty())) {
qmlInfo(this) << tr("Cannot geocode, valid query not set.");
return;
}
abortRequest(); // abort possible previous requests
setErrorString(""); // clear previous error string
setError(NoError);
if (coordinate_) {
setStatus(QDeclarativeGeocodeModel::Loading);
reply_ = geocodingManager->reverseGeocode(coordinate_->coordinate(), boundingArea());
if (reply_->isFinished()) {
if (reply_->error() == QGeocodeReply::NoError) {
geocodeFinished(reply_);
} else {
geocodeError(reply_, reply_->error(), reply_->errorString());
}
}
} else if (address_) {
setStatus(QDeclarativeGeocodeModel::Loading);
reply_ = geocodingManager->geocode(address_->address(), boundingArea());
if (reply_->isFinished()) {
if (reply_->error() == QGeocodeReply::NoError) {
geocodeFinished(reply_);
} else {
geocodeError(reply_, reply_->error(), reply_->errorString());
}
}
} else if (!searchString_.isEmpty()) {
setStatus(QDeclarativeGeocodeModel::Loading);
reply_ = geocodingManager->geocode(searchString_, limit_, offset_, boundingArea());
if (reply_->isFinished()) {
if (reply_->error() == QGeocodeReply::NoError) {
geocodeFinished(reply_);
} else {
geocodeError(reply_, reply_->error(), reply_->errorString());
}
}
}
}
void QDeclarativeGeocodeModel::abortRequest()
{
if (reply_) {
reply_->abort();
reply_->deleteLater();
reply_ = 0;
}
}
void QDeclarativeGeocodeModel::queryContentChanged()
{
if (autoUpdate_)
update();
}
// From QAbstractListModel
int QDeclarativeGeocodeModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return declarativeLocations_.count();
}
QVariant QDeclarativeGeocodeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= declarativeLocations_.count())
return QVariant();
if (role == QDeclarativeGeocodeModel::LocationRole) {
QObject* locationObject = declarativeLocations_.at(index.row());
Q_ASSERT(locationObject);
return QVariant::fromValue(locationObject);
}
return QVariant();
}
void QDeclarativeGeocodeModel::setPlugin(QDeclarativeGeoServiceProvider *plugin)
{
if (plugin_ == plugin)
return;
reset(); // reset the model
plugin_ = plugin;
if (complete_)
emit pluginChanged();
if (!plugin)
return;
if (plugin_->ready()) {
pluginReady();
} else {
connect(plugin_, SIGNAL(supportedFeaturesChanged(PluginFeatures)),
this, SLOT(pluginReady()));
}
}
void QDeclarativeGeocodeModel::pluginReady()
{
QGeoServiceProvider *serviceProvider = plugin_->sharedGeoServiceProvider();
QGeocodingManager *geocodingManager = serviceProvider->geocodingManager();
if (!geocodingManager || serviceProvider->error() != QGeoServiceProvider::NoError) {
qmlInfo(this) << tr("Warning: Plugin does not support (reverse) geocoding.");
return;
}
connect(geocodingManager, SIGNAL(finished(QGeocodeReply*)),
this, SLOT(geocodeFinished(QGeocodeReply*)));
connect(geocodingManager, SIGNAL(error(QGeocodeReply*,QGeocodeReply::Error,QString)),
this, SLOT(geocodeError(QGeocodeReply*,QGeocodeReply::Error,QString)));
}
/*!
\qmlproperty Plugin QtLocation5::GeocodeModel::plugin
This property holds the plugin that providers the actual geocoding service.
Note that all plugins do not necessarily provide geocoding (could e.g. provide
only routing or maps).
\sa Plugin
*/
QDeclarativeGeoServiceProvider* QDeclarativeGeocodeModel::plugin() const
{
return plugin_;
}
void QDeclarativeGeocodeModel::setBounds(QObject* bounds)
{
if (boundingArea_ == bounds)
return;
if (qobject_cast<QDeclarativeGeoBoundingBox*>(bounds)) {
boundingBox_ = qobject_cast<QDeclarativeGeoBoundingBox*>(bounds)->box();
} else if (qobject_cast<QDeclarativeGeoBoundingCircle*>(bounds)) {
boundingCircle_ = qobject_cast<QDeclarativeGeoBoundingCircle*>(bounds)->circle();
} else {
qmlInfo(this) << tr("Unsupported bound type (Box and Circle supported)");
return;
}
boundingArea_ = bounds;
emit boundsChanged();
}
/*!
\qmlproperty bounding area QtLocation5::GeocodeModel::bounds
This property holds the bounding area used to limit the results to those
within the area. his is particularly useful if query is only partially filled out,
as the service will attempt to (reverse) geocode all matches for the specified data.
Accepted element types are \l BoundingBox and \l BoundingCircle.
*/
QObject* QDeclarativeGeocodeModel::bounds() const
{
return boundingArea_;
}
void QDeclarativeGeocodeModel::geocodeFinished(QGeocodeReply *reply)
{
if (reply->error() != QGeocodeReply::NoError) {
return;
}
int oldCount = declarativeLocations_.count();
setLocations(reply->locations());
setErrorString("");
setError(NoError);
setStatus(QDeclarativeGeocodeModel::Ready);
reply->deleteLater();
reply_ = 0;
emit locationsChanged();
if (oldCount != declarativeLocations_.count())
emit countChanged();
}
void QDeclarativeGeocodeModel::geocodeError(QGeocodeReply *reply,
QGeocodeReply::Error error,
const QString &errorString)
{
Q_UNUSED(error);
int oldCount = declarativeLocations_.count();
if (oldCount > 0) {
// Reset the model
setLocations(reply->locations());
emit locationsChanged();
emit countChanged();
}
setErrorString(errorString);
setError(static_cast<QDeclarativeGeocodeModel::GeocodeError>(error));
setStatus(QDeclarativeGeocodeModel::Error);
reply->deleteLater();
reply_ = 0;
}
/*!
\qmlproperty enumeration QtLocation5::GeocodeModel::status
This read-only property holds the current status of the model.
\list
\o GeocodeModel.Null - No geocode requests have been issued or \l reset has been called.
\o GeocodeModel.Ready - Geocode request(s) have finished successfully.
\o GeocodeModel.Loading - Geocode request has been issued but not yet finished
\o GeocodeModel.Error - Geocoding error has occurred, details are in \l error and \l errorString
\endlist
*/
QDeclarativeGeocodeModel::Status QDeclarativeGeocodeModel::status() const
{
return status_;
}
void QDeclarativeGeocodeModel::setStatus(QDeclarativeGeocodeModel::Status status)
{
if (status_ == status)
return;
status_ = status;
emit statusChanged();
}
/*!
\qmlproperty enumeration QtLocation5::GeocodeModel::error
This read-only property holds the latest error value of the geocoding request.
\list
\o GeocodeModel.NoError - No error has occurred
\o GeocodeModel.EngineNotSetError - The plugin/service provider used does not support (reverse) geocoding
\o GeocodeModel.CommunicationError - An error occurred while communicating with the service provider
\o GeocodeModel.ParseError - The response from the service provider was in an unrecognizable format
\o GeocodeModel.UnsupportedOptionError - The requested operation or one of the options for the operation are not supported by the service provider.
\o GeocodeModel.CombinationError - An error occurred while results where being combined from multiple sources
\o GeocodeModel.UnknownError - An error occurred which does not fit into any of the other categories
\endlist
*/
QDeclarativeGeocodeModel::GeocodeError QDeclarativeGeocodeModel::error() const
{
return error_;
}
void QDeclarativeGeocodeModel::setError(GeocodeError error)
{
if (error_ == error)
return;
error_ = error;
emit errorChanged();
}
/*!
\qmlproperty string QtLocation5::GeocodeModel::errorString
This read-only property holds the textual presentation of latest geocoding error.
If no error has occurred or the model has been reset, an empty string is returned.
An empty string may also be returned if an error occurred which has no associated
textual representation.
*/
QString QDeclarativeGeocodeModel::errorString() const
{
return errorString_;
}
void QDeclarativeGeocodeModel::setErrorString(const QString &error)
{
if (errorString_ == error)
return;
errorString_ = error;
emit errorStringChanged();
}
void QDeclarativeGeocodeModel::setLocations(const QList<QGeoLocation> &locations)
{
beginResetModel();
qDeleteAll(declarativeLocations_);
declarativeLocations_.clear();
for (int i = 0; i < locations.count(); ++i) {
QDeclarativeGeoLocation* location = new QDeclarativeGeoLocation(locations.at(i), this);
declarativeLocations_.append(location);
}
endResetModel();
}
/*!
\qmlproperty int QtLocation5::GeocodeModel::count
This property holds how many locations the model currently has
Amongst other uses, you can use this value when accessing locations
via the QtLocation5::GeocodeModel::get -method.
*/
int QDeclarativeGeocodeModel::count() const
{
return declarativeLocations_.count();
}
/*!
\qmlmethod QtLocation5::GeocodeModel::get(int)
Returns the Location at given index. Use \l count property to check the
amount of locations available. The locations are indexed from zero, so the accessible range
is 0...(count - 1).
If you access out of bounds, a zero (null object) is returned and a warning is issued.
*/
QDeclarativeGeoLocation* QDeclarativeGeocodeModel::get(int index)
{
if (index < 0 || index >= declarativeLocations_.count()) {
qmlInfo(this) << tr("Error, too big or small index in get(): ") << index;
return 0;
}
return declarativeLocations_.at(index);
}
/*!
\qmlproperty int QtLocation5::GeocodeModel::limit
This property holds the maximum number of results. The \l limit and \l offset values only
applicable with free string geocoding (i.e. they are not considered when using addresses
or coordinates in the search query).
If limit is -1 the entire result set will be returned, otherwise at most limit results will be returned.
The limit and \l offset results can be used together to implement paging.
*/
int QDeclarativeGeocodeModel::limit() const
{
return limit_;
}
void QDeclarativeGeocodeModel::setLimit(int limit)
{
if (limit == limit_)
return;
limit_ = limit;
if (autoUpdate_) {
update();
}
emit limitChanged();
}
/*!
\qmlproperty int QtLocation5::GeocodeModel::offset
This property tells not to return the first 'offset' number of the results. The \l limit
and \l offset values are only applicable with free string geocoding (i.e. they are not considered
when using addresses or coordinates in the search query).
The \l limit and offset results can be used together to implement paging.
*/
int QDeclarativeGeocodeModel::offset() const
{
return offset_;
}
void QDeclarativeGeocodeModel::setOffset(int offset)
{
if (offset == offset_)
return;
offset_ = offset;
if (autoUpdate_) {
update();
}
emit offsetChanged();
}
/*!
\qmlmethod QtLocation5::GeocodeModel::clear()
Clears the location data of the model. Any outstanding requests or
errors remain intact.
*/
void QDeclarativeGeocodeModel::clear()
{
bool hasChanged = !declarativeLocations_.isEmpty();
setLocations(QList<QGeoLocation>());
if (hasChanged)
emit countChanged();
}
/*!
\qmlmethod QtLocation5::GeocodeModel::reset()
Resets the model. All location data is cleared, any outstanding requests
are aborted and possible errors are cleared. Model status will be set
to GeocodeModel.Null
*/
void QDeclarativeGeocodeModel::reset()
{
clear();
abortRequest();
setErrorString("");
setError(NoError);
setStatus(QDeclarativeGeocodeModel::Null);
}
/*!
\qmlproperty QVariant QtLocation5::GeocodeModel::query
This property holds the data of the geocoding request.
The property accepts three types of queries, which determines both the data and
the type of action to be performed:
\list
\o Address - Geocoding (address to coordinate)
\o Coordinate - Reverse geocoding (coordinate to address)
\o string - Geocoding (address to coordinate)
\endlist
*/
QVariant QDeclarativeGeocodeModel::query() const
{
return queryVariant_;
}
void QDeclarativeGeocodeModel::setQuery(const QVariant& query)
{
if (query == queryVariant_)
return;
QObject* object = qvariant_cast<QObject*>(query);
if (qobject_cast<QDeclarativeCoordinate*>(object)) {
if (coordinate_)
coordinate_->disconnect(this);
if (address_)
address_->disconnect(this);
coordinate_ = qobject_cast<QDeclarativeCoordinate*>(object);
connect(coordinate_, SIGNAL(latitudeChanged(double)), this, SLOT(queryContentChanged()));
connect(coordinate_, SIGNAL(longitudeChanged(double)), this, SLOT(queryContentChanged()));
connect(coordinate_, SIGNAL(altitudeChanged(double)), this, SLOT(queryContentChanged()));
address_ = 0;
searchString_.clear();
} else if (qobject_cast<QDeclarativeGeoAddress*>(object)) {
if (address_)
address_->disconnect(this);
if (coordinate_)
coordinate_->disconnect(this);
address_ = qobject_cast<QDeclarativeGeoAddress*>(object);
connect(address_, SIGNAL(countryChanged()), this, SLOT(queryContentChanged()));
connect(address_, SIGNAL(countryCodeChanged()), this, SLOT(queryContentChanged()));
connect(address_, SIGNAL(stateChanged()), this, SLOT(queryContentChanged()));
connect(address_, SIGNAL(countyChanged()), this, SLOT(queryContentChanged()));
connect(address_, SIGNAL(cityChanged()), this, SLOT(queryContentChanged()));
connect(address_, SIGNAL(districtChanged()), this, SLOT(queryContentChanged()));
connect(address_, SIGNAL(streetChanged()), this, SLOT(queryContentChanged()));
connect(address_, SIGNAL(postalCodeChanged()), this, SLOT(queryContentChanged()));
coordinate_ = 0;
searchString_.clear();
} else if (query.type() == QVariant::String) {
searchString_ = query.toString();
if (address_)
address_->disconnect(this);
if (coordinate_)
coordinate_->disconnect(this);
address_ = 0;
coordinate_ = 0;
} else {
qmlInfo(this) << tr("Unsupported query type for geocode model (Coordinate, string and Address supported).");
return;
}
queryVariant_ = query;
emit queryChanged();
if (autoUpdate_)
update();
}
/*!
\qmlproperty bool QtLocation5::GeocodeModel::autoUpdate
This property controls whether the Model automatically updates in response
to changes in its attached query. The default value of this property
is false.
If setting this value to 'true' and using an Address or Coordinate element
as the query, note that any change at all in the element's properties will
trigger a new request to be sent. If you are adjusting many properties of
the element whilst autoUpdate is enabled, this can generate large numbers
of useless (and later discarded) requests.
*/
bool QDeclarativeGeocodeModel::autoUpdate() const
{
return autoUpdate_;
}
void QDeclarativeGeocodeModel::setAutoUpdate(bool update)
{
if (autoUpdate_ == update)
return;
autoUpdate_ = update;
emit autoUpdateChanged();
}
#include "moc_qdeclarativegeocodemodel_p.cpp"
QT_END_NAMESPACE
|