30
30
QgsComposerPicture::QgsComposerPicture ( QgsComposition *composition ): QgsComposerItem( composition ), mMode( Unknown ), \
31
31
mSvgCacheUpToDate( false ), mCachedDpi( 0 ), mCachedRotation( 0 ), mCachedViewScaleFactor( -1 ), mRotationMap( 0 )
32
32
{
33
+ mPictureWidth = rect ().width ();
33
34
}
34
35
35
36
QgsComposerPicture::QgsComposerPicture (): QgsComposerItem( 0 ), mMode( Unknown ), mSvgCacheUpToDate( false ), mCachedRotation( 0 ), mCachedViewScaleFactor( -1 ), mRotationMap( 0 )
36
37
{
37
-
38
+ mPictureHeight = rect (). height ();
38
39
}
39
40
40
41
QgsComposerPicture::~QgsComposerPicture ()
@@ -61,8 +62,8 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
61
62
62
63
if ( mMode != Unknown )
63
64
{
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 ;
66
67
QRectF boundRect;
67
68
if ( mMode == SVG )
68
69
{
@@ -75,11 +76,8 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
75
76
76
77
double boundRectWidthMM = boundRect.width () / newDpi * 25.4 ;
77
78
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 ();
83
81
84
82
if ( mMode == SVG )
85
83
{
@@ -88,20 +86,20 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
88
86
// make nicer preview
89
87
if ( mComposition && mComposition ->plotStyle () == QgsComposition::Preview )
90
88
{
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 );
93
91
}
94
- mImage = QImage ( rotatedBoundImageWidth, rotatedBoundImageHeight , QImage::Format_ARGB32 );
92
+ mImage = QImage ( boundImageWidth, boundImageHeight , QImage::Format_ARGB32 );
95
93
updateImageFromSvg ();
96
94
}
97
95
}
98
96
99
97
painter->save ();
100
- painter->translate ( boundRectWidthMM / 2.0 , boundRectHeightMM / 2.0 );
98
+ painter->translate ( rect (). width () / 2.0 , rect (). height () / 2.0 );
101
99
painter->rotate ( mRotation );
102
- painter->translate ( -rotatedBoundImageWidthMM / 2.0 , -rotatedBoundImageHeightMM / 2.0 );
100
+ painter->translate ( -boundRectWidthMM / 2.0 , -boundRectHeightMM / 2.0 );
103
101
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 () ) );
105
103
106
104
painter->restore ();
107
105
}
@@ -183,6 +181,24 @@ QRectF QgsComposerPicture::boundedImageRect( double deviceWidth, double deviceHe
183
181
}
184
182
}
185
183
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
186
202
QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeight )
187
203
{
188
204
double imageToSvgRatio;
@@ -199,6 +215,7 @@ QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeig
199
215
return QRectF( 0, 0, width, deviceHeight );
200
216
}
201
217
}
218
+ #endif // 0
202
219
203
220
void QgsComposerPicture::updateImageFromSvg ()
204
221
{
@@ -216,9 +233,32 @@ void QgsComposerPicture::setSceneRect( const QRectF& rectangle )
216
233
{
217
234
mSvgCacheUpToDate = false ;
218
235
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
+
219
244
emit settingsChanged ();
220
245
}
221
246
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
+
222
262
void QgsComposerPicture::setRotationMap ( int composerMapId )
223
263
{
224
264
if ( !mComposition )
@@ -260,6 +300,8 @@ bool QgsComposerPicture::writeXML( QDomElement& elem, QDomDocument & doc ) const
260
300
}
261
301
QDomElement composerPictureElem = doc.createElement ( " ComposerPicture" );
262
302
composerPictureElem.setAttribute ( " file" , QgsProject::instance ()->writePath ( mSourceFile .fileName () ) );
303
+ composerPictureElem.setAttribute ( " pictureWidth" , mPictureWidth );
304
+ composerPictureElem.setAttribute ( " pictureHeight" , mPictureHeight );
263
305
if ( !mRotationMap )
264
306
{
265
307
composerPictureElem.setAttribute ( " mapId" , -1 );
@@ -281,6 +323,9 @@ bool QgsComposerPicture::readXML( const QDomElement& itemElem, const QDomDocumen
281
323
return false ;
282
324
}
283
325
326
+ mPictureWidth = itemElem.attribute ( " pictureWidth" , " 10" ).toDouble ();
327
+ mPictureHeight = itemElem.attribute ( " pictureHeight" , " 10" ).toDouble ();
328
+
284
329
QDomNodeList composerItemList = itemElem.elementsByTagName ( " ComposerItem" );
285
330
if ( composerItemList.size () > 0 )
286
331
{
0 commit comments