summaryrefslogtreecommitdiff
path: root/src/imports/location/qdeclarativerectanglemapitem.cpp
diff options
context:
space:
mode:
authorPaolo Angelelli <paolo.angelelli@qt.io>2016-08-06 21:26:10 +0200
committerPaolo Angelelli <paolo.angelelli@qt.io>2017-01-26 14:46:00 +0000
commitc062403a894a270101ae821fd73fa848f918ac54 (patch)
treeda4becf0a6e1c5685f1f57d5ce1d8890feab225c /src/imports/location/qdeclarativerectanglemapitem.cpp
parent12b62e04e4c17ddeaeaae46ec09af2eb7244e840 (diff)
downloadqtlocation-c062403a894a270101ae821fd73fa848f918ac54.tar.gz
Use geoshapes as geo-model in qdeclarativegeomapitems
This patch uses QGeoShapes as data model inside the QDeclarativeMapItemBase subclasses. Advantages: - avoids duplication of implementation for geo-related methods (now only inside QGeoShapes). - provides map items a bounding box, usable for area based item searching ,or simple viewport fitting - allows to follow the graphics-view pattern, and have the same items visualized in different Maps (not yet supported). Change-Id: I6eec738fef5d753d90bdeeb4b109be89155b25f8 Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src/imports/location/qdeclarativerectanglemapitem.cpp')
-rw-r--r--src/imports/location/qdeclarativerectanglemapitem.cpp84
1 files changed, 37 insertions, 47 deletions
diff --git a/src/imports/location/qdeclarativerectanglemapitem.cpp b/src/imports/location/qdeclarativerectanglemapitem.cpp
index 5b6a8914..a467f4c6 100644
--- a/src/imports/location/qdeclarativerectanglemapitem.cpp
+++ b/src/imports/location/qdeclarativerectanglemapitem.cpp
@@ -162,18 +162,18 @@ QDeclarativeMapLineProperties *QDeclarativeRectangleMapItem::border()
*/
void QDeclarativeRectangleMapItem::setTopLeft(const QGeoCoordinate &topLeft)
{
- if (topLeft_ == topLeft)
+ if (rectangle_.topLeft() == topLeft)
return;
- topLeft_ = topLeft;
+ rectangle_.setTopLeft(topLeft);
markSourceDirtyAndUpdate();
- emit topLeftChanged(topLeft_);
+ emit topLeftChanged(topLeft);
}
QGeoCoordinate QDeclarativeRectangleMapItem::topLeft()
{
- return topLeft_;
+ return rectangle_.topLeft();
}
/*!
@@ -194,18 +194,18 @@ void QDeclarativeRectangleMapItem::markSourceDirtyAndUpdate()
*/
void QDeclarativeRectangleMapItem::setBottomRight(const QGeoCoordinate &bottomRight)
{
- if (bottomRight_ == bottomRight)
+ if (rectangle_.bottomRight() == bottomRight)
return;
- bottomRight_ = bottomRight;
+ rectangle_.setBottomRight(bottomRight);
markSourceDirtyAndUpdate();
- emit bottomRightChanged(bottomRight_);
+ emit bottomRightChanged(bottomRight);
}
QGeoCoordinate QDeclarativeRectangleMapItem::bottomRight()
{
- return bottomRight_;
+ return rectangle_.bottomRight();
}
/*!
@@ -276,12 +276,12 @@ void QDeclarativeRectangleMapItem::updatePolish()
updatingGeometry_ = true;
QList<QGeoCoordinate> path;
- path << topLeft_;
- path << QGeoCoordinate(topLeft_.latitude(), bottomRight_.longitude());
- path << bottomRight_;
- path << QGeoCoordinate(bottomRight_.latitude(), topLeft_.longitude());
+ path << rectangle_.topLeft();
+ path << QGeoCoordinate(rectangle_.topLeft().latitude(), rectangle_.bottomRight().longitude());
+ path << rectangle_.bottomRight();
+ path << QGeoCoordinate(rectangle_.bottomRight().latitude(), rectangle_.topLeft().longitude());
- geometry_.setPreserveGeometry(true, topLeft_);
+ geometry_.setPreserveGeometry(true, rectangle_.topLeft());
geometry_.updateSourcePoints(*map(), path);
geometry_.updateScreenPoints(*map());
@@ -293,8 +293,7 @@ void QDeclarativeRectangleMapItem::updatePolish()
QList<QGeoCoordinate> closedPath = path;
closedPath << closedPath.first();
- borderGeometry_.setPreserveGeometry(true, topLeft_);
-
+ borderGeometry_.setPreserveGeometry(true, rectangle_.topLeft());
const QGeoCoordinate &geometryOrigin = geometry_.origin();
borderGeometry_.srcPoints_.clear();
@@ -328,11 +327,9 @@ void QDeclarativeRectangleMapItem::afterViewportChanged(const QGeoMapViewportCha
if (event.mapSize.width() <= 0 || event.mapSize.height() <= 0)
return;
- geometry_.setPreserveGeometry(true, topLeft_);
- borderGeometry_.setPreserveGeometry(true, topLeft_);
- geometry_.markSourceDirty();
- borderGeometry_.markSourceDirty();
- polishAndUpdate();
+ geometry_.setPreserveGeometry(true, rectangle_.topLeft());
+ borderGeometry_.setPreserveGeometry(true, rectangle_.topLeft());
+ markSourceDirtyAndUpdate();
}
/*!
@@ -343,6 +340,11 @@ bool QDeclarativeRectangleMapItem::contains(const QPointF &point) const
return (geometry_.contains(point) || borderGeometry_.contains(point));
}
+const QGeoShape &QDeclarativeRectangleMapItem::geoShape() const
+{
+ return rectangle_;
+}
+
/*!
\internal
*/
@@ -352,34 +354,22 @@ void QDeclarativeRectangleMapItem::geometryChanged(const QRectF &newGeometry, co
QDeclarativeGeoMapItemBase::geometryChanged(newGeometry, oldGeometry);
return;
}
+ // TODO: change the algorithm to preserve the distances and size
+ QGeoCoordinate newCenter = map()->geoProjection().itemPositionToCoordinate(QDoubleVector2D(newGeometry.center()), false);
+ QGeoCoordinate oldCenter = map()->geoProjection().itemPositionToCoordinate(QDoubleVector2D(oldGeometry.center()), false);
+ if (!newCenter.isValid() || !oldCenter.isValid())
+ return;
+ double offsetLongi = newCenter.longitude() - oldCenter.longitude();
+ double offsetLati = newCenter.latitude() - oldCenter.latitude();
+ if (offsetLati == 0.0 && offsetLongi == 0.0)
+ return;
- QDoubleVector2D newTopLeftPoint = QDoubleVector2D(x(),y());
- QGeoCoordinate newTopLeft = map()->geoProjection().itemPositionToCoordinate(newTopLeftPoint, false);
- if (newTopLeft.isValid()) {
- // calculate new geo width while checking for dateline crossing
- const double lonW = bottomRight_.longitude() > topLeft_.longitude() ?
- bottomRight_.longitude() - topLeft_.longitude() :
- bottomRight_.longitude() + 360 - topLeft_.longitude();
- const double latH = qAbs(bottomRight_.latitude() - topLeft_.latitude());
- QGeoCoordinate newBottomRight;
- // prevent dragging over valid min and max latitudes
- if (QLocationUtils::isValidLat(newTopLeft.latitude() - latH)) {
- newBottomRight.setLatitude(newTopLeft.latitude() - latH);
- } else {
- newBottomRight.setLatitude(QLocationUtils::clipLat(newTopLeft.latitude() - latH));
- newTopLeft.setLatitude(newBottomRight.latitude() + latH);
- }
- // handle dateline crossing
- newBottomRight.setLongitude(QLocationUtils::wrapLong(newTopLeft.longitude() + lonW));
- newBottomRight.setAltitude(newTopLeft.altitude());
- topLeft_ = newTopLeft;
- bottomRight_ = newBottomRight;
- geometry_.setPreserveGeometry(true, newTopLeft);
- borderGeometry_.setPreserveGeometry(true, newTopLeft);
- markSourceDirtyAndUpdate();
- emit topLeftChanged(topLeft_);
- emit bottomRightChanged(bottomRight_);
- }
+ rectangle_.translate(offsetLati, offsetLongi);
+ geometry_.setPreserveGeometry(true, rectangle_.topLeft());
+ borderGeometry_.setPreserveGeometry(true, rectangle_.topLeft());
+ markSourceDirtyAndUpdate();
+ emit topLeftChanged(rectangle_.topLeft());
+ emit bottomRightChanged(rectangle_.bottomRight());
// Not calling QDeclarativeGeoMapItemBase::geometryChanged() as it will be called from a nested
// call to this function.