Skip to content

Commit

Permalink
Simplify zoom-by-rect handling, to set zoom factor and center
Browse files Browse the repository at this point in the history
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 077eb6c
Showing 1 changed file with 13 additions and 42 deletions.
55 changes: 13 additions & 42 deletions src/gui/qgsmaptoolzoom.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 077eb6c

Please sign in to comment.