Skip to content
Permalink
Browse files

add toString and repr for qgsvector

  • Loading branch information
lbartoletti authored and nyalldawson committed Jan 15, 2019
1 parent 63a3445 commit c885cd7267afc0cf0670eb97b6769f9aba8d4097
@@ -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

};


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

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

@@ -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'>")

0 comments on commit c885cd7

Please sign in to comment.
You can’t perform that action at this time.