Skip to content

Commit 883dcab

Browse files
committed
Added conversion between QgsRectangle and QRectF. And tests.
1 parent 8a347eb commit 883dcab

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

python/core/qgsrectangle.sip

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class QgsRectangle
1616
QgsRectangle(double xmin=0, double ymin=0, double xmax=0, double ymax=0);
1717
//! Construct a rectangle from two points. The rectangle is normalized after construction.
1818
QgsRectangle(const QgsPoint & p1, const QgsPoint & p2);
19+
//! Construct a rectangle from a QRectF. The rectangle is normalized after construction.
20+
//@note added in 2.0
21+
QgsRectangle(const QRectF & qRectF );
1922
//! Copy constructor
2023
QgsRectangle(const QgsRectangle &other);
2124
//! Destructor
@@ -77,8 +80,11 @@ class QgsRectangle
7780
//! returns string representation in Wkt form
7881
QString asWktCoordinates() const;
7982
//! returns string representation as WKT Polygon
80-
//@note added om 2.0
83+
//@note added in 2.0
8184
QString asWktPolygon() const;
85+
//! returns a QRectF with same coordinates.
86+
//@note added in 2.0
87+
QRectF QgsRectangle::toRectF() const;
8288
//! returns string representation of form xmin,ymin xmax,ymax
8389
QString toString(bool automaticPrecision = false) const;
8490
//! overloaded toString that allows precision of numbers to be set

src/core/qgsrectangle.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <algorithm>
1919
#include <cmath>
2020
#include <limits>
21+
#include <QRectF>
2122
#include <QString>
2223
#include <QTextStream>
2324
#include <qnumeric.h>
@@ -37,6 +38,14 @@ QgsRectangle::QgsRectangle( QgsPoint const & p1, QgsPoint const & p2 )
3738
set( p1, p2 );
3839
}
3940

41+
QgsRectangle::QgsRectangle( QRectF const & qRectF )
42+
{
43+
xmin = qRectF.topLeft().x();
44+
ymin = qRectF.topLeft().y();
45+
xmax = qRectF.bottomRight().x();
46+
ymax = qRectF.bottomRight().y();
47+
}
48+
4049
QgsRectangle::QgsRectangle( const QgsRectangle &r )
4150
{
4251
xmin = r.xMinimum();
@@ -203,6 +212,13 @@ QString QgsRectangle::asWktPolygon() const
203212
return rep;
204213
}
205214

215+
//! returns a QRectF with same coordinates.
216+
//@note added in 2.0
217+
QRectF QgsRectangle::toRectF() const
218+
{
219+
return QRectF( (qreal)xmin, (qreal)ymin, (qreal)xmax - xmin, (qreal)ymax - ymin );
220+
}
221+
206222
// Return a string representation of the rectangle with automatic or high precision
207223
QString QgsRectangle::toString( bool automaticPrecision ) const
208224
{

src/core/qgsrectangle.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <iosfwd>
2222

2323
class QString;
24-
24+
class QRectF;
2525
#include "qgspoint.h"
2626

2727

@@ -38,6 +38,9 @@ class CORE_EXPORT QgsRectangle
3838
QgsRectangle( double xmin = 0, double ymin = 0, double xmax = 0, double ymax = 0 );
3939
//! Construct a rectangle from two points. The rectangle is normalized after construction.
4040
QgsRectangle( QgsPoint const & p1, QgsPoint const & p2 );
41+
//! Construct a rectangle from a QRectF. The rectangle is normalized after construction.
42+
//@note added in 2.0
43+
QgsRectangle( const QRectF & qRectF );
4144
//! Copy constructor
4245
QgsRectangle( const QgsRectangle &other );
4346
//! Destructor
@@ -98,8 +101,11 @@ class CORE_EXPORT QgsRectangle
98101
//! returns string representation in Wkt form
99102
QString asWktCoordinates() const;
100103
//! returns string representation as WKT Polygon
101-
//@note added om 2.0
104+
//@note added in 2.0
102105
QString asWktPolygon() const;
106+
//! returns a QRectF with same coordinates.
107+
//@note added in 2.0
108+
QRectF toRectF() const;
103109
//! returns string representation of form xmin,ymin xmax,ymax
104110
QString toString( bool automaticPrecision = false ) const;
105111
//! overloaded toString that allows precision of numbers to be set

tests/src/core/testqgsrectangle.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ void TestQgsRectangle::regression6194()
5959
// 100 wide, 200 high
6060
QgsRectangle rect1 = QgsRectangle( 10.0, 20.0, 110.0, 220.0 );
6161

62+
// Test conversion to QRectF and back
63+
QRectF qRectF = rect1.toRectF();
64+
QCOMPARE( qRectF.width(), 100.0 );
65+
QCOMPARE( qRectF.height(), 200.0 );
66+
QCOMPARE( qRectF.x(), 10.0 );
67+
QCOMPARE( qRectF.y(), 20.0 );
68+
QgsRectangle rect4 = QgsRectangle( qRectF );
69+
QCOMPARE( rect4.toString( 2 ), QString( "10.00,20.00 : 110.00,220.00" ) );
70+
6271
// 250 wide, 500 high
6372
QgsRectangle rect2;
6473
rect2.setXMinimum( 10.0 );

0 commit comments

Comments
 (0)