Skip to content
Permalink
Browse files
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.
  • Loading branch information
Sandro Santilli committed Jan 5, 2015
1 parent 0afdede commit 077eb6ca08b96d44459df486a856644c60ee102e
Showing with 13 additions and 42 deletions.
  1. +13 −42 src/gui/qgsmaptoolzoom.cpp
@@ -103,48 +103,19 @@ void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
mZoomRect.setRight( e->pos().x() );
mZoomRect.setBottom( e->pos().y() );

const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform();

// set the extent to the zoomBox
QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() );
QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right(), mZoomRect.top() );

QgsRectangle r;
r.setXMinimum( ll.x() );
r.setYMinimum( ll.y() );
r.setXMaximum( ur.x() );
r.setYMaximum( ur.y() );
r.normalize();

// prevent zooming to an empty extent
if ( r.width() == 0 || r.height() == 0 )
{
return;
}

if ( mZoomOut )
{
QgsPoint cer = r.center();
QgsRectangle extent = mCanvas->extent();

double sf;
if ( mZoomRect.width() > mZoomRect.height() )
{
sf = extent.width() / r.width();
}
else
{
sf = extent.height() / r.height();
}
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() ) );

}

mCanvas->setExtent( r );
// set center and zoom
const QSize& zoomRectSize = mZoomRect.size();
const QgsMapSettings& mapSettings = mCanvas->mapSettings();
const QSize& canvasSize = mapSettings.outputSize();
double sfx = ( double )zoomRectSize.width() / canvasSize.width();
double sfy = ( double )zoomRectSize.height() / canvasSize.height();

const QgsMapToPixel* m2p = mCanvas->getCoordinateTransform();
QgsPoint c = m2p->toMapCoordinates( mZoomRect.center() );

mCanvas->setCenter( c );
mCanvas->zoomByFactor( std::max( sfx, sfy ) );

mCanvas->refresh();
}
else // not dragging

0 comments on commit 077eb6c

Please sign in to comment.