Skip to content

Commit c27da1f

Browse files
author
mhugent
committed
Keep width and height of the composer map fixed if user sets the composer map extent to the map canvas extent
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10777 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent a479fa6 commit c27da1f

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/app/composer/qgscomposermapwidget.cpp

+25-6
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,34 @@ void QgsComposerMapWidget::on_mSetToMapCanvasExtentButton_clicked()
148148
const QgsMapRenderer* renderer = mComposerMap->mapRenderer();
149149
if ( renderer )
150150
{
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+
}
152171

153172
//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() ) );
158177

159-
mComposerMap->setNewExtent( canvasExtent );
178+
mComposerMap->setNewExtent( newExtent );
160179
}
161180
}
162181
}

0 commit comments

Comments
 (0)