Skip to content
Permalink
Browse files
[composer] Prevent hang on trying to export compositions and atlases …
…to files which can't be overwritten, and warn user instead (fix #9283) (fix #10016). Fix mouse cursor getting stuck after failed atlas exports.
  • Loading branch information
nyalldawson committed Jun 26, 2014
1 parent d039863 commit 899c2f8b5dc268ff4cbb111f94968b570159e933
@@ -364,11 +364,15 @@ class QgsComposition : QGraphicsScene
/** Print on a preconfigured printer */
void doPrint( QPrinter& printer, QPainter& painter );

/** Convenience function that prepares the printer and prints */
void print( QPrinter &printer );
/**Convenience function that prepares the printer and prints
* @returns true if print was successful
*/
bool print( QPrinter &printer );

/** Convenience function that prepares the printer for printing in PDF and prints */
void exportAsPDF( const QString& file );
/**Convenience function that prepares the printer for printing in PDF and prints
* @returns true if export was successful
*/
bool exportAsPDF( const QString& file );

//! print composer page to image
//! If the image does not fit into memory, a null image is returned
@@ -1406,7 +1406,16 @@ void QgsComposer::exportCompositionAsPDF( QgsComposer::OutputMode mode )
mComposition->beginPrintAsPDF( printer, outputFileName );
// set the correct resolution
mComposition->beginPrint( printer );
painter.begin( &printer );
bool printReady = painter.begin( &printer );
if ( !printReady )
{
QMessageBox::warning( this, tr( "Atlas processing error" ),
QString( tr( "Error creating %1." ) ).arg( outputFileName ),
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
return;
}
}

QProgressDialog progress( tr( "Rendering maps..." ), tr( "Abort" ), 0, atlasMap->numFeatures(), this );
@@ -1429,6 +1438,7 @@ void QgsComposer::exportCompositionAsPDF( QgsComposer::OutputMode mode )
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
return;
}
if ( !atlasOnASingleFile )
@@ -1441,7 +1451,17 @@ void QgsComposer::exportCompositionAsPDF( QgsComposer::OutputMode mode )
mComposition->beginPrintAsPDF( multiFilePrinter, outputFileName );
// set the correct resolution
mComposition->beginPrint( multiFilePrinter );
painter.begin( &multiFilePrinter );
bool printReady = painter.begin( &multiFilePrinter );
if ( !printReady )
{
QMessageBox::warning( this, tr( "Atlas processing error" ),
QString( tr( "Error creating %1." ) ).arg( outputFileName ),
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
return;
}
mComposition->doPrint( multiFilePrinter, painter );
painter.end();
}
@@ -1462,7 +1482,17 @@ void QgsComposer::exportCompositionAsPDF( QgsComposer::OutputMode mode )
}
else
{
mComposition->exportAsPDF( outputFileName );
bool exportOk = mComposition->exportAsPDF( outputFileName );
if ( !exportOk )
{
QMessageBox::warning( this, tr( "Atlas processing error" ),
QString( tr( "Error creating %1." ) ).arg( outputFileName ),
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
return;
}
}

if ( ! mComposition->useAdvancedEffects() )
@@ -1547,6 +1577,7 @@ void QgsComposer::printComposition( QgsComposer::OutputMode mode )
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
return;
}
QProgressDialog progress( tr( "Rendering maps..." ), tr( "Abort" ), 0, atlasMap->numFeatures(), this );
@@ -1569,6 +1600,7 @@ void QgsComposer::printComposition( QgsComposer::OutputMode mode )
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
return;
}

@@ -1681,15 +1713,25 @@ void QgsComposer::exportCompositionAsImage( QgsComposer::OutputMode mode )
mView->setPaintingEnabled( true );
return;
}
bool saveOk;
if ( i == 0 )
{
image.save( fileNExt.first, fileNExt.second.toLocal8Bit().constData() );
saveOk = 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() );
saveOk = image.save( outputFilePath, fileNExt.second.toLocal8Bit().constData() );
}
if ( !saveOk )
{
QMessageBox::warning( this, tr( "Image export error" ),
QString( tr( "Error creating %1." ) ).arg( fileNExt.first ),
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
return;
}
}

@@ -1800,6 +1842,7 @@ void QgsComposer::exportCompositionAsImage( QgsComposer::OutputMode mode )
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
return;
}

@@ -1823,6 +1866,7 @@ void QgsComposer::exportCompositionAsImage( QgsComposer::OutputMode mode )
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
return;
}

@@ -1831,16 +1875,25 @@ void QgsComposer::exportCompositionAsImage( QgsComposer::OutputMode mode )
for ( int i = 0; i < mComposition->numPages(); ++i )
{
QImage image = mComposition->printPageAsRaster( i );
QString imageFilename = filename;

if ( i == 0 )
if ( i != 0 )
{
image.save( filename, format.toLocal8Bit().constData() );
//append page number
QFileInfo fi( filename );
imageFilename = fi.absolutePath() + "/" + fi.baseName() + "_" + QString::number( i + 1 ) + "." + fi.suffix();
}
else

bool saveOk = image.save( imageFilename, format.toLocal8Bit().constData() );
if ( !saveOk )
{
QFileInfo fi( filename );
QString outputFilePath = fi.absolutePath() + "/" + fi.baseName() + "_" + QString::number( i + 1 ) + "." + fi.suffix();
image.save( outputFilePath, format.toLocal8Bit().constData() );
QMessageBox::warning( this, tr( "Atlas processing error" ),
QString( tr( "Error creating %1." ) ).arg( imageFilename ),
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
return;
}
}

@@ -2098,14 +2151,16 @@ void QgsComposer::exportCompositionAsSVG( QgsComposer::OutputMode mode )
{
QSvgGenerator generator;
generator.setTitle( QgsProject::instance()->title() );
QString currentFileName = outputFileName;
if ( i == 0 )
{
generator.setFileName( outputFileName );
}
else
{
QFileInfo fi( outputFileName );
generator.setFileName( fi.absolutePath() + "/" + fi.baseName() + "_" + QString::number( i + 1 ) + "." + fi.suffix() );
currentFileName = fi.absolutePath() + "/" + fi.baseName() + "_" + QString::number( i + 1 ) + "." + fi.suffix();
generator.setFileName( currentFileName );
}

//width in pixel
@@ -2116,7 +2171,17 @@ void QgsComposer::exportCompositionAsSVG( QgsComposer::OutputMode mode )
generator.setViewBox( QRect( 0, 0, width, height ) );
generator.setResolution( mComposition->printResolution() ); //because the rendering is done in mm, convert the dpi

QPainter p( &generator );
QPainter p;
bool createOk = p.begin( &generator );
if ( !createOk )
{
QMessageBox::warning( this, tr( "SVG export error" ),
QString( tr( "Error creating %1." ) ).arg( currentFileName ),
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
return;
}

mComposition->renderPage( &p, i );
p.end();
@@ -2224,8 +2289,19 @@ void QgsComposer::exportCompositionAsSVG( QgsComposer::OutputMode mode )
}
}
QFileInfo fi( outputFileName );
QFile out( i == 0 ? outputFileName : fi.absolutePath() + "/" + fi.baseName() + "_" + QString::number( i + 1 ) + "." + fi.suffix() );
out.open( QIODevice::WriteOnly | QIODevice::Text );
QString currentFileName = i == 0 ? outputFileName : fi.absolutePath() + "/" + fi.baseName() + "_" + QString::number( i + 1 ) + "." + fi.suffix();
QFile out( currentFileName );
bool openOk = out.open( QIODevice::WriteOnly | QIODevice::Text );
if ( !openOk )
{
QMessageBox::warning( this, tr( "SVG export error" ),
QString( tr( "Error creating %1." ) ).arg( currentFileName ),
QMessageBox::Ok,
QMessageBox::Ok );
mView->setPaintingEnabled( true );
return;
}

out.write( svg.toByteArray() );
}
}
@@ -2445,11 +2445,11 @@ void QgsComposition::beginPrintAsPDF( QPrinter& printer, const QString& file )
QgsPaintEngineHack::fixEngineFlags( printer.paintEngine() );
}

void QgsComposition::exportAsPDF( const QString& file )
bool QgsComposition::exportAsPDF( const QString& file )
{
QPrinter printer;
beginPrintAsPDF( printer, file );
print( printer );
return print( printer );
}

void QgsComposition::doPrint( QPrinter& printer, QPainter& p )
@@ -2499,11 +2499,19 @@ void QgsComposition::beginPrint( QPrinter &printer )
printer.setResolution( printResolution() );
}

void QgsComposition::print( QPrinter &printer )
bool QgsComposition::print( QPrinter &printer )
{
beginPrint( printer );
QPainter p( &printer );
QPainter p;
bool ready = p.begin( &printer );
if ( !ready )
{
//error beginning print
return false;
}
doPrint( printer, p );
p.end();
return true;
}

QImage QgsComposition::printPageAsRaster( int page )
@@ -424,11 +424,15 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
/** Print on a preconfigured printer */
void doPrint( QPrinter& printer, QPainter& painter );

/** Convenience function that prepares the printer and prints */
void print( QPrinter &printer );
/**Convenience function that prepares the printer and prints
* @returns true if print was successful
*/
bool print( QPrinter &printer );

/** Convenience function that prepares the printer for printing in PDF and prints */
void exportAsPDF( const QString& file );
/**Convenience function that prepares the printer for printing in PDF and prints
* @returns true if export was successful
*/
bool exportAsPDF( const QString& file );

//! print composer page to image
//! If the image does not fit into memory, a null image is returned

0 comments on commit 899c2f8

Please sign in to comment.