Skip to content

Commit

Permalink
[composer] Closer cropping to content for map items
Browse files Browse the repository at this point in the history
Sponsored by NIWA
  • Loading branch information
nyalldawson committed Sep 25, 2015
1 parent dce9da1 commit abf4e12
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 160 deletions.
28 changes: 27 additions & 1 deletion python/core/composer/qgscomposermapgrid.sip
Expand Up @@ -100,9 +100,22 @@ class QgsComposerMapGridStack : QgsComposerMapItemStack
/** Calculates the maximum distance grids within the stack extend
* beyond the QgsComposerMap's item rect
* @returns maximum grid extension
* @see calculateMaxGridExtension()
*/
double maxGridExtension() const;

/** Calculates the maximum distance grids within the stack extend beyond the
* QgsComposerMap's item rect. This method calculates the distance for each side of the
* map item seperately
* @param top storage for top extension
* @param right storage for right extension
* @param bottom storage for bottom extension
* @param left storage for left extension
* @note added in QGIS 2.12
* @see maxGridExtension()
*/
void calculateMaxGridExtension( double& top, double& right, double& bottom, double& left ) const;

};

//
Expand Down Expand Up @@ -286,7 +299,20 @@ class QgsComposerMapGrid : QgsComposerMapItem
* item rect
* @returns maximum extension in millimetres
*/
double maxExtension() const;
double maxExtension();

/** Calculates the maximum distance the grid extends beyond the
* QgsComposerMap's item rect. This method calculates the distance for each side of the
* map item seperately
* @param top storage for top extension
* @param right storage for right extension
* @param bottom storage for bottom extension
* @param left storage for left extension
* @note added in QGIS 2.12
* @see maxExtension()
*/
void calculateMaxExtension( double& top, double& right, double& bottom, double& left );


//
// GRID UNITS
Expand Down
8 changes: 8 additions & 0 deletions src/app/composer/qgscomposermapwidget.cpp
Expand Up @@ -807,6 +807,7 @@ void QgsComposerMapWidget::handleChangedFrameDisplay( QgsComposerMapGrid::Border
mComposerMap->beginCommand( tr( "Frame divisions changed" ) );
grid->setFrameDivisions( mode, border );
mComposerMap->endCommand();
mComposerMap->updateBoundingRect();
}

void QgsComposerMapWidget::handleChangedAnnotationDisplay( QgsComposerMapGrid::BorderSide border, const QString &text )
Expand Down Expand Up @@ -1677,6 +1678,7 @@ void QgsComposerMapWidget::on_mFrameWidthSpinBox_valueChanged( double val )

mComposerMap->beginCommand( tr( "Frame width changed" ) );
grid->setFrameWidth( val );
mComposerMap->updateBoundingRect();
mComposerMap->update();
mComposerMap->endCommand();
}
Expand All @@ -1691,6 +1693,7 @@ void QgsComposerMapWidget::on_mCheckGridLeftSide_toggled( bool checked )

mComposerMap->beginCommand( tr( "Frame left side changed" ) );
grid->setFrameSideFlag( QgsComposerMapGrid::FrameLeft, checked );
mComposerMap->updateBoundingRect();
mComposerMap->update();
mComposerMap->endCommand();
}
Expand All @@ -1705,6 +1708,7 @@ void QgsComposerMapWidget::on_mCheckGridRightSide_toggled( bool checked )

mComposerMap->beginCommand( tr( "Frame right side changed" ) );
grid->setFrameSideFlag( QgsComposerMapGrid::FrameRight, checked );
mComposerMap->updateBoundingRect();
mComposerMap->update();
mComposerMap->endCommand();
}
Expand All @@ -1719,6 +1723,7 @@ void QgsComposerMapWidget::on_mCheckGridTopSide_toggled( bool checked )

mComposerMap->beginCommand( tr( "Frame top side changed" ) );
grid->setFrameSideFlag( QgsComposerMapGrid::FrameTop, checked );
mComposerMap->updateBoundingRect();
mComposerMap->update();
mComposerMap->endCommand();
}
Expand All @@ -1733,6 +1738,7 @@ void QgsComposerMapWidget::on_mCheckGridBottomSide_toggled( bool checked )

mComposerMap->beginCommand( tr( "Frame bottom side changed" ) );
grid->setFrameSideFlag( QgsComposerMapGrid::FrameBottom, checked );
mComposerMap->updateBoundingRect();
mComposerMap->update();
mComposerMap->endCommand();
}
Expand Down Expand Up @@ -1956,6 +1962,7 @@ void QgsComposerMapWidget::on_mGridTypeComboBox_currentIndexChanged( const QStri
mGridBlendLabel->setVisible( false );
}

mComposerMap->updateBoundingRect();
mComposerMap->update();
mComposerMap->endCommand();
}
Expand All @@ -1978,6 +1985,7 @@ void QgsComposerMapWidget::on_mMapGridCRSButton_clicked()
mComposerMap->beginCommand( tr( "Grid CRS changed" ) );
QString selectedAuthId = crsDialog.selectedAuthId();
grid->setCrs( QgsCoordinateReferenceSystem( selectedAuthId ) );
mComposerMap->updateBoundingRect();
mMapGridCRSButton->setText( selectedAuthId );
mComposerMap->endCommand();
}
Expand Down
22 changes: 16 additions & 6 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -2000,14 +2000,24 @@ void QgsComposerMap::updateBoundingRect()
{
QRectF rectangle = rect();
double frameExtension = mFrame ? pen().widthF() / 2.0 : 0.0;
double maxGridExtension = mGridStack ? mGridStack->maxGridExtension() : 0;

double maxExtension = qMax( frameExtension, maxGridExtension );
double topExtension = 0.0;
double rightExtension = 0.0;
double bottomExtension = 0.0;
double leftExtension = 0.0;

rectangle.setLeft( rectangle.left() - maxExtension );
rectangle.setRight( rectangle.right() + maxExtension );
rectangle.setTop( rectangle.top() - maxExtension );
rectangle.setBottom( rectangle.bottom() + maxExtension );
if ( mGridStack )
mGridStack->calculateMaxGridExtension( topExtension, rightExtension, bottomExtension, leftExtension );

topExtension = qMax( topExtension, frameExtension );
rightExtension = qMax( rightExtension, frameExtension );
bottomExtension = qMax( bottomExtension, frameExtension );
leftExtension = qMax( leftExtension, frameExtension );

rectangle.setLeft( rectangle.left() - leftExtension );
rectangle.setRight( rectangle.right() + rightExtension );
rectangle.setTop( rectangle.top() - topExtension );
rectangle.setBottom( rectangle.bottom() + bottomExtension );
if ( rectangle != mCurrentRectangle )
{
prepareGeometryChange();
Expand Down

0 comments on commit abf4e12

Please sign in to comment.