From c885cd7267afc0cf0670eb97b6769f9aba8d4097 Mon Sep 17 00:00:00 2001 From: lbartoletti Date: Mon, 14 Jan 2019 22:39:43 +0100 Subject: [PATCH] add toString and repr for qgsvector --- python/core/auto_generated/qgsvector.sip.in | 13 +++++++++++ src/core/qgsvector.h | 24 +++++++++++++++++++++ src/core/qgsvector3d.h | 6 +++--- tests/src/core/testqgsvector.cpp | 6 +++--- tests/src/python/test_python_repr.py | 9 +++++++- 5 files changed, 51 insertions(+), 7 deletions(-) diff --git a/python/core/auto_generated/qgsvector.sip.in b/python/core/auto_generated/qgsvector.sip.in index df9e2e6870e..0ebac5a023f 100644 --- a/python/core/auto_generated/qgsvector.sip.in +++ b/python/core/auto_generated/qgsvector.sip.in @@ -119,6 +119,19 @@ Will throw a QgsException if called on a vector with length of 0. bool operator!=( QgsVector other ) const; + + QString toString( int precision = 17 ) const; +%Docstring +Returns a string representation of the vector. +Members will be truncated to the specified ``precision``. +%End + + SIP_PYOBJECT __repr__(); +%MethodCode + QString str = QStringLiteral( "" ).arg( sipCpp->toString() ); + sipRes = PyUnicode_FromString( str.toUtf8().constData() ); +%End + }; diff --git a/src/core/qgsvector.h b/src/core/qgsvector.h index c03c1f1eb87..a48788cfd20 100644 --- a/src/core/qgsvector.h +++ b/src/core/qgsvector.h @@ -16,6 +16,7 @@ #ifndef QGSVECTOR_H #define QGSVECTOR_H +#include "qgis.h" #include "qgis_core.h" #include @@ -157,6 +158,29 @@ class CORE_EXPORT QgsVector //! Inequality operator bool operator!=( QgsVector other ) const; + + /** + * Returns a string representation of the vector. + * Members will be truncated to the specified \a precision. + */ + QString toString( int precision = 17 ) const + { + QString str = "Vector ("; + str += qgsDoubleToString( mX, precision ); + str += ", "; + str += qgsDoubleToString( mY, precision ); + str += ')'; + return str; + } + +#ifdef SIP_RUN + SIP_PYOBJECT __repr__(); + % MethodCode + QString str = QStringLiteral( "" ).arg( sipCpp->toString() ); + sipRes = PyUnicode_FromString( str.toUtf8().constData() ); + % End +#endif + private: double mX = 0.0, mY = 0.0; diff --git a/src/core/qgsvector3d.h b/src/core/qgsvector3d.h index f050f396574..28e8a813809 100644 --- a/src/core/qgsvector3d.h +++ b/src/core/qgsvector3d.h @@ -150,11 +150,11 @@ class CORE_EXPORT QgsVector3D */ QString toString( int precision = 17 ) const { - QString str = "QgsVector3D ("; + QString str = "Vector3D ("; str += qgsDoubleToString( mX, precision ); - str += ' '; + str += ", "; str += qgsDoubleToString( mY, precision ); - str += ' '; + str += ", "; str += qgsDoubleToString( mZ, precision ); str += ')'; return str; diff --git a/tests/src/core/testqgsvector.cpp b/tests/src/core/testqgsvector.cpp index 7d32ea924e0..4dbb0cfd354 100644 --- a/tests/src/core/testqgsvector.cpp +++ b/tests/src/core/testqgsvector.cpp @@ -48,9 +48,9 @@ void TestQgsVector::cleanupTestCase() void TestQgsVector::vector3d() { //string - QCOMPARE( QgsVector3D().toString(), QString( "QgsVector3D (0 0 0)" ) ); - QCOMPARE( QgsVector3D( 0, 1, 2 ).toString(), QString( "QgsVector3D (0 1 2)" ) ); - QCOMPARE( QgsVector3D( 0.12, 1.234, 2.3456789 ).toString( 1 ), QString( "QgsVector3D (0.1 1.2 2.3)" ) ); + QCOMPARE( QgsVector3D().toString(), QString( "Vector3D (0, 0, 0)" ) ); + QCOMPARE( QgsVector3D( 0, 1, 2 ).toString(), QString( "Vector3D (0, 1, 2)" ) ); + QCOMPARE( QgsVector3D( 0.12, 1.234, 2.3456789 ).toString( 1 ), QString( "Vector3D (0.1, 1.2, 2.3)" ) ); QgsVector3D p0( 0.0, 0.0, 0.0 ); QgsVector3D p1( 1.0, 2.0, 3.0 ); diff --git a/tests/src/python/test_python_repr.py b/tests/src/python/test_python_repr.py index fd87aaa7421..880301846af 100644 --- a/tests/src/python/test_python_repr.py +++ b/tests/src/python/test_python_repr.py @@ -18,7 +18,7 @@ from PyQt5.QtCore import QVariant from qgis.testing import unittest, start_app from qgis.core import QgsGeometry, QgsPoint, QgsPointXY, QgsCircle, QgsCircularString, QgsCompoundCurve,\ QgsCurvePolygon, QgsEllipse, QgsLineString, QgsMultiCurve, QgsRectangle, QgsExpression, QgsField, QgsError,\ - QgsMimeDataUtils + QgsMimeDataUtils, QgsVector, QgsVector3D start_app() @@ -124,6 +124,13 @@ class TestPython__repr__(unittest.TestCase): r = QgsRectangle(1, 2, 3, 4) self.assertEqual(r.__repr__(), '') + def testQgsVector(self): + v = QgsVector(1, 2) + self.assertEqual(v.__repr__(), '') + + v = QgsVector3D(1, 2, 3) + self.assertEqual(v.__repr__(), '') + def testQgsExpressionRepr(self): e = QgsExpression('my expression') self.assertEqual(e.__repr__(), "")