Skip to content

Commit

Permalink
add toString and repr for qgsvector
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti authored and nyalldawson committed Jan 15, 2019
1 parent 63a3445 commit c885cd7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
13 changes: 13 additions & 0 deletions python/core/auto_generated/qgsvector.sip.in
Expand Up @@ -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

};


Expand Down
24 changes: 24 additions & 0 deletions src/core/qgsvector.h
Expand Up @@ -16,6 +16,7 @@
#ifndef QGSVECTOR_H
#define QGSVECTOR_H

#include "qgis.h"
#include "qgis_core.h"
#include <QtGlobal>

Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsvector3d.h
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/src/core/testqgsvector.cpp
Expand Up @@ -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 );
Expand Down
9 changes: 8 additions & 1 deletion tests/src/python/test_python_repr.py
Expand Up @@ -18,7 +18,7 @@
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()

Expand Down Expand Up @@ -124,6 +124,13 @@ def testQgsRectangleRepr(self):
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'>")
Expand Down

0 comments on commit c885cd7

Please sign in to comment.