summaryrefslogtreecommitdiff
path: root/src/location/declarativemaps/qdeclarativegeomap.cpp
diff options
context:
space:
mode:
authorPaolo Angelelli <paolo.angelelli@qt.io>2018-12-06 13:34:49 +0100
committerPaolo Angelelli <paolo.angelelli@qt.io>2018-12-11 00:14:39 +0000
commit56e07f78579196fc33580ca5f20073a156b0e2dd (patch)
tree5a572f0a8a2028a6fceb57823c359ca6fecea16b /src/location/declarativemaps/qdeclarativegeomap.cpp
parentc8658686cddee13563aca337c4172a9864caefda (diff)
downloadqtlocation-56e07f78579196fc33580ca5f20073a156b0e2dd.tar.gz
Add Map.fitViewportToGeoShape(shape, margins)
This method extends the functionality of setting the visibleRegion, by also allowing to specify the margins in pixels. Task-number: QTBUG-69640 Change-Id: I196d0410782992ad2ac954aa08e226521b87ba7b Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src/location/declarativemaps/qdeclarativegeomap.cpp')
-rw-r--r--src/location/declarativemaps/qdeclarativegeomap.cpp95
1 files changed, 60 insertions, 35 deletions
diff --git a/src/location/declarativemaps/qdeclarativegeomap.cpp b/src/location/declarativemaps/qdeclarativegeomap.cpp
index 63587efe..3c73ca12 100644
--- a/src/location/declarativemaps/qdeclarativegeomap.cpp
+++ b/src/location/declarativemaps/qdeclarativegeomap.cpp
@@ -1305,7 +1305,7 @@ void QDeclarativeGeoMap::setVisibleRegion(const QGeoShape &shape)
return;
}
- fitViewportToGeoShape();
+ fitViewportToGeoShape(m_visibleRegion);
}
QGeoShape QDeclarativeGeoMap::visibleRegion() const
@@ -1445,39 +1445,6 @@ QMargins QDeclarativeGeoMap::mapMargins() const
, height() - va.height() - va.y());
}
-// TODO: offer the possibility to specify the margins.
-void QDeclarativeGeoMap::fitViewportToGeoShape()
-{
- if (m_map->geoProjection().projectionType() == QGeoProjection::ProjectionWebMercator) {
- // This case remains handled here, and not inside QGeoMap*::fitViewportToGeoRectangle,
- // in order to honor animations on center and zoomLevel
- const QGeoProjectionWebMercator &p = static_cast<const QGeoProjectionWebMercator&>(m_map->geoProjection());
- const int borderSize = 10;
- const QMargins borders(borderSize, borderSize, borderSize, borderSize);
-
- if (!m_map || !m_visibleRegion.isValid())
- return;
-
- const QMargins margins = borders + mapMargins();
- const QPair<QGeoCoordinate, qreal> fitData = p.fitViewportToGeoRectangle(m_visibleRegion,
- margins);
- if (!fitData.first.isValid())
- return;
-
- // position camera to the center of bounding box
- setProperty("center", QVariant::fromValue(fitData.first)); // not using setCenter(centerCoordinate) to honor a possible animation set on the center property
-
- if (!qIsFinite(fitData.second))
- return;
- double newZoom = qMax<double>(minimumZoomLevel(), fitData.second);
- setProperty("zoomLevel", QVariant::fromValue(newZoom)); // not using setZoomLevel(newZoom) to honor a possible animation set on the zoomLevel property
- } else if (m_map->capabilities() & QGeoMap::SupportsFittingViewportToGeoRectangle) {
- // Animations cannot be honored in this case, as m_map acts as a black box
- m_map->fitViewportToGeoRectangle(m_visibleRegion);
- }
-}
-
-
/*!
\qmlproperty list<MapType> QtLocation::Map::supportedMapTypes
@@ -1604,6 +1571,64 @@ void QDeclarativeGeoMap::clearData()
}
/*!
+ \qmlmethod void QtLocation::Map::fitViewportToGeoShape(geoShape, margins)
+
+ Fits the viewport to a specific geo shape.
+ The margins are in screen pixels.
+
+ \note If the projection used by the plugin is not WebMercator, and the plugin does not have fitting to
+ shape capability, this method will do nothing.
+
+ \sa visibleRegion
+ \since 5.13
+*/
+void QDeclarativeGeoMap::fitViewportToGeoShape(const QGeoShape &shape, QVariant margins)
+{
+ QMargins m(10, 10, 10, 10); // lets defaults to 10 if margins is invalid
+ switch (margins.type()) {
+ case QMetaType::Int:
+ case QMetaType::Double: {
+ const int value = int(margins.toDouble());
+ m = QMargins(value, value, value, value);
+ }
+ break;
+ // ToDo: Support distinct margins in some QML form. Perhaps QRect?
+ default:
+ break;
+ }
+ fitViewportToGeoShape(shape, m);
+}
+
+void QDeclarativeGeoMap::fitViewportToGeoShape(const QGeoShape &shape, const QMargins &borders)
+{
+ if (!m_map || !shape.isValid())
+ return;
+
+ if (m_map->geoProjection().projectionType() == QGeoProjection::ProjectionWebMercator) {
+ // This case remains handled here, and not inside QGeoMap*::fitViewportToGeoRectangle,
+ // in order to honor animations on center and zoomLevel
+ const QMargins margins = borders + mapMargins();
+ const QGeoProjectionWebMercator &p = static_cast<const QGeoProjectionWebMercator&>(m_map->geoProjection());
+ const QPair<QGeoCoordinate, qreal> fitData = p.fitViewportToGeoRectangle(shape.boundingGeoRectangle(),
+ margins);
+ if (!fitData.first.isValid())
+ return;
+
+ // position camera to the center of bounding box
+ setProperty("center", QVariant::fromValue(fitData.first)); // not using setCenter(centerCoordinate) to honor a possible animation set on the center property
+
+ if (!qIsFinite(fitData.second))
+ return;
+ double newZoom = qMax<double>(minimumZoomLevel(), fitData.second);
+ setProperty("zoomLevel", QVariant::fromValue(newZoom)); // not using setZoomLevel(newZoom) to honor a possible animation set on the zoomLevel property
+ } else if (m_map->capabilities() & QGeoMap::SupportsFittingViewportToGeoRectangle) {
+ // Animations cannot be honored in this case, as m_map acts as a black box
+ m_map->fitViewportToGeoRectangle(m_visibleRegion, borders);
+ }
+ // else out of luck
+}
+
+/*!
\qmlproperty string QtLocation::Map::errorString
This read-only property holds the textual presentation of the latest mapping provider error.
@@ -2205,7 +2230,7 @@ void QDeclarativeGeoMap::geometryChanged(const QRectF &newGeometry, const QRectF
Multiple fitViewportTo*() calls replace each other.
*/
if (m_pendingFitViewport && width() && height()) {
- fitViewportToGeoShape();
+ fitViewportToGeoShape(m_visibleRegion);
m_pendingFitViewport = false;
}