Skip to content

Commit

Permalink
Follow up commit ff2b2e8 : safer approach + test case
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn authored and nyalldawson committed Apr 26, 2020
1 parent 3145353 commit 5843fc4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
21 changes: 21 additions & 0 deletions python/core/auto_generated/processing/qgsprocessingoutputs.sip.in
Expand Up @@ -96,12 +96,33 @@ Sets the ``description`` for the output. This is the user-visible string
used to identify this output.

.. seealso:: :py:func:`description`
%End

void setAutoCreated( bool autoCreated );
%Docstring
Sets whether an output was automatically created when adding a parameter.

:param autoCreated: set to ``True`` if the output is to be considered as automatically created.

.. seealso:: :py:func:`autoCreated`

.. versionadded:: 3.14
%End

bool autoCreated() const;
%Docstring
Returns ``True`` if the output was automatically created when adding a parameter.

.. seealso:: :py:func:`setAutoCreated`

.. versionadded:: 3.14
%End

protected:




};

typedef QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions;
Expand Down
15 changes: 9 additions & 6 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -328,12 +328,14 @@ void QgsProcessingAlgorithm::removeParameter( const QString &name )
{
delete def;
mParameters.removeAll( def );
}
const QgsProcessingOutputDefinition *outputDef = outputDefinition( name );
if ( outputDef )
{
delete outputDef;
mOutputs.removeAll( outputDef );

// remove output automatically created when adding parameter
const QgsProcessingOutputDefinition *outputDef = QgsProcessingAlgorithm::outputDefinition( name );
if ( outputDef && outputDef->autoCreated() )
{
delete outputDef;
mOutputs.removeAll( outputDef );
}
}
}

Expand Down Expand Up @@ -853,6 +855,7 @@ bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParamete
QgsProcessingOutputDefinition *output( dest->toOutputDefinition() );
if ( !output )
return true; // nothing created - but nothing went wrong - so return true
output->setAutoCreated( true );

if ( !addOutput( output ) )
{
Expand Down
17 changes: 17 additions & 0 deletions src/core/processing/qgsprocessingoutputs.h
Expand Up @@ -112,6 +112,21 @@ class CORE_EXPORT QgsProcessingOutputDefinition
*/
void setDescription( const QString &description ) { mDescription = description; }

/**
* Sets whether an output was automatically created when adding a parameter.
* \param autoCreated set to TRUE if the output is to be considered as automatically created.
* \see autoCreated()
* \since QGIS 3.14
*/
void setAutoCreated( bool autoCreated ) { mAutoCreated = autoCreated; }

/**
* Returns TRUE if the output was automatically created when adding a parameter.
* \see setAutoCreated()
* \since QGIS 3.14
*/
bool autoCreated() const { return mAutoCreated; }

protected:

//! Output name
Expand All @@ -120,6 +135,8 @@ class CORE_EXPORT QgsProcessingOutputDefinition
//! Output description
QString mDescription;

bool mAutoCreated = false;

};

//! List of processing parameters
Expand Down
21 changes: 21 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -164,6 +164,23 @@ class DummyAlgorithm : public QgsProcessingAlgorithm
QVERIFY( addParameter( p1C ) );
QCOMPARE( parameterDefinitions().count(), 8 );

// remove parameter and auto created output
QgsProcessingParameterVectorDestination *p9 = new QgsProcessingParameterVectorDestination( "p9", "output" );
QVERIFY( addParameter( p9 ) );
QVERIFY( outputDefinition( "p9" ) );
QCOMPARE( outputDefinition( "p9" )->name(), QStringLiteral( "p9" ) );
QCOMPARE( outputDefinition( "p9" )->type(), QStringLiteral( "outputVector" ) );
removeParameter( "p9" );
QVERIFY( !outputDefinition( "p9" ) );

// remove parameter and check manually added output isn't removed
QVERIFY( addParameter( new QgsProcessingParameterVectorDestination( "p10", "output" ), false ) );
QVERIFY( addOutput( new QgsProcessingOutputVectorLayer( "p10" ) ) );
QCOMPARE( outputDefinition( "p10" )->name(), QStringLiteral( "p10" ) );
QCOMPARE( outputDefinition( "p10" )->type(), QStringLiteral( "outputVector" ) );
removeParameter( "p10" );
QVERIFY( outputDefinition( "p10" ) );

// parameterDefinition should be case insensitive, but prioritize correct case matches
QCOMPARE( parameterDefinition( "p1" ), parameterDefinitions().at( 0 ) );
QCOMPARE( parameterDefinition( "P1" ), parameterDefinitions().at( 7 ) );
Expand Down Expand Up @@ -195,6 +212,10 @@ class DummyAlgorithm : public QgsProcessingAlgorithm
QCOMPARE( outputDefinitions().count(), 1 );
QCOMPARE( outputDefinitions().at( 0 )->name(), QString( "p1" ) );

// make sure manually added outputs are not deleted by calling removeParameter
removeParameter( "p1" );
QCOMPARE( outputDefinitions().count(), 1 );

QVERIFY( !addOutput( nullptr ) );
QCOMPARE( outputDefinitions().count(), 1 );
// duplicate name!
Expand Down
Binary file modified tests/testdata/points_gpkg.gpkg
Binary file not shown.

0 comments on commit 5843fc4

Please sign in to comment.