[QGIS Commit] r15124 - in trunk/qgis/src: core/composer mapserver

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Feb 3 10:08:37 EST 2011


Author: mhugent
Date: 2011-02-03 07:08:37 -0800 (Thu, 03 Feb 2011)
New Revision: 15124

Modified:
   trunk/qgis/src/core/composer/qgscomposition.cpp
   trunk/qgis/src/core/composer/qgscomposition.h
   trunk/qgis/src/mapserver/qgswmsserver.cpp
   trunk/qgis/src/mapserver/qgswmsserver.h
Log:
Option to print as raster also via WMS (mainly as a workaround because of some Qt pdf problems

Modified: trunk/qgis/src/core/composer/qgscomposition.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposition.cpp	2011-02-03 09:25:33 UTC (rev 15123)
+++ trunk/qgis/src/core/composer/qgscomposition.cpp	2011-02-03 15:08:37 UTC (rev 15124)
@@ -25,7 +25,7 @@
 #include <QSettings>
 
 QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ):
-    QGraphicsScene( 0 ), mMapRenderer( mapRenderer ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mSnapToGrid( false ),
+    QGraphicsScene( 0 ), mMapRenderer( mapRenderer ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster( false ), mSnapToGrid( false ),
     mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
 {
   setBackgroundBrush( Qt::gray );
@@ -37,10 +37,6 @@
   mPaperItem->setZValue( 0 );
   mPrintResolution = 300; //hardcoded default
   loadGridAppearanceSettings();
-
-  //mPrintAsRaster
-  QSettings s;
-  mPrintAsRaster = s.value( "/qgis/composerPrintAsRaster", false ).toBool();
 }
 
 QgsComposition::QgsComposition():
@@ -48,9 +44,6 @@
     mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
 {
   loadGridAppearanceSettings();
-  //mPrintAsRaster
-  QSettings s;
-  mPrintAsRaster = s.value( "/qgis/composerPrintAsRaster", false ).toBool();
 }
 
 QgsComposition::~QgsComposition()
@@ -197,6 +190,7 @@
   compositionElem.setAttribute( "snapGridOffsetY", mSnapGridOffsetY );
 
   compositionElem.setAttribute( "printResolution", mPrintResolution );
+  compositionElem.setAttribute( "printAsRaster", mPrintAsRaster );
 
   composerElem.appendChild( compositionElem );
 
@@ -236,6 +230,7 @@
   mSnapGridResolution = compositionElem.attribute( "snapGridResolution" ).toDouble();
   mSnapGridOffsetX = compositionElem.attribute( "snapGridOffsetX" ).toDouble();
   mSnapGridOffsetY = compositionElem.attribute( "snapGridOffsetY" ).toDouble();
+  mPrintAsRaster = compositionElem.attribute( "printAsRaster" ).toInt();
 
   mPrintResolution = compositionElem.attribute( "printResolution", "300" ).toInt();
 
@@ -716,13 +711,6 @@
   saveGridAppearanceSettings();
 }
 
-void QgsComposition::setPrintAsRaster( bool enabled )
-{
-  mPrintAsRaster = enabled;
-  QSettings s;
-  s.setValue( "/qgis/composerPrintAsRaster", QVariant( mPrintAsRaster ) );
-}
-
 void QgsComposition::loadGridAppearanceSettings()
 {
   //read grid style, grid color and pen width from settings

Modified: trunk/qgis/src/core/composer/qgscomposition.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposition.h	2011-02-03 09:25:33 UTC (rev 15123)
+++ trunk/qgis/src/core/composer/qgscomposition.h	2011-02-03 15:08:37 UTC (rev 15124)
@@ -107,7 +107,7 @@
     void setPrintResolution( int dpi ) {mPrintResolution = dpi;}
 
     bool printAsRaster() const {return mPrintAsRaster;}
-    void setPrintAsRaster( bool enabled );
+    void setPrintAsRaster( bool enabled ) { mPrintAsRaster = enabled; }
 
     /**Returns pointer to map renderer of qgis map canvas*/
     QgsMapRenderer* mapRenderer() {return mMapRenderer;}

Modified: trunk/qgis/src/mapserver/qgswmsserver.cpp
===================================================================
--- trunk/qgis/src/mapserver/qgswmsserver.cpp	2011-02-03 09:25:33 UTC (rev 15123)
+++ trunk/qgis/src/mapserver/qgswmsserver.cpp	2011-02-03 15:08:37 UTC (rev 15124)
@@ -383,26 +383,32 @@
     QPainter p( &generator );
     QRectF sourceArea( 0, 0, c->paperWidth(), c->paperHeight() );
     QRectF targetArea( 0, 0, width, height );
-    c->render( &p, targetArea, sourceArea );
+    if ( c->printAsRaster() ) //embed one raster into the svg
+    {
+      QImage* img = printCompositionToImage( c );
+      if ( img )
+      {
+        p.drawImage( targetArea, *img, QRectF( 0, 0, img->width(), img->height() ) );
+      }
+      delete img;
+    }
+    else
+    {
+      c->render( &p, targetArea, sourceArea );
+    }
     p.end();
   }
   else if ( formatString.compare( "png", Qt::CaseInsensitive ) == 0 || formatString.compare( "jpg", Qt::CaseInsensitive ) == 0 )
   {
-    int width = ( int )( c->paperWidth() * c->printResolution() / 25.4 ); //width in pixel
-    int height = ( int )( c->paperHeight() * c->printResolution() / 25.4 ); //height in pixel
-    QImage image( QSize( width, height ), QImage::Format_ARGB32 );
-    image.setDotsPerMeterX( c->printResolution() / 25.4 * 1000 );
-    image.setDotsPerMeterY( c->printResolution() / 25.4 * 1000 );
-    image.fill( 0 );
-    QPainter p( &image );
-    QRectF sourceArea( 0, 0, c->paperWidth(), c->paperHeight() );
-    QRectF targetArea( 0, 0, width, height );
-    c->render( &p, targetArea, sourceArea );
-    p.end();
-    ba = new QByteArray();
-    QBuffer buffer( ba );
-    buffer.open( QIODevice::WriteOnly );
-    image.save( &buffer, formatString.toLocal8Bit().data(), -1 );
+    QImage* image = printCompositionToImage( c );
+    if ( image )
+    {
+      ba = new QByteArray();
+      QBuffer buffer( ba );
+      buffer.open( QIODevice::WriteOnly );
+      image->save( &buffer, formatString.toLocal8Bit().data(), -1 );
+    }
+    delete image;
   }
   else if ( formatString.compare( "pdf", Qt::CaseInsensitive ) == 0 )
   {
@@ -419,11 +425,22 @@
     printer.setOutputFormat( QPrinter::PdfFormat );
     printer.setOutputFileName( tempFile.fileName() );
     printer.setPaperSize( QSizeF( c->paperWidth(), c->paperHeight() ), QPrinter::Millimeter );
-
     QRectF paperRectMM = printer.pageRect( QPrinter::Millimeter );
     QRectF paperRectPixel = printer.pageRect( QPrinter::DevicePixel );
     QPainter p( &printer );
-    c->render( &p, paperRectPixel, paperRectMM );
+    if ( c->printAsRaster() ) //embed one raster into the pdf
+    {
+      QImage* img = printCompositionToImage( c );
+      if ( img )
+      {
+        p.drawImage( paperRectPixel, *img, QRectF( 0, 0, img->width(), img->height() ) );
+      }
+      delete img;
+    }
+    else //vector pdf
+    {
+      c->render( &p, paperRectPixel, paperRectMM );
+    }
     p.end();
 
     ba = new QByteArray();
@@ -438,6 +455,22 @@
   return ba;
 }
 
+QImage* QgsWMSServer::printCompositionToImage( QgsComposition* c ) const
+{
+  int width = ( int )( c->paperWidth() * c->printResolution() / 25.4 ); //width in pixel
+  int height = ( int )( c->paperHeight() * c->printResolution() / 25.4 ); //height in pixel
+  QImage* image = new QImage( QSize( width, height ), QImage::Format_ARGB32 );
+  image->setDotsPerMeterX( c->printResolution() / 25.4 * 1000 );
+  image->setDotsPerMeterY( c->printResolution() / 25.4 * 1000 );
+  image->fill( 0 );
+  QPainter p( image );
+  QRectF sourceArea( 0, 0, c->paperWidth(), c->paperHeight() );
+  QRectF targetArea( 0, 0, width, height );
+  c->render( &p, targetArea, sourceArea );
+  p.end();
+  return image;
+}
+
 QImage* QgsWMSServer::getMap()
 {
   QStringList layersList, stylesList, layerIdList;

Modified: trunk/qgis/src/mapserver/qgswmsserver.h
===================================================================
--- trunk/qgis/src/mapserver/qgswmsserver.h	2011-02-03 09:25:33 UTC (rev 15123)
+++ trunk/qgis/src/mapserver/qgswmsserver.h	2011-02-03 15:08:37 UTC (rev 15124)
@@ -26,6 +26,7 @@
 class QgsCoordinateReferenceSystem;
 class QgsComposerLayerItem;
 class QgsComposerLegendItem;
+class QgsComposition;
 class QgsMapLayer;
 class QgsMapRenderer;
 class QgsPoint;
@@ -135,6 +136,8 @@
     void drawLegendSymbolV2( QgsComposerLegendItem* item, QPainter* p, double boxSpace, double currentY, double& symbolWidth, double& symbolHeight, double dpi, double yDownShift ) const;
     void drawRasterSymbol( QgsComposerLegendItem* item, QPainter* p, double boxSpace, double currentY, double symbolWidth, double symbolHeight, double yDownShift ) const;
 
+    QImage* printCompositionToImage( QgsComposition* c ) const;
+
     /**Map containing the WMS parameters*/
     std::map<QString, QString> mParameterMap;
     QgsConfigParser* mConfigParser;



More information about the QGIS-commit mailing list