Skip to content
Permalink
Browse files

Non-blocking save as image/PDF dialogs (#4874)

  • Loading branch information
nirvn committed Jul 18, 2017
1 parent d70f53c commit 3037f22482eb4eb3a46481ff912871ad17ff7130
@@ -31,7 +31,8 @@ class QgsMapRendererTask : QgsTask

QgsMapRendererTask( const QgsMapSettings &ms,
const QString &fileName,
const QString &fileFormat = QString( "PNG" ) );
const QString &fileFormat = QString( "PNG" ),
const bool forceRaster = false );
%Docstring
Constructor for QgsMapRendererTask to render a map to an image file.
%End
@@ -5740,173 +5740,34 @@ void QgisApp::updateFilterLegend()

void QgisApp::saveMapAsImage()
{
QList< QgsMapDecoration * > decorations;
QString activeDecorations;
QList< QgsDecorationItem * > decorations;
Q_FOREACH ( QgsDecorationItem *decoration, mDecorationItems )
{
if ( decoration->enabled() )
{
decorations << decoration;
if ( activeDecorations.isEmpty() )
activeDecorations = decoration->name().toLower();
else
activeDecorations += QString( ", %1" ).arg( decoration->name().toLower() );
}
}

QgsMapSaveDialog dlg( this, mMapCanvas, activeDecorations );
if ( !dlg.exec() )
return;

QPair< QString, QString> fileNameAndFilter = QgsGuiUtils::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
if ( fileNameAndFilter.first != QLatin1String( "" ) )
{
QgsMapSettings ms = QgsMapSettings();
dlg.applyMapSettings( ms );

QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask( ms, fileNameAndFilter.first, fileNameAndFilter.second );

if ( dlg.drawAnnotations() )
{
mapRendererTask->addAnnotations( QgsProject::instance()->annotationManager()->annotations() );
}

if ( dlg.drawDecorations() )
{
mapRendererTask->addDecorations( decorations );
}

mapRendererTask->setSaveWorldFile( dlg.saveWorldFile() );

connect( mapRendererTask, &QgsMapRendererTask::renderingComplete, this, [ = ]
{
messageBar()->pushSuccess( tr( "Save as image" ), tr( "Successfully saved map to image" ) );
} );
connect( mapRendererTask, &QgsMapRendererTask::errorOccurred, this, [ = ]( int error )
{
switch ( error )
{
case QgsMapRendererTask::ImageAllocationFail:
{
messageBar()->pushWarning( tr( "Save as image" ), tr( "Could not allocate required memory for image" ) );
break;
}
case QgsMapRendererTask::ImageSaveFail:
{
messageBar()->pushWarning( tr( "Save as image" ), tr( "Could not save the image to file" ) );
break;
}
}
} );

QgsApplication::taskManager()->addTask( mapRendererTask );
}

QgsMapSaveDialog *dlg = new QgsMapSaveDialog( this, mMapCanvas, decorations, QgsProject::instance()->annotationManager()->annotations() );
dlg->setAttribute( Qt::WA_DeleteOnClose );
dlg->show();
} // saveMapAsImage

void QgisApp::saveMapAsPdf()
{
QList< QgsMapDecoration * > decorations;
QString activeDecorations;
QList< QgsDecorationItem * > decorations;
Q_FOREACH ( QgsDecorationItem *decoration, mDecorationItems )
{
if ( decoration->enabled() )
{
decorations << decoration;
if ( activeDecorations.isEmpty() )
activeDecorations = decoration->name().toLower();
else
activeDecorations += QString( ", %1" ).arg( decoration->name().toLower() );
}
}

QgsMapSaveDialog dlg( this, mMapCanvas, activeDecorations, QgsMapSaveDialog::Pdf );
if ( !dlg.exec() )
return;

QgsSettings settings;
QString lastUsedDir = settings.value( QStringLiteral( "UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save map as" ), lastUsedDir, tr( "PDF Format" ) + " (*.pdf *.PDF)" );
if ( !fileName.isEmpty() )
{
QgsMapSettings ms = QgsMapSettings();
dlg.applyMapSettings( ms );

QPrinter *printer = new QPrinter();
printer->setOutputFileName( fileName );
printer->setOutputFormat( QPrinter::PdfFormat );
printer->setOrientation( QPrinter::Portrait );
// paper size needs to be given in millimeters in order to be able to set a resolution to pass onto the map renderer
printer->setPaperSize( dlg.size() * 25.4 / dlg.dpi(), QPrinter::Millimeter );
printer->setPageMargins( 0, 0, 0, 0, QPrinter::Millimeter );
printer->setResolution( dlg.dpi() );

QPainter *p = new QPainter();
QImage *image = nullptr;
if ( dlg.saveAsRaster() )
{
image = new QImage( dlg.size(), QImage::Format_ARGB32 );
if ( image->isNull() )
{
messageBar()->pushWarning( tr( "Save as PDF" ), tr( "Could not allocate required memory for image" ) );
delete p;
delete image;
delete printer;

return;
}

image->setDotsPerMeterX( 1000 * dlg.dpi() / 25.4 );
image->setDotsPerMeterY( 1000 * dlg.dpi() / 25.4 );
p->begin( image );
}
else
{
p->begin( printer );
}

QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask( ms, p );

if ( dlg.drawAnnotations() )
{
mapRendererTask->addAnnotations( QgsProject::instance()->annotationManager()->annotations() );
}

if ( dlg.drawDecorations() )
{
mapRendererTask->addDecorations( decorations );
}

mapRendererTask->setSaveWorldFile( dlg.saveWorldFile() );

connect( mapRendererTask, &QgsMapRendererTask::renderingComplete, this, [ this, p, image, printer ]
{
p->end();

if ( image )
{
QPainter pp;
pp.begin( printer );
QRectF rect( 0, 0, image->width(), image->height() );
pp.drawImage( rect, *image, rect );
pp.end();
}

messageBar()->pushSuccess( tr( "Save as PDF" ), tr( "Successfully saved map to PDF" ) );
delete p;
delete image;
delete printer;
} );
connect( mapRendererTask, &QgsMapRendererTask::errorOccurred, this, [ this, p, image, printer ]( int )
{
delete p;
delete image;
delete printer;
} );

QgsApplication::taskManager()->addTask( mapRendererTask );
}

QgsMapSaveDialog *dlg = new QgsMapSaveDialog( this, mMapCanvas, decorations, QgsProject::instance()->annotationManager()->annotations(), QgsMapSaveDialog::Pdf );
dlg->setAttribute( Qt::WA_DeleteOnClose );
dlg->show();
} // saveMapAsPdf

//overloaded version of the above function
@@ -18,25 +18,32 @@
#include "qgsmapsavedialog.h"

#include "qgis.h"
#include "qgisapp.h"
#include "qgsscalecalculator.h"
#include "qgsdecorationitem.h"
#include "qgsexpressioncontext.h"
#include "qgsextentgroupbox.h"
#include "qgsmapsettings.h"
#include "qgsmapsettingsutils.h"
#include "qgsmaprenderertask.h"
#include "qgsproject.h"
#include "qgssettings.h"

#include <QCheckBox>
#include <QSpinBox>
#include <QFileDialog>
#include <QImage>
#include <QList>
#include <QPainter>
#include <QPrinter>
#include <QSpinBox>

Q_GUI_EXPORT extern int qt_defaultDpiX();

QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, const QString &activeDecorations, DialogType type )
QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, QList< QgsDecorationItem * > decorations, QList< QgsAnnotation *> annotations, DialogType type )
: QDialog( parent )
, mDialogType( type )
, mMapCanvas( mapCanvas )
, mAnnotations( annotations )
{
setupUi( this );

@@ -57,6 +64,15 @@ QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, co
mScaleWidget->setMapCanvas( mMapCanvas );
mScaleWidget->setShowCurrentScaleButton( true );

QString activeDecorations;
Q_FOREACH ( QgsDecorationItem *decoration, decorations )
{
mDecorations << decoration;
if ( activeDecorations.isEmpty() )
activeDecorations = decoration->name().toLower();
else
activeDecorations += QString( ", %1" ).arg( decoration->name().toLower() );
}
mDrawDecorations->setText( tr( "Draw active decorations: %1" ).arg( !activeDecorations.isEmpty() ? activeDecorations : tr( "none" ) ) );

connect( mResolutionSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsMapSaveDialog::updateDpi );
@@ -92,6 +108,8 @@ QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, co

this->setWindowTitle( tr( "Save map as PDF" ) );
}

connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsMapSaveDialog::accepted );
}

void QgsMapSaveDialog::updateDpi( int dpi )
@@ -222,3 +240,89 @@ void QgsMapSaveDialog::applyMapSettings( QgsMapSettings &mapSettings )

mapSettings.setExpressionContext( expressionContext );
}

void QgsMapSaveDialog::accepted()
{
if ( mDialogType == Image )
{
QPair< QString, QString> fileNameAndFilter = QgsGuiUtils::getSaveAsImageName( QgisApp::instance(), tr( "Choose a file name to save the map image as" ) );
if ( fileNameAndFilter.first != QLatin1String( "" ) )
{
QgsMapSettings ms = QgsMapSettings();
applyMapSettings( ms );

QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask( ms, fileNameAndFilter.first, fileNameAndFilter.second );

if ( drawAnnotations() )
{
mapRendererTask->addAnnotations( mAnnotations );
}

if ( drawDecorations() )
{
mapRendererTask->addDecorations( mDecorations );
}

mapRendererTask->setSaveWorldFile( saveWorldFile() );

connect( mapRendererTask, &QgsMapRendererTask::renderingComplete, [ = ]
{
QgisApp::instance()->messageBar()->pushSuccess( tr( "Save as image" ), tr( "Successfully saved map to image" ) );
} );
connect( mapRendererTask, &QgsMapRendererTask::errorOccurred, [ = ]( int error )
{
switch ( error )
{
case QgsMapRendererTask::ImageAllocationFail:
{
QgisApp::instance()->messageBar()->pushWarning( tr( "Save as image" ), tr( "Could not allocate required memory for image" ) );
break;
}
case QgsMapRendererTask::ImageSaveFail:
{
QgisApp::instance()->messageBar()->pushWarning( tr( "Save as image" ), tr( "Could not save the map to file" ) );
break;
}
}
} );

QgsApplication::taskManager()->addTask( mapRendererTask );
}
}
else
{
QgsSettings settings;
QString lastUsedDir = settings.value( QStringLiteral( "UI/lastSaveAsImageDir" ), QDir::homePath() ).toString();
QString fileName = QFileDialog::getSaveFileName( QgisApp::instance(), tr( "Save map as" ), lastUsedDir, tr( "PDF Format" ) + " (*.pdf *.PDF)" );
if ( !fileName.isEmpty() )
{
QgsMapSettings ms = QgsMapSettings();
applyMapSettings( ms );

QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask( ms, fileName, QStringLiteral( "PDF" ), saveAsRaster() );

if ( drawAnnotations() )
{
mapRendererTask->addAnnotations( mAnnotations );
}

if ( drawDecorations() )
{
mapRendererTask->addDecorations( mDecorations );
}

mapRendererTask->setSaveWorldFile( saveWorldFile() );

connect( mapRendererTask, &QgsMapRendererTask::renderingComplete, [ = ]
{
QgisApp::instance()->messageBar()->pushSuccess( tr( "Save as PDF" ), tr( "Successfully saved map to PDF" ) );
} );
connect( mapRendererTask, &QgsMapRendererTask::errorOccurred, [ = ]( int )
{
QgisApp::instance()->messageBar()->pushWarning( tr( "Save as PDF" ), tr( "Could not save the map to PDF..." ) );
} );

QgsApplication::taskManager()->addTask( mapRendererTask );
}
}
}
@@ -21,8 +21,9 @@
#include "ui_qgsmapsavedialog.h"

#include "qgisapp.h"
#include "qgsrectangle.h"
#include "qgsmapcanvas.h"
#include "qgsmapdecoration.h"
#include "qgsrectangle.h"

#include <QDialog>
#include <QSize>
@@ -45,7 +46,10 @@ class APP_EXPORT QgsMapSaveDialog: public QDialog, private Ui::QgsMapSaveDialog

/** Constructor for QgsMapSaveDialog
*/
QgsMapSaveDialog( QWidget *parent = nullptr, QgsMapCanvas *mapCanvas = nullptr, const QString &activeDecorations = QString(), DialogType type = Image );
QgsMapSaveDialog( QWidget *parent = nullptr, QgsMapCanvas *mapCanvas = nullptr,
QList< QgsDecorationItem * > decorations = QList< QgsDecorationItem * >(),
QList< QgsAnnotation *> annotations = QList< QgsAnnotation * >(),
DialogType type = Image );

//! returns extent rectangle
QgsRectangle extent() const;
@@ -73,6 +77,8 @@ class APP_EXPORT QgsMapSaveDialog: public QDialog, private Ui::QgsMapSaveDialog

private:

void accepted();

void updateDpi( int dpi );
void updateOutputWidth( int width );
void updateOutputHeight( int height );
@@ -82,6 +88,9 @@ class APP_EXPORT QgsMapSaveDialog: public QDialog, private Ui::QgsMapSaveDialog

DialogType mDialogType;
QgsMapCanvas *mMapCanvas;
QList< QgsMapDecoration * > mDecorations;
QList< QgsAnnotation *> mAnnotations;

QgsRectangle mExtent;
int mDpi;
QSize mSize;

0 comments on commit 3037f22

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