summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron McCarthy <mccarthy.aaron@gmail.com>2014-07-28 09:57:04 +1000
committerAaron McCarthy <mccarthy.aaron@gmail.com>2014-12-04 05:31:54 +0100
commitb8681cdb51cc5d7040ecbe08b014bace2444c6f1 (patch)
treeebfe6a4e4cbbf178b1df2339abf91f7b4c84a2cf
parentfddb7c51231e1649081de3a1d5f2ad191e9cd7dc (diff)
downloadqtlocation-b8681cdb51cc5d7040ecbe08b014bace2444c6f1.tar.gz
Add center() function to QGeoShape.
It is frequently useful to calculate the geometric center of a shape. Both QGeoCircle and QGeoRectangle already define center() functions, however, the application developer must cast each QGeoShape into either a QGeoCirlce or QGeoRectangle before calling. Providing QGeoShape::center() allows application code to be simplified. Existing QGeoCircle::center() and QGeoRectangle::center() functions are kept for compatibility. Change-Id: I92b727ab5e713f70174588a27040446c992ae14e Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
-rw-r--r--src/plugins/geoservices/nokia/qplacemanagerengine_nokiav2.cpp28
-rw-r--r--src/plugins/position/positionpoll/qgeoareamonitor_polling.cpp21
-rw-r--r--src/positioning/qgeocircle.cpp29
-rw-r--r--src/positioning/qgeocircle_p.h4
-rw-r--r--src/positioning/qgeorectangle.cpp38
-rw-r--r--src/positioning/qgeorectangle_p.h2
-rw-r--r--src/positioning/qgeoshape.cpp15
-rw-r--r--src/positioning/qgeoshape.h2
-rw-r--r--src/positioning/qgeoshape_p.h2
-rw-r--r--tests/auto/qgeocircle/tst_qgeocircle.cpp4
-rw-r--r--tests/auto/qgeorectangle/tst_qgeorectangle.cpp2
11 files changed, 77 insertions, 70 deletions
diff --git a/src/plugins/geoservices/nokia/qplacemanagerengine_nokiav2.cpp b/src/plugins/geoservices/nokia/qplacemanagerengine_nokiav2.cpp
index d200712e..5da9c3be 100644
--- a/src/plugins/geoservices/nokia/qplacemanagerengine_nokiav2.cpp
+++ b/src/plugins/geoservices/nokia/qplacemanagerengine_nokiav2.cpp
@@ -329,27 +329,15 @@ QPlaceContentReply *QPlaceManagerEngineNokiaV2::getPlaceContent(const QPlaceCont
static bool addAtForBoundingArea(const QGeoShape &area,
QUrlQuery *queryItems)
{
- QGeoCoordinate center;
- switch (area.type()) {
- case QGeoShape::RectangleType:
- center = QGeoRectangle(area).center();
- break;
- case QGeoShape::CircleType:
- center = QGeoCircle(area).center();
- break;
- case QGeoShape::UnknownType:
- break;
- }
-
- if (!center.isValid()) {
+ QGeoCoordinate center = area.center();
+ if (!center.isValid())
return false;
- } else {
- queryItems->addQueryItem(QStringLiteral("at"),
- QString::number(center.latitude()) +
- QLatin1Char(',') +
- QString::number(center.longitude()));
- return true;
- }
+
+ queryItems->addQueryItem(QStringLiteral("at"),
+ QString::number(center.latitude()) +
+ QLatin1Char(',') +
+ QString::number(center.longitude()));
+ return true;
}
QPlaceSearchReply *QPlaceManagerEngineNokiaV2::search(const QPlaceSearchRequest &query)
diff --git a/src/plugins/position/positionpoll/qgeoareamonitor_polling.cpp b/src/plugins/position/positionpoll/qgeoareamonitor_polling.cpp
index 895272f8..3f0acee5 100644
--- a/src/plugins/position/positionpoll/qgeoareamonitor_polling.cpp
+++ b/src/plugins/position/positionpoll/qgeoareamonitor_polling.cpp
@@ -420,26 +420,7 @@ QList<QGeoAreaMonitorInfo> QGeoAreaMonitorPolling::activeMonitors(const QGeoShap
const MonitorTable list = d->activeMonitors();
foreach (const QGeoAreaMonitorInfo &monitor, list) {
- QGeoCoordinate center;
- switch (monitor.area().type()) {
- case QGeoShape::CircleType:
- {
- QGeoCircle circle(monitor.area());
- center = circle.center();
- break;
- }
- case QGeoShape::RectangleType:
- {
- QGeoRectangle rectangle(monitor.area());
- center = rectangle.center();
- break;
- }
- case QGeoShape::UnknownType:
- {
- break;
- }
- }
- if (region.contains(center))
+ if (region.contains(monitor.area().center()))
results.append(monitor);
}
diff --git a/src/positioning/qgeocircle.cpp b/src/positioning/qgeocircle.cpp
index 459ce824..61336d1f 100644
--- a/src/positioning/qgeocircle.cpp
+++ b/src/positioning/qgeocircle.cpp
@@ -151,7 +151,7 @@ bool QGeoCircle::operator!=(const QGeoCircle &other) const
bool QGeoCirclePrivate::isValid() const
{
- return center.isValid() && !qIsNaN(radius) && radius >= -1e-7;
+ return m_center.isValid() && !qIsNaN(radius) && radius >= -1e-7;
}
bool QGeoCirclePrivate::isEmpty() const
@@ -166,17 +166,17 @@ void QGeoCircle::setCenter(const QGeoCoordinate &center)
{
Q_D(QGeoCircle);
- d->center = center;
+ d->m_center = center;
}
/*!
- Returns the center coordinate of this geo circle.
+ Returns the center coordinate of this geo circle. Equivalent to QGeoShape::center().
*/
QGeoCoordinate QGeoCircle::center() const
{
Q_D(const QGeoCircle);
- return d->center;
+ return d->center();
}
/*!
@@ -205,13 +205,18 @@ bool QGeoCirclePrivate::contains(const QGeoCoordinate &coordinate) const
return false;
// see QTBUG-41447 for details
- qreal distance = center.distanceTo(coordinate);
+ qreal distance = m_center.distanceTo(coordinate);
if (qFuzzyCompare(distance, radius) || distance <= radius)
return true;
return false;
}
+QGeoCoordinate QGeoCirclePrivate::center() const
+{
+ return m_center;
+}
+
/*!
Extends the circle to include \a coordinate
*/
@@ -220,7 +225,7 @@ void QGeoCirclePrivate::extendShape(const QGeoCoordinate &coordinate)
if (!isValid() || !coordinate.isValid() || contains(coordinate))
return;
- radius = center.distanceTo(coordinate);
+ radius = m_center.distanceTo(coordinate);
}
/*!
@@ -235,8 +240,8 @@ void QGeoCircle::translate(double degreesLatitude, double degreesLongitude)
Q_D(QGeoCircle);
- double lat = d->center.latitude();
- double lon = d->center.longitude();
+ double lat = d->m_center.latitude();
+ double lon = d->m_center.longitude();
lat += degreesLatitude;
lon += degreesLongitude;
@@ -262,7 +267,7 @@ void QGeoCircle::translate(double degreesLatitude, double degreesLongitude)
lon -= 180;
}
- d->center = QGeoCoordinate(lat, lon);
+ d->m_center = QGeoCoordinate(lat, lon);
}
/*!
@@ -290,12 +295,12 @@ QGeoCirclePrivate::QGeoCirclePrivate()
}
QGeoCirclePrivate::QGeoCirclePrivate(const QGeoCoordinate &center, qreal radius)
-: QGeoShapePrivate(QGeoShape::CircleType), center(center), radius(radius)
+: QGeoShapePrivate(QGeoShape::CircleType), m_center(center), radius(radius)
{
}
QGeoCirclePrivate::QGeoCirclePrivate(const QGeoCirclePrivate &other)
-: QGeoShapePrivate(QGeoShape::CircleType), center(other.center),
+: QGeoShapePrivate(QGeoShape::CircleType), m_center(other.m_center),
radius(other.radius)
{
}
@@ -314,7 +319,7 @@ bool QGeoCirclePrivate::operator==(const QGeoShapePrivate &other) const
const QGeoCirclePrivate &otherCircle = static_cast<const QGeoCirclePrivate &>(other);
- return radius == otherCircle.radius && center == otherCircle.center;
+ return radius == otherCircle.radius && m_center == otherCircle.m_center;
}
QT_END_NAMESPACE
diff --git a/src/positioning/qgeocircle_p.h b/src/positioning/qgeocircle_p.h
index d6fe37b5..635bd2ec 100644
--- a/src/positioning/qgeocircle_p.h
+++ b/src/positioning/qgeocircle_p.h
@@ -62,13 +62,15 @@ public:
bool isEmpty() const;
bool contains(const QGeoCoordinate &coordinate) const;
+ QGeoCoordinate center() const Q_DECL_OVERRIDE;
+
void extendShape(const QGeoCoordinate &coordinate);
QGeoShapePrivate *clone() const;
bool operator==(const QGeoShapePrivate &other) const;
- QGeoCoordinate center;
+ QGeoCoordinate m_center;
qreal radius;
};
diff --git a/src/positioning/qgeorectangle.cpp b/src/positioning/qgeorectangle.cpp
index d514edf9..808e091c 100644
--- a/src/positioning/qgeorectangle.cpp
+++ b/src/positioning/qgeorectangle.cpp
@@ -374,28 +374,13 @@ void QGeoRectangle::setCenter(const QGeoCoordinate &center)
}
/*!
- Returns the center of this geo rectangle.
+ Returns the center of this geo rectangle. Equivalent to QGeoShape::center().
*/
QGeoCoordinate QGeoRectangle::center() const
{
- if (!isValid())
- return QGeoCoordinate();
-
Q_D(const QGeoRectangle);
- double cLat = (d->topLeft.latitude() + d->bottomRight.latitude()) / 2.0;
-
- double cLon = (d->bottomRight.longitude() + d->topLeft.longitude()) / 2.0;
- if (d->topLeft.longitude() > d->bottomRight.longitude()) {
- cLon = cLon - 180.0;
- }
-
- if (cLon < -180.0)
- cLon += 360.0;
- if (cLon > 180.0)
- cLon -= 360.0;
-
- return QGeoCoordinate(cLat, cLon);
+ return d->center();
}
/*!
@@ -586,6 +571,25 @@ bool QGeoRectanglePrivate::contains(const QGeoCoordinate &coordinate) const
return true;
}
+QGeoCoordinate QGeoRectanglePrivate::center() const
+{
+ if (!isValid())
+ return QGeoCoordinate();
+
+ double cLat = (topLeft.latitude() + bottomRight.latitude()) / 2.0;
+ double cLon = (bottomRight.longitude() + topLeft.longitude()) / 2.0;
+
+ if (topLeft.longitude() > bottomRight.longitude())
+ cLon = cLon - 180.0;
+
+ if (cLon < -180.0)
+ cLon += 360.0;
+ if (cLon > 180.0)
+ cLon -= 360.0;
+
+ return QGeoCoordinate(cLat, cLon);
+}
+
/*!
Returns whether the geo rectangle \a rectangle is contained within this
geo rectangle.
diff --git a/src/positioning/qgeorectangle_p.h b/src/positioning/qgeorectangle_p.h
index 1c5bc6f2..817fd360 100644
--- a/src/positioning/qgeorectangle_p.h
+++ b/src/positioning/qgeorectangle_p.h
@@ -62,6 +62,8 @@ public:
bool isEmpty() const;
bool contains(const QGeoCoordinate &coordinate) const;
+ QGeoCoordinate center() const Q_DECL_OVERRIDE;
+
void extendShape(const QGeoCoordinate &coordinate);
QGeoShapePrivate *clone() const;
diff --git a/src/positioning/qgeoshape.cpp b/src/positioning/qgeoshape.cpp
index 932f7618..c331ef81 100644
--- a/src/positioning/qgeoshape.cpp
+++ b/src/positioning/qgeoshape.cpp
@@ -185,6 +185,21 @@ bool QGeoShape::contains(const QGeoCoordinate &coordinate) const
}
/*!
+ Returns the coordinate located at the geometric center of the geo shape.
+
+ \since 5.5
+*/
+QGeoCoordinate QGeoShape::center() const
+{
+ Q_D(const QGeoShape);
+
+ if (d)
+ return d->center();
+ else
+ return QGeoCoordinate();
+}
+
+/*!
Extends the geo shape to also cover the coordinate \a coordinate
*/
void QGeoShape::extendShape(const QGeoCoordinate &coordinate)
diff --git a/src/positioning/qgeoshape.h b/src/positioning/qgeoshape.h
index 3704ba6a..edaf263f 100644
--- a/src/positioning/qgeoshape.h
+++ b/src/positioning/qgeoshape.h
@@ -61,6 +61,8 @@ public:
bool isEmpty() const;
bool contains(const QGeoCoordinate &coordinate) const;
+ QGeoCoordinate center() const;
+
void extendShape(const QGeoCoordinate &coordinate);
bool operator==(const QGeoShape &other) const;
diff --git a/src/positioning/qgeoshape_p.h b/src/positioning/qgeoshape_p.h
index 62ddd77c..154a4505 100644
--- a/src/positioning/qgeoshape_p.h
+++ b/src/positioning/qgeoshape_p.h
@@ -61,6 +61,8 @@ public:
virtual bool isEmpty() const = 0;
virtual bool contains(const QGeoCoordinate &coordinate) const = 0;
+ virtual QGeoCoordinate center() const = 0;
+
virtual void extendShape(const QGeoCoordinate &coordinate) = 0;
virtual QGeoShapePrivate *clone() const = 0;
diff --git a/tests/auto/qgeocircle/tst_qgeocircle.cpp b/tests/auto/qgeocircle/tst_qgeocircle.cpp
index e821db3d..695b1cfb 100644
--- a/tests/auto/qgeocircle/tst_qgeocircle.cpp
+++ b/tests/auto/qgeocircle/tst_qgeocircle.cpp
@@ -174,6 +174,10 @@ void tst_QGeoCircle::center()
QGeoCircle c;
c.setCenter(QGeoCoordinate(1,1));
QCOMPARE(c.center(), QGeoCoordinate(1,1));
+
+ QGeoShape shape = c;
+ QCOMPARE(shape.center(), c.center());
+
c.setCenter(QGeoCoordinate(5,10));
QCOMPARE(c.center(), QGeoCoordinate(5,10));
}
diff --git a/tests/auto/qgeorectangle/tst_qgeorectangle.cpp b/tests/auto/qgeorectangle/tst_qgeorectangle.cpp
index 2e6d274b..0a358c35 100644
--- a/tests/auto/qgeorectangle/tst_qgeorectangle.cpp
+++ b/tests/auto/qgeorectangle/tst_qgeorectangle.cpp
@@ -829,7 +829,9 @@ void tst_QGeoRectangle::center()
QFETCH(QGeoCoordinate, newCenter);
QFETCH(QGeoRectangle, newBox);
+ QGeoShape shape = box;
QCOMPARE(box.center(), oldCenter);
+ QCOMPARE(shape.center(), oldCenter);
box.setCenter(newCenter);
QCOMPARE(box, newBox);
}