Skip to content

Commit

Permalink
[processing] Default to "Model Input" sources for map layer parameters
Browse files Browse the repository at this point in the history
when configuring a new algorithm

While the previous behaviour of defaulting to a static value makes
sense for things like numeric parameters, this is a very rare use
case for map layer parameters. For better new user experience we can
default instead to showing model inputs for these parameter types.
  • Loading branch information
nyalldawson committed Feb 4, 2022
1 parent 25b42ed commit 91cf895
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ Sets the parent ``dialog`` in which the widget is shown.
virtual QgsExpressionContext createExpressionContext() const;


void setSourceType( QgsProcessingModelChildParameterSource::Source type );
%Docstring
Sets the current source ``type`` for the parameter.

.. versionadded:: 3.24
%End

};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,13 @@ This is shown in modeler widget wrappers when using the "pre-calculated" express
and should give helpful text to users to indicate the expected results from the expression.

This is purely a text format and no expression validation is made against it.
%End

virtual QgsProcessingModelChildParameterSource::Source defaultModelSource( const QgsProcessingParameterDefinition *parameter ) const;
%Docstring
Returns the default source type to use for the widget for the specified ``parameter``.

.. versionadded:: 3.24
%End

};
Expand Down
8 changes: 7 additions & 1 deletion src/gui/processing/qgsprocessingmodelerparameterwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ class GUI_EXPORT QgsProcessingModelerParameterWidget : public QWidget, public Qg

QgsExpressionContext createExpressionContext() const override;

/**
* Sets the current source \a type for the parameter.
*
* \since QGIS 3.24
*/
void setSourceType( QgsProcessingModelChildParameterSource::Source type );

private slots:

void sourceMenuAboutToShow();
Expand All @@ -212,7 +219,6 @@ class GUI_EXPORT QgsProcessingModelerParameterWidget : public QWidget, public Qg

SourceType currentSourceType() const;

void setSourceType( QgsProcessingModelChildParameterSource::Source type );
void updateUi();

QgsProcessingModelAlgorithm *mModel = nullptr;
Expand Down
11 changes: 11 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ QgsProcessingModelerParameterWidget *QgsProcessingParameterWidgetFactoryInterfac
std::unique_ptr< QgsProcessingModelerParameterWidget > widget = std::make_unique< QgsProcessingModelerParameterWidget >( model, childId, parameter, context );
widget->populateSources( compatibleParameterTypes(), compatibleOutputTypes(), compatibleDataTypes( parameter ) );
widget->setExpressionHelpText( modelerExpressionFormatString() );

if ( parameter->isDestination() )
widget->setSourceType( QgsProcessingModelChildParameterSource::ModelOutput );
else
widget->setSourceType( defaultModelSource( parameter ) );

return widget.release();
}

Expand All @@ -388,6 +394,11 @@ QString QgsProcessingParameterWidgetFactoryInterface::modelerExpressionFormatStr
return QString();
}

QgsProcessingModelChildParameterSource::Source QgsProcessingParameterWidgetFactoryInterface::defaultModelSource( const QgsProcessingParameterDefinition * ) const
{
return QgsProcessingModelChildParameterSource::StaticValue;
}

//
// QgsProcessingGuiUtils
//
Expand Down
8 changes: 8 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgis_sip.h"
#include "qgsprocessinggui.h"
#include "qgsvectorlayer.h"
#include "qgsprocessingmodelchildparametersource.h"

class QgsProcessingParameterDefinition;
class QgsProcessingContext;
Expand Down Expand Up @@ -661,6 +662,13 @@ class GUI_EXPORT QgsProcessingParameterWidgetFactoryInterface
*/
virtual QString modelerExpressionFormatString() const;

/**
* Returns the default source type to use for the widget for the specified \a parameter.
*
* \since QGIS 3.24
*/
virtual QgsProcessingModelChildParameterSource::Source defaultModelSource( const QgsProcessingParameterDefinition *parameter ) const;

};

/**
Expand Down
5 changes: 5 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6022,6 +6022,11 @@ QString QgsProcessingMapLayerWidgetWrapper::modelerExpressionFormatString() cons
return tr( "path to a map layer" );
}

QgsProcessingModelChildParameterSource::Source QgsProcessingMapLayerWidgetWrapper::defaultModelSource( const QgsProcessingParameterDefinition * ) const
{
return QgsProcessingModelChildParameterSource::ModelParameter;
}

QString QgsProcessingMapLayerWidgetWrapper::parameterType() const
{
return QgsProcessingParameterMapLayer::typeName();
Expand Down
1 change: 1 addition & 0 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,7 @@ class GUI_EXPORT QgsProcessingMapLayerWidgetWrapper : public QgsAbstractProcessi

QStringList compatibleOutputTypes() const override;
QString modelerExpressionFormatString() const override;
QgsProcessingModelChildParameterSource::Source defaultModelSource( const QgsProcessingParameterDefinition *parameter ) const override;

private:

Expand Down
15 changes: 15 additions & 0 deletions tests/src/gui/testprocessinggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,9 @@ void TestProcessingGui::testModelerWrapper()
model.addModelParameter( new TestParamType( "test_type", "p2" ), testParam );
QgsProcessingModelParameter testDestParam( "p3" );
model.addModelParameter( new QgsProcessingParameterFileDestination( "test_dest", "p3" ), testDestParam );
QgsProcessingModelParameter testLayerParam( "p4" );
model.addModelParameter( new QgsProcessingParameterMapLayer( "p4", "test_layer" ), testLayerParam );

// try to create a parameter widget, no factories registered
QgsProcessingGuiRegistry registry;
QgsProcessingContext context;
Expand All @@ -750,9 +753,21 @@ void TestProcessingGui::testModelerWrapper()
QVERIFY( w );
delete w;

w = registry.createModelerParameterWidget( &model, QStringLiteral( "a" ), model.parameterDefinition( "p1" ), context );
QVERIFY( w );
// should default to static value
QCOMPARE( w->value().value< QgsProcessingModelChildParameterSource>().source(), QgsProcessingModelChildParameterSource::StaticValue );
delete w;

w = registry.createModelerParameterWidget( &model, QStringLiteral( "a" ), model.parameterDefinition( "p4" ), context );
QVERIFY( w );
// a layer parameter should default to "model input" type
QCOMPARE( w->value().value< QgsProcessingModelChildParameterSource>().source(), QgsProcessingModelChildParameterSource::ModelParameter );
delete w;

// widget tests
w = new QgsProcessingModelerParameterWidget( &model, "alg1", model.parameterDefinition( "p1" ), context );
QCOMPARE( w->value().value< QgsProcessingModelChildParameterSource>().source(), QgsProcessingModelChildParameterSource::StaticValue );
QCOMPARE( w->parameterDefinition()->name(), QStringLiteral( "p1" ) );
QLabel *l = w->createLabel();
QVERIFY( l );
Expand Down

0 comments on commit 91cf895

Please sign in to comment.