Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #202 from homann/issue6194
Fix for #6194
  • Loading branch information
jef-n committed Aug 17, 2012
2 parents b37a3b6 + b5a59fd commit b7e6e64
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 31 deletions.
2 changes: 1 addition & 1 deletion python/core/core.sip
Expand Up @@ -72,7 +72,7 @@
%Include qgsrastershaderfunction.sip
%Include qgsrastertransparency.sip
%Include qgsrasterviewport.sip
%Include qgsrect.sip
%Include qgsrectangle.sip
%Include qgsrendercontext.sip
%Include qgsrenderer.sip
%Include qgsrunprocess.sip
Expand Down
3 changes: 2 additions & 1 deletion python/core/qgsrect.sip → python/core/qgsrectangle.sip
Expand Up @@ -56,7 +56,8 @@ class QgsRectangle
//! Scale the rectangle around its center point
void scale(double, const QgsPoint *c =0);
//! Expand the rectangle to support zoom out scaling
void expand(double, const QgsPoint *c = 0);
// @deprecated use scale instead
void expand(double, const QgsPoint *c = 0) /Deprecated/;
//! return the intersection with the given rectangle
QgsRectangle intersect(const QgsRectangle *rect);
//! returns true when rectangle intersects with other rectangle
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsidentifyresults.cpp
Expand Up @@ -866,7 +866,7 @@ void QgsIdentifyResults::zoomToFeature()
{
QgsPoint c = rect.center();
rect = mCanvas->extent();
rect.expand( 0.25, &c );
rect.scale( 0.5, &c );
}

mCanvas->setExtent( rect );
Expand Down
23 changes: 0 additions & 23 deletions src/core/qgsrectangle.cpp
Expand Up @@ -106,29 +106,6 @@ void QgsRectangle::scale( double scaleFactor, const QgsPoint * cp )
ymax = centerY + newHeight / 2.0;
}

void QgsRectangle::expand( double scaleFactor, const QgsPoint * cp )
{
// scale from the center
double centerX, centerY;
if ( cp )
{
centerX = cp->x();
centerY = cp->y();
}
else
{
centerX = xmin + width() / 2;
centerY = ymin + height() / 2;
}

double newWidth = width() * scaleFactor;
double newHeight = height() * scaleFactor;
xmin = centerX - newWidth;
xmax = centerX + newWidth;
ymin = centerY - newHeight;
ymax = centerY + newHeight;
}

QgsRectangle QgsRectangle::intersect( const QgsRectangle * rect ) const
{
QgsRectangle intersection = QgsRectangle();
Expand Down
5 changes: 2 additions & 3 deletions src/core/qgsrectangle.h
Expand Up @@ -76,9 +76,8 @@ class CORE_EXPORT QgsRectangle
//! Center point of the rectangle
QgsPoint center() const;
//! Scale the rectangle around its center point
void scale( double, const QgsPoint *c = 0 );
//! Expand the rectangle to support zoom out scaling
void expand( double, const QgsPoint *c = 0 );
void scale( double scaleFactor, const QgsPoint *c = 0 );
Q_DECL_DEPRECATED void expand( double scaleFactor, const QgsPoint *c = 0 ) { return scale( scaleFactor * 2.0, c ); }
//! return the intersection with the given rectangle
QgsRectangle intersect( const QgsRectangle *rect ) const;
//! returns true when rectangle intersects with other rectangle
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsmapcanvas.cpp
Expand Up @@ -723,7 +723,7 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer* layer )
// zoom in
QgsPoint c = rect.center();
rect = extent();
rect.expand( 0.25, &c );
rect.scale( 0.5, &c );
}
//zoom to an area
else
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsmaptoolzoom.cpp
Expand Up @@ -128,7 +128,8 @@ void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
{
sf = extent.height() / r.height();
}
r.expand( sf );
sf = sf * 2.0;
r.scale( sf );

QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) );
QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) );
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -99,3 +99,4 @@ ADD_QGIS_TEST(dataitemtest testqgsdataitem.cpp)
ADD_QGIS_TEST(composermaptest testqgscomposermap.cpp)
ADD_QGIS_TEST(stylev2test testqgsstylev2.cpp)
ADD_QGIS_TEST(composerhtmltest testqgscomposerhtml.cpp )
ADD_QGIS_TEST(rectangletest testqgsrectangle.cpp)
89 changes: 89 additions & 0 deletions tests/src/core/testqgsrectangle.cpp
@@ -0,0 +1,89 @@
/***************************************************************************
testqgsrectangle.cpp
--------------------------------------
Date : Tue 14 Aug 2012
Copyright : (C) 2012 by Magnus Homann
Email : magnus at homann dot se
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QtTest>
#include <QObject>
#include <QString>
#include <QObject>
//header for class being tested
#include <qgsrectangle.h>
#include <qgspoint.h>
#include "qgslogger.h"

class TestQgsRectangle: public QObject
{
Q_OBJECT;
private slots:
void manipulate();
void regression6194();
private:
QgsRectangle mRect1;
QgsRectangle mRect2;
QgsRectangle mRect3;
QgsRectangle mRect4;
QgsPoint mPoint1;
QgsPoint mPoint2;
};

void TestQgsRectangle::manipulate()
{
// Set up two intersecting rectangles and normalize
mRect1.set( 4.0, 5.0, 1.0, 2.0 );
mRect2.set( 3.0, 3.0, 7.0, 1.0 );
// Check intersection
QVERIFY( mRect2.intersects( mRect1 ) );
// Create intersection
mRect3 = mRect2.intersect( & mRect1 );
// Check width and height (real numbers, careful)
QCOMPARE( mRect3.width(), 1.0 );
QCOMPARE( mRect3.height(), 1.0 );
// And check that the result is contained in both
QVERIFY( mRect1.contains( mRect3 ) );
QVERIFY( mRect2.contains( mRect3 ) );
QVERIFY( ! mRect2.contains( mRect1 ) );

// Create the union
mRect3.unionRect( mRect1 );
mRect3.unionRect( mRect2 );
// Check union
QVERIFY( mRect3 == QgsRectangle( 1.0, 1.0, 7.0, 5.0 ) );
};

void TestQgsRectangle::regression6194()
{
// 100 wide, 200 high
mRect1 = QgsRectangle( 10.0, 20.0, 110.0, 220.0 );

// 250 wide, 500 high
mRect2.setXMinimum( 10.0 );
mRect2.setYMinimum( 20.0 );
mRect2.setXMaximum( 260.0 );
mRect2.setYMaximum( 520.0 );

// Scale by 2.5, keeping bottom left as is.
mRect1.scale( 2.5, & QgsPoint( 135.0, 270.0 ) );

QVERIFY( mRect2.xMinimum() == mRect1.xMinimum() );
QVERIFY( mRect2.yMinimum() == mRect1.yMinimum() );
QVERIFY( mRect2.xMaximum() == mRect1.xMaximum() );
QVERIFY( mRect2.yMaximum() == mRect1.yMaximum() );
QVERIFY( mRect1 == mRect2 );
};

QTEST_MAIN( TestQgsRectangle )
#include "moc_testqgsrectangle.cxx"




0 comments on commit b7e6e64

Please sign in to comment.