Skip to content

Commit

Permalink
[processing] Add method to set an explicit override for the default
Browse files Browse the repository at this point in the history
value of a parameter for the GUI only

This allows us freedom to change the default settings for an algorithm
shown when opening the algorithm in the toolbox/batch/model without
changing the underlying default value used for the parameter in the
raw API (which we can't do easily without potentially breaking 3rd
party scripts/plugins)
  • Loading branch information
nyalldawson committed Nov 24, 2020
1 parent 99fa35c commit 83ccb65
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 49 deletions.
Expand Up @@ -420,6 +420,10 @@ the parameter's behavior and use in depth.
Returns the default value for the parameter.

.. seealso:: :py:func:`setDefaultValue`

.. seealso:: :py:func:`defaultValueForGui`

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

void setDefaultValue( const QVariant &value );
Expand All @@ -428,6 +432,52 @@ Sets the default ``value`` for the parameter. Caller takes responsibility
to ensure that ``value`` is a valid input for the parameter subclass.

.. seealso:: :py:func:`defaultValue`

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

QVariant guiDefaultValueOverride() const;
%Docstring
Returns the default value to use in the GUI for the parameter.

Usually this will return an invalid variant, which indicates that the standard :py:func:`~QgsProcessingParameterDefinition.defaultValue`
will be used in the GUI.

.. seealso:: :py:func:`defaultValue`

.. seealso:: :py:func:`setGuiDefaultValueOverride`

.. seealso:: :py:func:`defaultValueForGui`

.. versionadded:: 3.18
%End

void setGuiDefaultValueOverride( const QVariant &value );
%Docstring
Sets the default ``value`` to use for the parameter in GUI widgets. Caller takes responsibility
to ensure that ``value`` is a valid input for the parameter subclass.

Usually the :py:func:`~QgsProcessingParameterDefinition.guiDefaultValueOverride` is a invalid variant, which indicates that the standard :py:func:`~QgsProcessingParameterDefinition.defaultValue`
should be used in the GUI. In cases where it is decided that a previous default value was inappropriate,
setting a non-invalid default GUI value can be used to change the default value for the parameter shown
to users when running algorithms without changing the actual :py:func:`~QgsProcessingParameterDefinition.defaultValue` and potentially breaking
third party scripts.

.. seealso:: :py:func:`guiDefaultValueOverride`

.. seealso:: :py:func:`setDefaultValue`

.. versionadded:: 3.18
%End

QVariant defaultValueForGui() const;
%Docstring
Returns the default value to use for the parameter in a GUI.

This will be the parameter's :py:func:`~QgsProcessingParameterDefinition.defaultValue`, unless a :py:func:`~QgsProcessingParameterDefinition.guiDefaultValueOverride` is set to
override that.

.. versionadded:: 3.18
%End

Flags flags() const;
Expand Down Expand Up @@ -665,6 +715,7 @@ The ``variables`` list should contain the variable names only, without the usual




};

QFlags<QgsProcessingParameterDefinition::Flag> operator|(QgsProcessingParameterDefinition::Flag f1, QFlags<QgsProcessingParameterDefinition::Flag> f2);
Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -2447,6 +2447,7 @@ QVariantMap QgsProcessingParameterDefinition::toVariantMap() const
map.insert( QStringLiteral( "description" ), mDescription );
map.insert( QStringLiteral( "help" ), mHelp );
map.insert( QStringLiteral( "default" ), mDefault );
map.insert( QStringLiteral( "defaultGui" ), mGuiDefault );
map.insert( QStringLiteral( "flags" ), static_cast< int >( mFlags ) );
map.insert( QStringLiteral( "metadata" ), mMetadata );
return map;
Expand All @@ -2458,6 +2459,7 @@ bool QgsProcessingParameterDefinition::fromVariantMap( const QVariantMap &map )
mDescription = map.value( QStringLiteral( "description" ) ).toString();
mHelp = map.value( QStringLiteral( "help" ) ).toString();
mDefault = map.value( QStringLiteral( "default" ) );
mGuiDefault = map.value( QStringLiteral( "defaultGui" ) );
mFlags = static_cast< Flags >( map.value( QStringLiteral( "flags" ) ).toInt() );
mMetadata = map.value( QStringLiteral( "metadata" ) ).toMap();
return true;
Expand Down
47 changes: 47 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -521,16 +521,60 @@ class CORE_EXPORT QgsProcessingParameterDefinition
/**
* Returns the default value for the parameter.
* \see setDefaultValue()
* \see defaultValueForGui()
* \see guiDefaultValueOverride()
*/
QVariant defaultValue() const { return mDefault; }

/**
* Sets the default \a value for the parameter. Caller takes responsibility
* to ensure that \a value is a valid input for the parameter subclass.
* \see defaultValue()
* \see setGuiDefaultValueOverride()
*/
void setDefaultValue( const QVariant &value ) { mDefault = value; }

/**
* Returns the default value to use in the GUI for the parameter.
*
* Usually this will return an invalid variant, which indicates that the standard defaultValue()
* will be used in the GUI.
*
* \see defaultValue()
* \see setGuiDefaultValueOverride()
* \see defaultValueForGui()
*
* \since QGIS 3.18
*/
QVariant guiDefaultValueOverride() const { return mGuiDefault; }

/**
* Sets the default \a value to use for the parameter in GUI widgets. Caller takes responsibility
* to ensure that \a value is a valid input for the parameter subclass.
*
* Usually the guiDefaultValueOverride() is a invalid variant, which indicates that the standard defaultValue()
* should be used in the GUI. In cases where it is decided that a previous default value was inappropriate,
* setting a non-invalid default GUI value can be used to change the default value for the parameter shown
* to users when running algorithms without changing the actual defaultValue() and potentially breaking
* third party scripts.
*
* \see guiDefaultValueOverride()
* \see setDefaultValue()
*
* \since QGIS 3.18
*/
void setGuiDefaultValueOverride( const QVariant &value ) { mGuiDefault = value; }

/**
* Returns the default value to use for the parameter in a GUI.
*
* This will be the parameter's defaultValue(), unless a guiDefaultValueOverride() is set to
* override that.
*
* \since QGIS 3.18
*/
QVariant defaultValueForGui() const { return mGuiDefault.isValid() ? mGuiDefault : mDefault; }

/**
* Returns any flags associated with the parameter.
* \see setFlags()
Expand Down Expand Up @@ -745,6 +789,9 @@ class CORE_EXPORT QgsProcessingParameterDefinition
//! Default value for parameter
QVariant mDefault;

//! Default value for parameter in GUI
QVariant mGuiDefault;

//! Parameter flags
Flags mFlags;

Expand Down
2 changes: 1 addition & 1 deletion src/gui/processing/qgsprocessingmaplayercombobox.cpp
Expand Up @@ -251,7 +251,7 @@ void QgsProcessingMapLayerComboBox::setValue( const QVariant &value, QgsProcessi
}
else
{
val = val.value< QgsProperty >().valueAsString( context.expressionContext(), mParameter->defaultValue().toString() );
val = val.value< QgsProperty >().valueAsString( context.expressionContext(), mParameter->defaultValueForGui().toString() );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/processing/qgsprocessingoutputdestinationwidget.cpp
Expand Up @@ -55,7 +55,7 @@ QgsProcessingLayerOutputDestinationWidget::QgsProcessingLayerOutputDestinationWi
QgsSettings settings;
mEncoding = settings.value( QStringLiteral( "/Processing/encoding" ), QStringLiteral( "System" ) ).toString();

if ( !mParameter->defaultValue().isValid() )
if ( !mParameter->defaultValueForGui().isValid() )
{
// no default value -- we default to either skipping the output or a temporary output, depending on the createByDefault value
if ( mParameter->flags() & QgsProcessingParameterDefinition::FlagOptional && !mParameter->createByDefault() )
Expand All @@ -65,7 +65,7 @@ QgsProcessingLayerOutputDestinationWidget::QgsProcessingLayerOutputDestinationWi
}
else
{
setValue( mParameter->defaultValue() );
setValue( mParameter->defaultValueForGui() );
}

setToolTip( mParameter->toolTip() );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/processing/qgsprocessingwidgetwrapper.cpp
Expand Up @@ -153,7 +153,7 @@ QWidget *QgsAbstractProcessingParameterWidgetWrapper::createWrappedWidget( QgsPr
if ( !dynamic_cast<const QgsProcessingDestinationParameter * >( mParameterDefinition ) )
{
// an exception -- output widgets handle this themselves
setWidgetValue( mParameterDefinition->defaultValue(), context );
setWidgetValue( mParameterDefinition->defaultValueForGui(), context );
}

return wrappedWidget;
Expand Down

0 comments on commit 83ccb65

Please sign in to comment.