@@ -148,15 +148,34 @@ void QgsComposerMapWidget::on_mSetToMapCanvasExtentButton_clicked()
148
148
const QgsMapRenderer* renderer = mComposerMap ->mapRenderer ();
149
149
if ( renderer )
150
150
{
151
- QgsRectangle canvasExtent = renderer->extent ();
151
+ QgsRectangle newExtent = renderer->extent ();
152
+
153
+ // Make sure the width/height ratio is the same as in current composer map extent.
154
+ // This is to keep the map item frame and the page layout fixed
155
+ QgsRectangle currentMapExtent = mComposerMap ->extent ();
156
+ double currentWidthHeightRatio = currentMapExtent.width () / currentMapExtent.height ();
157
+ double newWidthHeightRatio = newExtent.width () / newExtent.height ();
158
+
159
+ if (currentWidthHeightRatio < newWidthHeightRatio)
160
+ {
161
+ // enlarge height of new extent
162
+ double newHeight = newExtent.width () / currentWidthHeightRatio;
163
+ newExtent.setYMinimum (newExtent.yMaximum () - newHeight);
164
+ }
165
+ else if (currentWidthHeightRatio > newWidthHeightRatio)
166
+ {
167
+ // enlarge width of new extent
168
+ double newWidth = currentWidthHeightRatio * newExtent.height ();
169
+ newExtent.setXMaximum (newExtent.xMinimum () + newWidth);
170
+ }
152
171
153
172
// fill text into line edits
154
- mXMinLineEdit ->setText ( QString::number ( canvasExtent .xMinimum () ) );
155
- mXMaxLineEdit ->setText ( QString::number ( canvasExtent .xMaximum () ) );
156
- mYMinLineEdit ->setText ( QString::number ( canvasExtent .yMinimum () ) );
157
- mYMaxLineEdit ->setText ( QString::number ( canvasExtent .yMaximum () ) );
173
+ mXMinLineEdit ->setText ( QString::number ( newExtent .xMinimum () ) );
174
+ mXMaxLineEdit ->setText ( QString::number ( newExtent .xMaximum () ) );
175
+ mYMinLineEdit ->setText ( QString::number ( newExtent .yMinimum () ) );
176
+ mYMaxLineEdit ->setText ( QString::number ( newExtent .yMaximum () ) );
158
177
159
- mComposerMap ->setNewExtent ( canvasExtent );
178
+ mComposerMap ->setNewExtent ( newExtent );
160
179
}
161
180
}
162
181
}
0 commit comments