Skip to content
Permalink
Browse files

[processing] Add missing layers overwrite parameter to two layout exp…

…orts algorithms
  • Loading branch information
nirvn authored and nyalldawson committed Oct 3, 2020
1 parent b0189b4 commit b7c28df02a77ef4aeddd07af11b641e540c294b5
@@ -17,6 +17,7 @@

#include "qgsalgorithmlayouttoimage.h"
#include "qgslayout.h"
#include "qgslayoutitemmap.h"
#include "qgsprintlayout.h"
#include "qgsprocessingoutputs.h"
#include "qgslayoutexporter.h"
@@ -63,6 +64,10 @@ void QgsLayoutToImageAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterLayout( QStringLiteral( "LAYOUT" ), QObject::tr( "Print layout" ) ) );

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() );

std::unique_ptr< QgsProcessingParameterNumber > dpiParam = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "DPI" ), QObject::tr( "DPI (leave blank for default layout DPI)" ), QgsProcessingParameterNumber::Double, QVariant(), true, 0 );
dpiParam->setFlags( dpiParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( dpiParam.release() );
@@ -111,13 +116,30 @@ QgsLayoutToImageAlgorithm *QgsLayoutToImageAlgorithm::createInstance() const
QVariantMap QgsLayoutToImageAlgorithm::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() );

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() )
{
map->setKeepLayerSet( true );
map->setLayers( layers );
}
}
}

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

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

if ( parameters.value( QStringLiteral( "DPI" ) ).isValid() )
@@ -17,6 +17,7 @@

#include "qgsalgorithmlayouttopdf.h"
#include "qgslayout.h"
#include "qgslayoutitemmap.h"
#include "qgsprintlayout.h"
#include "qgsprocessingoutputs.h"
#include "qgslayoutexporter.h"
@@ -62,6 +63,10 @@ void QgsLayoutToPdfAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterLayout( QStringLiteral( "LAYOUT" ), QObject::tr( "Print layout" ) ) );

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() );

std::unique_ptr< QgsProcessingParameterNumber > dpiParam = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "DPI" ), QObject::tr( "DPI (leave blank for default layout DPI)" ), QgsProcessingParameterNumber::Double, QVariant(), true, 0 );
dpiParam->setFlags( dpiParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( dpiParam.release() );
@@ -116,13 +121,30 @@ QgsLayoutToPdfAlgorithm *QgsLayoutToPdfAlgorithm::createInstance() const
QVariantMap QgsLayoutToPdfAlgorithm::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() );

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() )
{
map->setKeepLayerSet( true );
map->setLayers( layers );
}
}
}

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

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

if ( parameters.value( QStringLiteral( "DPI" ) ).isValid() )
@@ -4596,18 +4596,26 @@ void TestQgsProcessingAlgs::exportLayoutPng()
QgsPrintLayout *layout = new QgsPrintLayout( &p );
layout->initializeDefaults();
layout->setName( QStringLiteral( "my layout" ) );

QgsLayoutItemMap *map = new QgsLayoutItemMap( layout );
map->setBackgroundEnabled( false );
map->setFrameEnabled( false );
map->attemptSetSceneRect( QRectF( 20, 20, 200, 100 ) );
layout->addLayoutItem( map );
map->setExtent( mPointsLayer->extent() );

p.layoutManager()->addLayout( layout );

std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:printlayouttoimage" ) ) );
QVERIFY( alg != nullptr );

const QString outputPdf = QDir::tempPath() + "/my_layout.png";
if ( QFile::exists( outputPdf ) )
QFile::remove( outputPdf );
QString outputPng = QDir::tempPath() + "/my_layout.png";
if ( QFile::exists( outputPng ) )
QFile::remove( outputPng );

QVariantMap parameters;
parameters.insert( QStringLiteral( "LAYOUT" ), QStringLiteral( "missing" ) );
parameters.insert( QStringLiteral( "OUTPUT" ), outputPdf );
parameters.insert( QStringLiteral( "OUTPUT" ), outputPng );

bool ok = false;
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
@@ -4617,13 +4625,24 @@ void TestQgsProcessingAlgs::exportLayoutPng()
results = alg->run( parameters, *context, &feedback, &ok );
// invalid layout name
QVERIFY( !ok );
QVERIFY( !QFile::exists( outputPdf ) );
QVERIFY( !QFile::exists( outputPng ) );

parameters.insert( QStringLiteral( "LAYOUT" ), QStringLiteral( "my layout" ) );
results = alg->run( parameters, *context, &feedback, &ok );
QVERIFY( ok );
QVERIFY( QFile::exists( outputPng ) );

QVERIFY( QFile::exists( outputPdf ) );
outputPng = QDir::tempPath() + "/my_layout_custom_layers.png";
if ( QFile::exists( outputPng ) )
QFile::remove( outputPng );

parameters.insert( QStringLiteral( "OUTPUT" ), outputPng );
parameters.insert( QStringLiteral( "LAYERS" ), QVariantList() << QVariant::fromValue( mPointsLayer ) );
parameters.insert( QStringLiteral( "DPI" ), 96 );
results = alg->run( parameters, *context, &feedback, &ok );
QVERIFY( ok );
QVERIFY( QFile::exists( outputPng ) );
QVERIFY( imageCheck( "export_layout_custom_layers", outputPng ) );
}

void TestQgsProcessingAlgs::exportAtlasLayoutPdf()
Binary file not shown.

0 comments on commit b7c28df

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