[QGIS Commit] r14972 - trunk/qgis/src/core/composer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Fri Dec 24 02:14:58 EST 2010


Author: mhugent
Date: 2010-12-23 23:14:58 -0800 (Thu, 23 Dec 2010)
New Revision: 14972

Modified:
   trunk/qgis/src/core/composer/qgscomposerpicture.cpp
   trunk/qgis/src/core/composer/qgscomposerpicture.h
Log:
Apply patch #3295 to draw SVG composer pictures as vectors, provided by JD

Modified: trunk/qgis/src/core/composer/qgscomposerpicture.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposerpicture.cpp	2010-12-23 18:11:33 UTC (rev 14971)
+++ trunk/qgis/src/core/composer/qgscomposerpicture.cpp	2010-12-24 07:14:58 UTC (rev 14972)
@@ -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()
 {
 
@@ -53,13 +54,7 @@
   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 )
   {
     double rectPixelWidth = /*rect().width()*/mPictureWidth * newDpi / 25.4;
@@ -79,35 +74,23 @@
     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() )
@@ -129,14 +112,13 @@
   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
     {
@@ -217,21 +199,8 @@
 }
 #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
@@ -333,9 +302,7 @@
   }
 
 
-  mSvgCacheUpToDate = false;
   mDefaultSvgSize = QSize( 0, 0 );
-  mCachedDpi = 0;
 
   QString fileName = QgsProject::instance()->readPath( itemElem.attribute( "file" ) );
   setPictureFile( fileName );

Modified: trunk/qgis/src/core/composer/qgscomposerpicture.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposerpicture.h	2010-12-23 18:11:33 UTC (rev 14971)
+++ trunk/qgis/src/core/composer/qgscomposerpicture.h	2010-12-24 07:14:58 UTC (rev 14972)
@@ -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, ...)
@@ -84,18 +85,11 @@
     /**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)*/



More information about the QGIS-commit mailing list