Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add api to export layout atlas as images
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent b6f1425 commit d81bf5d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
18 changes: 18 additions & 0 deletions python/core/layout/qgslayoutexporter.sip
Expand Up @@ -120,10 +120,12 @@ Returns the rendered image, or a null QImage if the image does not fit into avai
enum ExportResult
{
Success,
Canceled,
MemoryError,
FileError,
PrintError,
SvgLayerError,
IteratorError,
};

struct ImageExportSettings
Expand Down Expand Up @@ -198,6 +200,22 @@ error was encountered. If an error code is returned, errorFile() can be called
to determine the filename for the export which encountered the error.
%End


static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
QString &error /Out/, QgsFeedback *feedback = 0 );
%Docstring
Exports a layout ``iterator`` to raster images, 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.
%End


struct PdfExportSettings
{
PdfExportSettings();
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutatlas.cpp
Expand Up @@ -206,6 +206,7 @@ class AtlasFeatureSorter

int QgsLayoutAtlas::updateFeatures()
{
mCurrentFeatureNo = -1;
if ( !mCoverageLayer )
{
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutatlas.h
Expand Up @@ -365,7 +365,7 @@ class CORE_EXPORT QgsLayoutAtlas : public QObject, public QgsAbstractLayoutItera
// id of each iterated feature (after filtering and sorting) paired with atlas page name
QVector< QPair<QgsFeatureId, QString> > mFeatureIds;
// current atlas feature number
int mCurrentFeatureNo = 0;
int mCurrentFeatureNo = -1;
QgsFeature mCurrentFeature;

QgsExpressionContext createExpressionContext();
Expand Down
47 changes: 47 additions & 0 deletions src/core/layout/qgslayoutexporter.cpp
Expand Up @@ -21,6 +21,8 @@
#include "qgsogrutils.h"
#include "qgspaintenginehack.h"
#include "qgslayoutguidecollection.h"
#include "qgsabstractlayoutiterator.h"
#include "qgsfeedback.h"
#include <QImageWriter>
#include <QSize>
#include <QSvgGenerator>
Expand Down Expand Up @@ -394,6 +396,51 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToImage( const QString
return Success;
}

QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath, const QString &extension, const QgsLayoutExporter::ImageExportSettings &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, extension );
ExportResult result = exporter.exportToImage( 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::exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &s )
{
if ( !mLayout )
Expand Down
19 changes: 19 additions & 0 deletions src/core/layout/qgslayoutexporter.h
Expand Up @@ -27,6 +27,7 @@
class QgsLayout;
class QPainter;
class QgsLayoutItemMap;
class QgsAbstractLayoutIterator;

/**
* \ingroup core
Expand Down Expand Up @@ -129,10 +130,12 @@ class CORE_EXPORT QgsLayoutExporter
enum ExportResult
{
Success, //!< Export was successful
Canceled, //!< Export was canceled
MemoryError, //!< Unable to allocate memory required to export
FileError, //!< Could not write to destination file, likely due to a lock held by another application
PrintError, //!< Could not start printing to destination device
SvgLayerError, //!< Could not create layered SVG file
IteratorError, //!< Error iterating over layout
};

//! Contains settings relating to exporting layouts to raster images
Expand Down Expand Up @@ -206,6 +209,22 @@ class CORE_EXPORT QgsLayoutExporter
*/
ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );


/**
* Exports a layout \a iterator to raster images, 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.
*/
static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
QString &error SIP_OUT, QgsFeedback *feedback = nullptr );


//! Contains settings relating to exporting layouts to PDF
struct PdfExportSettings
{
Expand Down

0 comments on commit d81bf5d

Please sign in to comment.