diff options
author | Alex Blasche <alexander.blasche@theqtcompany.com> | 2015-05-20 11:30:13 +0200 |
---|---|---|
committer | Alex Blasche <alexander.blasche@theqtcompany.com> | 2015-05-26 13:29:26 +0000 |
commit | 9c61ad227761c9f83ea441c6a8db2d570a4009a3 (patch) | |
tree | 71320572d0be2d2c4d2ad5a96445567596b06602 | |
parent | 68115c86eb0bfbe3218ed4fa3af3b006720a8c43 (diff) | |
download | qtlocation-9c61ad227761c9f83ea441c6a8db2d570a4009a3.tar.gz |
Workaround for failing conversion between Q_GADGET's
QGeoRectangle and QGeoCircle are subclasses of QGeoShape. Before this
patch setting a QGeoShape property of a QML object is not possible
because the QGeoRectangle/QGeoCircle cannot be converted to a QGeoShape.
This patch addresses the problem by providing custom converters for
the above classes. It enables QVariant::convert() for the classes above.
Change-Id: I7daba5254dad0df598324432db3b1819e68648b6
Task-number: QTBUG-46232
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Michal Klocek <michal.klocek@theqtcompany.com>
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
-rw-r--r-- | src/positioning/qgeocircle.cpp | 15 | ||||
-rw-r--r-- | src/positioning/qgeorectangle.cpp | 18 | ||||
-rw-r--r-- | tests/auto/qgeoshape/tst_qgeoshape.cpp | 20 |
3 files changed, 53 insertions, 0 deletions
diff --git a/src/positioning/qgeocircle.cpp b/src/positioning/qgeocircle.cpp index 6b5b24fd..c52b631e 100644 --- a/src/positioning/qgeocircle.cpp +++ b/src/positioning/qgeocircle.cpp @@ -100,12 +100,24 @@ inline const QGeoCirclePrivate *QGeoCircle::d_func() const return static_cast<const QGeoCirclePrivate *>(d_ptr.constData()); } +struct CircleVariantConversions +{ + CircleVariantConversions() + { + QMetaType::registerConverter<QGeoShape, QGeoCircle>(); + QMetaType::registerConverter<QGeoCircle, QGeoShape>(); + } +}; + +Q_GLOBAL_STATIC(CircleVariantConversions, initCircleConversions) + /*! Constructs a new, invalid geo circle. */ QGeoCircle::QGeoCircle() : QGeoShape(new QGeoCirclePrivate) { + initCircleConversions(); } /*! @@ -113,6 +125,7 @@ QGeoCircle::QGeoCircle() */ QGeoCircle::QGeoCircle(const QGeoCoordinate ¢er, qreal radius) { + initCircleConversions(); d_ptr = new QGeoCirclePrivate(center, radius); } @@ -122,6 +135,7 @@ QGeoCircle::QGeoCircle(const QGeoCoordinate ¢er, qreal radius) QGeoCircle::QGeoCircle(const QGeoCircle &other) : QGeoShape(other) { + initCircleConversions(); } /*! @@ -130,6 +144,7 @@ QGeoCircle::QGeoCircle(const QGeoCircle &other) QGeoCircle::QGeoCircle(const QGeoShape &other) : QGeoShape(other) { + initCircleConversions(); if (type() != QGeoShape::CircleType) d_ptr = new QGeoCirclePrivate; } diff --git a/src/positioning/qgeorectangle.cpp b/src/positioning/qgeorectangle.cpp index 2e615494..421e6b29 100644 --- a/src/positioning/qgeorectangle.cpp +++ b/src/positioning/qgeorectangle.cpp @@ -180,12 +180,25 @@ inline const QGeoRectanglePrivate *QGeoRectangle::d_func() const return static_cast<const QGeoRectanglePrivate *>(d_ptr.constData()); } +struct RectangleVariantConversions +{ + RectangleVariantConversions() + { + QMetaType::registerConverter<QGeoRectangle, QGeoShape>(); + QMetaType::registerConverter<QGeoShape, QGeoRectangle>(); + } +}; + + +Q_GLOBAL_STATIC(RectangleVariantConversions, initRectangleConversions) + /*! Constructs a new, invalid geo rectangle. */ QGeoRectangle::QGeoRectangle() : QGeoShape(new QGeoRectanglePrivate) { + initRectangleConversions(); } /*! @@ -200,6 +213,7 @@ QGeoRectangle::QGeoRectangle() */ QGeoRectangle::QGeoRectangle(const QGeoCoordinate ¢er, double degreesWidth, double degreesHeight) { + initRectangleConversions(); d_ptr = new QGeoRectanglePrivate(center, center); setWidth(degreesWidth); setHeight(degreesHeight); @@ -211,6 +225,7 @@ QGeoRectangle::QGeoRectangle(const QGeoCoordinate ¢er, double degreesWidth, */ QGeoRectangle::QGeoRectangle(const QGeoCoordinate &topLeft, const QGeoCoordinate &bottomRight) { + initRectangleConversions(); d_ptr = new QGeoRectanglePrivate(topLeft, bottomRight); } @@ -219,6 +234,7 @@ QGeoRectangle::QGeoRectangle(const QGeoCoordinate &topLeft, const QGeoCoordinate */ QGeoRectangle::QGeoRectangle(const QList<QGeoCoordinate> &coordinates) { + initRectangleConversions(); if (coordinates.isEmpty()) { d_ptr = new QGeoRectanglePrivate; } else { @@ -237,6 +253,7 @@ QGeoRectangle::QGeoRectangle(const QList<QGeoCoordinate> &coordinates) QGeoRectangle::QGeoRectangle(const QGeoRectangle &other) : QGeoShape(other) { + initRectangleConversions(); } /*! @@ -245,6 +262,7 @@ QGeoRectangle::QGeoRectangle(const QGeoRectangle &other) QGeoRectangle::QGeoRectangle(const QGeoShape &other) : QGeoShape(other) { + initRectangleConversions(); if (type() != QGeoShape::RectangleType) d_ptr = new QGeoRectanglePrivate; } diff --git a/tests/auto/qgeoshape/tst_qgeoshape.cpp b/tests/auto/qgeoshape/tst_qgeoshape.cpp index 301018a9..5f094010 100644 --- a/tests/auto/qgeoshape/tst_qgeoshape.cpp +++ b/tests/auto/qgeoshape/tst_qgeoshape.cpp @@ -59,6 +59,7 @@ private slots: void testArea(); void debug_data(); void debug(); + void conversions(); }; void tst_qgeoshape::testArea() @@ -111,5 +112,24 @@ void tst_qgeoshape::debug() QCOMPARE(tst_qgeoshape_debug, debugString); } +void tst_qgeoshape::conversions() +{ + QVariant varShape = QVariant::fromValue(QGeoShape()); + QVariant varRect = QVariant::fromValue(QGeoRectangle( + QGeoCoordinate(1, 1), + QGeoCoordinate(2, 2))); + QVariant varCircle = QVariant::fromValue(QGeoCircle(QGeoCoordinate(3, 3), 1000)); + + QVERIFY(varShape.canConvert<QGeoShape>()); + QVERIFY(varShape.canConvert<QGeoCircle>()); + QVERIFY(varShape.canConvert<QGeoRectangle>()); + QVERIFY(!varRect.canConvert<QGeoCircle>()); + QVERIFY(varRect.canConvert<QGeoRectangle>()); + QVERIFY(varRect.canConvert<QGeoShape>()); + QVERIFY(varCircle.canConvert<QGeoCircle>()); + QVERIFY(!varCircle.canConvert<QGeoRectangle>()); + QVERIFY(varCircle.canConvert<QGeoShape>()); +} + QTEST_MAIN(tst_qgeoshape) #include "tst_qgeoshape.moc" |