void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) { Q_UNUSED( itemStyle ); Q_UNUSED( pWidget ); if ( !painter ) { return; } if ( !shouldDrawItem() ) { return; } drawBackground( painter ); //int newDpi = ( painter->device()->logicalDpiX() + painter->device()->logicalDpiY() ) / 2; //picture resizing if ( mMode != Unknown ) { double boundRectWidthMM; double boundRectHeightMM; QRect imageRect; if ( mResizeMode == QgsComposerPicture::Zoom || mResizeMode == QgsComposerPicture::ZoomResizeFrame ) { boundRectWidthMM = mPictureWidth; boundRectHeightMM = mPictureHeight; imageRect = QRect( 0, 0, mImage.width(), mImage.height() ); } else if ( mResizeMode == QgsComposerPicture::Stretch ) { boundRectWidthMM = rect().width(); boundRectHeightMM = rect().height(); imageRect = QRect( 0, 0, mImage.width(), mImage.height() ); } else if ( mResizeMode == QgsComposerPicture::Clip ) { boundRectWidthMM = rect().width(); boundRectHeightMM = rect().height(); int imageRectWidthPixels = mImage.width(); int imageRectHeightPixels = mImage.height(); imageRect = clippedImageRect( boundRectWidthMM, boundRectHeightMM, QSize( imageRectWidthPixels, imageRectHeightPixels ) ); } else { boundRectWidthMM = rect().width(); boundRectHeightMM = rect().height(); imageRect = QRect( 0, 0, rect().width() * mComposition->printResolution() / 25.4, rect().height() * mComposition->printResolution() / 25.4 ); } painter->save(); //antialiasing on painter->setRenderHint( QPainter::Antialiasing, true ); //zoom mode - calculate anchor point and rotation if ( mResizeMode == Zoom ) { //TODO - allow placement modes with rotation set. for now, setting a rotation //always places picture in center of frame if ( !qgsDoubleNear( mPictureRotation, 0.0 ) ) { painter->translate( rect().width() / 2.0, rect().height() / 2.0 ); painter->rotate( mPictureRotation ); painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 ); } else { //shift painter to edge/middle of frame depending on placement double diffX = rect().width() - boundRectWidthMM; double diffY = rect().height() - boundRectHeightMM; double dX = 0; double dY = 0; switch ( mPictureAnchor ) { case UpperLeft: case MiddleLeft: case LowerLeft: //nothing to do break; case UpperMiddle: case Middle: case LowerMiddle: dX = diffX / 2.0; break; case UpperRight: case MiddleRight: case LowerRight: dX = diffX; break; } switch ( mPictureAnchor ) { case UpperLeft: case UpperMiddle: case UpperRight: //nothing to do break; case MiddleLeft: case Middle: case MiddleRight: dY = diffY / 2.0; break; case LowerLeft: case LowerMiddle: case LowerRight: dY = diffY; break; } painter->translate( dX, dY ); } } else if ( mResizeMode == ZoomResizeFrame ) { if ( !qgsDoubleNear( mPictureRotation, 0.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 ) { mSVG.render( painter, QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) ); } else if ( mMode == RASTER ) { painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, imageRect ); } painter->restore(); } //frame and selection boxes drawFrame( painter ); if ( isSelected() ) { drawSelectionBoxes( painter ); } }
void QgsLayoutItemPicture::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) { QPainter *painter = context.painter(); painter->save(); // painter is scaled to dots, so scale back to layout units painter->scale( context.scaleFactor(), context.scaleFactor() ); //picture resizing if ( mMode != FormatUnknown ) { double boundRectWidthMM; double boundRectHeightMM; QRect imageRect; if ( mResizeMode == QgsLayoutItemPicture::Zoom || mResizeMode == QgsLayoutItemPicture::ZoomResizeFrame ) { boundRectWidthMM = mPictureWidth; boundRectHeightMM = mPictureHeight; imageRect = QRect( 0, 0, mImage.width(), mImage.height() ); } else if ( mResizeMode == QgsLayoutItemPicture::Stretch ) { boundRectWidthMM = rect().width(); boundRectHeightMM = rect().height(); imageRect = QRect( 0, 0, mImage.width(), mImage.height() ); } else if ( mResizeMode == QgsLayoutItemPicture::Clip ) { boundRectWidthMM = rect().width(); boundRectHeightMM = rect().height(); int imageRectWidthPixels = mImage.width(); int imageRectHeightPixels = mImage.height(); imageRect = clippedImageRect( boundRectWidthMM, boundRectHeightMM, QSize( imageRectWidthPixels, imageRectHeightPixels ) ); } else { boundRectWidthMM = rect().width(); boundRectHeightMM = rect().height(); imageRect = QRect( 0, 0, mLayout->convertFromLayoutUnits( rect().width(), QgsUnitTypes::LayoutMillimeters ).length() * mLayout->context().dpi() / 25.4, mLayout->convertFromLayoutUnits( rect().height(), QgsUnitTypes::LayoutMillimeters ).length() * mLayout->context().dpi() / 25.4 ); } //zoom mode - calculate anchor point and rotation if ( mResizeMode == Zoom ) { //TODO - allow placement modes with rotation set. for now, setting a rotation //always places picture in center of frame if ( !qgsDoubleNear( mPictureRotation, 0.0 ) ) { painter->translate( rect().width() / 2.0, rect().height() / 2.0 ); painter->rotate( mPictureRotation ); painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 ); } else { //shift painter to edge/middle of frame depending on placement double diffX = rect().width() - boundRectWidthMM; double diffY = rect().height() - boundRectHeightMM; double dX = 0; double dY = 0; switch ( mPictureAnchor ) { case UpperLeft: case MiddleLeft: case LowerLeft: //nothing to do break; case UpperMiddle: case Middle: case LowerMiddle: dX = diffX / 2.0; break; case UpperRight: case MiddleRight: case LowerRight: dX = diffX; break; } switch ( mPictureAnchor ) { case UpperLeft: case UpperMiddle: case UpperRight: //nothing to do break; case MiddleLeft: case Middle: case MiddleRight: dY = diffY / 2.0; break; case LowerLeft: case LowerMiddle: case LowerRight: dY = diffY; break; } painter->translate( dX, dY ); } } else if ( mResizeMode == ZoomResizeFrame ) { if ( !qgsDoubleNear( mPictureRotation, 0.0 ) ) { painter->translate( rect().width() / 2.0, rect().height() / 2.0 ); painter->rotate( mPictureRotation ); painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 ); } } if ( mMode == FormatSVG ) { mSVG.render( painter, QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) ); } else if ( mMode == FormatRaster ) { painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, imageRect ); } } painter->restore(); }