Skip to content
Permalink
Browse files

More changes for multipage print

  • Loading branch information
mhugent committed Jul 16, 2012
1 parent b8932af commit 7230ec90a56957b6817d17509967fb849b9e440f
Showing with 97 additions and 82 deletions.
  1. +93 −82 src/app/composer/qgscomposer.cpp
  2. +4 −0 src/app/composer/qgscomposer.h
@@ -592,75 +592,87 @@ void QgsComposer::print( QPrinter &printer )
QApplication::setOverrideCursor( Qt::BusyCursor );

bool printAsRaster = mComposition->printAsRaster();
//mView->setScene( 0 );

if ( printAsRaster )
{
//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 );
QRectF sourceArea( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
QRectF targetArea( 0, 0, width, height );
mView->setPaintingEnabled( false );
mComposition->render( &imagePainter, targetArea, sourceArea );
mView->setPaintingEnabled( true );
p.drawImage( targetArea, image, targetArea );
}
else
for ( int i = 0; i < mComposition->numPages(); ++i )
{
QApplication::restoreOverrideCursor();
int answer = QMessageBox::warning( 0,
tr( "Image too large" ),
tr( "Creation of image with %1x%2 pixels failed. Retry without 'Print As Raster'?" )
.arg( width ).arg( height ),
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Ok );
if ( answer == QMessageBox::Cancel )
if ( i > 0 )
{
mComposition->setPlotStyle( savedPlotStyle );
return;
printer.newPage();
}

QApplication::setOverrideCursor( Qt::BusyCursor );
printAsRaster = false;
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 )
{
//better in case of custom page size, but only possible with Qt>=4.4.0
QRectF paperRectMM = printer.pageRect( QPrinter::Millimeter );
QRectF paperRectPixel = printer.pageRect( QPrinter::DevicePixel );

mView->setPaintingEnabled( false );
for ( int i = 0; i < mComposition->numPages(); ++i )
{
if ( i > 0 )
{
if ( !printer.newPage() )
{
return;
}
printer.newPage();
}
mComposition->renderPage( &p, i );
}
//mComposition->render( &p, paperRectPixel, paperRectMM );
mView->setPaintingEnabled( true );
}

mComposition->setPlotStyle( savedPlotStyle );
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 )
{
return;
}

if ( containsWMSLayer() )
{
showWMSPrintingWarning();
@@ -688,36 +700,25 @@ void QgsComposer::on_mActionExportAsImage_triggered()

QPair<QString, QString> fileNExt = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );

QgsDebugMsg( QString( "Selected filter: %1" ).arg( fileNExt.first ) );
QgsDebugMsg( QString( "Image type: %1" ).arg( fileNExt.second ) );

if ( fileNExt.first.isEmpty() )
return;

QImage image( QSize( width, height ), QImage::Format_ARGB32 );
if ( image.isNull() )
{
QMessageBox::warning( 0,
tr( "Image too big" ),
tr( "Creation of image with %1x%2 pixels failed. Export aborted." )
.arg( width ).arg( height ),
QMessageBox::Ok );
return;
}

mComposition->setPlotStyle( QgsComposition::Print );
image.setDotsPerMeterX( mComposition->printResolution() / 25.4 * 1000 );
image.setDotsPerMeterY( mComposition->printResolution() / 25.4 * 1000 );
image.fill( 0 );
QPainter p( &image );
QRectF sourceArea( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
QRectF targetArea( 0, 0, width, height );
mView->setPaintingEnabled( false );
mComposition->render( &p, targetArea, sourceArea );
p.end();
mComposition->setPlotStyle( QgsComposition::Preview );
mView->setPaintingEnabled( true );
image.save( fileNExt.first, fileNExt.second.toLocal8Bit().constData() );
for ( int i = 0; i < mComposition->numPages(); ++i )
{
QImage image = printPageAsRaster( i );
if ( i == 0 )
{
image.save( fileNExt.first, fileNExt.second.toLocal8Bit().constData() );
}
else
{
QFileInfo fi( fileNExt.first );
QString outputFilePath = fi.absolutePath() + "/" + fi.baseName() + "_" + QString::number( i + 1 ) + "." + fi.suffix();
image.save( outputFilePath, fileNExt.second.toLocal8Bit().constData() );
}
}
}


@@ -773,29 +774,39 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
settings.setValue( "/UI/lastSaveAsSvgFile", outputFileName );
mComposition->setPlotStyle( QgsComposition::Print );

QSvgGenerator generator;
mView->setPaintingEnabled( false );
for ( int i = 0; i < mComposition->numPages(); ++i )
{
QSvgGenerator generator;
#if QT_VERSION >= 0x040500
generator.setTitle( QgsProject::instance()->title() );
generator.setTitle( QgsProject::instance()->title() );
#endif
generator.setFileName( outputFileName );
//width in pixel
int width = ( int )( mComposition->paperWidth() * mComposition->printResolution() / 25.4 );
//height in pixel
int height = ( int )( mComposition->paperHeight() * mComposition->printResolution() / 25.4 );
generator.setSize( QSize( width, height ) );
if ( i == 0 )
{
generator.setFileName( outputFileName );
}
else
{
QFileInfo fi( outputFileName );
generator.setFileName( fi.absolutePath() + "/" + fi.baseName() + "_" + QString::number( i + 1 ) + "." + fi.suffix() );
}

//width in pixel
int width = ( int )( mComposition->paperWidth() * mComposition->printResolution() / 25.4 );
//height in pixel
int height = ( int )( mComposition->paperHeight() * mComposition->printResolution() / 25.4 );
generator.setSize( QSize( width, height ) );
#if QT_VERSION >= 0x040500
generator.setViewBox( QRect( 0, 0, width, height ) );
generator.setViewBox( QRect( 0, 0, width, height ) );
#endif
generator.setResolution( mComposition->printResolution() ); //because the rendering is done in mm, convert the dpi
generator.setResolution( mComposition->printResolution() ); //because the rendering is done in mm, convert the dpi

QPainter p( &generator );
QPainter p( &generator );

QRectF sourceArea( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
QRectF targetArea( 0, 0, width, height );
mView->setPaintingEnabled( false );
mComposition->render( &p, targetArea, sourceArea );
p.end();
mComposition->setPlotStyle( QgsComposition::Preview );
mComposition->renderPage( &p, i );
p.end();
mComposition->setPlotStyle( QgsComposition::Preview );
}
mView->setPaintingEnabled( true );
}

@@ -283,6 +283,10 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! 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 );

0 comments on commit 7230ec9

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