Skip to content
Permalink
Browse files

Move non-gui part of printing functionality to composition

  • Loading branch information
mhugent committed Jul 18, 2012
1 parent fe2252d commit 717a1c34dcee71f01b6b22e1d4baa2f8138d2497
@@ -45,7 +45,6 @@
#include "qgsmessageviewer.h"
#include "qgscontexthelp.h"
#include "qgscursors.h"
#include "qgspaintenginehack.h"

#include <QCloseEvent>
#include <QCheckBox>
@@ -533,6 +532,16 @@ void QgsComposer::on_mActionRefreshView_triggered()

void QgsComposer::on_mActionExportAsPDF_triggered()
{
if ( !mComposition || !mView )
{
return;
}

if ( containsWMSLayer() )
{
showWMSPrintingWarning();
}

QSettings myQSettings; // where we keep last used filter in persistent state
QString lastUsedFile = myQSettings.value( "/UI/lastSaveAsPdfFile", "qgis.pdf" ).toString();
QFileInfo file( lastUsedFile );
@@ -543,7 +552,9 @@ void QgsComposer::on_mActionExportAsPDF_triggered()
file.path(),
tr( "PDF Format" ) + " (*.pdf *.PDF)" );
if ( outputFileName.isEmpty() )
{
return;
}

if ( !outputFileName.endsWith( ".pdf", Qt::CaseInsensitive ) )
{
@@ -552,129 +563,42 @@ void QgsComposer::on_mActionExportAsPDF_triggered()

myQSettings.setValue( "/UI/lastSaveAsPdfFile", outputFileName );

QPrinter printer;
printer.setOutputFormat( QPrinter::PdfFormat );
printer.setOutputFileName( outputFileName );
printer.setPaperSize( QSizeF( mComposition->paperWidth(), mComposition->paperHeight() ), QPrinter::Millimeter );

QgsPaintEngineHack::fixEngineFlags( printer.paintEngine() );
print( printer );
QApplication::setOverrideCursor( Qt::BusyCursor );
mView->setPaintingEnabled( false );
mComposition->exportAsPDF( outputFileName );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
}

void QgsComposer::on_mActionPrint_triggered()
{
//orientation and page size are already set to QPrinter in the page setup dialog
QPrintDialog printDialog( &mPrinter, 0 );
if ( printDialog.exec() != QDialog::Accepted )
if ( !mComposition || !mView )
{
return;
}

print( mPrinter );
}

void QgsComposer::print( QPrinter &printer )
{
if ( !mComposition || !mView )
return;

if ( containsWMSLayer() )
{
showWMSPrintingWarning();
}

//set resolution based on composer setting
printer.setFullPage( true );
printer.setColorMode( QPrinter::Color );

//set user-defined resolution
printer.setResolution( mComposition->printResolution() );

QPainter p( &printer );

QgsComposition::PlotStyle savedPlotStyle = mComposition->plotStyle();
mComposition->setPlotStyle( QgsComposition::Print );

QApplication::setOverrideCursor( Qt::BusyCursor );

bool printAsRaster = mComposition->printAsRaster();

if ( printAsRaster )
{
for ( int i = 0; i < mComposition->numPages(); ++i )
{
if ( i > 0 )
{
printer.newPage();
}

QImage image = printPageAsRaster( i );

if ( image.isNull() )
{
QApplication::restoreOverrideCursor();
int answer = QMessageBox::warning( 0,
tr( "Image too large" ),
tr( "Creation of image failed. Retry without 'Print As Raster'?" ),
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Ok );
if ( answer == QMessageBox::Cancel )
{
mComposition->setPlotStyle( savedPlotStyle );
return;
}

QApplication::setOverrideCursor( Qt::BusyCursor );
printAsRaster = false;
}
else
{
QRectF targetArea( 0, 0, image.width(), image.height() );
p.drawImage( targetArea, image, targetArea );
}
}
}

if ( !printAsRaster )
//orientation and page size are already set to QPrinter in the page setup dialog
QPrintDialog printDialog( &mPrinter, 0 );
if ( printDialog.exec() != QDialog::Accepted )
{
mView->setPaintingEnabled( false );
for ( int i = 0; i < mComposition->numPages(); ++i )
{
if ( i > 0 )
{
printer.newPage();
}
mComposition->renderPage( &p, i );
}
mView->setPaintingEnabled( true );
return;
}

mComposition->setPlotStyle( savedPlotStyle );
QApplication::setOverrideCursor( Qt::BusyCursor );
mView->setPaintingEnabled( false );
mComposition->print( mPrinter );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
}

QImage QgsComposer::printPageAsRaster( int page )
{
//print out via QImage, code copied from on_mActionExportAsImage_activated
int width = ( int )( mComposition->printResolution() * mComposition->paperWidth() / 25.4 );
int height = ( int )( mComposition-> printResolution() * mComposition->paperHeight() / 25.4 );
QImage image( QSize( width, height ), QImage::Format_ARGB32 );
if ( !image.isNull() )
{
image.setDotsPerMeterX( mComposition->printResolution() / 25.4 * 1000 );
image.setDotsPerMeterY( mComposition->printResolution() / 25.4 * 1000 );
image.fill( 0 );
QPainter imagePainter( &image );
mView->setPaintingEnabled( false );
mComposition->renderPage( &imagePainter, page );
mView->setPaintingEnabled( true );
}
return image;
}

void QgsComposer::on_mActionExportAsImage_triggered()
{
if ( !mComposition )
if ( !mComposition || !mView )
{
return;
}
@@ -711,12 +635,11 @@ void QgsComposer::on_mActionExportAsImage_triggered()
return;
}

QgsComposition::PlotStyle savedPlotStyle = mComposition->plotStyle();
mComposition->setPlotStyle( QgsComposition::Print );
mView->setPaintingEnabled( false );

for ( int i = 0; i < mComposition->numPages(); ++i )
{
QImage image = printPageAsRaster( i );
QImage image = mComposition->printPageAsRaster( i );
if ( i == 0 )
{
image.save( fileNExt.first, fileNExt.second.toLocal8Bit().constData() );
@@ -728,8 +651,7 @@ void QgsComposer::on_mActionExportAsImage_triggered()
image.save( outputFilePath, fileNExt.second.toLocal8Bit().constData() );
}
}

mComposition->setPlotStyle( savedPlotStyle );
mView->setPaintingEnabled( true );
}


@@ -784,9 +706,6 @@ void QgsComposer::on_mActionExportAsSVG_triggered()

settings.setValue( "/UI/lastSaveAsSvgFile", outputFileName );

QgsComposition::PlotStyle savedPlotStyle = mComposition->plotStyle();
mComposition->setPlotStyle( QgsComposition::Print );

mView->setPaintingEnabled( false );
for ( int i = 0; i < mComposition->numPages(); ++i )
{
@@ -819,8 +738,6 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
mComposition->renderPage( &p, i );
p.end();
}

mComposition->setPlotStyle( savedPlotStyle );
mView->setPaintingEnabled( true );
}

@@ -280,13 +280,6 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Changes elements that are not suitable for this project
void cleanupAfterTemplateRead();

//! Print to a printer object
void print( QPrinter &printer );

//! print composer page to image
//! If the image does not fit into memory, a null image is returned
QImage printPageAsRaster( int page );

//! Writes state under DOM element
void writeXML( QDomNode& parentNode, QDomDocument& doc );

@@ -16,7 +16,6 @@

#include "qgscomposition.h"
#include "qgscomposeritem.h"
#include "qgspaperitem.h"
#include "qgscomposerarrow.h"
#include "qgscomposerlabel.h"
#include "qgscomposerlegend.h"
@@ -27,10 +26,13 @@
#include "qgscomposershape.h"
#include "qgscomposerattributetable.h"
#include "qgslogger.h"
#include "qgspaintenginehack.h"
#include "qgspaperitem.h"
#include <QDomDocument>
#include <QDomElement>
#include <QGraphicsRectItem>
#include <QPainter>
#include <QPrinter>
#include <QSettings>

QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ):
@@ -1236,6 +1238,76 @@ void QgsComposition::removePaperItems()
mPages.clear();
}

void QgsComposition::exportAsPDF( const QString& file )
{
QPrinter printer;
printer.setOutputFormat( QPrinter::PdfFormat );
printer.setOutputFileName( file );
printer.setPaperSize( QSizeF( paperWidth(), paperHeight() ), QPrinter::Millimeter );

QgsPaintEngineHack::fixEngineFlags( printer.paintEngine() );
print( printer );
}

void QgsComposition::print( QPrinter &printer )
{
//set resolution based on composer setting
printer.setFullPage( true );
printer.setColorMode( QPrinter::Color );

//set user-defined resolution
printer.setResolution( printResolution() );

QPainter p( &printer );

if ( mPrintAsRaster )
{
for ( int i = 0; i < numPages(); ++i )
{
if ( i > 0 )
{
printer.newPage();
}

QImage image = printPageAsRaster( i );
if ( !image.isNull() )
{
QRectF targetArea( 0, 0, image.width(), image.height() );
p.drawImage( targetArea, image, targetArea );
}
}
}

if ( !mPrintAsRaster )
{
for ( int i = 0; i < numPages(); ++i )
{
if ( i > 0 )
{
printer.newPage();
}
renderPage( &p, i );
}
}
}

QImage QgsComposition::printPageAsRaster( int page )
{
//print out via QImage, code copied from on_mActionExportAsImage_activated
int width = ( int )( printResolution() * paperWidth() / 25.4 );
int height = ( int )( printResolution() * paperHeight() / 25.4 );
QImage image( QSize( width, height ), QImage::Format_ARGB32 );
if ( !image.isNull() )
{
image.setDotsPerMeterX( printResolution() / 25.4 * 1000 );
image.setDotsPerMeterY( printResolution() / 25.4 * 1000 );
image.fill( 0 );
QPainter imagePainter( &image );
renderPage( &imagePainter, page );
}
return image;
}

void QgsComposition::renderPage( QPainter* p, int page )
{
if ( mPages.size() <= page )
@@ -1256,5 +1328,11 @@ void QgsComposition::renderPage( QPainter* p, int page )
}

QRectF paperRect = QRectF( paperItem->transform().dx(), paperItem->transform().dy(), paperItem->rect().width(), paperItem->rect().height() );

QgsComposition::PlotStyle savedPlotStyle = mPlotStyle;
mPlotStyle = QgsComposition::Print;

render( p, QRectF( 0, 0, paintDevice->width(), paintDevice->height() ), paperRect );

mPlotStyle = savedPlotStyle;
}
@@ -221,6 +221,17 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
/**Convenience function to create a QgsAddRemoveItemCommand, connect its signals and push it to the undo stack*/
void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );


//printing

void exportAsPDF( const QString& file );

void print( QPrinter &printer );

//! print composer page to image
//! If the image does not fit into memory, a null image is returned
QImage printPageAsRaster( int page );

/**Render a page to a paint device
@note added in version 1.9*/
void renderPage( QPainter* p, int page );

0 comments on commit 717a1c3

Please sign in to comment.
You can’t perform that action at this time.