From 45f2cd5583ad4115bd8cbd687729d465910696ca Mon Sep 17 00:00:00 2001 From: jdugge Date: Tue, 21 Mar 2017 06:58:39 +0100 Subject: [PATCH] Fix duplicate QgsRectangle.toString methods Condenses the 2 duplicate methods to a single method which is usable from the Python bindings. --- python/core/qgsrectangle.sip | 10 ++++--- src/core/qgsrectangle.cpp | 23 ++++++-------- src/core/qgsrectangle.h | 11 ++++--- tests/src/python/test_qgsrectangle.py | 43 ++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 23 deletions(-) diff --git a/python/core/qgsrectangle.sip b/python/core/qgsrectangle.sip index d21e8f61790b..12c7703754cb 100644 --- a/python/core/qgsrectangle.sip +++ b/python/core/qgsrectangle.sip @@ -90,10 +90,12 @@ class QgsRectangle QString asWktPolygon() const; //! returns a QRectF with same coordinates. QRectF toRectF() const; - //! returns string representation of form xmin,ymin xmax,ymax - QString toString( bool automaticPrecision = false ) const; - //! overloaded toString that allows precision of numbers to be set - QString toString( int precision ) const; + /** + * returns a string representation of form xmin,ymin : xmax,ymax + * Coordinates will be truncated to the specified precision. + * If the specified precision is less than 0, a suitable minimum precision is used. + */ + QString toString( int precision = 16 ) const; //! returns rectangle as a polygon QString asPolygon() const; /** Comparison operator diff --git a/src/core/qgsrectangle.cpp b/src/core/qgsrectangle.cpp index 881427e07c22..a87e89d7e7b6 100644 --- a/src/core/qgsrectangle.cpp +++ b/src/core/qgsrectangle.cpp @@ -265,30 +265,25 @@ QRectF QgsRectangle::toRectF() const return QRectF( static_cast< qreal >( xmin ), static_cast< qreal >( ymin ), static_cast< qreal >( xmax - xmin ), static_cast< qreal >( ymax - ymin ) ); } -// Return a string representation of the rectangle with automatic or high precision -QString QgsRectangle::toString( bool automaticPrecision ) const +// Returns a string representation of form xmin,ymin : xmax,ymax. Coordinates will be truncated +// to the specified \a precision. If \a precision is less than 0 then a suitable minimum precision +// will be automatically calculated. +QString QgsRectangle::toString( int precision ) const { - if ( automaticPrecision ) + QString rep; + + if ( precision < 0 ) { - int precision = 0; - if ( ( width() < 1 || height() < 1 ) && ( width() > 0 && height() > 0 ) ) + precision = 0; + if ( ( width() < 10 || height() < 10 ) && ( width() > 0 && height() > 0 ) ) { precision = static_cast( ceil( -1.0 * log10( qMin( width(), height() ) ) ) ) + 1; // sanity check if ( precision > 20 ) precision = 20; } - return toString( precision ); } - else - return toString( 16 ); -} -// overloaded version of above fn to allow precision to be set -// Return a string representation of the rectangle with high precision -QString QgsRectangle::toString( int precision ) const -{ - QString rep; if ( isEmpty() ) rep = QStringLiteral( "Empty" ); else diff --git a/src/core/qgsrectangle.h b/src/core/qgsrectangle.h index 556f0a5d984d..23136321f8c3 100644 --- a/src/core/qgsrectangle.h +++ b/src/core/qgsrectangle.h @@ -115,10 +115,13 @@ class CORE_EXPORT QgsRectangle QString asWktPolygon() const; //! returns a QRectF with same coordinates. QRectF toRectF() const; - //! returns string representation of form xmin,ymin xmax,ymax - QString toString( bool automaticPrecision = false ) const; - //! overloaded toString that allows precision of numbers to be set - QString toString( int precision ) const; + + /** + * returns a string representation of form xmin,ymin : xmax,ymax + * Coordinates will be truncated to the specified precision. + * If the specified precision is less than 0, a suitable minimum precision is used. + */ + QString toString( int precision = 16 ) const; //! returns rectangle as a polygon QString asPolygon() const; diff --git a/tests/src/python/test_qgsrectangle.py b/tests/src/python/test_qgsrectangle.py index b87f53d1378f..4e432fb8b1e7 100644 --- a/tests/src/python/test_qgsrectangle.py +++ b/tests/src/python/test_qgsrectangle.py @@ -151,7 +151,8 @@ def testUnion(self): assert rect1.contains(rect2), myMessage print((rect1.toString())) - assert rect1 == QgsRectangle(0.0, 0.0, 7.0, 7.0), 'Wrong combine with rectangle result' + assert rect1 == QgsRectangle( + 0.0, 0.0, 7.0, 7.0), 'Wrong combine with rectangle result' rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0) rect1.combineExtentWith(6.0, 2.0) @@ -196,6 +197,46 @@ def testAsWktPolygon(self): (myExpectedWkt, myWkt)) assert compareWkt(myWkt, myExpectedWkt), myMessage + def testToString(self): + """Test the different string representations""" + rect = QgsRectangle(0, 0.1, 0.2, 0.3) + myExpectedString = '0.0000000000000000,0.1000000000000000 : 0.2000000000000000,0.3000000000000000' + myString = rect.toString() + myMessage = ('Expected: %s\nGot: %s\n' % + (myExpectedString, myString)) + assert myString == myExpectedString, myMessage + + myExpectedString = '0,0 : 0,0' + myString = rect.toString(0) + myMessage = ('Expected: %s\nGot: %s\n' % + (myExpectedString, myString)) + assert myString == myExpectedString, myMessage + + myExpectedString = '0.00,0.10 : 0.20,0.30' + myString = rect.toString(2) + myMessage = ('Expected: %s\nGot: %s\n' % + (myExpectedString, myString)) + assert myString == myExpectedString, myMessage + + myExpectedString = '0.0,0.1 : 0.2,0.3' + myString = rect.toString(1) + myMessage = ('Expected: %s\nGot: %s\n' % + (myExpectedString, myString)) + assert myString == myExpectedString, myMessage + + myExpectedString = '0.00,0.10 : 0.20,0.30' + myString = rect.toString(-1) + myMessage = ('Expected: %s\nGot: %s\n' % + (myExpectedString, myString)) + assert myString == myExpectedString, myMessage + + rect = QgsRectangle(5000000.01111, -0.3, 5000000.44111, 99.8) + myExpectedString = '5000000.01,-0.30 : 5000000.44,99.80' + myString = rect.toString(-1) + myMessage = ('Expected: %s\nGot: %s\n' % + (myExpectedString, myString)) + assert myString == myExpectedString, myMessage + if __name__ == '__main__': unittest.main()