Skip to content

Commit

Permalink
Use QgsTemporaryCursorOverride in layout designer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 2, 2018
1 parent 83f5486 commit ff2ee0b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -1755,7 +1755,7 @@ void QgsLayoutDesignerDialog::print()


mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

QgsLayoutExporter::PrintExportSettings printSettings;
printSettings.rasterizeWholeImage = mLayout->customProperty( QStringLiteral( "rasterize" ), false ).toBool();
Expand Down Expand Up @@ -1795,6 +1795,7 @@ void QgsLayoutDesignerDialog::print()
{
message = tr( "Could not create print device." );
}
cursorOverride.release();
QMessageBox::warning( this, tr( "Print Layout" ),
message,
QMessageBox::Ok,
Expand All @@ -1803,6 +1804,7 @@ void QgsLayoutDesignerDialog::print()
}

case QgsLayoutExporter::MemoryError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Memory Allocation Error" ),
tr( "Printing the layout "
"resulted in a memory overflow.\n\n"
Expand All @@ -1819,7 +1821,6 @@ void QgsLayoutDesignerDialog::print()
}

mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
}

void QgsLayoutDesignerDialog::exportToRaster()
Expand Down Expand Up @@ -1857,7 +1858,7 @@ void QgsLayoutDesignerDialog::exportToRaster()
return;

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

// force a refresh, to e.g. update data defined properties, tables, etc
mLayout->refresh();
Expand All @@ -1881,13 +1882,15 @@ void QgsLayoutDesignerDialog::exportToRaster()
break;

case QgsLayoutExporter::FileError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Image Export Error" ),
tr( "Cannot write to %1.\n\nThis file may be open in another application." ).arg( exporter.errorFile() ),
QMessageBox::Ok,
QMessageBox::Ok );
break;

case QgsLayoutExporter::MemoryError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Image Export Error" ),
tr( "Trying to create image %1 (%2×%3 @ %4dpi ) "
"resulted in a memory overflow.\n\n"
Expand All @@ -1898,7 +1901,6 @@ void QgsLayoutDesignerDialog::exportToRaster()


}
QApplication::restoreOverrideCursor();
mView->setPaintingEnabled( true );
}

Expand Down Expand Up @@ -1957,7 +1959,7 @@ void QgsLayoutDesignerDialog::exportToPdf()
settings.setValue( QStringLiteral( "lastSaveAsPdfFile" ), outputFileName, QgsSettings::App );

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

QgsLayoutExporter::PdfExportSettings pdfSettings;
pdfSettings.rasterizeWholeImage = mLayout->customProperty( QStringLiteral( "rasterize" ), false ).toBool();
Expand All @@ -1979,13 +1981,15 @@ void QgsLayoutDesignerDialog::exportToPdf()
}

case QgsLayoutExporter::FileError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Export to PDF" ),
tr( "Cannot write to %1.\n\nThis file may be open in another application." ).arg( outputFileName ),
QMessageBox::Ok,
QMessageBox::Ok );
break;

case QgsLayoutExporter::PrintError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Export to PDF" ),
tr( "Could not create print device." ),
QMessageBox::Ok,
Expand All @@ -1994,6 +1998,7 @@ void QgsLayoutDesignerDialog::exportToPdf()


case QgsLayoutExporter::MemoryError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Export to PDF" ),
tr( "Exporting the PDF "
"resulted in a memory overflow.\n\n"
Expand All @@ -2009,7 +2014,6 @@ void QgsLayoutDesignerDialog::exportToPdf()
}

mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
}

void QgsLayoutDesignerDialog::exportToSvg()
Expand Down Expand Up @@ -2068,7 +2072,7 @@ void QgsLayoutDesignerDialog::exportToSvg()
mLayout->project()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), exportAsText );

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

// force a refresh, to e.g. update data defined properties, tables, etc
mLayout->refresh();
Expand All @@ -2086,20 +2090,23 @@ void QgsLayoutDesignerDialog::exportToSvg()
}

case QgsLayoutExporter::FileError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Export to SVG" ),
tr( "Cannot write to %1.\n\nThis file may be open in another application." ).arg( outputFileName ),
QMessageBox::Ok,
QMessageBox::Ok );
break;

case QgsLayoutExporter::SvgLayerError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Export to SVG" ),
tr( "Cannot create layered SVG file %1." ).arg( outputFileName ),
QMessageBox::Ok,
QMessageBox::Ok );
break;

case QgsLayoutExporter::PrintError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Export to SVG" ),
tr( "Could not create print device." ),
QMessageBox::Ok,
Expand All @@ -2108,6 +2115,7 @@ void QgsLayoutDesignerDialog::exportToSvg()


case QgsLayoutExporter::MemoryError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Export to SVG" ),
tr( "Exporting the SVG "
"resulted in a memory overflow.\n\n"
Expand All @@ -2123,7 +2131,6 @@ void QgsLayoutDesignerDialog::exportToSvg()

mView->setPaintingEnabled( true );
mLayout->project()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), prevSettingLabelsAsOutlines );
QApplication::restoreOverrideCursor();
}

void QgsLayoutDesignerDialog::showAtlasSettings( bool checked )
Expand Down Expand Up @@ -2309,7 +2316,7 @@ void QgsLayoutDesignerDialog::printAtlas()
}

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

QgsLayoutExporter::PrintExportSettings printSettings;
printSettings.rasterizeWholeImage = mLayout->customProperty( QStringLiteral( "rasterize" ), false ).toBool();
Expand Down Expand Up @@ -2372,6 +2379,7 @@ void QgsLayoutDesignerDialog::printAtlas()
{
message = tr( "Could not create print device." );
}
cursorOverride.release();
QMessageBox::warning( this, tr( "Print Atlas" ),
message,
QMessageBox::Ok,
Expand All @@ -2380,6 +2388,7 @@ void QgsLayoutDesignerDialog::printAtlas()
}

case QgsLayoutExporter::MemoryError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Print Atlas" ),
tr( "Printing the layout "
"resulted in a memory overflow.\n\n"
Expand All @@ -2388,6 +2397,7 @@ void QgsLayoutDesignerDialog::printAtlas()
break;

case QgsLayoutExporter::IteratorError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Print Atlas" ),
tr( "Error encountered while printing atlas." ),
QMessageBox::Ok,
Expand All @@ -2402,7 +2412,6 @@ void QgsLayoutDesignerDialog::printAtlas()
}

mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
}

void QgsLayoutDesignerDialog::exportAtlasToRaster()
Expand Down Expand Up @@ -2481,7 +2490,7 @@ void QgsLayoutDesignerDialog::exportAtlasToRaster()
return;

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

QString error;
std::unique_ptr< QgsFeedback > feedback = qgis::make_unique< QgsFeedback >();
Expand Down Expand Up @@ -2512,7 +2521,7 @@ void QgsLayoutDesignerDialog::exportAtlasToRaster()

QString fileName = QDir( dir ).filePath( QStringLiteral( "atlas" ) ); // filename is overridden by atlas
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToImage( printAtlas, fileName, fileExt, settings, error, feedback.get() );
QApplication::restoreOverrideCursor();
cursorOverride.release();

switch ( result )
{
Expand Down Expand Up @@ -2632,7 +2641,7 @@ void QgsLayoutDesignerDialog::exportAtlasToSvg()
mLayout->project()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), exportAsText );

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

QString error;
std::unique_ptr< QgsFeedback > feedback = qgis::make_unique< QgsFeedback >();
Expand Down Expand Up @@ -2664,7 +2673,7 @@ void QgsLayoutDesignerDialog::exportAtlasToSvg()
QString filename = QDir( dir ).filePath( QStringLiteral( "atlas" ) ); // filename is overridden by atlas
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToSvg( printAtlas, filename, svgSettings, error, feedback.get() );

QApplication::restoreOverrideCursor();
cursorOverride.release();
switch ( result )
{
case QgsLayoutExporter::Success:
Expand Down Expand Up @@ -2832,7 +2841,7 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
}

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

QgsLayoutExporter::PdfExportSettings pdfSettings;
pdfSettings.rasterizeWholeImage = mLayout->customProperty( QStringLiteral( "rasterize" ), false ).toBool();
Expand Down Expand Up @@ -2877,6 +2886,7 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
result = QgsLayoutExporter::exportToPdfs( printAtlas, outputFileName, pdfSettings, error, feedback.get() );
}

cursorOverride.release();
switch ( result )
{
case QgsLayoutExporter::Success:
Expand Down Expand Up @@ -2935,7 +2945,6 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
}

mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
}

void QgsLayoutDesignerDialog::exportReportToRaster()
Expand All @@ -2962,7 +2971,7 @@ void QgsLayoutDesignerDialog::exportReportToRaster()
return;

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

QString error;
std::unique_ptr< QgsFeedback > feedback = qgis::make_unique< QgsFeedback >();
Expand Down Expand Up @@ -2995,7 +3004,7 @@ void QgsLayoutDesignerDialog::exportReportToRaster()
QString dir = fi.path();
QString fileName = dir + '/' + fi.baseName();
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToImage( dynamic_cast< QgsReport * >( mMasterLayout ), fileName, fileNExt.second, settings, error, feedback.get() );
QApplication::restoreOverrideCursor();
cursorOverride.release();

switch ( result )
{
Expand Down Expand Up @@ -3077,7 +3086,7 @@ void QgsLayoutDesignerDialog::exportReportToSvg()
mMasterLayout->layoutProject()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), exportAsText );

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

QString error;
std::unique_ptr< QgsFeedback > feedback = qgis::make_unique< QgsFeedback >();
Expand Down Expand Up @@ -3111,7 +3120,7 @@ void QgsLayoutDesignerDialog::exportReportToSvg()
QString dir = fi.path();
QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToSvg( dynamic_cast< QgsReport * >( mMasterLayout ), outFile, svgSettings, error, feedback.get() );

QApplication::restoreOverrideCursor();
cursorOverride.release();
switch ( result )
{
case QgsLayoutExporter::Success:
Expand Down Expand Up @@ -3198,7 +3207,7 @@ void QgsLayoutDesignerDialog::exportReportToPdf()
settings.setValue( QStringLiteral( "lastSaveAsPdfFile" ), outputFileName, QgsSettings::App );

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

bool rasterize = false;
bool forceVectorOutput = false;
Expand Down Expand Up @@ -3241,6 +3250,7 @@ void QgsLayoutDesignerDialog::exportReportToPdf()
} );

QgsLayoutExporter::ExportResult result = QgsLayoutExporter::exportToPdf( dynamic_cast< QgsReport * >( mMasterLayout ), outputFileName, pdfSettings, error, feedback.get() );
cursorOverride.release();

switch ( result )
{
Expand Down Expand Up @@ -3291,7 +3301,6 @@ void QgsLayoutDesignerDialog::exportReportToPdf()
}

mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
}

void QgsLayoutDesignerDialog::printReport()
Expand All @@ -3303,7 +3312,7 @@ void QgsLayoutDesignerDialog::printReport()
}

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );

QgsLayoutExporter::PrintExportSettings printSettings;
if ( mLayout )
Expand Down Expand Up @@ -3367,6 +3376,7 @@ void QgsLayoutDesignerDialog::printReport()
{
message = tr( "Could not create print device." );
}
cursorOverride.release();
QMessageBox::warning( this, tr( "Print Report" ),
message,
QMessageBox::Ok,
Expand All @@ -3375,6 +3385,7 @@ void QgsLayoutDesignerDialog::printReport()
}

case QgsLayoutExporter::MemoryError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Print Report" ),
tr( "Printing the report "
"resulted in a memory overflow.\n\n"
Expand All @@ -3383,6 +3394,7 @@ void QgsLayoutDesignerDialog::printReport()
break;

case QgsLayoutExporter::IteratorError:
cursorOverride.release();
QMessageBox::warning( this, tr( "Print Report" ),
tr( "Error encountered while printing report." ),
QMessageBox::Ok,
Expand All @@ -3397,7 +3409,6 @@ void QgsLayoutDesignerDialog::printReport()
}

mView->setPaintingEnabled( true );
QApplication::restoreOverrideCursor();
}

void QgsLayoutDesignerDialog::showReportSettings( bool checked )
Expand Down
10 changes: 10 additions & 0 deletions src/gui/qgsguiutils.cpp
Expand Up @@ -248,5 +248,15 @@ QgsTemporaryCursorOverride::QgsTemporaryCursorOverride( const QCursor &cursor )

QgsTemporaryCursorOverride::~QgsTemporaryCursorOverride()
{
if ( mHasOverride )
QApplication::restoreOverrideCursor();
}

void QgsTemporaryCursorOverride::release()
{
if ( !mHasOverride )
return;

mHasOverride = false;
QApplication::restoreOverrideCursor();
}
9 changes: 9 additions & 0 deletions src/gui/qgsguiutils.h
Expand Up @@ -184,6 +184,15 @@ class GUI_EXPORT QgsTemporaryCursorOverride

~QgsTemporaryCursorOverride();

/**
* Releases the cursor override early (i.e. before this object is destroyed).
*/
void release();

private:

bool mHasOverride = true;

};

#endif // QGSGUIUTILS_H

0 comments on commit ff2ee0b

Please sign in to comment.