Skip to content
Permalink
Browse files

Fix for scale/extend issue #6194. Added test case.

  • Loading branch information
homann committed Aug 17, 2012
1 parent ed98805 commit 6fd2dccbd72959c1db921224f88fb36d209ddc6d
@@ -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 );
@@ -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();
@@ -77,8 +77,6 @@ class CORE_EXPORT QgsRectangle
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 );
//! return the intersection with the given rectangle
QgsRectangle intersect( const QgsRectangle *rect ) const;
//! returns true when rectangle intersects with other rectangle
@@ -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
@@ -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() ) );
@@ -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)
@@ -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 6fd2dcc

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