Skip to content

Commit 9cb73c0

Browse files
author
mhugent
committedDec 24, 2010
Apply patch #3295 to draw SVG composer pictures as vectors, provided by JD
git-svn-id: http://svn.osgeo.org/qgis/trunk@14972 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 363ddcf commit 9cb73c0

File tree

2 files changed

+16
-55
lines changed

2 files changed

+16
-55
lines changed
 

‎src/core/composer/qgscomposerpicture.cpp

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@
2828

2929

3030
QgsComposerPicture::QgsComposerPicture( QgsComposition *composition ): QgsComposerItem( composition ), mMode( Unknown ), \
31-
mSvgCacheUpToDate( false ), mCachedDpi( 0 ), mCachedRotation( 0 ), mCachedViewScaleFactor( -1 ), mRotationMap( 0 )
31+
mRotationMap( 0 )
3232
{
3333
mPictureWidth = rect().width();
3434
}
3535

36-
QgsComposerPicture::QgsComposerPicture(): QgsComposerItem( 0 ), mMode( Unknown ), mSvgCacheUpToDate( false ), mCachedRotation( 0 ), mCachedViewScaleFactor( -1 ), mRotationMap( 0 )
36+
QgsComposerPicture::QgsComposerPicture(): QgsComposerItem( 0 ), mMode( Unknown ), mRotationMap( 0 )
3737
{
3838
mPictureHeight = rect().height();
3939
}
4040

41+
4142
QgsComposerPicture::~QgsComposerPicture()
4243
{
4344

@@ -53,12 +54,6 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
5354
drawBackground( painter );
5455

5556
int newDpi = ( painter->device()->logicalDpiX() + painter->device()->logicalDpiY() ) / 2;
56-
double viewScaleFactor = horizontalViewScaleFactor();
57-
58-
if ( newDpi != mCachedDpi || mCachedRotation != mRotation || mCachedViewScaleFactor != viewScaleFactor )
59-
{
60-
mSvgCacheUpToDate = false;
61-
}
6257

6358
if ( mMode != Unknown )
6459
{
@@ -79,35 +74,23 @@ void QgsComposerPicture::paint( QPainter* painter, const QStyleOptionGraphicsIte
7974
double boundImageWidth = boundRect.width();
8075
double boundImageHeight = boundRect.height();
8176

82-
if ( mMode == SVG )
83-
{
84-
if ( !mSvgCacheUpToDate )
85-
{
86-
//make nicer preview
87-
if ( mComposition && mComposition->plotStyle() == QgsComposition::Preview )
88-
{
89-
boundImageWidth *= qMin( viewScaleFactor, 10.0 );
90-
boundImageHeight *= qMin( viewScaleFactor, 10.0 );
91-
}
92-
mImage = QImage( boundImageWidth, boundImageHeight, QImage::Format_ARGB32 );
93-
updateImageFromSvg();
94-
}
95-
}
96-
9777
painter->save();
9878
painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
9979
painter->rotate( mRotation );
10080
painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
10181

102-
painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
82+
if ( mMode == SVG )
83+
{
84+
mSVG.render( painter, QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) );
85+
}
86+
else if ( mMode == RASTER )
87+
{
88+
painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
89+
}
10390

10491
painter->restore();
10592
}
10693

107-
mCachedDpi = newDpi;
108-
mCachedRotation = mRotation;
109-
mCachedViewScaleFactor = viewScaleFactor;
110-
11194
//frame and selection boxes
11295
drawFrame( painter );
11396
if ( isSelected() )
@@ -129,14 +112,13 @@ void QgsComposerPicture::setPictureFile( const QString& path )
129112
if ( sourceFileSuffix.compare( "svg", Qt::CaseInsensitive ) == 0 )
130113
{
131114
//try to open svg
132-
QSvgRenderer validTestRenderer( mSourceFile.fileName() );
133-
if ( validTestRenderer.isValid() )
115+
mSVG.load( mSourceFile.fileName() );
116+
if ( mSVG.isValid() )
134117
{
135118
mMode = SVG;
136-
QRect viewBox = validTestRenderer.viewBox(); //take width/height ratio from view box instead of default size
119+
QRect viewBox = mSVG.viewBox(); //take width/height ratio from view box instead of default size
137120
mDefaultSvgSize.setWidth( viewBox.width() );
138121
mDefaultSvgSize.setHeight( viewBox.height() );
139-
mSvgCacheUpToDate = false;
140122
}
141123
else
142124
{
@@ -217,21 +199,8 @@ QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeig
217199
}
218200
#endif //0
219201

220-
void QgsComposerPicture::updateImageFromSvg()
221-
{
222-
mImage.fill( 0 );
223-
QPainter p( &mImage );
224-
p.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing, true );
225-
QSvgRenderer theRenderer( mSourceFile.fileName() );
226-
theRenderer.render( &p );
227-
mSvgCacheUpToDate = true;
228-
}
229-
230-
231-
232202
void QgsComposerPicture::setSceneRect( const QRectF& rectangle )
233203
{
234-
mSvgCacheUpToDate = false;
235204
QgsComposerItem::setSceneRect( rectangle );
236205

237206
//consider to change size of the shape if the rectangle changes width and/or height
@@ -333,9 +302,7 @@ bool QgsComposerPicture::readXML( const QDomElement& itemElem, const QDomDocumen
333302
}
334303

335304

336-
mSvgCacheUpToDate = false;
337305
mDefaultSvgSize = QSize( 0, 0 );
338-
mCachedDpi = 0;
339306

340307
QString fileName = QgsProject::instance()->readPath( itemElem.attribute( "file" ) );
341308
setPictureFile( fileName );

‎src/core/composer/qgscomposerpicture.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgscomposeritem.h"
2121
#include <QFile>
2222
#include <QImage>
23+
#include <QSvgRenderer>
2324

2425
/** \ingroup MapComposer
2526
* A composer class that displays svg files or raster format (jpg, png, ...)
@@ -84,18 +85,11 @@ class CORE_EXPORT QgsComposerPicture: public QgsComposerItem
8485
/**Calculates bounding rect for image such that aspect ratio is correct*/
8586
QRectF boundedImageRect( double deviceWidth, double deviceHeight );
8687

87-
/**Updates content of mImage using svg generator*/
88-
void updateImageFromSvg();
89-
9088

9189
QImage mImage;
90+
QSvgRenderer mSVG;
9291
QFile mSourceFile;
9392
Mode mMode;
94-
/**False if image needs to be rendered from svg*/
95-
bool mSvgCacheUpToDate;
96-
int mCachedDpi; //store dpis for which the svg cache is valid
97-
double mCachedRotation; //store last rotation value to generate new pixmap from svg on change
98-
double mCachedViewScaleFactor;
9993

10094
QSize mDefaultSvgSize;
10195
/**Map that sets the rotation (or 0 if this picture uses map independent rotation)*/

0 commit comments

Comments
 (0)