Skip to content
Permalink
Browse files

Added conversion between QgsRectangle and QRectF. And tests.

  • Loading branch information
homann committed Sep 16, 2012
1 parent 8a347eb commit 883dcab1fedb0f3f4838ccc165909aac93b66225
Showing with 40 additions and 3 deletions.
  1. +7 −1 python/core/qgsrectangle.sip
  2. +16 −0 src/core/qgsrectangle.cpp
  3. +8 −2 src/core/qgsrectangle.h
  4. +9 −0 tests/src/core/testqgsrectangle.cpp
@@ -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
@@ -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
@@ -18,6 +18,7 @@
#include <algorithm>
#include <cmath>
#include <limits>
#include <QRectF>
#include <QString>
#include <QTextStream>
#include <qnumeric.h>
@@ -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();
@@ -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
{
@@ -21,7 +21,7 @@
#include <iosfwd>

class QString;

class QRectF;
#include "qgspoint.h"


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

0 comments on commit 883dcab

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