Skip to content
Permalink
Browse files

Merge branch 'composer_multi_pages'

  • Loading branch information
mhugent committed Jul 17, 2012
2 parents 03b0526 + 19d6499 commit 3a663fbcb7592bcb28ffd5a3298251635ef695f7
@@ -39,6 +39,11 @@ class QgsComposition: QGraphicsScene
/**Returns width of paper item*/
double paperWidth() const;

/**Note: added in version 1.9*/
void setNumPages( int pages );
/**Note: added in version 1.9*/
int numPages() const;

void setSnapToGridEnabled( bool b );
bool snapToGridEnabled() const;

@@ -172,4 +177,8 @@ class QgsComposition: 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 );

/**Render a page to a paint device
@note added in version 1.9*/
void renderPage( QPainter* p, int page );
};
@@ -478,7 +478,13 @@ void QgsComposer::zoomFull( void )
{
if ( mView )
{
mView->fitInView( 0, 0, mComposition->paperWidth() + 1, mComposition->paperHeight() + 1, Qt::KeepAspectRatio );
int nPages = mComposition->numPages();
if ( nPages < 1 )
{
return;
}
double height = mComposition->paperHeight() * nPages + mComposition->spaceBetweenPages() * ( nPages - 1 );
mView->fitInView( 0, 0, mComposition->paperWidth() + 1, height + 1, Qt::KeepAspectRatio );
}
}

@@ -592,64 +598,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() )
for ( int i = 0; i < mComposition->numPages(); ++i )
{
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
{
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 );
mComposition->render( &p, paperRectPixel, paperRectMM );
for ( int i = 0; i < mComposition->numPages(); ++i )
{
if ( i > 0 )
{
printer.newPage();
}
mComposition->renderPage( &p, i );
}
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();
@@ -677,36 +706,30 @@ 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;
}

QgsComposition::PlotStyle savedPlotStyle = mComposition->plotStyle();
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() );
}
}

mComposition->setPlotStyle( savedPlotStyle );
}


@@ -760,31 +783,44 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
}

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

QgsComposition::PlotStyle savedPlotStyle = mComposition->plotStyle();
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( savedPlotStyle );
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 );

@@ -41,6 +41,8 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )

if ( mComposition )
{
mNumPagesSpinBox->setValue( mComposition->numPages() );

//read printout resolution from composition
mResolutionSpinBox->setValue( mComposition->printResolution() );

@@ -328,6 +330,15 @@ void QgsCompositionWidget::on_mPaperHeightDoubleSpinBox_editingFinished()
applyWidthHeight();
}

void QgsCompositionWidget::on_mNumPagesSpinBox_valueChanged( int value )
{
if ( !mComposition )
{
return;
}
mComposition->setNumPages( value );
}

void QgsCompositionWidget::displayCompositionWidthHeight()
{
if ( !mComposition )
@@ -519,6 +530,7 @@ void QgsCompositionWidget::blockSignals( bool block )
mPaperUnitsComboBox->blockSignals( block );
mPaperWidthDoubleSpinBox->blockSignals( block );
mPaperHeightDoubleSpinBox->blockSignals( block );
mNumPagesSpinBox->blockSignals( block );
mPaperOrientationComboBox->blockSignals( block );
mResolutionSpinBox->blockSignals( block );
mPrintAsRasterCheckBox->blockSignals( block );
@@ -45,6 +45,7 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
void on_mPaperOrientationComboBox_currentIndexChanged( const QString& text );
void on_mPaperWidthDoubleSpinBox_editingFinished();
void on_mPaperHeightDoubleSpinBox_editingFinished();
void on_mNumPagesSpinBox_valueChanged( int value );
void on_mResolutionSpinBox_valueChanged( const int value );
void on_mPrintAsRasterCheckBox_stateChanged( int state );

0 comments on commit 3a663fb

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