Skip to content

Commit

Permalink
Restore atlas multiple pdf exports
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 427da5c commit 7d8953f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
16 changes: 16 additions & 0 deletions python/core/layout/qgslayoutexporter.sip
Expand Up @@ -270,9 +270,25 @@ The ``fileName`` argument gives the destination file name for the output PDF.
Returns a result code indicating whether the export was successful or an
error was encountered. If an error was obtained then ``error`` will be set
to the error description.

.. seealso:: :py:func:`exportToPdfs()`
%End

static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
const QgsLayoutExporter::PdfExportSettings &settings,
QString &error /Out/, QgsFeedback *feedback = 0 );
%Docstring
Exports a layout ``iterator`` to multiple PDF files, with the specified export ``settings``.

The ``baseFilePath`` argument gives a base file path, which is modified by the
iterator to obtain file paths for each iterator feature.

Returns a result code indicating whether the export was successful or an
error was encountered. If an error was obtained then ``error`` will be set
to the error description.

.. seealso:: :py:func:`exportToPdf()`
%End

struct SvgExportSettings
{
Expand Down
59 changes: 58 additions & 1 deletion src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -2351,6 +2351,63 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
}
settings.setValue( QStringLiteral( "UI/lastSaveAsPdfFile" ), outputFileName );
}
else
{
if ( printAtlas->filenameExpression().isEmpty() )
{
int res = QMessageBox::warning( nullptr, tr( "Export Atlas" ),
tr( "The filename expression is empty. A default one will be used instead." ),
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Ok );
if ( res == QMessageBox::Cancel )
{
return;
}
QString error;
printAtlas->setFilenameExpression( QStringLiteral( "'output_'||@atlas_featurenumber" ), error );
}


QString lastUsedDir = settings.value( QStringLiteral( "UI/lastSaveAtlasAsPdfDir" ), QDir::homePath() ).toString();

QFileDialog dlg( this, tr( "Export Atlas to Directory" ) );
dlg.setFileMode( QFileDialog::Directory );
dlg.setOption( QFileDialog::ShowDirsOnly, true );
dlg.setDirectory( lastUsedDir );
if ( !dlg.exec() )
{
return;
}

#ifdef Q_OS_MAC
QgisApp::instance()->activateWindow();
this->raise();
#endif

const QStringList files = dlg.selectedFiles();
if ( files.empty() || files.at( 0 ).isEmpty() )
{
return;
}
QString dir = files.at( 0 );
if ( dir.isEmpty() )
{
return;
}
settings.setValue( QStringLiteral( "UI/lastSaveAtlasAsPdfDir" ), dir );

// test directory (if it exists and is writable)
if ( !QDir( dir ).exists() || !QFileInfo( dir ).isWritable() )
{
QMessageBox::warning( nullptr, tr( "Unable to write into the directory" ),
tr( "The given output directory is not writable. Canceling." ),
QMessageBox::Ok,
QMessageBox::Ok );
return;
}

outputFileName = dir;
}

mView->setPaintingEnabled( false );
QApplication::setOverrideCursor( Qt::BusyCursor );
Expand Down Expand Up @@ -2395,7 +2452,7 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
}
else
{

result = QgsLayoutExporter::exportToPdfs( printAtlas, outputFileName, pdfSettings, error, feedback.get() );
}

switch ( result )
Expand Down
45 changes: 45 additions & 0 deletions src/core/layout/qgslayoutexporter.cpp
Expand Up @@ -559,6 +559,51 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToPdf( QgsAbstractLayou
return Success;
}

QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath, const QgsLayoutExporter::PdfExportSettings &settings, QString &error, QgsFeedback *feedback )
{
QgsLayoutExporter exporter( iterator->layout() );
error.clear();

if ( !iterator->beginRender() )
return IteratorError;

int total = iterator->count();
double step = total > 0 ? 100.0 / total : 100.0;
int i = 0;
while ( iterator->next() )
{
if ( feedback )
{
feedback->setProperty( "progress", QObject::tr( "Exporting %1 of %2" ).arg( i + 1 ).arg( total ) );
feedback->setProgress( step * i );
}
if ( feedback && feedback->isCanceled() )
{
iterator->endRender();
return Canceled;
}

QString filePath = iterator->filePath( baseFilePath, QStringLiteral( "pdf" ) );
ExportResult result = exporter.exportToPdf( filePath, settings );
if ( result != Success )
{
if ( result == FileError )
error = QObject::tr( "Cannot write to %1. This file may be open in another application." ).arg( filePath );
iterator->endRender();
return result;
}
i++;
}

if ( feedback )
{
feedback->setProgress( 100 );
}

iterator->endRender();
return Success;
}

QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &s )
{
if ( !mLayout )
Expand Down
18 changes: 17 additions & 1 deletion src/core/layout/qgslayoutexporter.h
Expand Up @@ -275,12 +275,28 @@ class CORE_EXPORT QgsLayoutExporter
* Returns a result code indicating whether the export was successful or an
* error was encountered. If an error was obtained then \a error will be set
* to the error description.
*
* \see exportToPdfs()
*/
static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
const QgsLayoutExporter::PdfExportSettings &settings,
QString &error SIP_OUT, QgsFeedback *feedback = nullptr );


/**
* Exports a layout \a iterator to multiple PDF files, with the specified export \a settings.
*
* The \a baseFilePath argument gives a base file path, which is modified by the
* iterator to obtain file paths for each iterator feature.
*
* Returns a result code indicating whether the export was successful or an
* error was encountered. If an error was obtained then \a error will be set
* to the error description.
*
* \see exportToPdf()
*/
static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
const QgsLayoutExporter::PdfExportSettings &settings,
QString &error SIP_OUT, QgsFeedback *feedback = nullptr );

//! Contains settings relating to exporting layouts to SVG
struct SvgExportSettings
Expand Down

0 comments on commit 7d8953f

Please sign in to comment.