summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-07-15 17:13:33 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-07-19 08:11:40 +0000
commitb683cbe594791f886cdd8612e60be93b77282a4b (patch)
tree87e1acf8b9369b2aa87e0267d5b1af1d073daf31
parentc5738389abfbd07df03c37be5ec8e1d8bcdb66ac (diff)
QGeoShape: fix serialization of QGeoPolygon and QGeoPath
There were two problems: 1. The deserialization method was still assuming that QList uses int to index the elements, so it tried to extract the polygon and path size as int, while it was serialized as qsizetype. 2. The QGeoPath didn't serialize and deserialize its width - it was simply lost. This patch fixes both problems and adds some unit-tests to cover these cases. Change-Id: If0ac87731b4481fde6b91e71fb121b3e916b0bfe Reviewed-by: Alex Blasche <alexander.blasche@qt.io> (cherry picked from commit b7fa0a29ff70dd3316941cf9ec9936d7c1a8434c) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/positioning/qgeoshape.cpp13
-rw-r--r--tests/auto/qgeoshape/tst_qgeoshape.cpp71
2 files changed, 79 insertions, 5 deletions
diff --git a/src/positioning/qgeoshape.cpp b/src/positioning/qgeoshape.cpp
index 859df96e..d4daf813 100644
--- a/src/positioning/qgeoshape.cpp
+++ b/src/positioning/qgeoshape.cpp
@@ -357,6 +357,7 @@ QDataStream &operator<<(QDataStream &stream, const QGeoShape &shape)
}
case QGeoShape::PathType: {
QGeoPath p = shape;
+ stream << p.width();
stream << p.path().size();
for (const auto &c: p.path())
stream << c;
@@ -400,21 +401,23 @@ QDataStream &operator>>(QDataStream &stream, QGeoShape &shape)
case QGeoShape::PathType: {
QList<QGeoCoordinate> l;
QGeoCoordinate c;
- int sz;
+ qreal width;
+ stream >> width;
+ qsizetype sz;
stream >> sz;
- for (int i = 0; i < sz; i++) {
+ for (qsizetype i = 0; i < sz; i++) {
stream >> c;
l.append(c);
}
- shape = QGeoPath(l);
+ shape = QGeoPath(l, width);
break;
}
case QGeoShape::PolygonType: {
QList<QGeoCoordinate> l;
QGeoCoordinate c;
- int sz;
+ qsizetype sz;
stream >> sz;
- for (int i = 0; i < sz; i++) {
+ for (qsizetype i = 0; i < sz; i++) {
stream >> c;
l.append(c);
}
diff --git a/tests/auto/qgeoshape/tst_qgeoshape.cpp b/tests/auto/qgeoshape/tst_qgeoshape.cpp
index 72611ff6..77f36a28 100644
--- a/tests/auto/qgeoshape/tst_qgeoshape.cpp
+++ b/tests/auto/qgeoshape/tst_qgeoshape.cpp
@@ -31,6 +31,8 @@
#include <QtCore/QDebug>
#include <QtPositioning/QGeoRectangle>
#include <QtPositioning/QGeoCircle>
+#include <QtPositioning/QGeoPath>
+#include <QtPositioning/QGeoPolygon>
QString tst_qgeoshape_debug;
@@ -55,6 +57,7 @@ private slots:
void debug_data();
void debug();
void conversions();
+ void serialization();
};
void tst_qgeoshape::testArea()
@@ -126,5 +129,73 @@ void tst_qgeoshape::conversions()
QVERIFY(varCircle.canConvert<QGeoShape>());
}
+void tst_qgeoshape::serialization()
+{
+ QByteArray data;
+ // empty shape
+ {
+ QGeoShape shape;
+ QDataStream writeStream(&data, QDataStream::WriteOnly);
+ writeStream << shape;
+
+ QGeoShape otherShape;
+ QDataStream readStream(&data, QDataStream::ReadOnly);
+ readStream >> otherShape;
+
+ QCOMPARE(otherShape, shape);
+ }
+ // circle
+ {
+ QGeoCircle circle(QGeoCoordinate(1.1, 2.2), 10.5);
+ QDataStream writeStream(&data, QDataStream::WriteOnly);
+ writeStream << circle;
+
+ QGeoShape otherCircle;
+ QDataStream readStream(&data, QDataStream::ReadOnly);
+ readStream >> otherCircle;
+
+ QCOMPARE(otherCircle, circle);
+ }
+ // rectangle
+ {
+ QGeoRectangle rectangle(QGeoCoordinate(30, 160), QGeoCoordinate(-30, 170));
+ QDataStream writeStream(&data, QDataStream::WriteOnly);
+ writeStream << rectangle;
+
+ QGeoShape otherRectangle;
+ QDataStream readStream(&data, QDataStream::ReadOnly);
+ readStream >> otherRectangle;
+
+ QCOMPARE(otherRectangle, rectangle);
+ }
+ // polygon
+ {
+ QGeoPolygon polygon({ QGeoCoordinate(30, 160),
+ QGeoCoordinate(0, 170),
+ QGeoCoordinate(-30, 160) });
+ QDataStream writeStream(&data, QDataStream::WriteOnly);
+ writeStream << polygon;
+
+ QGeoShape otherPolygon;
+ QDataStream readStream(&data, QDataStream::ReadOnly);
+ readStream >> otherPolygon;
+
+ QCOMPARE(otherPolygon, polygon);
+ }
+ // path
+ {
+ QGeoPath path({ QGeoCoordinate(30, 160), QGeoCoordinate(0, 170),
+ QGeoCoordinate(-30, 180) }, 0.5);
+ QDataStream writeStream(&data, QDataStream::WriteOnly);
+ writeStream << path;
+
+ QGeoShape otherPath;
+ QDataStream readStream(&data, QDataStream::ReadOnly);
+ readStream >> otherPath;
+
+ QCOMPARE(otherPath, path);
+ }
+}
+
QTEST_MAIN(tst_qgeoshape)
#include "tst_qgeoshape.moc"