Skip to content

Commit

Permalink
Merge branch 'master' of github.com:qgis/Quantum-GIS
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jul 17, 2012
2 parents 0cfe094 + 74021a4 commit aea41ee
Show file tree
Hide file tree
Showing 22 changed files with 1,070 additions and 209 deletions.
9 changes: 9 additions & 0 deletions python/core/qgscomposition.sip
Expand Up @@ -39,6 +39,11 @@ class QgsComposition: QGraphicsScene
/**Returns width of paper item*/ /**Returns width of paper item*/
double paperWidth() const; 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 ); void setSnapToGridEnabled( bool b );
bool snapToGridEnabled() const; bool snapToGridEnabled() const;


Expand Down Expand Up @@ -172,4 +177,8 @@ class QgsComposition: QGraphicsScene


/**Convenience function to create a QgsAddRemoveItemCommand, connect its signals and push it to the undo stack*/ /**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 ); 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 );
}; };
192 changes: 114 additions & 78 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -478,7 +478,13 @@ void QgsComposer::zoomFull( void )
{ {
if ( mView ) 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 );
} }
} }


Expand Down Expand Up @@ -592,64 +598,87 @@ void QgsComposer::print( QPrinter &printer )
QApplication::setOverrideCursor( Qt::BusyCursor ); QApplication::setOverrideCursor( Qt::BusyCursor );


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


if ( printAsRaster ) if ( printAsRaster )
{ {
//print out via QImage, code copied from on_mActionExportAsImage_activated for ( int i = 0; i < mComposition->numPages(); ++i )
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 ); if ( i > 0 )
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 )
{ {
mComposition->setPlotStyle( savedPlotStyle ); printer.newPage();
return;
} }


QApplication::setOverrideCursor( Qt::BusyCursor ); QImage image = printPageAsRaster( i );
printAsRaster = false;
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 ) 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 ); 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 ); mView->setPaintingEnabled( true );
} }


mComposition->setPlotStyle( savedPlotStyle ); mComposition->setPlotStyle( savedPlotStyle );
QApplication::restoreOverrideCursor(); 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() void QgsComposer::on_mActionExportAsImage_triggered()
{ {
if ( !mComposition )
{
return;
}

if ( containsWMSLayer() ) if ( containsWMSLayer() )
{ {
showWMSPrintingWarning(); showWMSPrintingWarning();
Expand Down Expand Up @@ -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" ) ); 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() ) 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; return;
} }


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

mComposition->setPlotStyle( savedPlotStyle );
} }




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


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

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


QSvgGenerator generator; mView->setPaintingEnabled( false );
for ( int i = 0; i < mComposition->numPages(); ++i )
{
QSvgGenerator generator;
#if QT_VERSION >= 0x040500 #if QT_VERSION >= 0x040500
generator.setTitle( QgsProject::instance()->title() ); generator.setTitle( QgsProject::instance()->title() );
#endif #endif
generator.setFileName( outputFileName ); if ( i == 0 )
//width in pixel {
int width = ( int )( mComposition->paperWidth() * mComposition->printResolution() / 25.4 ); generator.setFileName( outputFileName );
//height in pixel }
int height = ( int )( mComposition->paperHeight() * mComposition->printResolution() / 25.4 ); else
generator.setSize( QSize( width, height ) ); {
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 #if QT_VERSION >= 0x040500
generator.setViewBox( QRect( 0, 0, width, height ) ); generator.setViewBox( QRect( 0, 0, width, height ) );
#endif #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() ); mComposition->renderPage( &p, i );
QRectF targetArea( 0, 0, width, height ); p.end();
mView->setPaintingEnabled( false ); }
mComposition->render( &p, targetArea, sourceArea );
p.end(); mComposition->setPlotStyle( savedPlotStyle );
mComposition->setPlotStyle( QgsComposition::Preview );
mView->setPaintingEnabled( true ); mView->setPaintingEnabled( true );
} }


Expand Down
4 changes: 4 additions & 0 deletions src/app/composer/qgscomposer.h
Expand Up @@ -283,6 +283,10 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Print to a printer object //! Print to a printer object
void print( QPrinter &printer ); 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 //! Writes state under DOM element
void writeXML( QDomNode& parentNode, QDomDocument& doc ); void writeXML( QDomNode& parentNode, QDomDocument& doc );


Expand Down
12 changes: 12 additions & 0 deletions src/app/composer/qgscompositionwidget.cpp
Expand Up @@ -41,6 +41,8 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )


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

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


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


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

void QgsCompositionWidget::displayCompositionWidthHeight() void QgsCompositionWidget::displayCompositionWidthHeight()
{ {
if ( !mComposition ) if ( !mComposition )
Expand Down Expand Up @@ -519,6 +530,7 @@ void QgsCompositionWidget::blockSignals( bool block )
mPaperUnitsComboBox->blockSignals( block ); mPaperUnitsComboBox->blockSignals( block );
mPaperWidthDoubleSpinBox->blockSignals( block ); mPaperWidthDoubleSpinBox->blockSignals( block );
mPaperHeightDoubleSpinBox->blockSignals( block ); mPaperHeightDoubleSpinBox->blockSignals( block );
mNumPagesSpinBox->blockSignals( block );
mPaperOrientationComboBox->blockSignals( block ); mPaperOrientationComboBox->blockSignals( block );
mResolutionSpinBox->blockSignals( block ); mResolutionSpinBox->blockSignals( block );
mPrintAsRasterCheckBox->blockSignals( block ); mPrintAsRasterCheckBox->blockSignals( block );
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscompositionwidget.h
Expand Up @@ -45,6 +45,7 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
void on_mPaperOrientationComboBox_currentIndexChanged( const QString& text ); void on_mPaperOrientationComboBox_currentIndexChanged( const QString& text );
void on_mPaperWidthDoubleSpinBox_editingFinished(); void on_mPaperWidthDoubleSpinBox_editingFinished();
void on_mPaperHeightDoubleSpinBox_editingFinished(); void on_mPaperHeightDoubleSpinBox_editingFinished();
void on_mNumPagesSpinBox_valueChanged( int value );
void on_mResolutionSpinBox_valueChanged( const int value ); void on_mResolutionSpinBox_valueChanged( const int value );
void on_mPrintAsRasterCheckBox_stateChanged( int state ); void on_mPrintAsRasterCheckBox_stateChanged( int state );


Expand Down

0 comments on commit aea41ee

Please sign in to comment.