Skip to content

Commit

Permalink
[composer] Allow rotation for pictures set to "zoom & resize" mode
Browse files Browse the repository at this point in the history
Previously rotation was only allowed for pictures set to "Zoom" mode,
which causes pictures to change size when the rotation changes (frame
size is constant). This change allows pictures set to "Zoom and resize"
mode to have rotation set. Altering the rotation when in this mode
keeps the picture size constant and changes the frame size to suit.

(fix #10640)
  • Loading branch information
nyalldawson committed Oct 19, 2014
1 parent a7d708f commit da5766c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/app/composer/qgscomposerpicturewidget.cpp
Expand Up @@ -225,7 +225,8 @@ void QgsComposerPictureWidget::on_mResizeModeComboBox_currentIndexChanged( int i
mPicture->endCommand();

//disable picture rotation for non-zoom modes
mRotationGroupBox->setEnabled( mPicture->resizeMode() == QgsComposerPicture::Zoom );
mRotationGroupBox->setEnabled( mPicture->resizeMode() == QgsComposerPicture::Zoom ||
mPicture->resizeMode() == QgsComposerPicture::ZoomResizeFrame );

//disable anchor point control for certain zoom modes
if ( mPicture->resizeMode() == QgsComposerPicture::Zoom ||
Expand Down Expand Up @@ -408,7 +409,8 @@ void QgsComposerPictureWidget::setGuiElementValues()

mResizeModeComboBox->setCurrentIndex(( int )mPicture->resizeMode() );
//disable picture rotation for non-zoom modes
mRotationGroupBox->setEnabled( mPicture->resizeMode() == QgsComposerPicture::Zoom );
mRotationGroupBox->setEnabled( mPicture->resizeMode() == QgsComposerPicture::Zoom ||
mPicture->resizeMode() == QgsComposerPicture::ZoomResizeFrame );

mAnchorPointComboBox->setCurrentIndex(( int )mPicture->pictureAnchor() );
//disable anchor point control for certain zoom modes
Expand Down
33 changes: 30 additions & 3 deletions src/core/composer/qgscomposerpicture.cpp
Expand Up @@ -202,6 +202,15 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
painter->translate( dX, dY );
}
}
else if ( mResizeMode == ZoomResizeFrame )
{
if ( mPictureRotation != 0 )
{
painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
painter->rotate( mPictureRotation );
painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
}
}

if ( mMode == SVG )
{
Expand Down Expand Up @@ -510,9 +519,9 @@ void QgsComposerPicture::setSceneRect( const QRectF& rectangle )

if ( mResizeMode == ZoomResizeFrame && !rect().isEmpty() && !( currentPictureSize.isEmpty() ) )
{
//if width has changed less than height, then fix width and set height correspondingly
//if width has changed more than height, then fix width and set height correspondingly
//else, do the opposite
if ( qAbs( rect().width() - rectangle.width() ) <
if ( qAbs( rect().width() - rectangle.width() ) >
qAbs( rect().height() - rectangle.height() ) )
{
newRect.setHeight( currentPictureSize.height() * newRect.width() / currentPictureSize.width() );
Expand All @@ -532,7 +541,7 @@ void QgsComposerPicture::setSceneRect( const QRectF& rectangle )
}

//find largest scaling of picture with this rotation which fits in item
if ( mResizeMode == Zoom )
if ( mResizeMode == Zoom || mResizeMode == ZoomResizeFrame )
{
QRectF rotatedImageRect = QgsComposerUtils::largestRotatedRectWithinBounds( QRectF( 0, 0, currentPictureSize.width(), currentPictureSize.height() ), newRect, mPictureRotation );
mPictureWidth = rotatedImageRect.width();
Expand All @@ -556,6 +565,7 @@ void QgsComposerPicture::setRotation( double r )

void QgsComposerPicture::setPictureRotation( double r )
{
double oldRotation = mPictureRotation;
mPictureRotation = r;

if ( mResizeMode == Zoom )
Expand All @@ -567,6 +577,23 @@ void QgsComposerPicture::setPictureRotation( double r )
mPictureHeight = rotatedImageRect.height();
update();
}
else if ( mResizeMode == ZoomResizeFrame )
{
QSizeF currentPictureSize = pictureSize();
QRectF oldRect = QRectF( pos().x(), pos().y(), rect().width(), rect().height() );

//calculate actual size of image inside frame
QRectF rotatedImageRect = QgsComposerUtils::largestRotatedRectWithinBounds( QRectF( 0, 0, currentPictureSize.width(), currentPictureSize.height() ), rect(), oldRotation );

//rotate image rect by new rotation and get bounding box
QTransform tr;
tr.rotate( mPictureRotation );
QRectF newRect = tr.mapRect( QRectF( 0, 0, rotatedImageRect.width(), rotatedImageRect.height() ) );

//keep the center in the same location
newRect.moveCenter( oldRect.center() );
setSceneRect( newRect );
}

emit pictureRotationChanged( mPictureRotation );
}
Expand Down

0 comments on commit da5766c

Please sign in to comment.