3030QgsComposerPicture::QgsComposerPicture ( QgsComposition *composition ): QgsComposerItem( composition ), mMode( Unknown ), \
3131 mSvgCacheUpToDate( false ), mCachedDpi( 0 ), mCachedRotation( 0 ), mCachedViewScaleFactor( -1 ), mRotationMap( 0 )
3232{
33+ mPictureWidth = rect ().width ();
3334}
3435
3536QgsComposerPicture::QgsComposerPicture (): QgsComposerItem( 0 ), mMode( Unknown ), mSvgCacheUpToDate( false ), mCachedRotation( 0 ), mCachedViewScaleFactor( -1 ), mRotationMap( 0 )
3637{
37-
38+ mPictureHeight = rect (). height ();
3839}
3940
4041QgsComposerPicture::~QgsComposerPicture ()
@@ -61,8 +62,8 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
6162
6263 if ( mMode != Unknown )
6364 {
64- double rectPixelWidth = rect ().width () * newDpi / 25.4 ;
65- double rectPixelHeight = rect ().height () * newDpi / 25.4 ;
65+ double rectPixelWidth = /* rect().width()*/ mPictureWidth * newDpi / 25.4 ;
66+ double rectPixelHeight = /* rect().height()*/ mPictureHeight * newDpi / 25.4 ;
6667 QRectF boundRect;
6768 if ( mMode == SVG )
6869 {
@@ -75,11 +76,8 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
7576
7677 double boundRectWidthMM = boundRect.width () / newDpi * 25.4 ;
7778 double boundRectHeightMM = boundRect.height () / newDpi * 25.4 ;
78- double rotatedBoundImageWidth = boundRect.width ();
79- double rotatedBoundImageHeight = boundRect.height ();
80- imageSizeConsideringRotation ( rotatedBoundImageWidth, rotatedBoundImageHeight );
81- double rotatedBoundImageWidthMM = rotatedBoundImageWidth / newDpi * 25.4 ;
82- double rotatedBoundImageHeightMM = rotatedBoundImageHeight / newDpi * 25.4 ;
79+ double boundImageWidth = boundRect.width ();
80+ double boundImageHeight = boundRect.height ();
8381
8482 if ( mMode == SVG )
8583 {
@@ -88,20 +86,20 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
8886 // make nicer preview
8987 if ( mComposition && mComposition ->plotStyle () == QgsComposition::Preview )
9088 {
91- rotatedBoundImageWidth *= std::min ( viewScaleFactor, 10.0 );
92- rotatedBoundImageHeight *= std::min ( viewScaleFactor, 10.0 );
89+ boundImageWidth *= std::min ( viewScaleFactor, 10.0 );
90+ boundImageHeight *= std::min ( viewScaleFactor, 10.0 );
9391 }
94- mImage = QImage ( rotatedBoundImageWidth, rotatedBoundImageHeight , QImage::Format_ARGB32 );
92+ mImage = QImage ( boundImageWidth, boundImageHeight , QImage::Format_ARGB32 );
9593 updateImageFromSvg ();
9694 }
9795 }
9896
9997 painter->save ();
100- painter->translate ( boundRectWidthMM / 2.0 , boundRectHeightMM / 2.0 );
98+ painter->translate ( rect (). width () / 2.0 , rect (). height () / 2.0 );
10199 painter->rotate ( mRotation );
102- painter->translate ( -rotatedBoundImageWidthMM / 2.0 , -rotatedBoundImageHeightMM / 2.0 );
100+ painter->translate ( -boundRectWidthMM / 2.0 , -boundRectHeightMM / 2.0 );
103101
104- painter->drawImage ( QRectF ( 0 , 0 , rotatedBoundImageWidthMM , rotatedBoundImageHeightMM ), mImage , QRectF ( 0 , 0 , mImage .width (), mImage .height () ) );
102+ painter->drawImage ( QRectF ( 0 , 0 , boundRectWidthMM , boundRectHeightMM ), mImage , QRectF ( 0 , 0 , mImage .width (), mImage .height () ) );
105103
106104 painter->restore ();
107105 }
@@ -183,6 +181,24 @@ QRectF QgsComposerPicture::boundedImageRect( double deviceWidth, double deviceHe
183181 }
184182}
185183
184+ QRectF QgsComposerPicture::boundedSVGRect ( double deviceWidth, double deviceHeight )
185+ {
186+ double imageToSvgRatio;
187+ if ( deviceWidth / mDefaultSvgSize .width () > deviceHeight / mDefaultSvgSize .height () )
188+ {
189+ imageToSvgRatio = deviceHeight / mDefaultSvgSize .height ();
190+ double width = mDefaultSvgSize .width () * imageToSvgRatio;
191+ return QRectF ( 0 , 0 , width, deviceHeight );
192+ }
193+ else
194+ {
195+ imageToSvgRatio = deviceWidth / mDefaultSvgSize .width ();
196+ double height = mDefaultSvgSize .height () * imageToSvgRatio;
197+ return QRectF ( 0 , 0 , deviceWidth, height );
198+ }
199+ }
200+
201+ #if 0
186202QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeight )
187203{
188204 double imageToSvgRatio;
@@ -199,6 +215,7 @@ QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeig
199215 return QRectF( 0, 0, width, deviceHeight );
200216 }
201217}
218+ #endif // 0
202219
203220void QgsComposerPicture::updateImageFromSvg ()
204221{
@@ -216,9 +233,32 @@ void QgsComposerPicture::setSceneRect( const QRectF& rectangle )
216233{
217234 mSvgCacheUpToDate = false ;
218235 QgsComposerItem::setSceneRect ( rectangle );
236+
237+ // consider to change size of the shape if the rectangle changes width and/or height
238+ double newPictureWidth = rectangle.width ();
239+ double newPictureHeight = rectangle.height ();
240+ imageSizeConsideringRotation ( newPictureWidth, newPictureHeight );
241+ mPictureWidth = newPictureWidth;
242+ mPictureHeight = newPictureHeight;
243+
219244 emit settingsChanged ();
220245}
221246
247+ void QgsComposerPicture::setRotation ( double r )
248+ {
249+ // adapt rectangle size
250+ double width = mPictureWidth ;
251+ double height = mPictureHeight ;
252+ sizeChangedByRotation ( width, height );
253+
254+ // adapt scene rect to have the same center and the new width / height
255+ double x = transform ().dx () + rect ().width () / 2.0 - width / 2.0 ;
256+ double y = transform ().dy () + rect ().height () / 2.0 - height / 2.0 ;
257+ QgsComposerItem::setSceneRect ( QRectF ( x, y, width, height ) );
258+
259+ QgsComposerItem::setRotation ( r );
260+ }
261+
222262void QgsComposerPicture::setRotationMap ( int composerMapId )
223263{
224264 if ( !mComposition )
@@ -260,6 +300,8 @@ bool QgsComposerPicture::writeXML( QDomElement& elem, QDomDocument & doc ) const
260300 }
261301 QDomElement composerPictureElem = doc.createElement ( " ComposerPicture" );
262302 composerPictureElem.setAttribute ( " file" , QgsProject::instance ()->writePath ( mSourceFile .fileName () ) );
303+ composerPictureElem.setAttribute ( " pictureWidth" , mPictureWidth );
304+ composerPictureElem.setAttribute ( " pictureHeight" , mPictureHeight );
263305 if ( !mRotationMap )
264306 {
265307 composerPictureElem.setAttribute ( " mapId" , -1 );
@@ -281,6 +323,9 @@ bool QgsComposerPicture::readXML( const QDomElement& itemElem, const QDomDocumen
281323 return false ;
282324 }
283325
326+ mPictureWidth = itemElem.attribute ( " pictureWidth" , " 10" ).toDouble ();
327+ mPictureHeight = itemElem.attribute ( " pictureHeight" , " 10" ).toDouble ();
328+
284329 QDomNodeList composerItemList = itemElem.elementsByTagName ( " ComposerItem" );
285330 if ( composerItemList.size () > 0 )
286331 {
0 commit comments