114void QDeclarativeGeocodeModel::update()
120 setError(EngineNotSetError, tr(
"Cannot geocode, plugin not set."));
124 QGeoServiceProvider *serviceProvider = plugin_->sharedGeoServiceProvider();
125 if (!serviceProvider)
128 QGeoCodingManager *geocodingManager = serviceProvider->geocodingManager();
129 if (!geocodingManager) {
130 setError(EngineNotSetError, tr(
"Cannot geocode, geocode manager not set."));
133 if (!coordinate_.isValid() && (!address_ || address_->address().isEmpty()) &&
134 (searchString_.isEmpty())) {
135 setError(ParseError, tr(
"Cannot geocode, valid query not set."));
139 setError(NoError, QString());
141 if (coordinate_.isValid()) {
142 setStatus(QDeclarativeGeocodeModel::Loading);
143 reply_ = geocodingManager->reverseGeocode(coordinate_, boundingArea_);
144 if (reply_->isFinished()) {
145 if (reply_->error() == QGeoCodeReply::NoError) {
146 geocodeFinished(reply_);
148 geocodeError(reply_, reply_->error(), reply_->errorString());
151 }
else if (address_) {
152 setStatus(QDeclarativeGeocodeModel::Loading);
153 reply_ = geocodingManager->geocode(address_->address(), boundingArea_);
154 if (reply_->isFinished()) {
155 if (reply_->error() == QGeoCodeReply::NoError) {
156 geocodeFinished(reply_);
158 geocodeError(reply_, reply_->error(), reply_->errorString());
161 }
else if (!searchString_.isEmpty()) {
162 setStatus(QDeclarativeGeocodeModel::Loading);
163 reply_ = geocodingManager->geocode(searchString_, limit_, offset_, boundingArea_);
164 if (reply_->isFinished()) {
165 if (reply_->error() == QGeoCodeReply::NoError) {
166 geocodeFinished(reply_);
168 geocodeError(reply_, reply_->error(), reply_->errorString());
210QVariant QDeclarativeGeocodeModel::data(
const QModelIndex &index,
int role)
const
212 if (!index.isValid())
214 if (index.row() >= declarativeLocations_.count())
216 if (role == QDeclarativeGeocodeModel::LocationRole) {
217 QObject *locationObject = declarativeLocations_.at(index.row());
218 Q_ASSERT(locationObject);
219 return QVariant::fromValue(locationObject);
224QHash<
int, QByteArray> QDeclarativeGeocodeModel::roleNames()
const
226 QHash<
int, QByteArray> roleNames = QAbstractItemModel::roleNames();
227 roleNames.insert(LocationRole,
"locationData");
234void QDeclarativeGeocodeModel::setPlugin(QDeclarativeGeoServiceProvider *plugin)
236 if (plugin_ == plugin)
242 emit pluginChanged();
247 if (plugin_->isAttached()) {
250 connect(plugin_, &QDeclarativeGeoServiceProvider::attached,
251 this, &QDeclarativeGeocodeModel::pluginReady);
258void QDeclarativeGeocodeModel::pluginReady()
260 QGeoServiceProvider *serviceProvider = plugin_->sharedGeoServiceProvider();
261 QGeoCodingManager *geocodingManager = serviceProvider->geocodingManager();
263 if (serviceProvider->geocodingError() != QGeoServiceProvider::NoError) {
264 QDeclarativeGeocodeModel::GeocodeError newError = UnknownError;
265 switch (serviceProvider->geocodingError()) {
266 case QGeoServiceProvider::NotSupportedError:
267 newError = EngineNotSetError;
break;
268 case QGeoServiceProvider::UnknownParameterError:
269 newError = UnknownParameterError;
break;
270 case QGeoServiceProvider::MissingRequiredParameterError:
271 newError = MissingRequiredParameterError;
break;
272 case QGeoServiceProvider::ConnectionError:
273 newError = CommunicationError;
break;
278 setError(newError, serviceProvider->geocodingErrorString());
282 if (!geocodingManager) {
283 setError(EngineNotSetError,tr(
"Plugin does not support (reverse) geocoding."));
287 connect(geocodingManager, &QGeoCodingManager::finished,
288 this, &QDeclarativeGeocodeModel::geocodeFinished);
289 connect(geocodingManager, &QGeoCodingManager::errorOccurred,
290 this, &QDeclarativeGeocodeModel::geocodeError);
292 if (complete_ && autoUpdate_)
311void QDeclarativeGeocodeModel::setBounds(
const QVariant &boundingArea)
315 if (boundingArea.userType() == qMetaTypeId<QGeoRectangle>())
316 s = boundingArea.value<QGeoRectangle>();
317 else if (boundingArea.userType() == qMetaTypeId<QGeoCircle>())
318 s = boundingArea.value<QGeoCircle>();
319 else if (boundingArea.userType() == qMetaTypeId<QGeoShape>())
320 s = boundingArea.value<QGeoShape>();
323 if (boundingArea_ == s)
327 emit boundsChanged();
340QVariant QDeclarativeGeocodeModel::bounds()
const
342 if (boundingArea_.type() == QGeoShape::RectangleType)
343 return QVariant::fromValue(QGeoRectangle(boundingArea_));
344 else if (boundingArea_.type() == QGeoShape::CircleType)
345 return QVariant::fromValue(QGeoCircle(boundingArea_));
346 else if (boundingArea_.type() == QGeoShape::PolygonType)
347 return QVariant::fromValue(QGeoPolygon(boundingArea_));
349 return QVariant::fromValue(boundingArea_);
352void QDeclarativeGeocodeModel::geocodeFinished(QGeoCodeReply *reply)
354 if (reply != reply_ || reply->error() != QGeoCodeReply::NoError)
357 reply->deleteLater();
359 int oldCount = declarativeLocations_.count();
360 setLocations(reply->locations());
361 setError(NoError, QString());
362 setStatus(QDeclarativeGeocodeModel::Ready);
363 emit locationsChanged();
364 if (oldCount != declarativeLocations_.count())
371void QDeclarativeGeocodeModel::geocodeError(QGeoCodeReply *reply,
372 QGeoCodeReply::Error error,
373 const QString &errorString)
378 reply->deleteLater();
380 int oldCount = declarativeLocations_.count();
383 setLocations(reply->locations());
384 emit locationsChanged();
387 setError(
static_cast<QDeclarativeGeocodeModel::GeocodeError>(error), errorString);
388 setStatus(QDeclarativeGeocodeModel::Error);
475 declarativeLocations_.append(
new QDeclarativeGeoLocation(location,
this));
572void QDeclarativeGeocodeModel::reset()
575 if (!declarativeLocations_.isEmpty()) {
576 setLocations(QList<QGeoLocation>());
582 setError(NoError, QString());
583 setStatus(QDeclarativeGeocodeModel::Null);
618void QDeclarativeGeocodeModel::setQuery(
const QVariant &query)
620 if (query == queryVariant_)
623 if (query.userType() == qMetaTypeId<QGeoCoordinate>()) {
625 address_->disconnect(
this);
628 searchString_.clear();
630 coordinate_ = query.value<QGeoCoordinate>();
631 }
else if (query.typeId() == QMetaType::QString) {
632 searchString_ = query.toString();
634 address_->disconnect(
this);
637 coordinate_ = QGeoCoordinate();
638 }
else if (QObject *object = query.value<QObject *>()) {
639 if (QDeclarativeGeoAddress *address = qobject_cast<QDeclarativeGeoAddress *>(object)) {
641 address_->disconnect(
this);
642 coordinate_ = QGeoCoordinate();
643 searchString_.clear();
646 connect(address_, &QDeclarativeGeoAddress::countryChanged,
647 this, &QDeclarativeGeocodeModel::queryContentChanged);
648 connect(address_, &QDeclarativeGeoAddress::countryCodeChanged,
649 this, &QDeclarativeGeocodeModel::queryContentChanged);
650 connect(address_, &QDeclarativeGeoAddress::stateChanged,
651 this, &QDeclarativeGeocodeModel::queryContentChanged);
652 connect(address_, &QDeclarativeGeoAddress::countyChanged,
653 this, &QDeclarativeGeocodeModel::queryContentChanged);
654 connect(address_, &QDeclarativeGeoAddress::cityChanged,
655 this, &QDeclarativeGeocodeModel::queryContentChanged);
656 connect(address_, &QDeclarativeGeoAddress::districtChanged,
657 this, &QDeclarativeGeocodeModel::queryContentChanged);
658 connect(address_, &QDeclarativeGeoAddress::streetChanged,
659 this, &QDeclarativeGeocodeModel::queryContentChanged);
660 connect(address_, &QDeclarativeGeoAddress::postalCodeChanged,
661 this, &QDeclarativeGeocodeModel::queryContentChanged);
663 qmlWarning(
this) << QStringLiteral(
"Unsupported query type for geocode model ")
664 << QStringLiteral(
"(coordinate, string and Address supported).");
668 qmlWarning(
this) << QStringLiteral(
"Unsupported query type for geocode model ")
669 << QStringLiteral(
"(coordinate, string and Address supported).");
673 queryVariant_ = query;