@@ -103,48 +103,19 @@ void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
103
103
mZoomRect .setRight ( e->pos ().x () );
104
104
mZoomRect .setBottom ( e->pos ().y () );
105
105
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
+
148
119
mCanvas ->refresh ();
149
120
}
150
121
else // not dragging
0 commit comments