Skip to content
Permalink
Browse files

[processing] Add method to set an explicit override for the default

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)

(cherry picked from commit 83ccb65)
  • Loading branch information
nyalldawson committed Dec 21, 2020
1 parent 2d9a1c2 commit f3d22299bcfc2585ac0b4e01e4aa49b6635ce15f
@@ -404,6 +404,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 );
@@ -412,6 +416,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;
@@ -649,6 +699,7 @@ The ``variables`` list should contain the variable names only, without the usual




};

QFlags<QgsProcessingParameterDefinition::Flag> operator|(QgsProcessingParameterDefinition::Flag f1, QFlags<QgsProcessingParameterDefinition::Flag> f2);
@@ -2362,6 +2362,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;
@@ -2373,6 +2374,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;
@@ -505,16 +505,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()
@@ -729,6 +773,9 @@ class CORE_EXPORT QgsProcessingParameterDefinition
//! Default value for parameter
QVariant mDefault;

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

//! Parameter flags
Flags mFlags;

@@ -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() );
}
}

@@ -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() )
@@ -65,7 +65,7 @@ QgsProcessingLayerOutputDestinationWidget::QgsProcessingLayerOutputDestinationWi
}
else
{
setValue( mParameter->defaultValue() );
setValue( mParameter->defaultValueForGui() );
}

setToolTip( mParameter->toolTip() );
@@ -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;

0 comments on commit f3d2229

Please sign in to comment.
You can’t perform that action at this time.