Skip to content

Commit

Permalink
Option to print as raster also via WMS (mainly as a workaround becaus…
Browse files Browse the repository at this point in the history
…e of some Qt pdf problems

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15124 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Feb 3, 2011
1 parent 6645348 commit 3f01957
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
18 changes: 3 additions & 15 deletions src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -37,20 +37,13 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ):
mPaperItem->setZValue( 0 );
mPrintResolution = 300; //hardcoded default
loadGridAppearanceSettings();

//mPrintAsRaster
QSettings s;
mPrintAsRaster = s.value( "/qgis/composerPrintAsRaster", false ).toBool();
}

QgsComposition::QgsComposition():
QGraphicsScene( 0 ), mMapRenderer( 0 ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster( false ),
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()
Expand Down Expand Up @@ -197,6 +190,7 @@ bool QgsComposition::writeXML( QDomElement& composerElem, QDomDocument& doc )
compositionElem.setAttribute( "snapGridOffsetY", mSnapGridOffsetY );

compositionElem.setAttribute( "printResolution", mPrintResolution );
compositionElem.setAttribute( "printAsRaster", mPrintAsRaster );

composerElem.appendChild( compositionElem );

Expand Down Expand Up @@ -236,6 +230,7 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
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();

Expand Down Expand Up @@ -716,13 +711,6 @@ void QgsComposition::setGridStyle( GridStyle s )
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
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
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;}
Expand Down
69 changes: 51 additions & 18 deletions src/mapserver/qgswmsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,26 +383,32 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
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 )
{
Expand All @@ -419,11 +425,22 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
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();
Expand All @@ -438,6 +455,22 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
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;
Expand Down
3 changes: 3 additions & 0 deletions src/mapserver/qgswmsserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
class QgsCoordinateReferenceSystem;
class QgsComposerLayerItem;
class QgsComposerLegendItem;
class QgsComposition;
class QgsMapLayer;
class QgsMapRenderer;
class QgsPoint;
Expand Down Expand Up @@ -135,6 +136,8 @@ class QgsWMSServer
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;
Expand Down

0 comments on commit 3f01957

Please sign in to comment.