115void QDeclarativeGeocodeModel::update()
121 setError(EngineNotSetError, tr(
"Cannot geocode, plugin not set."));
125 QGeoServiceProvider *serviceProvider = plugin_->sharedGeoServiceProvider();
126 if (!serviceProvider)
129 QGeoCodingManager *geocodingManager = serviceProvider->geocodingManager();
130 if (!geocodingManager) {
131 setError(EngineNotSetError, tr(
"Cannot geocode, geocode manager not set."));
134 if (!coordinate_.isValid() && (!address_ || address_->address().isEmpty()) &&
135 (searchString_.isEmpty())) {
136 setError(ParseError, tr(
"Cannot geocode, valid query not set."));
140 setError(NoError, QString());
142 if (coordinate_.isValid()) {
143 setStatus(QDeclarativeGeocodeModel::Loading);
144 reply_ = geocodingManager->reverseGeocode(coordinate_, boundingArea_);
145 if (reply_->isFinished()) {
146 if (reply_->error() == QGeoCodeReply::NoError) {
147 geocodeFinished(reply_);
149 geocodeError(reply_, reply_->error(), reply_->errorString());
152 }
else if (address_) {
153 setStatus(QDeclarativeGeocodeModel::Loading);
154 reply_ = geocodingManager->geocode(address_->address(), boundingArea_);
155 if (reply_->isFinished()) {
156 if (reply_->error() == QGeoCodeReply::NoError) {
157 geocodeFinished(reply_);
159 geocodeError(reply_, reply_->error(), reply_->errorString());
162 }
else if (!searchString_.isEmpty()) {
163 setStatus(QDeclarativeGeocodeModel::Loading);
164 reply_ = geocodingManager->geocode(searchString_, limit_, offset_, boundingArea_);
165 if (reply_->isFinished()) {
166 if (reply_->error() == QGeoCodeReply::NoError) {
167 geocodeFinished(reply_);
169 geocodeError(reply_, reply_->error(), reply_->errorString());
211QVariant QDeclarativeGeocodeModel::data(
const QModelIndex &index,
int role)
const
213 if (!index.isValid())
215 if (index.row() >= declarativeLocations_.count())
217 if (role == QDeclarativeGeocodeModel::LocationRole) {
218 QObject *locationObject = declarativeLocations_.at(index.row());
219 Q_ASSERT(locationObject);
220 return QVariant::fromValue(locationObject);
225QHash<
int, QByteArray> QDeclarativeGeocodeModel::roleNames()
const
227 QHash<
int, QByteArray> roleNames = QAbstractItemModel::roleNames();
228 roleNames.insert(LocationRole,
"locationData");
235void QDeclarativeGeocodeModel::setPlugin(QDeclarativeGeoServiceProvider *plugin)
237 if (plugin_ == plugin)
243 emit pluginChanged();
248 if (plugin_->isAttached()) {
251 connect(plugin_, &QDeclarativeGeoServiceProvider::attached,
252 this, &QDeclarativeGeocodeModel::pluginReady);
259void QDeclarativeGeocodeModel::pluginReady()
261 QGeoServiceProvider *serviceProvider = plugin_->sharedGeoServiceProvider();
262 QGeoCodingManager *geocodingManager = serviceProvider->geocodingManager();
264 if (serviceProvider->geocodingError() != QGeoServiceProvider::NoError) {
265 QDeclarativeGeocodeModel::GeocodeError newError = UnknownError;
266 switch (serviceProvider->geocodingError()) {
267 case QGeoServiceProvider::NotSupportedError:
268 newError = EngineNotSetError;
break;
269 case QGeoServiceProvider::UnknownParameterError:
270 newError = UnknownParameterError;
break;
271 case QGeoServiceProvider::MissingRequiredParameterError:
272 newError = MissingRequiredParameterError;
break;
273 case QGeoServiceProvider::ConnectionError:
274 newError = CommunicationError;
break;
279 setError(newError, serviceProvider->geocodingErrorString());
283 if (!geocodingManager) {
284 setError(EngineNotSetError,tr(
"Plugin does not support (reverse) geocoding."));
288 connect(geocodingManager, &QGeoCodingManager::finished,
289 this, &QDeclarativeGeocodeModel::geocodeFinished);
290 connect(geocodingManager, &QGeoCodingManager::errorOccurred,
291 this, &QDeclarativeGeocodeModel::geocodeError);
293 if (complete_ && autoUpdate_)
312void QDeclarativeGeocodeModel::setBounds(
const QVariant &boundingArea)
316 if (boundingArea.userType() == qMetaTypeId<QGeoRectangle>())
317 s = boundingArea.value<QGeoRectangle>();
318 else if (boundingArea.userType() == qMetaTypeId<QGeoCircle>())
319 s = boundingArea.value<QGeoCircle>();
320 else if (boundingArea.userType() == qMetaTypeId<QGeoShape>())
321 s = boundingArea.value<QGeoShape>();
324 if (boundingArea_ == s)
328 emit boundsChanged();
341QVariant QDeclarativeGeocodeModel::bounds()
const
343 if (boundingArea_.type() == QGeoShape::RectangleType)
344 return QVariant::fromValue(QGeoRectangle(boundingArea_));
345 else if (boundingArea_.type() == QGeoShape::CircleType)
346 return QVariant::fromValue(QGeoCircle(boundingArea_));
347 else if (boundingArea_.type() == QGeoShape::PolygonType)
348 return QVariant::fromValue(QGeoPolygon(boundingArea_));
350 return QVariant::fromValue(boundingArea_);
353void QDeclarativeGeocodeModel::geocodeFinished(QGeoCodeReply *reply)
355 if (reply != reply_ || reply->error() != QGeoCodeReply::NoError)
358 reply->deleteLater();
360 int oldCount = declarativeLocations_.count();
361 setLocations(reply->locations());
362 setError(NoError, QString());
363 setStatus(QDeclarativeGeocodeModel::Ready);
364 emit locationsChanged();
365 if (oldCount != declarativeLocations_.count())
372void QDeclarativeGeocodeModel::geocodeError(QGeoCodeReply *reply,
373 QGeoCodeReply::Error error,
374 const QString &errorString)
379 reply->deleteLater();
381 int oldCount = declarativeLocations_.count();
384 setLocations(reply->locations());
385 emit locationsChanged();
388 setError(
static_cast<QDeclarativeGeocodeModel::GeocodeError>(error), errorString);
389 setStatus(QDeclarativeGeocodeModel::Error);
476 declarativeLocations_.append(
new QDeclarativeGeoLocation(location,
this));
573void QDeclarativeGeocodeModel::reset()
576 if (!declarativeLocations_.isEmpty()) {
577 setLocations(QList<QGeoLocation>());
583 setError(NoError, QString());
584 setStatus(QDeclarativeGeocodeModel::Null);
619void QDeclarativeGeocodeModel::setQuery(
const QVariant &query)
621 if (query == queryVariant_)
624 if (query.userType() == qMetaTypeId<QGeoCoordinate>()) {
626 address_->disconnect(
this);
629 searchString_.clear();
631 coordinate_ = query.value<QGeoCoordinate>();
632 }
else if (query.typeId() == QMetaType::QString) {
633 searchString_ = query.toString();
635 address_->disconnect(
this);
638 coordinate_ = QGeoCoordinate();
639 }
else if (QObject *object = query.value<QObject *>()) {
640 if (QDeclarativeGeoAddress *address = qobject_cast<QDeclarativeGeoAddress *>(object)) {
642 address_->disconnect(
this);
643 coordinate_ = QGeoCoordinate();
644 searchString_.clear();
647 connect(address_, &QDeclarativeGeoAddress::countryChanged,
648 this, &QDeclarativeGeocodeModel::queryContentChanged);
649 connect(address_, &QDeclarativeGeoAddress::countryCodeChanged,
650 this, &QDeclarativeGeocodeModel::queryContentChanged);
651 connect(address_, &QDeclarativeGeoAddress::stateChanged,
652 this, &QDeclarativeGeocodeModel::queryContentChanged);
653 connect(address_, &QDeclarativeGeoAddress::countyChanged,
654 this, &QDeclarativeGeocodeModel::queryContentChanged);
655 connect(address_, &QDeclarativeGeoAddress::cityChanged,
656 this, &QDeclarativeGeocodeModel::queryContentChanged);
657 connect(address_, &QDeclarativeGeoAddress::districtChanged,
658 this, &QDeclarativeGeocodeModel::queryContentChanged);
659 connect(address_, &QDeclarativeGeoAddress::streetChanged,
660 this, &QDeclarativeGeocodeModel::queryContentChanged);
661 connect(address_, &QDeclarativeGeoAddress::postalCodeChanged,
662 this, &QDeclarativeGeocodeModel::queryContentChanged);
664 qmlWarning(
this) << QStringLiteral(
"Unsupported query type for geocode model ")
665 << QStringLiteral(
"(coordinate, string and Address supported).");
669 qmlWarning(
this) << QStringLiteral(
"Unsupported query type for geocode model ")
670 << QStringLiteral(
"(coordinate, string and Address supported).");
674 queryVariant_ = query;