Skip to content

Commit

Permalink
Added conversion between QgsRectangle and QRectF. And tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
homann committed Sep 16, 2012
1 parent 8a347eb commit 883dcab
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
8 changes: 7 additions & 1 deletion python/core/qgsrectangle.sip
Expand Up @@ -16,6 +16,9 @@ class QgsRectangle
QgsRectangle(double xmin=0, double ymin=0, double xmax=0, double ymax=0);
//! Construct a rectangle from two points. The rectangle is normalized after construction.
QgsRectangle(const QgsPoint & p1, const QgsPoint & p2);
//! Construct a rectangle from a QRectF. The rectangle is normalized after construction.
//@note added in 2.0
QgsRectangle(const QRectF & qRectF );
//! Copy constructor
QgsRectangle(const QgsRectangle &other);
//! Destructor
Expand Down Expand Up @@ -77,8 +80,11 @@ class QgsRectangle
//! returns string representation in Wkt form
QString asWktCoordinates() const;
//! returns string representation as WKT Polygon
//@note added om 2.0
//@note added in 2.0
QString asWktPolygon() const;
//! returns a QRectF with same coordinates.
//@note added in 2.0
QRectF QgsRectangle::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
Expand Down
16 changes: 16 additions & 0 deletions src/core/qgsrectangle.cpp
Expand Up @@ -18,6 +18,7 @@
#include <algorithm>
#include <cmath>
#include <limits>
#include <QRectF>
#include <QString>
#include <QTextStream>
#include <qnumeric.h>
Expand All @@ -37,6 +38,14 @@ QgsRectangle::QgsRectangle( QgsPoint const & p1, QgsPoint const & p2 )
set( p1, p2 );
}

QgsRectangle::QgsRectangle( QRectF const & qRectF )
{
xmin = qRectF.topLeft().x();
ymin = qRectF.topLeft().y();
xmax = qRectF.bottomRight().x();
ymax = qRectF.bottomRight().y();
}

QgsRectangle::QgsRectangle( const QgsRectangle &r )
{
xmin = r.xMinimum();
Expand Down Expand Up @@ -203,6 +212,13 @@ QString QgsRectangle::asWktPolygon() const
return rep;
}

//! returns a QRectF with same coordinates.
//@note added in 2.0
QRectF QgsRectangle::toRectF() const
{
return QRectF( (qreal)xmin, (qreal)ymin, (qreal)xmax - xmin, (qreal)ymax - ymin );
}

// Return a string representation of the rectangle with automatic or high precision
QString QgsRectangle::toString( bool automaticPrecision ) const
{
Expand Down
10 changes: 8 additions & 2 deletions src/core/qgsrectangle.h
Expand Up @@ -21,7 +21,7 @@
#include <iosfwd>

class QString;

class QRectF;
#include "qgspoint.h"


Expand All @@ -38,6 +38,9 @@ class CORE_EXPORT QgsRectangle
QgsRectangle( double xmin = 0, double ymin = 0, double xmax = 0, double ymax = 0 );
//! Construct a rectangle from two points. The rectangle is normalized after construction.
QgsRectangle( QgsPoint const & p1, QgsPoint const & p2 );
//! Construct a rectangle from a QRectF. The rectangle is normalized after construction.
//@note added in 2.0
QgsRectangle( const QRectF & qRectF );
//! Copy constructor
QgsRectangle( const QgsRectangle &other );
//! Destructor
Expand Down Expand Up @@ -98,8 +101,11 @@ class CORE_EXPORT QgsRectangle
//! returns string representation in Wkt form
QString asWktCoordinates() const;
//! returns string representation as WKT Polygon
//@note added om 2.0
//@note added in 2.0
QString asWktPolygon() const;
//! returns a QRectF with same coordinates.
//@note added in 2.0
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
Expand Down
9 changes: 9 additions & 0 deletions tests/src/core/testqgsrectangle.cpp
Expand Up @@ -59,6 +59,15 @@ void TestQgsRectangle::regression6194()
// 100 wide, 200 high
QgsRectangle rect1 = QgsRectangle( 10.0, 20.0, 110.0, 220.0 );

// Test conversion to QRectF and back
QRectF qRectF = rect1.toRectF();
QCOMPARE( qRectF.width(), 100.0 );
QCOMPARE( qRectF.height(), 200.0 );
QCOMPARE( qRectF.x(), 10.0 );
QCOMPARE( qRectF.y(), 20.0 );
QgsRectangle rect4 = QgsRectangle( qRectF );
QCOMPARE( rect4.toString( 2 ), QString( "10.00,20.00 : 110.00,220.00" ) );

// 250 wide, 500 high
QgsRectangle rect2;
rect2.setXMinimum( 10.0 );
Expand Down

0 comments on commit 883dcab

Please sign in to comment.