Skip to content

Commit

Permalink
Add API to set optional destination parameters as not created by default
Browse files Browse the repository at this point in the history
This allows optional outputs (such as null geometry features detected
by the Remove Null Geometries algorithm) to be skipped by default
when desirable.
  • Loading branch information
nyalldawson committed Jul 16, 2017
1 parent 5deb8fc commit 1342f4d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
%End

QList< QgsProcessingModelChildParameterSource > availableSourcesForChild( const QString &childId, const QStringList &parameterTypes = QStringList(),
const QStringList &outputTypes = QStringList(), const QList< int > dataTypes = QList< int >() ) const;
const QStringList &outputTypes = QStringList(), const QList< int > &dataTypes = QList< int >() ) const;
%Docstring
Returns a list of possible sources which can be used for the parameters for a child
algorithm in the model. Returned sources are those which match either one of the
Expand Down
15 changes: 15 additions & 0 deletions python/core/processing/qgsprocessingparameters.sip
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,21 @@ class QgsProcessingDestinationParameter : QgsProcessingParameterDefinition
:rtype: str
%End

bool createByDefault() const;
%Docstring
Returns true if the destination should be created by default. For optional parameters,
a return value of false indicates that the destination should not be created by default.
.. seealso:: setCreateByDefault()
:rtype: bool
%End

void setCreateByDefault( bool createByDefault );
%Docstring
Sets whether the destination should be created by default. For optional parameters,
a value of false indicates that the destination should not be created by default.
.. seealso:: createByDefault()
%End

};


Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/DestinationSelectionPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self, parameter, alg):
self.use_temporary = True

if hasattr(self.leText, 'setPlaceholderText'):
if parameter.flags() & QgsProcessingParameterDefinition.FlagOptional and parameter.defaultValue() is None:
if parameter.flags() & QgsProcessingParameterDefinition.FlagOptional and not parameter.createByDefault():
self.leText.setPlaceholderText(self.SKIP_OUTPUT)
self.use_temporary = False
elif isinstance(self.parameter, QgsProcessingParameterFeatureSink) \
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ QgsExpressionContextScope *QgsProcessingModelAlgorithm::createExpressionContextS
return scope.release();
}

QgsProcessingModelChildParameterSources QgsProcessingModelAlgorithm::availableSourcesForChild( const QString &childId, const QStringList &parameterTypes, const QStringList &outputTypes, const QList<int> dataTypes ) const
QgsProcessingModelChildParameterSources QgsProcessingModelAlgorithm::availableSourcesForChild( const QString &childId, const QStringList &parameterTypes, const QStringList &outputTypes, const QList<int> &dataTypes ) const
{
QgsProcessingModelChildParameterSources sources;

Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/models/qgsprocessingmodelalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
* sources to those with compatible data types for the parameter/outputs.
*/
QList< QgsProcessingModelChildParameterSource > availableSourcesForChild( const QString &childId, const QStringList &parameterTypes = QStringList(),
const QStringList &outputTypes = QStringList(), const QList< int > dataTypes = QList< int >() ) const;
const QStringList &outputTypes = QStringList(), const QList< int > &dataTypes = QList< int >() ) const;

/**
* Definition of a expression context variable available during model execution.
Expand Down
18 changes: 12 additions & 6 deletions src/core/processing/qgsnativealgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,10 @@ void QgsExtractByExpressionAlgorithm::initAlgorithm( const QVariantMap & )
addParameter( new QgsProcessingParameterExpression( QStringLiteral( "EXPRESSION" ), QObject::tr( "Expression" ), QVariant(), QStringLiteral( "INPUT" ) ) );

addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Matching features" ) ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "FAIL_OUTPUT" ), QObject::tr( "Non-matching" ),
QgsProcessing::TypeVectorAny, QVariant(), true ) );
QgsProcessingParameterFeatureSink *failOutput = new QgsProcessingParameterFeatureSink( QStringLiteral( "FAIL_OUTPUT" ), QObject::tr( "Non-matching" ),
QgsProcessing::TypeVectorAny, QVariant(), true );
failOutput->setCreateByDefault( false );
addParameter( failOutput );
}

QString QgsExtractByExpressionAlgorithm::shortHelpString() const
Expand Down Expand Up @@ -917,8 +919,10 @@ void QgsExtractByAttributeAlgorithm::initAlgorithm( const QVariantMap & )
addParameter( new QgsProcessingParameterString( QStringLiteral( "VALUE" ), QObject::tr( "Value" ), QVariant(), false, true ) );

addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extracted (attribute)" ) ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "FAIL_OUTPUT" ), QObject::tr( "Extracted (non-matching)" ),
QgsProcessing::TypeVectorAny, QVariant(), true ) );
QgsProcessingParameterFeatureSink *failOutput = new QgsProcessingParameterFeatureSink( QStringLiteral( "FAIL_OUTPUT" ), QObject::tr( "Extracted (non-matching)" ),
QgsProcessing::TypeVectorAny, QVariant(), true );
failOutput->setCreateByDefault( false );
addParameter( failOutput );
}

QString QgsExtractByAttributeAlgorithm::shortHelpString() const
Expand Down Expand Up @@ -1101,8 +1105,10 @@ void QgsRemoveNullGeometryAlgorithm::initAlgorithm( const QVariantMap & )

addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Non null geometries" ),
QgsProcessing::TypeVectorAny, QVariant(), true ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "NULL_OUTPUT" ), QObject::tr( "Null geometries" ),
QgsProcessing::TypeVectorAny, QVariant(), true ) );
QgsProcessingParameterFeatureSink *nullOutput = new QgsProcessingParameterFeatureSink( QStringLiteral( "NULL_OUTPUT" ), QObject::tr( "Null geometries" ),
QgsProcessing::TypeTable, QVariant(), true );
nullOutput->setCreateByDefault( false );
addParameter( nullOutput );
}

QString QgsRemoveNullGeometryAlgorithm::shortHelpString() const
Expand Down
12 changes: 12 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2917,13 +2917,15 @@ QVariantMap QgsProcessingDestinationParameter::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
map.insert( QStringLiteral( "supports_non_file_outputs" ), mSupportsNonFileBasedOutputs );
map.insert( QStringLiteral( "create_by_default" ), mCreateByDefault );
return map;
}

bool QgsProcessingDestinationParameter::fromVariantMap( const QVariantMap &map )
{
QgsProcessingParameterDefinition::fromVariantMap( map );
mSupportsNonFileBasedOutputs = map.value( QStringLiteral( "supports_non_file_outputs" ) ).toBool();
mCreateByDefault = map.value( QStringLiteral( "create_by_default" ), QStringLiteral( "1" ) ).toBool();
return true;
}

Expand All @@ -2932,6 +2934,16 @@ QString QgsProcessingDestinationParameter::generateTemporaryDestination() const
return QgsProcessingUtils::generateTempFilename( name() + '.' + defaultFileExtension() );
}

bool QgsProcessingDestinationParameter::createByDefault() const
{
return mCreateByDefault;
}

void QgsProcessingDestinationParameter::setCreateByDefault( bool createByDefault )
{
mCreateByDefault = createByDefault;
}

QgsProcessingParameterVectorDestination::QgsProcessingParameterVectorDestination( const QString &name, const QString &description, QgsProcessing::LayerType type, const QVariant &defaultValue, bool optional )
: QgsProcessingDestinationParameter( name, description, defaultValue, optional )
, mDataType( type )
Expand Down
15 changes: 15 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -1555,9 +1555,24 @@ class CORE_EXPORT QgsProcessingDestinationParameter : public QgsProcessingParame
*/
virtual QString generateTemporaryDestination() const;

/**
* Returns true if the destination should be created by default. For optional parameters,
* a return value of false indicates that the destination should not be created by default.
* \see setCreateByDefault()
*/
bool createByDefault() const;

/**
* Sets whether the destination should be created by default. For optional parameters,
* a value of false indicates that the destination should not be created by default.
* \see createByDefault()
*/
void setCreateByDefault( bool createByDefault );

private:

bool mSupportsNonFileBasedOutputs = true;
bool mCreateByDefault = true;

};

Expand Down
3 changes: 3 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3573,6 +3573,8 @@ void TestQgsProcessing::parameterFeatureSink()
QVERIFY( def->checkValueIsAcceptable( "" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "layer1231123" ) ) );
def->setCreateByDefault( false );
QVERIFY( !def->createByDefault() );

code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##optional=optional sink" ) );
Expand All @@ -3582,6 +3584,7 @@ void TestQgsProcessing::parameterFeatureSink()
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
QCOMPARE( fromCode->dataType(), def->dataType() );
QVERIFY( !def->createByDefault() );

// test hasGeometry
QVERIFY( QgsProcessingParameterFeatureSink( "test", QString(), QgsProcessing::TypeAny ).hasGeometry() );
Expand Down

0 comments on commit 1342f4d

Please sign in to comment.