Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Life is simpler when you clone
  • Loading branch information
nirvn committed Aug 11, 2020
1 parent be49737 commit e7cfe66
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 107 deletions.
59 changes: 5 additions & 54 deletions src/analysis/processing/qgsalgorithmlayoutatlastoimage.cpp
Expand Up @@ -124,56 +124,13 @@ QgsLayoutAtlasToImageAlgorithm *QgsLayoutAtlasToImageAlgorithm::createInstance()
QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
// this needs to be done in main thread, layouts are not thread safe
QgsPrintLayout *layout = parameterAsLayout( parameters, QStringLiteral( "LAYOUT" ), context );
if ( !layout )
QgsPrintLayout *l = parameterAsLayout( parameters, QStringLiteral( "LAYOUT" ), context );
if ( !l )
throw QgsProcessingException( QObject::tr( "Cannot find layout with name \"%1\"" ).arg( parameters.value( QStringLiteral( "LAYOUT" ) ).toString() ) );

std::unique_ptr< QgsPrintLayout > layout( l->clone() );
QgsLayoutAtlas *atlas = layout->atlas();

QgsVectorLayer *previousCoverageLayer = atlas->coverageLayer();
const bool previousEnabled = atlas->enabled();
const QString previousFilenameExpression = atlas->filenameExpression();
const QString previousFilterExpression = atlas->filterExpression();
const bool previousSortFeatures = atlas->sortFeatures();
const bool previousSortAscending = atlas->sortAscending();
const QString previousSortExpression = atlas->sortExpression();
QStringList previousMapItemLayersReset;

auto restoreAtlas = [ layout, atlas,
previousCoverageLayer,
previousEnabled,
previousFilenameExpression,
previousFilterExpression,
previousSortFeatures,
previousSortAscending,
previousSortExpression,
&previousMapItemLayersReset ]()
{
QString error;
atlas->setEnabled( previousEnabled );
atlas->setCoverageLayer( previousCoverageLayer );
atlas->setFilenameExpression( previousFilenameExpression, error );
atlas->setFilterExpression( previousFilterExpression, error );
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->setKeepLayerSet( false );
map->setLayers( QList<QgsMapLayer *>() );
}
}
}
};

QString expression, error;
QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "COVERAGE_LAYER" ), context );
if ( layer )
Expand All @@ -185,7 +142,6 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
atlas->setFilterExpression( expression, error );
if ( !expression.isEmpty() && !error.isEmpty() )
{
restoreAtlas();
throw QgsProcessingException( QObject::tr( "Error setting atlas filter expression" ) );
}
error.clear();
Expand All @@ -212,7 +168,6 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
atlas->setFilenameExpression( expression, error );
if ( !error.isEmpty() )
{
restoreAtlas();
throw QgsProcessingException( QObject::tr( "Error setting atlas filename expression" ) );
}

Expand All @@ -230,7 +185,7 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
int idx = parameterAsEnum( parameters, QStringLiteral( "EXTENSION" ), context );
QString extension = '.' + imageFormats.at( idx );

QgsLayoutExporter exporter( layout );
QgsLayoutExporter exporter( layout.get() );
QgsLayoutExporter::ImageExportSettings settings;

if ( parameters.value( QStringLiteral( "DPI" ) ).isValid() )
Expand All @@ -256,17 +211,13 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
QgsLayoutItemMap *map = dynamic_cast<QgsLayoutItemMap *>( item );
if ( map && !map->followVisibilityPreset() && !map->keepLayerSet() )
{
previousMapItemLayersReset << map->uuid();
map->setKeepLayerSet( true );
map->setLayers( layers );
}
}
}

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

switch ( result )
switch ( exporter.exportToImage( atlas, fileName, extension, settings, error, feedback ) )
{
case QgsLayoutExporter::Success:
{
Expand Down
58 changes: 5 additions & 53 deletions src/analysis/processing/qgsalgorithmlayoutatlastopdf.cpp
Expand Up @@ -23,8 +23,6 @@
#include "qgsprocessingoutputs.h"
#include "qgslayoutexporter.h"

#include <QImageWriter>

///@cond PRIVATE

QString QgsLayoutAtlasToPdfAlgorithm::name() const
Expand Down Expand Up @@ -128,53 +126,13 @@ QgsLayoutAtlasToPdfAlgorithm *QgsLayoutAtlasToPdfAlgorithm::createInstance() con
QVariantMap QgsLayoutAtlasToPdfAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
// this needs to be done in main thread, layouts are not thread safe
QgsPrintLayout *layout = parameterAsLayout( parameters, QStringLiteral( "LAYOUT" ), context );
if ( !layout )
QgsPrintLayout *l = parameterAsLayout( parameters, QStringLiteral( "LAYOUT" ), context );
if ( !l )
throw QgsProcessingException( QObject::tr( "Cannot find layout with name \"%1\"" ).arg( parameters.value( QStringLiteral( "LAYOUT" ) ).toString() ) );

std::unique_ptr< QgsPrintLayout > layout( l->clone() );
QgsLayoutAtlas *atlas = layout->atlas();

QgsVectorLayer *previousCoverageLayer = atlas->coverageLayer();
const bool previousEnabled = atlas->enabled();
const QString previousFilterExpression = atlas->filterExpression();
const bool previousSortFeatures = atlas->sortFeatures();
const bool previousSortAscending = atlas->sortAscending();
const QString previousSortExpression = atlas->sortExpression();
QStringList previousMapItemLayersReset;

auto restoreAtlas = [ layout, atlas,
previousCoverageLayer,
previousEnabled,
previousFilterExpression,
previousSortFeatures,
previousSortAscending,
previousSortExpression,
&previousMapItemLayersReset ]()
{
QString error;
atlas->setEnabled( previousEnabled );
atlas->setCoverageLayer( previousCoverageLayer );
atlas->setFilterExpression( previousFilterExpression, error );
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->setKeepLayerSet( false );
map->setLayers( QList<QgsMapLayer *>() );
}
}
}
};

QString expression, error;
QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "COVERAGE_LAYER" ), context );
if ( layer )
Expand All @@ -186,7 +144,6 @@ QVariantMap QgsLayoutAtlasToPdfAlgorithm::processAlgorithm( const QVariantMap &p
atlas->setFilterExpression( expression, error );
if ( !expression.isEmpty() && !error.isEmpty() )
{
restoreAtlas();
throw QgsProcessingException( QObject::tr( "Error setting atlas filter expression" ) );
}
error.clear();
Expand All @@ -209,7 +166,7 @@ QVariantMap QgsLayoutAtlasToPdfAlgorithm::processAlgorithm( const QVariantMap &p
throw QgsProcessingException( QObject::tr( "Layout being export doesn't have an enabled atlas" ) );
}

QgsLayoutExporter exporter( layout );
QgsLayoutExporter exporter( layout.get() );
QgsLayoutExporter::PdfExportSettings settings;

if ( parameters.value( QStringLiteral( "DPI" ) ).isValid() )
Expand Down Expand Up @@ -237,19 +194,14 @@ QVariantMap QgsLayoutAtlasToPdfAlgorithm::processAlgorithm( const QVariantMap &p
QgsLayoutItemMap *map = dynamic_cast<QgsLayoutItemMap *>( item );
if ( map && !map->followVisibilityPreset() && !map->keepLayerSet() )
{
previousMapItemLayersReset << map->uuid();
map->setKeepLayerSet( true );
map->setLayers( layers );
}
}
}

const QString dest = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );

QgsLayoutExporter::ExportResult result = exporter.exportToPdf( atlas, dest, settings, error, feedback );
restoreAtlas();

switch ( result )
switch ( exporter.exportToPdf( atlas, dest, settings, error, feedback ) )
{
case QgsLayoutExporter::Success:
{
Expand Down

0 comments on commit e7cfe66

Please sign in to comment.