@@ -85,7 +85,10 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
85
85
// calculate mExtent based on width/height ratio and map canvas extent
86
86
if ( mMapRenderer )
87
87
{
88
- mExtent = mMapRenderer ->extent ();
88
+ QgsRectangle mapExtent = mMapRenderer ->extent ();
89
+ // make extent make item shape while keeping centre unchanged
90
+ adjustExtentToItemShape ( width, height, mapExtent );
91
+ mExtent = mapExtent;
89
92
}
90
93
setSceneRect ( QRectF ( x, y, width, height ) );
91
94
setToolTip ( tr ( " Map %1" ).arg ( mId ) );
@@ -126,6 +129,29 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
126
129
initGridAnnotationFormatFromProject ();
127
130
}
128
131
132
+ void QgsComposerMap::adjustExtentToItemShape ( double itemWidth, double itemHeight, QgsRectangle& extent ) const
133
+ {
134
+ double itemWidthHeightRatio = itemWidth / itemHeight;
135
+ double newWidthHeightRatio = extent.width () / extent.height ();
136
+
137
+ if ( itemWidthHeightRatio <= newWidthHeightRatio )
138
+ {
139
+ // enlarge height of new extent, ensuring the map center stays the same
140
+ double newHeight = extent.width () / itemWidthHeightRatio;
141
+ double deltaHeight = newHeight - extent.height ();
142
+ extent.setYMinimum ( extent.yMinimum () - deltaHeight / 2 );
143
+ extent.setYMaximum ( extent.yMaximum () + deltaHeight / 2 );
144
+ }
145
+ else
146
+ {
147
+ // enlarge width of new extent, ensuring the map center stays the same
148
+ double newWidth = itemWidthHeightRatio * extent.height ();
149
+ double deltaWidth = newWidth - extent.width ();
150
+ extent.setXMinimum ( extent.xMinimum () - deltaWidth / 2 );
151
+ extent.setXMaximum ( extent.xMaximum () + deltaWidth / 2 );
152
+ }
153
+ }
154
+
129
155
void QgsComposerMap::extentCenteredOnOverview ( QgsRectangle& extent ) const
130
156
{
131
157
extent = mExtent ;
0 commit comments