Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mhugent committed Dec 24, 2010
1 parent 363ddcf commit 9cb73c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 55 deletions.
61 changes: 14 additions & 47 deletions src/core/composer/qgscomposerpicture.cpp
Expand Up @@ -28,16 +28,17 @@


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

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


QgsComposerPicture::~QgsComposerPicture()
{

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

int newDpi = ( painter->device()->logicalDpiX() + painter->device()->logicalDpiY() ) / 2;
double viewScaleFactor = horizontalViewScaleFactor();

if ( newDpi != mCachedDpi || mCachedRotation != mRotation || mCachedViewScaleFactor != viewScaleFactor )
{
mSvgCacheUpToDate = false;
}

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

if ( mMode == SVG )
{
if ( !mSvgCacheUpToDate )
{
//make nicer preview
if ( mComposition && mComposition->plotStyle() == QgsComposition::Preview )
{
boundImageWidth *= qMin( viewScaleFactor, 10.0 );
boundImageHeight *= qMin( viewScaleFactor, 10.0 );
}
mImage = QImage( boundImageWidth, boundImageHeight, QImage::Format_ARGB32 );
updateImageFromSvg();
}
}

painter->save();
painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
painter->rotate( mRotation );
painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );

painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
if ( mMode == SVG )
{
mSVG.render( painter, QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) );
}
else if ( mMode == RASTER )
{
painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
}

painter->restore();
}

mCachedDpi = newDpi;
mCachedRotation = mRotation;
mCachedViewScaleFactor = viewScaleFactor;

//frame and selection boxes
drawFrame( painter );
if ( isSelected() )
Expand All @@ -129,14 +112,13 @@ void QgsComposerPicture::setPictureFile( const QString& path )
if ( sourceFileSuffix.compare( "svg", Qt::CaseInsensitive ) == 0 )
{
//try to open svg
QSvgRenderer validTestRenderer( mSourceFile.fileName() );
if ( validTestRenderer.isValid() )
mSVG.load( mSourceFile.fileName() );
if ( mSVG.isValid() )
{
mMode = SVG;
QRect viewBox = validTestRenderer.viewBox(); //take width/height ratio from view box instead of default size
QRect viewBox = mSVG.viewBox(); //take width/height ratio from view box instead of default size
mDefaultSvgSize.setWidth( viewBox.width() );
mDefaultSvgSize.setHeight( viewBox.height() );
mSvgCacheUpToDate = false;
}
else
{
Expand Down Expand Up @@ -217,21 +199,8 @@ QRectF QgsComposerPicture::boundedSVGRect( double deviceWidth, double deviceHeig
}
#endif //0

void QgsComposerPicture::updateImageFromSvg()
{
mImage.fill( 0 );
QPainter p( &mImage );
p.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing, true );
QSvgRenderer theRenderer( mSourceFile.fileName() );
theRenderer.render( &p );
mSvgCacheUpToDate = true;
}



void QgsComposerPicture::setSceneRect( const QRectF& rectangle )
{
mSvgCacheUpToDate = false;
QgsComposerItem::setSceneRect( rectangle );

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


mSvgCacheUpToDate = false;
mDefaultSvgSize = QSize( 0, 0 );
mCachedDpi = 0;

QString fileName = QgsProject::instance()->readPath( itemElem.attribute( "file" ) );
setPictureFile( fileName );
Expand Down
10 changes: 2 additions & 8 deletions src/core/composer/qgscomposerpicture.h
Expand Up @@ -20,6 +20,7 @@
#include "qgscomposeritem.h"
#include <QFile>
#include <QImage>
#include <QSvgRenderer>

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

/**Updates content of mImage using svg generator*/
void updateImageFromSvg();


QImage mImage;
QSvgRenderer mSVG;
QFile mSourceFile;
Mode mMode;
/**False if image needs to be rendered from svg*/
bool mSvgCacheUpToDate;
int mCachedDpi; //store dpis for which the svg cache is valid
double mCachedRotation; //store last rotation value to generate new pixmap from svg on change
double mCachedViewScaleFactor;

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

0 comments on commit 9cb73c0

Please sign in to comment.