Skip to content

Commit b7e6e64

Browse files
committedAug 17, 2012
Merge pull request #202 from homann/issue6194
Fix for #6194
2 parents b37a3b6 + b5a59fd commit b7e6e64

File tree

9 files changed

+99
-31
lines changed

9 files changed

+99
-31
lines changed
 

‎python/core/core.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
%Include qgsrastershaderfunction.sip
7373
%Include qgsrastertransparency.sip
7474
%Include qgsrasterviewport.sip
75-
%Include qgsrect.sip
75+
%Include qgsrectangle.sip
7676
%Include qgsrendercontext.sip
7777
%Include qgsrenderer.sip
7878
%Include qgsrunprocess.sip

‎python/core/qgsrect.sip renamed to ‎python/core/qgsrectangle.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class QgsRectangle
5656
//! Scale the rectangle around its center point
5757
void scale(double, const QgsPoint *c =0);
5858
//! Expand the rectangle to support zoom out scaling
59-
void expand(double, const QgsPoint *c = 0);
59+
// @deprecated use scale instead
60+
void expand(double, const QgsPoint *c = 0) /Deprecated/;
6061
//! return the intersection with the given rectangle
6162
QgsRectangle intersect(const QgsRectangle *rect);
6263
//! returns true when rectangle intersects with other rectangle

‎src/app/qgsidentifyresults.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ void QgsIdentifyResults::zoomToFeature()
866866
{
867867
QgsPoint c = rect.center();
868868
rect = mCanvas->extent();
869-
rect.expand( 0.25, &c );
869+
rect.scale( 0.5, &c );
870870
}
871871

872872
mCanvas->setExtent( rect );

‎src/core/qgsrectangle.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,6 @@ void QgsRectangle::scale( double scaleFactor, const QgsPoint * cp )
106106
ymax = centerY + newHeight / 2.0;
107107
}
108108

109-
void QgsRectangle::expand( double scaleFactor, const QgsPoint * cp )
110-
{
111-
// scale from the center
112-
double centerX, centerY;
113-
if ( cp )
114-
{
115-
centerX = cp->x();
116-
centerY = cp->y();
117-
}
118-
else
119-
{
120-
centerX = xmin + width() / 2;
121-
centerY = ymin + height() / 2;
122-
}
123-
124-
double newWidth = width() * scaleFactor;
125-
double newHeight = height() * scaleFactor;
126-
xmin = centerX - newWidth;
127-
xmax = centerX + newWidth;
128-
ymin = centerY - newHeight;
129-
ymax = centerY + newHeight;
130-
}
131-
132109
QgsRectangle QgsRectangle::intersect( const QgsRectangle * rect ) const
133110
{
134111
QgsRectangle intersection = QgsRectangle();

‎src/core/qgsrectangle.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ class CORE_EXPORT QgsRectangle
7676
//! Center point of the rectangle
7777
QgsPoint center() const;
7878
//! Scale the rectangle around its center point
79-
void scale( double, const QgsPoint *c = 0 );
80-
//! Expand the rectangle to support zoom out scaling
81-
void expand( double, const QgsPoint *c = 0 );
79+
void scale( double scaleFactor, const QgsPoint *c = 0 );
80+
Q_DECL_DEPRECATED void expand( double scaleFactor, const QgsPoint *c = 0 ) { return scale( scaleFactor * 2.0, c ); }
8281
//! return the intersection with the given rectangle
8382
QgsRectangle intersect( const QgsRectangle *rect ) const;
8483
//! returns true when rectangle intersects with other rectangle

‎src/gui/qgsmapcanvas.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer* layer )
723723
// zoom in
724724
QgsPoint c = rect.center();
725725
rect = extent();
726-
rect.expand( 0.25, &c );
726+
rect.scale( 0.5, &c );
727727
}
728728
//zoom to an area
729729
else

‎src/gui/qgsmaptoolzoom.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
128128
{
129129
sf = extent.height() / r.height();
130130
}
131-
r.expand( sf );
131+
sf = sf * 2.0;
132+
r.scale( sf );
132133

133134
QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) );
134135
QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) );

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,4 @@ ADD_QGIS_TEST(dataitemtest testqgsdataitem.cpp)
9999
ADD_QGIS_TEST(composermaptest testqgscomposermap.cpp)
100100
ADD_QGIS_TEST(stylev2test testqgsstylev2.cpp)
101101
ADD_QGIS_TEST(composerhtmltest testqgscomposerhtml.cpp )
102+
ADD_QGIS_TEST(rectangletest testqgsrectangle.cpp)

‎tests/src/core/testqgsrectangle.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/***************************************************************************
2+
testqgsrectangle.cpp
3+
--------------------------------------
4+
Date : Tue 14 Aug 2012
5+
Copyright : (C) 2012 by Magnus Homann
6+
Email : magnus at homann dot se
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include <QtTest>
16+
#include <QObject>
17+
#include <QString>
18+
#include <QObject>
19+
//header for class being tested
20+
#include <qgsrectangle.h>
21+
#include <qgspoint.h>
22+
#include "qgslogger.h"
23+
24+
class TestQgsRectangle: public QObject
25+
{
26+
Q_OBJECT;
27+
private slots:
28+
void manipulate();
29+
void regression6194();
30+
private:
31+
QgsRectangle mRect1;
32+
QgsRectangle mRect2;
33+
QgsRectangle mRect3;
34+
QgsRectangle mRect4;
35+
QgsPoint mPoint1;
36+
QgsPoint mPoint2;
37+
};
38+
39+
void TestQgsRectangle::manipulate()
40+
{
41+
// Set up two intersecting rectangles and normalize
42+
mRect1.set( 4.0, 5.0, 1.0, 2.0 );
43+
mRect2.set( 3.0, 3.0, 7.0, 1.0 );
44+
// Check intersection
45+
QVERIFY( mRect2.intersects( mRect1 ) );
46+
// Create intersection
47+
mRect3 = mRect2.intersect( & mRect1 );
48+
// Check width and height (real numbers, careful)
49+
QCOMPARE( mRect3.width(), 1.0 );
50+
QCOMPARE( mRect3.height(), 1.0 );
51+
// And check that the result is contained in both
52+
QVERIFY( mRect1.contains( mRect3 ) );
53+
QVERIFY( mRect2.contains( mRect3 ) );
54+
QVERIFY( ! mRect2.contains( mRect1 ) );
55+
56+
// Create the union
57+
mRect3.unionRect( mRect1 );
58+
mRect3.unionRect( mRect2 );
59+
// Check union
60+
QVERIFY( mRect3 == QgsRectangle( 1.0, 1.0, 7.0, 5.0 ) );
61+
};
62+
63+
void TestQgsRectangle::regression6194()
64+
{
65+
// 100 wide, 200 high
66+
mRect1 = QgsRectangle( 10.0, 20.0, 110.0, 220.0 );
67+
68+
// 250 wide, 500 high
69+
mRect2.setXMinimum( 10.0 );
70+
mRect2.setYMinimum( 20.0 );
71+
mRect2.setXMaximum( 260.0 );
72+
mRect2.setYMaximum( 520.0 );
73+
74+
// Scale by 2.5, keeping bottom left as is.
75+
mRect1.scale( 2.5, & QgsPoint( 135.0, 270.0 ) );
76+
77+
QVERIFY( mRect2.xMinimum() == mRect1.xMinimum() );
78+
QVERIFY( mRect2.yMinimum() == mRect1.yMinimum() );
79+
QVERIFY( mRect2.xMaximum() == mRect1.xMaximum() );
80+
QVERIFY( mRect2.yMaximum() == mRect1.yMaximum() );
81+
QVERIFY( mRect1 == mRect2 );
82+
};
83+
84+
QTEST_MAIN( TestQgsRectangle )
85+
#include "moc_testqgsrectangle.cxx"
86+
87+
88+
89+

0 commit comments

Comments
 (0)
Please sign in to comment.