Skip to content

Commit

Permalink
[processing] Add a method to override map layers used by map items in…
Browse files Browse the repository at this point in the history
… export atlas layout alg
  • Loading branch information
nirvn committed Aug 3, 2020
1 parent 9ef2567 commit 4ebe3bd
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions src/analysis/processing/qgsalgorithmlayoutatlastoimage.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsalgorithmlayoutatlastoimage.h"
#include "qgslayout.h"
#include "qgslayoutatlas.h"
#include "qgslayoutitemmap.h"
#include "qgsprintlayout.h"
#include "qgsprocessingoutputs.h"
#include "qgslayoutexporter.h"
Expand Down Expand Up @@ -53,29 +54,31 @@ QString QgsLayoutAtlasToImageAlgorithm::groupId() const

QString QgsLayoutAtlasToImageAlgorithm::shortDescription() const
{
return QObject::tr( "Exports an atlas layout atlas as image." );
return QObject::tr( "Exports an atlas layout as a set of image." );
}

QString QgsLayoutAtlasToImageAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm outputs an atlas layout image file (e.g. PNG or JPEG images)." );
return QObject::tr( "This algorithm outputs an atlas layout to a set of image files (e.g. PNG or JPEG images)." );
}

void QgsLayoutAtlasToImageAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterLayout( QStringLiteral( "LAYOUT" ), QObject::tr( "Atlas layout" ) ) );

addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "COVERAGE_LAYER" ), QObject::tr( "Coverage layer" ) ) );

addParameter( new QgsProcessingParameterExpression( QStringLiteral( "FILTER_EXPRESSION" ), QObject::tr( "Filter expression" ), QString(), QStringLiteral( "COVERAGE_LAYER" ), true ) );

addParameter( new QgsProcessingParameterExpression( QStringLiteral( "SORTBY_EXPRESSION" ), QObject::tr( "Sort expression" ), QString(), QStringLiteral( "COVERAGE_LAYER" ), true ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "SORTBY_REVERSE" ), QObject::tr( "Reverse sort order (used when a sort expression is provided)" ), false, true ) );

addParameter( new QgsProcessingParameterExpression( QStringLiteral( "FILENAME_EXPRESSION" ), QObject::tr( "Output filename expression" ), QStringLiteral( "'output_'||@atlas_featurenumber" ), QStringLiteral( "COVERAGE_LAYER" ) ) );

addParameter( new QgsProcessingParameterFile( QStringLiteral( "FOLDER" ), QObject::tr( "Output folder" ), QgsProcessingParameterFile::Folder ) );


std::unique_ptr< QgsProcessingParameterMultipleLayers > layersParam = qgis::make_unique< QgsProcessingParameterMultipleLayers>( QStringLiteral( "LAYERS" ), QObject::tr( "Map layers to assign to unlocked map item(s)" ), QgsProcessing::TypeMapLayer, QVariant(), true );
layersParam->setFlags( layersParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( layersParam.release() );

QStringList imageFormats;
const auto supportedImageFormats { QImageWriter::supportedImageFormats() };
for ( const auto format : QImageWriter::supportedImageFormats() )
Expand Down Expand Up @@ -132,15 +135,17 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
const bool previousSortFeatures = atlas->sortFeatures();
const bool previousSortAscending = atlas->sortAscending();
const QString previousSortExpression = atlas->sortExpression();

auto restoreAtlas = [ atlas,
previousCoverageLayer,
previousEnabled,
previousFilenameExpression,
previousFilterExpression,
previousSortFeatures,
previousSortAscending,
previousSortExpression ]()
QStringList previousMapItemLayersReset;

auto restoreAtlas = [ layout, atlas,
previousCoverageLayer,
previousEnabled,
previousFilenameExpression,
previousFilterExpression,
previousSortFeatures,
previousSortAscending,
previousSortExpression,
previousMapItemLayersReset ]()
{
QString error;
atlas->setEnabled( previousEnabled );
Expand All @@ -150,6 +155,20 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
atlas->setSortFeatures( previousSortFeatures );
atlas->setSortAscending( previousSortAscending );
atlas->setSortExpression( previousSortExpression );

if ( previousMapItemLayersReset.size() > 0 )
{
const QList<QGraphicsItem *> items = layout->items();
for ( QGraphicsItem *graphicsItem : items )
{
QgsLayoutItem *item = dynamic_cast<QgsLayoutItem *>( graphicsItem );
QgsLayoutItemMap *map = dynamic_cast<QgsLayoutItemMap *>( item );
if ( map && previousMapItemLayersReset.contains( map->uuid() ) )
{
map->setLayers( QList<QgsMapLayer *>() );
}
}
}
};

QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "COVERAGE_LAYER" ), context );
Expand Down Expand Up @@ -210,6 +229,22 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
else
settings.flags = settings.flags & ~QgsLayoutRenderContext::FlagAntialiasing;

const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
if ( layers.size() > 0 )
{
const QList<QGraphicsItem *> items = layout->items();
for ( QGraphicsItem *graphicsItem : items )
{
QgsLayoutItem *item = dynamic_cast<QgsLayoutItem *>( graphicsItem );
QgsLayoutItemMap *map = dynamic_cast<QgsLayoutItemMap *>( item );
if ( map && !map->followVisibilityPreset() && !map->keepLayerSet() )
{
previousMapItemLayersReset << map->uuid();
map->setLayers( layers );
}
}
}

QgsLayoutExporter::ExportResult result = exporter.exportToImage( atlas, fileName, extension, settings, error, feedback );
restoreAtlas();

Expand Down

0 comments on commit 4ebe3bd

Please sign in to comment.