Skip to content
Permalink
Browse files

Add api to export layout atlas as images

  • Loading branch information
nyalldawson committed Dec 23, 2017
1 parent b6f1425 commit d81bf5d95ab5655741b645b81583a28c26804e1e
@@ -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
@@ -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();
@@ -206,6 +206,7 @@ class AtlasFeatureSorter

int QgsLayoutAtlas::updateFeatures()
{
mCurrentFeatureNo = -1;
if ( !mCoverageLayer )
{
return 0;
@@ -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();
@@ -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>
@@ -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 )
@@ -27,6 +27,7 @@
class QgsLayout;
class QPainter;
class QgsLayoutItemMap;
class QgsAbstractLayoutIterator;

/**
* \ingroup core
@@ -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
@@ -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
{

0 comments on commit d81bf5d

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