Skip to content

Commit

Permalink
[FEATURE][processing] Remember parameter values between model designe…
Browse files Browse the repository at this point in the history
…r runs

When designing a model, users typically will need to run the model
many times as they tweak its structure.

This change causes the parameters used when running the model from
the designer to be remembered and saved into the model, so that
each time you run the model from the designer you don't have to
re-set all the input parameter values to the desired test ones.

Makes iterative model design SO much easier!

Sponsored by Alta Ehf
  • Loading branch information
nyalldawson committed Mar 6, 2020
1 parent b3864db commit 2a1bf56
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Expand Up @@ -441,6 +441,32 @@ These variables are added to the model's expression context scope, allowing for
.. seealso:: :py:func:`variables`

.. versionadded:: 3.8
%End

QVariantMap designerParameterValues() const;
%Docstring
Returns the parameter values to use as default values when running this model through the
designer dialog.

This usually corresponds to the last set of parameter values used when the model was
run through the designer.

.. seealso:: :py:func:`setDesignerParameterValues`

.. versionadded:: 3.14
%End

void setDesignerParameterValues( const QVariantMap &values );
%Docstring
Sets the parameter ``values`` to use as default values when running this model through the
designer dialog.

This usually corresponds to the last set of parameter values used when the model was
run through the designer.

.. seealso:: :py:func:`designerParameterValues`

.. versionadded:: 3.14
%End

protected:
Expand Down
4 changes: 4 additions & 0 deletions python/plugins/processing/modeler/ModelerDialog.py
Expand Up @@ -543,8 +543,12 @@ def runModel(self):
return

dlg = AlgorithmDialog(self.model.create(), parent=iface.mainWindow())
dlg.setParameters(self.model.designerParameterValues())
dlg.exec_()

if dlg.wasExecuted():
self.model.setDesignerParameterValues(dlg.getParameterValues())

def save(self):
self.saveModel(False)

Expand Down
3 changes: 3 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -1162,6 +1162,8 @@ QVariant QgsProcessingModelAlgorithm::toVariant() const

map.insert( QStringLiteral( "modelVariables" ), mVariables );

map.insert( QStringLiteral( "designerParameterValues" ), mDesignerParameterValues );

return map;
}

Expand All @@ -1175,6 +1177,7 @@ bool QgsProcessingModelAlgorithm::loadVariant( const QVariant &model )
mHelpContent = map.value( QStringLiteral( "help" ) ).toMap();

mVariables = map.value( QStringLiteral( "modelVariables" ) ).toMap();
mDesignerParameterValues = map.value( QStringLiteral( "designerParameterValues" ) ).toMap();

mChildAlgorithms.clear();
QVariantMap childMap = map.value( QStringLiteral( "children" ) ).toMap();
Expand Down
28 changes: 28 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.h
Expand Up @@ -398,6 +398,32 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
*/
void setVariables( const QVariantMap &variables );

/**
* Returns the parameter values to use as default values when running this model through the
* designer dialog.
*
* This usually corresponds to the last set of parameter values used when the model was
* run through the designer.
*
* \see setDesignerParameterValues()
*
* \since QGIS 3.14
*/
QVariantMap designerParameterValues() const { return mDesignerParameterValues; }

/**
* Sets the parameter \a values to use as default values when running this model through the
* designer dialog.
*
* This usually corresponds to the last set of parameter values used when the model was
* run through the designer.
*
* \see designerParameterValues()
*
* \since QGIS 3.14
*/
void setDesignerParameterValues( const QVariantMap &values ) { mDesignerParameterValues = values; }

protected:

QgsProcessingAlgorithm *createInstance() const override SIP_FACTORY;
Expand All @@ -424,6 +450,8 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm

QVariantMap mVariables;

QVariantMap mDesignerParameterValues;

void dependsOnChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;
void dependentChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;

Expand Down

0 comments on commit 2a1bf56

Please sign in to comment.