28
28
29
29
30
30
QgsComposerPicture::QgsComposerPicture ( QgsComposition *composition )
31
- : QgsComposerItem( composition ), mMode( Unknown ), mPictureRotation( 0 ), mRotationMap( 0 )
31
+ : QgsComposerItem( composition ), mMode( Unknown ), mPictureRotation( 0 ), mRotationMap( 0 ),
32
+ mResizeMode( QgsComposerPicture::Zoom )
32
33
{
33
34
mPictureWidth = rect ().width ();
34
35
}
35
36
36
- QgsComposerPicture::QgsComposerPicture (): QgsComposerItem( 0 ), mMode( Unknown ), mPictureRotation( 0 ), mRotationMap( 0 )
37
+ QgsComposerPicture::QgsComposerPicture (): QgsComposerItem( 0 ), mMode( Unknown ), mPictureRotation( 0 ), mRotationMap( 0 ), mResizeMode( QgsComposerPicture::Zoom )
37
38
{
38
39
mPictureHeight = rect ().height ();
39
40
}
@@ -58,21 +59,47 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
58
59
59
60
if ( mMode != Unknown )
60
61
{
61
- double boundRectWidthMM = mPictureWidth ;
62
- double boundRectHeightMM = mPictureHeight ;
63
-
62
+ double boundRectWidthMM;
63
+ double boundRectHeightMM;
64
+ double imageRectWidthMM;
65
+ double imageRectHeightMM;
66
+ if ( mResizeMode == QgsComposerPicture::Zoom || mResizeMode == QgsComposerPicture::ZoomResizeFrame )
67
+ {
68
+ boundRectWidthMM = mPictureWidth ;
69
+ boundRectHeightMM = mPictureHeight ;
70
+ imageRectWidthMM = mImage .width ();
71
+ imageRectHeightMM = mImage .height ();
72
+ }
73
+ else if ( mResizeMode == QgsComposerPicture::Stretch )
74
+ {
75
+ boundRectWidthMM = rect ().width ();
76
+ boundRectHeightMM = rect ().height ();
77
+ imageRectWidthMM = mImage .width ();
78
+ imageRectHeightMM = mImage .height ();
79
+ }
80
+ else
81
+ {
82
+ boundRectWidthMM = rect ().width ();
83
+ boundRectHeightMM = rect ().height ();
84
+ imageRectWidthMM = rect ().width () * mComposition ->printResolution () / 25.4 ;
85
+ imageRectHeightMM = rect ().height () * mComposition ->printResolution () / 25.4 ;
86
+ }
64
87
painter->save ();
65
- painter->translate ( rect ().width () / 2.0 , rect ().height () / 2.0 );
66
- painter->rotate ( mPictureRotation );
67
- painter->translate ( -boundRectWidthMM / 2.0 , -boundRectHeightMM / 2.0 );
88
+
89
+ if ( mResizeMode == Zoom )
90
+ {
91
+ painter->translate ( rect ().width () / 2.0 , rect ().height () / 2.0 );
92
+ painter->rotate ( mPictureRotation );
93
+ painter->translate ( -boundRectWidthMM / 2.0 , -boundRectHeightMM / 2.0 );
94
+ }
68
95
69
96
if ( mMode == SVG )
70
97
{
71
98
mSVG .render ( painter, QRectF ( 0 , 0 , boundRectWidthMM, boundRectHeightMM ) );
72
99
}
73
100
else if ( mMode == RASTER )
74
101
{
75
- painter->drawImage ( QRectF ( 0 , 0 , boundRectWidthMM, boundRectHeightMM ), mImage , QRectF ( 0 , 0 , mImage . width (), mImage . height () ) );
102
+ painter->drawImage ( QRectF ( 0 , 0 , boundRectWidthMM, boundRectHeightMM ), mImage , QRectF ( 0 , 0 , imageRectWidthMM, imageRectHeightMM ) );
76
103
}
77
104
78
105
painter->restore ();
@@ -204,14 +231,53 @@ QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeig
204
231
205
232
void QgsComposerPicture::setSceneRect ( const QRectF& rectangle )
206
233
{
207
- QgsComposerItem::setSceneRect ( rectangle );
208
234
209
- // find largest scaling of picture with this rotation which fits in item
210
235
QSizeF currentPictureSize = pictureSize ();
211
- QRectF rotatedImageRect = largestRotatedRectWithinBounds ( QRectF ( 0 , 0 , currentPictureSize.width (), currentPictureSize.height () ), rectangle, mPictureRotation );
212
- mPictureWidth = rotatedImageRect.width ();
213
- mPictureHeight = rotatedImageRect.height ();
214
236
237
+ if ( mResizeMode == QgsComposerPicture::Clip )
238
+ {
239
+ QgsComposerItem::setSceneRect ( rectangle );
240
+ mPictureWidth = rectangle.width ();
241
+ mPictureHeight = rectangle.height ();
242
+ return ;
243
+ }
244
+
245
+ QRectF newRect = rectangle;
246
+
247
+ if ( mResizeMode == ZoomResizeFrame && !rect ().isEmpty () )
248
+ {
249
+ // if width has changed less than height, then fix width and set height correspondingly
250
+ // else, do the opposite
251
+ if ( qAbs ( rect ().width () - rectangle.width () ) <
252
+ qAbs ( rect ().height () - rectangle.height () ) )
253
+ {
254
+ newRect.setHeight ( currentPictureSize.height () * newRect.width () / currentPictureSize.width () );
255
+ }
256
+ else
257
+ {
258
+ newRect.setWidth ( currentPictureSize.width () * newRect.height () / currentPictureSize.height () );
259
+ }
260
+ }
261
+ else if ( mResizeMode == FrameToImageSize )
262
+ {
263
+ newRect.setWidth ( currentPictureSize.width () * 25.4 / mComposition ->printResolution () );
264
+ newRect.setHeight ( currentPictureSize.height () * 25.4 / mComposition ->printResolution () );
265
+ }
266
+
267
+ // find largest scaling of picture with this rotation which fits in item
268
+ if ( mResizeMode == Zoom )
269
+ {
270
+ QRectF rotatedImageRect = largestRotatedRectWithinBounds ( QRectF ( 0 , 0 , currentPictureSize.width (), currentPictureSize.height () ), newRect, mPictureRotation );
271
+ mPictureWidth = rotatedImageRect.width ();
272
+ mPictureHeight = rotatedImageRect.height ();
273
+ }
274
+ else
275
+ {
276
+ mPictureWidth = newRect.width ();
277
+ mPictureHeight = newRect.height ();
278
+ }
279
+
280
+ QgsComposerItem::setSceneRect ( newRect );
215
281
emit itemChanged ();
216
282
}
217
283
@@ -225,13 +291,16 @@ void QgsComposerPicture::setPictureRotation( double r )
225
291
{
226
292
mPictureRotation = r;
227
293
228
- // find largest scaling of picture with this rotation which fits in item
229
- QSizeF currentPictureSize = pictureSize ();
230
- QRectF rotatedImageRect = largestRotatedRectWithinBounds ( QRectF ( 0 , 0 , currentPictureSize.width (), currentPictureSize.height () ), rect (), mPictureRotation );
231
- mPictureWidth = rotatedImageRect.width ();
232
- mPictureHeight = rotatedImageRect.height ();
294
+ if ( mResizeMode == Zoom )
295
+ {
296
+ // find largest scaling of picture with this rotation which fits in item
297
+ QSizeF currentPictureSize = pictureSize ();
298
+ QRectF rotatedImageRect = largestRotatedRectWithinBounds ( QRectF ( 0 , 0 , currentPictureSize.width (), currentPictureSize.height () ), rect (), mPictureRotation );
299
+ mPictureWidth = rotatedImageRect.width ();
300
+ mPictureHeight = rotatedImageRect.height ();
301
+ update ();
302
+ }
233
303
234
- update ();
235
304
emit pictureRotationChanged ( mPictureRotation );
236
305
}
237
306
@@ -264,6 +333,18 @@ void QgsComposerPicture::setRotationMap( int composerMapId )
264
333
emit pictureRotationChanged ( mPictureRotation );
265
334
}
266
335
336
+ void QgsComposerPicture::setResizeMode ( QgsComposerPicture::ResizeMode mode )
337
+ {
338
+ mResizeMode = mode;
339
+ if ( mode == QgsComposerPicture::ZoomResizeFrame || mode == QgsComposerPicture::FrameToImageSize
340
+ || ( mode == QgsComposerPicture::Zoom && mPictureRotation != 0 ) )
341
+ {
342
+ // call set scene rect to force item to resize to fit picture
343
+ setSceneRect ( QRectF ( pos ().x (), pos ().y (), rect ().width (), rect ().height () ) );
344
+ }
345
+ update ();
346
+ }
347
+
267
348
QString QgsComposerPicture::pictureFile () const
268
349
{
269
350
return mSourceFile .fileName ();
@@ -279,6 +360,7 @@ bool QgsComposerPicture::writeXML( QDomElement& elem, QDomDocument & doc ) const
279
360
composerPictureElem.setAttribute ( " file" , QgsProject::instance ()->writePath ( mSourceFile .fileName () ) );
280
361
composerPictureElem.setAttribute ( " pictureWidth" , QString::number ( mPictureWidth ) );
281
362
composerPictureElem.setAttribute ( " pictureHeight" , QString::number ( mPictureHeight ) );
363
+ composerPictureElem.setAttribute ( " resizeMode" , QString::number (( int )mResizeMode ) );
282
364
283
365
// rotation
284
366
composerPictureElem.setAttribute ( " pictureRotation" , QString::number ( mPictureRotation ) );
@@ -305,6 +387,7 @@ bool QgsComposerPicture::readXML( const QDomElement& itemElem, const QDomDocumen
305
387
306
388
mPictureWidth = itemElem.attribute ( " pictureWidth" , " 10" ).toDouble ();
307
389
mPictureHeight = itemElem.attribute ( " pictureHeight" , " 10" ).toDouble ();
390
+ mResizeMode = QgsComposerPicture::ResizeMode ( itemElem.attribute ( " resizeMode" , " 0" ).toInt () );
308
391
309
392
QDomNodeList composerItemList = itemElem.elementsByTagName ( " ComposerItem" );
310
393
if ( composerItemList.size () > 0 )
0 commit comments