add toString and repr for qgsvector

This commit is contained in:
lbartoletti 2019-01-14 22:39:43 +01:00 committed by Nyall Dawson
parent 63a3445521
commit c885cd7267
5 changed files with 51 additions and 7 deletions

View File

@ -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( "<QgsVector: %1>" ).arg( sipCpp->toString() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End
};

View File

@ -16,6 +16,7 @@
#ifndef QGSVECTOR_H
#define QGSVECTOR_H
#include "qgis.h"
#include "qgis_core.h"
#include <QtGlobal>
@ -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( "<QgsVector: %1>" ).arg( sipCpp->toString() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
private:
double mX = 0.0, mY = 0.0;

View File

@ -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;

View File

@ -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 );

View File

@ -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__(), '<QgsRectangle: 1 2, 3 4>')
def testQgsVector(self):
v = QgsVector(1, 2)
self.assertEqual(v.__repr__(), '<QgsVector: Vector (1, 2)>')
v = QgsVector3D(1, 2, 3)
self.assertEqual(v.__repr__(), '<QgsVector3D: Vector3D (1, 2, 3)>')
def testQgsExpressionRepr(self):
e = QgsExpression('my expression')
self.assertEqual(e.__repr__(), "<QgsExpression: 'my expression'>")