Skip to content

Commit c885cd7

Browse files
lbartolettinyalldawson
authored andcommittedJan 15, 2019
add toString and repr for qgsvector
1 parent 63a3445 commit c885cd7

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed
 

‎python/core/auto_generated/qgsvector.sip.in

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ Will throw a QgsException if called on a vector with length of 0.
119119

120120
bool operator!=( QgsVector other ) const;
121121

122+
123+
QString toString( int precision = 17 ) const;
124+
%Docstring
125+
Returns a string representation of the vector.
126+
Members will be truncated to the specified ``precision``.
127+
%End
128+
129+
SIP_PYOBJECT __repr__();
130+
%MethodCode
131+
QString str = QStringLiteral( "<QgsVector: %1>" ).arg( sipCpp->toString() );
132+
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
133+
%End
134+
122135
};
123136

124137

‎src/core/qgsvector.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef QGSVECTOR_H
1717
#define QGSVECTOR_H
1818

19+
#include "qgis.h"
1920
#include "qgis_core.h"
2021
#include <QtGlobal>
2122

@@ -157,6 +158,29 @@ class CORE_EXPORT QgsVector
157158
//! Inequality operator
158159
bool operator!=( QgsVector other ) const;
159160

161+
162+
/**
163+
* Returns a string representation of the vector.
164+
* Members will be truncated to the specified \a precision.
165+
*/
166+
QString toString( int precision = 17 ) const
167+
{
168+
QString str = "Vector (";
169+
str += qgsDoubleToString( mX, precision );
170+
str += ", ";
171+
str += qgsDoubleToString( mY, precision );
172+
str += ')';
173+
return str;
174+
}
175+
176+
#ifdef SIP_RUN
177+
SIP_PYOBJECT __repr__();
178+
% MethodCode
179+
QString str = QStringLiteral( "<QgsVector: %1>" ).arg( sipCpp->toString() );
180+
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
181+
% End
182+
#endif
183+
160184
private:
161185
double mX = 0.0, mY = 0.0;
162186

‎src/core/qgsvector3d.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ class CORE_EXPORT QgsVector3D
150150
*/
151151
QString toString( int precision = 17 ) const
152152
{
153-
QString str = "QgsVector3D (";
153+
QString str = "Vector3D (";
154154
str += qgsDoubleToString( mX, precision );
155-
str += ' ';
155+
str += ", ";
156156
str += qgsDoubleToString( mY, precision );
157-
str += ' ';
157+
str += ", ";
158158
str += qgsDoubleToString( mZ, precision );
159159
str += ')';
160160
return str;

‎tests/src/core/testqgsvector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ void TestQgsVector::cleanupTestCase()
4848
void TestQgsVector::vector3d()
4949
{
5050
//string
51-
QCOMPARE( QgsVector3D().toString(), QString( "QgsVector3D (0 0 0)" ) );
52-
QCOMPARE( QgsVector3D( 0, 1, 2 ).toString(), QString( "QgsVector3D (0 1 2)" ) );
53-
QCOMPARE( QgsVector3D( 0.12, 1.234, 2.3456789 ).toString( 1 ), QString( "QgsVector3D (0.1 1.2 2.3)" ) );
51+
QCOMPARE( QgsVector3D().toString(), QString( "Vector3D (0, 0, 0)" ) );
52+
QCOMPARE( QgsVector3D( 0, 1, 2 ).toString(), QString( "Vector3D (0, 1, 2)" ) );
53+
QCOMPARE( QgsVector3D( 0.12, 1.234, 2.3456789 ).toString( 1 ), QString( "Vector3D (0.1, 1.2, 2.3)" ) );
5454

5555
QgsVector3D p0( 0.0, 0.0, 0.0 );
5656
QgsVector3D p1( 1.0, 2.0, 3.0 );

‎tests/src/python/test_python_repr.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from qgis.testing import unittest, start_app
1919
from qgis.core import QgsGeometry, QgsPoint, QgsPointXY, QgsCircle, QgsCircularString, QgsCompoundCurve,\
2020
QgsCurvePolygon, QgsEllipse, QgsLineString, QgsMultiCurve, QgsRectangle, QgsExpression, QgsField, QgsError,\
21-
QgsMimeDataUtils
21+
QgsMimeDataUtils, QgsVector, QgsVector3D
2222

2323
start_app()
2424

@@ -124,6 +124,13 @@ def testQgsRectangleRepr(self):
124124
r = QgsRectangle(1, 2, 3, 4)
125125
self.assertEqual(r.__repr__(), '<QgsRectangle: 1 2, 3 4>')
126126

127+
def testQgsVector(self):
128+
v = QgsVector(1, 2)
129+
self.assertEqual(v.__repr__(), '<QgsVector: Vector (1, 2)>')
130+
131+
v = QgsVector3D(1, 2, 3)
132+
self.assertEqual(v.__repr__(), '<QgsVector3D: Vector3D (1, 2, 3)>')
133+
127134
def testQgsExpressionRepr(self):
128135
e = QgsExpression('my expression')
129136
self.assertEqual(e.__repr__(), "<QgsExpression: 'my expression'>")

0 commit comments

Comments
 (0)
Please sign in to comment.