Skip to content

Commit 077eb6c

Browse files
author
Sandro Santilli
committedJan 5, 2015
Simplify zoom-by-rect handling, to set zoom factor and center
Basically avoids using the now loosly defined "extent" concept. Enhances zooming experience with rotated maps.
1 parent 0afdede commit 077eb6c

File tree

1 file changed

+13
-42
lines changed

1 file changed

+13
-42
lines changed
 

‎src/gui/qgsmaptoolzoom.cpp

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -103,48 +103,19 @@ void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
103103
mZoomRect.setRight( e->pos().x() );
104104
mZoomRect.setBottom( e->pos().y() );
105105

106-
const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform();
107-
108-
// set the extent to the zoomBox
109-
QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() );
110-
QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right(), mZoomRect.top() );
111-
112-
QgsRectangle r;
113-
r.setXMinimum( ll.x() );
114-
r.setYMinimum( ll.y() );
115-
r.setXMaximum( ur.x() );
116-
r.setYMaximum( ur.y() );
117-
r.normalize();
118-
119-
// prevent zooming to an empty extent
120-
if ( r.width() == 0 || r.height() == 0 )
121-
{
122-
return;
123-
}
124-
125-
if ( mZoomOut )
126-
{
127-
QgsPoint cer = r.center();
128-
QgsRectangle extent = mCanvas->extent();
129-
130-
double sf;
131-
if ( mZoomRect.width() > mZoomRect.height() )
132-
{
133-
sf = extent.width() / r.width();
134-
}
135-
else
136-
{
137-
sf = extent.height() / r.height();
138-
}
139-
sf = sf * 2.0;
140-
r.scale( sf );
141-
142-
QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) );
143-
QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) );
144-
145-
}
146-
147-
mCanvas->setExtent( r );
106+
// set center and zoom
107+
const QSize& zoomRectSize = mZoomRect.size();
108+
const QgsMapSettings& mapSettings = mCanvas->mapSettings();
109+
const QSize& canvasSize = mapSettings.outputSize();
110+
double sfx = ( double )zoomRectSize.width() / canvasSize.width();
111+
double sfy = ( double )zoomRectSize.height() / canvasSize.height();
112+
113+
const QgsMapToPixel* m2p = mCanvas->getCoordinateTransform();
114+
QgsPoint c = m2p->toMapCoordinates( mZoomRect.center() );
115+
116+
mCanvas->setCenter( c );
117+
mCanvas->zoomByFactor( std::max( sfx, sfy ) );
118+
148119
mCanvas->refresh();
149120
}
150121
else // not dragging

0 commit comments

Comments
 (0)
Please sign in to comment.