Skip to content

Commit

Permalink
Add testOnly option to parameterAsOutputLayer
Browse files Browse the repository at this point in the history
If true, then the evaluated result won't be treated as an actual layer to load
  • Loading branch information
nyalldawson committed Jun 7, 2024
1 parent ed5a41f commit e6baabe
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1189,10 +1189,13 @@ need to handle deletion of the returned layer.
Evaluates the parameter with matching ``definition`` to a output layer destination.
%End

static QString parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context );
static QString parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, bool testOnly = false );
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a output layer destination.

Since QGIS 3.38 the ``testOnly`` argument can be set to ``True`` to evaluate the parameter to an output layer destination for advance testing only. This
prevents default behavior such as output post-processing which would otherwise occur.

.. versionadded:: 3.4
%End

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1189,10 +1189,13 @@ need to handle deletion of the returned layer.
Evaluates the parameter with matching ``definition`` to a output layer destination.
%End

static QString parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context );
static QString parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, bool testOnly = false );
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a output layer destination.

Since QGIS 3.38 the ``testOnly`` argument can be set to ``True`` to evaluate the parameter to an output layer destination for advance testing only. This
prevents default behavior such as output post-processing which would otherwise occur.

.. versionadded:: 3.4
%End

Expand Down
5 changes: 3 additions & 2 deletions src/core/processing/qgsprocessingparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingPara
return parameterAsOutputLayer( definition, val, context );
}

QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, bool testOnly )
{
QVariant val = value;

Expand Down Expand Up @@ -992,7 +992,8 @@ QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingPara
else if ( definition && definition->type() == QgsProcessingParameterVectorTileDestination::typeName() )
layerTypeHint = QgsProcessingUtils::LayerHint::VectorTile;

context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, layerTypeHint ) );
if ( !testOnly )
context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, layerTypeHint ) );
}

return dest;
Expand Down
6 changes: 5 additions & 1 deletion src/core/processing/qgsprocessingparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -1303,9 +1303,13 @@ class CORE_EXPORT QgsProcessingParameters

/**
* Evaluates the parameter with matching \a definition and \a value to a output layer destination.
*
* Since QGIS 3.38 the \a testOnly argument can be set to TRUE to evaluate the parameter to an output layer destination for advance testing only. This
* prevents default behavior such as output post-processing which would otherwise occur.
*
* \since QGIS 3.4
*/
static QString parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context );
static QString parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context, bool testOnly = false );

/**
* Evaluates the parameter with matching \a definition to a file based output destination.
Expand Down
5 changes: 5 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8090,6 +8090,11 @@ void TestQgsProcessing::parameterVectorOut()
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ).name, QStringLiteral( "desc" ) );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ).layerTypeHint, QgsProcessingUtils::LayerHint::Vector );

// if we set testOnly = true, then layer should not be loaded on completion
context.setLayersToLoadOnCompletion( {} );
QCOMPARE( QgsProcessingParameters::parameterAsOutputLayer( def.get(), QVariant::fromValue( fs ), context, true ), QStringLiteral( "test.shp" ) );
QCOMPARE( context.layersToLoadOnCompletion().size(), 0 );

// with name overloading
QgsProcessingContext context2;
fs = QgsProcessingOutputLayerDefinition( QStringLiteral( "test.shp" ) );
Expand Down

0 comments on commit e6baabe

Please sign in to comment.