Skip to content

Commit 1a45a79

Browse files
committed
[processing][model] Fix handling of mandatory checkbox in output definitions
1 parent 4ccd444 commit 1a45a79

6 files changed

Lines changed: 53 additions & 10 deletions

File tree

‎python/core/processing/models/qgsprocessingmodeloutput.sip.in‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ Sets the default value for the model output.
5858

5959
.. seealso:: :py:func:`defaultValue`
6060

61+
.. versionadded:: 3.2
62+
%End
63+
64+
bool isMandatory() const;
65+
%Docstring
66+
Returns true if the output is mandatory. This may override the associated
67+
child algorithm's parameter optional status - e.g. allowing
68+
an optional output from an algorithm to be mandatory within a model.
69+
70+
.. seealso:: :py:func:`setMandatory`
71+
72+
.. versionadded:: 3.2
73+
%End
74+
75+
void setMandatory( bool mandatory );
76+
%Docstring
77+
Sets whether the output is ``mandatory``. This may override the associated
78+
child algorithm's parameter optional status - e.g. allowing
79+
an optional output from an algorithm to be mandatory within a model.
80+
81+
.. seealso:: :py:func:`isMandatory`
82+
6183
.. versionadded:: 3.2
6284
%End
6385

‎python/plugins/processing/modeler/ModelerGraphicItem.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def editElement(self):
233233
model_output = child_alg.modelOutput(self.element.name())
234234
model_output.setDescription(dlg.param.description())
235235
model_output.setDefaultValue(dlg.param.defaultValue())
236+
model_output.setMandatory(not (dlg.param.flags() & QgsProcessingParameterDefinition.FlagOptional))
236237
self.model.updateDestinationParameters()
237238

238239
def updateAlgorithm(self, alg):

‎python/plugins/processing/modeler/ModelerParameterDefinitionDialog.py‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,34 +443,29 @@ def accept(self):
443443
name=name,
444444
description=self.param.description(),
445445
type=self.param.dataType(),
446-
defaultValue=self.defaultWidget.getValue(),
447-
optional=self.param.flags() & QgsProcessingParameterDefinition.FlagOptional)
446+
defaultValue=self.defaultWidget.getValue())
448447
elif (isinstance(self.param, QgsProcessingParameterFileDestination)):
449448
self.param = QgsProcessingParameterFileDestination(
450449
name=name,
451450
description=self.param.description(),
452451
fileFilter=self.param.fileFilter(),
453-
defaultValue=str(self.defaultWidget.getValue()),
454-
optional=self.param.flags() & QgsProcessingParameterDefinition.FlagOptional)
452+
defaultValue=str(self.defaultWidget.getValue()))
455453
elif (isinstance(self.param, QgsProcessingParameterFolderDestination)):
456454
self.param = QgsProcessingParameterFolderDestination(
457455
name=name,
458456
description=self.param.description(),
459-
defaultValue=str(self.defaultWidget.getValue()),
460-
optional=self.param.flags() & QgsProcessingParameterDefinition.FlagOptional)
457+
defaultValue=str(self.defaultWidget.getValue()))
461458
elif (isinstance(self.param, QgsProcessingParameterRasterDestination)):
462459
self.param = QgsProcessingParameterRasterDestination(
463460
name=name,
464461
description=self.param.description(),
465-
defaultValue=str(self.defaultWidget.getValue()),
466-
optional=self.param.flags() & QgsProcessingParameterDefinition.FlagOptional)
462+
defaultValue=str(self.defaultWidget.getValue()))
467463
elif (isinstance(self.param, QgsProcessingParameterVectorDestination)):
468464
self.param = QgsProcessingParameterVectorDestination(
469465
name=name,
470466
description=self.param.description(),
471467
type=self.param.dataType(),
472-
defaultValue=str(self.defaultWidget.getValue()),
473-
optional=self.param.flags() & QgsProcessingParameterDefinition.FlagOptional)
468+
defaultValue=str(self.defaultWidget.getValue()))
474469

475470
else:
476471
if self.paramType:
@@ -488,6 +483,8 @@ def accept(self):
488483

489484
if not self.requiredCheck.isChecked():
490485
self.param.setFlags(self.param.flags() | QgsProcessingParameterDefinition.FlagOptional)
486+
else:
487+
self.param.setFlags(self.param.flags() & ~QgsProcessingParameterDefinition.FlagOptional)
491488

492489
settings = QgsSettings()
493490
settings.setValue("/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry())

‎src/core/processing/models/qgsprocessingmodelalgorithm.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,8 @@ void QgsProcessingModelAlgorithm::updateDestinationParameters()
786786
// Even if an output was hidden in a child algorithm, we want to show it here for the final
787787
// outputs.
788788
param->setFlags( param->flags() & ~QgsProcessingParameterDefinition::FlagHidden );
789+
if ( outputIt->isMandatory() )
790+
param->setFlags( param->flags() & ~QgsProcessingParameterDefinition::FlagOptional );
789791
param->setName( outputIt->childId() + ':' + outputIt->name() );
790792
param->setDescription( outputIt->description() );
791793
param->setDefaultValue( outputIt->defaultValue() );

‎src/core/processing/models/qgsprocessingmodeloutput.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ QVariant QgsProcessingModelOutput::toVariant() const
4242

4343
map.insert( QStringLiteral( "child_id" ), mChildId );
4444
map.insert( QStringLiteral( "output_name" ), mOutputName );
45+
map.insert( QStringLiteral( "mandatory" ), mMandatory );
4546
saveCommonProperties( map );
4647
return map;
4748
}
@@ -72,6 +73,7 @@ bool QgsProcessingModelOutput::loadVariant( const QVariantMap &map )
7273

7374
mChildId = map.value( QStringLiteral( "child_id" ) ).toString();
7475
mOutputName = map.value( QStringLiteral( "output_name" ) ).toString();
76+
mMandatory = map.value( QStringLiteral( "mandatory" ), false ).toBool();
7577
restoreCommonProperties( map );
7678
return true;
7779
}

‎src/core/processing/models/qgsprocessingmodeloutput.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ class CORE_EXPORT QgsProcessingModelOutput : public QgsProcessingModelComponent
7070
*/
7171
void setDefaultValue( const QVariant &value ) { mDefaultValue = value; }
7272

73+
/**
74+
* Returns true if the output is mandatory. This may override the associated
75+
* child algorithm's parameter optional status - e.g. allowing
76+
* an optional output from an algorithm to be mandatory within a model.
77+
* \see setMandatory()
78+
* \since QGIS 3.2
79+
*/
80+
bool isMandatory() const { return mMandatory; }
81+
82+
/**
83+
* Sets whether the output is \a mandatory. This may override the associated
84+
* child algorithm's parameter optional status - e.g. allowing
85+
* an optional output from an algorithm to be mandatory within a model.
86+
* \see isMandatory()
87+
* \since QGIS 3.2
88+
*/
89+
void setMandatory( bool mandatory ) { mMandatory = mandatory; }
90+
7391
/**
7492
* Returns the child algorithm ID from which this output is generated.
7593
* \see setChildId()
@@ -112,6 +130,7 @@ class CORE_EXPORT QgsProcessingModelOutput : public QgsProcessingModelComponent
112130
QVariant mDefaultValue;
113131
QString mChildId;
114132
QString mOutputName;
133+
bool mMandatory = false;
115134
};
116135

117136
///@endcond

0 commit comments

Comments
 (0)