Skip to content

Commit

Permalink
[composer] Ensure that extent of new map items is centered on current…
Browse files Browse the repository at this point in the history
… centre of map
  • Loading branch information
nyalldawson committed Dec 22, 2013
1 parent 52c59da commit b40e3c0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposermapwidget.cpp
Expand Up @@ -185,7 +185,7 @@ void QgsComposerMapWidget::on_mSetToMapCanvasExtentButton_clicked()
newExtent.setYMinimum( newExtent.yMinimum() - deltaHeight / 2 );
newExtent.setYMaximum( newExtent.yMaximum() + deltaHeight / 2 );
}
else if ( currentWidthHeightRatio > newWidthHeightRatio )
else
{
//enlarge width of new extent, ensuring the map center stays the same
double newWidth = currentWidthHeightRatio * newExtent.height();
Expand Down
28 changes: 27 additions & 1 deletion src/core/composer/qgscomposermap.cpp
Expand Up @@ -85,7 +85,10 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
//calculate mExtent based on width/height ratio and map canvas extent
if ( mMapRenderer )
{
mExtent = mMapRenderer->extent();
QgsRectangle mapExtent = mMapRenderer->extent();
//make extent make item shape while keeping centre unchanged
adjustExtentToItemShape( width, height, mapExtent );
mExtent = mapExtent;
}
setSceneRect( QRectF( x, y, width, height ) );
setToolTip( tr( "Map %1" ).arg( mId ) );
Expand Down Expand Up @@ -126,6 +129,29 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
initGridAnnotationFormatFromProject();
}

void QgsComposerMap::adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const
{
double itemWidthHeightRatio = itemWidth / itemHeight;
double newWidthHeightRatio = extent.width() / extent.height();

if ( itemWidthHeightRatio <= newWidthHeightRatio )
{
//enlarge height of new extent, ensuring the map center stays the same
double newHeight = extent.width() / itemWidthHeightRatio;
double deltaHeight = newHeight - extent.height();
extent.setYMinimum( extent.yMinimum() - deltaHeight / 2 );
extent.setYMaximum( extent.yMaximum() + deltaHeight / 2 );
}
else
{
//enlarge width of new extent, ensuring the map center stays the same
double newWidth = itemWidthHeightRatio * extent.height();
double deltaWidth = newWidth - extent.width();
extent.setXMinimum( extent.xMinimum() - deltaWidth / 2 );
extent.setXMaximum( extent.xMaximum() + deltaWidth / 2 );
}
}

void QgsComposerMap::extentCenteredOnOverview( QgsRectangle& extent ) const
{
extent = mExtent;
Expand Down
4 changes: 4 additions & 0 deletions src/core/composer/qgscomposermap.h
Expand Up @@ -479,6 +479,10 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
/**True if annotation items, rubber band, etc. from the main canvas should be displayed*/
bool mDrawCanvasItems;

/**Adjusts an extent rectangle to match the provided item width and height, so that extent
* center of extent remains the same */
void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;

/**Draws the map grid*/
void drawGrid( QPainter* p );
void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines );
Expand Down

0 comments on commit b40e3c0

Please sign in to comment.