void QgsComposerPicture::setPictureRotation( double r ) { double oldRotation = mPictureRotation; mPictureRotation = r; if ( mResizeMode == Zoom ) { //find largest scaling of picture with this rotation which fits in item QSizeF currentPictureSize = pictureSize(); QRectF rotatedImageRect = QgsComposerUtils::largestRotatedRectWithinBounds( QRectF( 0, 0, currentPictureSize.width(), currentPictureSize.height() ), rect(), mPictureRotation ); mPictureWidth = rotatedImageRect.width(); 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() ); QgsComposerItem::setSceneRect( newRect ); emit itemChanged(); } emit pictureRotationChanged( mPictureRotation ); }
void QgsComposerPicture::setSceneRect( const QRectF& rectangle ) { QSizeF currentPictureSize = pictureSize(); if ( mResizeMode == QgsComposerPicture::Clip ) { QgsComposerItem::setSceneRect( rectangle ); mPictureWidth = rectangle.width(); mPictureHeight = rectangle.height(); return; } QRectF newRect = rectangle; if ( mResizeMode == ZoomResizeFrame && !rect().isEmpty() && !( currentPictureSize.isEmpty() ) ) { //if width has changed less than height, then fix width and set height correspondingly //else, do the opposite if ( qAbs( rect().width() - rectangle.width() ) < qAbs( rect().height() - rectangle.height() ) ) { newRect.setHeight( currentPictureSize.height() * newRect.width() / currentPictureSize.width() ); } else { newRect.setWidth( currentPictureSize.width() * newRect.height() / currentPictureSize.height() ); } } else if ( mResizeMode == FrameToImageSize ) { if ( !( currentPictureSize.isEmpty() ) ) { newRect.setWidth( currentPictureSize.width() * 25.4 / mComposition->printResolution() ); newRect.setHeight( currentPictureSize.height() * 25.4 / mComposition->printResolution() ); } } //find largest scaling of picture with this rotation which fits in item if ( mResizeMode == Zoom ) { QRectF rotatedImageRect = largestRotatedRectWithinBounds( QRectF( 0, 0, currentPictureSize.width(), currentPictureSize.height() ), newRect, mPictureRotation ); mPictureWidth = rotatedImageRect.width(); mPictureHeight = rotatedImageRect.height(); } else { mPictureWidth = newRect.width(); mPictureHeight = newRect.height(); } QgsComposerItem::setSceneRect( newRect ); emit itemChanged(); }
void QgsComposerPicture::setPictureRotation( double r ) { mPictureRotation = r; if ( mResizeMode == Zoom ) { //find largest scaling of picture with this rotation which fits in item QSizeF currentPictureSize = pictureSize(); QRectF rotatedImageRect = largestRotatedRectWithinBounds( QRectF( 0, 0, currentPictureSize.width(), currentPictureSize.height() ), rect(), mPictureRotation ); mPictureWidth = rotatedImageRect.width(); mPictureHeight = rotatedImageRect.height(); update(); } emit pictureRotationChanged( mPictureRotation ); }
void QgsComposerPicture::setSceneRect( const QRectF& rectangle ) { QSizeF currentPictureSize = pictureSize(); if ( mResizeMode == QgsComposerPicture::Clip ) { QgsComposerItem::setSceneRect( rectangle ); mPictureWidth = rectangle.width(); mPictureHeight = rectangle.height(); } else { QRectF newRect = rectangle; if ( mResizeMode == ZoomResizeFrame && !rect().isEmpty() && !( currentPictureSize.isEmpty() ) ) { QSizeF targetImageSize; if ( qgsDoubleNear( mPictureRotation, 0.0 ) ) { targetImageSize = currentPictureSize; } else { //calculate aspect ratio of bounds of rotated image QTransform tr; tr.rotate( mPictureRotation ); QRectF rotatedBounds = tr.mapRect( QRectF( 0, 0, currentPictureSize.width(), currentPictureSize.height() ) ); targetImageSize = QSizeF( rotatedBounds.width(), rotatedBounds.height() ); } //if height has changed more than width, then fix width and set height correspondingly //else, do the opposite if ( qAbs( rect().width() - rectangle.width() ) < qAbs( rect().height() - rectangle.height() ) ) { newRect.setHeight( targetImageSize.height() * newRect.width() / targetImageSize.width() ); } else { newRect.setWidth( targetImageSize.width() * newRect.height() / targetImageSize.height() ); } } else if ( mResizeMode == FrameToImageSize ) { if ( !( currentPictureSize.isEmpty() ) ) { newRect.setWidth( currentPictureSize.width() * 25.4 / mComposition->printResolution() ); newRect.setHeight( currentPictureSize.height() * 25.4 / mComposition->printResolution() ); } } //find largest scaling of picture with this rotation which fits in item if ( mResizeMode == Zoom || mResizeMode == ZoomResizeFrame ) { QRectF rotatedImageRect = QgsComposerUtils::largestRotatedRectWithinBounds( QRectF( 0, 0, currentPictureSize.width(), currentPictureSize.height() ), newRect, mPictureRotation ); mPictureWidth = rotatedImageRect.width(); mPictureHeight = rotatedImageRect.height(); } else { mPictureWidth = newRect.width(); mPictureHeight = newRect.height(); } QgsComposerItem::setSceneRect( newRect ); emit itemChanged(); } if ( mMode == SVG && !mLoadingSvg ) { mLoadingSvg = true; refreshPicture(); mLoadingSvg = false; } }
QSizeF QgsLayoutItemPicture::applyItemSizeConstraint( const QSizeF &targetSize ) { QSizeF currentPictureSize = pictureSize(); QSizeF newSize = targetSize; if ( mResizeMode == QgsLayoutItemPicture::Clip ) { mPictureWidth = targetSize.width(); mPictureHeight = targetSize.height(); } else { if ( mResizeMode == ZoomResizeFrame && !rect().isEmpty() && !( currentPictureSize.isEmpty() ) ) { QSizeF targetImageSize; if ( qgsDoubleNear( mPictureRotation, 0.0 ) ) { targetImageSize = currentPictureSize; } else { //calculate aspect ratio of bounds of rotated image QTransform tr; tr.rotate( mPictureRotation ); QRectF rotatedBounds = tr.mapRect( QRectF( 0, 0, currentPictureSize.width(), currentPictureSize.height() ) ); targetImageSize = QSizeF( rotatedBounds.width(), rotatedBounds.height() ); } //if height has changed more than width, then fix width and set height correspondingly //else, do the opposite if ( std::fabs( rect().width() - targetSize.width() ) < std::fabs( rect().height() - targetSize.height() ) ) { newSize.setHeight( targetImageSize.height() * newSize.width() / targetImageSize.width() ); } else { newSize.setWidth( targetImageSize.width() * newSize.height() / targetImageSize.height() ); } } else if ( mResizeMode == FrameToImageSize ) { if ( !( currentPictureSize.isEmpty() ) ) { QgsLayoutSize sizeMM = mLayout->convertFromLayoutUnits( currentPictureSize, QgsUnitTypes::LayoutMillimeters ); newSize.setWidth( sizeMM.width() * 25.4 / mLayout->context().dpi() ); newSize.setHeight( sizeMM.height() * 25.4 / mLayout->context().dpi() ); } } //find largest scaling of picture with this rotation which fits in item if ( mResizeMode == Zoom || mResizeMode == ZoomResizeFrame ) { QRectF rotatedImageRect = QgsLayoutUtils::largestRotatedRectWithinBounds( QRectF( 0, 0, currentPictureSize.width(), currentPictureSize.height() ), QRectF( 0, 0, newSize.width(), newSize.height() ), mPictureRotation ); mPictureWidth = rotatedImageRect.width(); mPictureHeight = rotatedImageRect.height(); } else { mPictureWidth = newSize.width(); mPictureHeight = newSize.height(); } if ( newSize != targetSize ) { emit changed(); } } return newSize; }