Skip to content

Commit

Permalink
Add metadata to permit processing parameters to limit the available
Browse files Browse the repository at this point in the history
types of value source they can be linked to in models
  • Loading branch information
nyalldawson committed Aug 25, 2023
1 parent 440cac2 commit 32abc34
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
54 changes: 40 additions & 14 deletions src/gui/processing/qgsprocessingmodelerparameterwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ QgsProcessingModelerParameterWidget::QgsProcessingModelerParameterWidget( QgsPro

QHBoxLayout *hLayout = new QHBoxLayout();

{
const QVariantList acceptedSourcesMetadata = mParameterDefinition->metadata().value( QStringLiteral( "model_widget" ) ).toMap().value( QStringLiteral( "accepted_sources" ) ).toList();
for ( const QVariant &acceptedSource : acceptedSourcesMetadata )
{
mLimitedSources.append( static_cast< Qgis::ProcessingModelChildParameterSource >( acceptedSource.toInt() ) );
}
}

mSourceButton = new QToolButton();
mSourceButton->setFocusPolicy( Qt::StrongFocus );

Expand Down Expand Up @@ -279,36 +287,47 @@ void QgsProcessingModelerParameterWidget::sourceMenuAboutToShow()

const SourceType currentSource = currentSourceType();

if ( mParameterDefinition->isDestination() )
if ( mParameterDefinition->isDestination()
&& ( mLimitedSources.empty() || mLimitedSources.contains( Qgis::ProcessingModelChildParameterSource::ModelOutput ) ) )
{
QAction *modelOutputAction = mSourceMenu->addAction( tr( "Model Output" ) );
modelOutputAction->setCheckable( currentSource == ModelOutput );
modelOutputAction->setChecked( currentSource == ModelOutput );
modelOutputAction->setData( QVariant::fromValue( Qgis::ProcessingModelChildParameterSource::ModelOutput ) );
}

if ( mHasStaticWrapper )
if ( mHasStaticWrapper
&& ( mLimitedSources.empty() || mLimitedSources.contains( Qgis::ProcessingModelChildParameterSource::StaticValue ) ) )
{
QAction *fixedValueAction = mSourceMenu->addAction( tr( "Value" ) );
fixedValueAction->setCheckable( currentSource == StaticValue );
fixedValueAction->setChecked( currentSource == StaticValue );
fixedValueAction->setData( QVariant::fromValue( Qgis::ProcessingModelChildParameterSource::StaticValue ) );
}

QAction *calculatedValueAction = mSourceMenu->addAction( tr( "Pre-calculated Value" ) );
calculatedValueAction->setCheckable( currentSource == Expression );
calculatedValueAction->setChecked( currentSource == Expression );
calculatedValueAction->setData( QVariant::fromValue( Qgis::ProcessingModelChildParameterSource::Expression ) );
if ( mLimitedSources.empty() || mLimitedSources.contains( Qgis::ProcessingModelChildParameterSource::Expression ) )
{
QAction *calculatedValueAction = mSourceMenu->addAction( tr( "Pre-calculated Value" ) );
calculatedValueAction->setCheckable( currentSource == Expression );
calculatedValueAction->setChecked( currentSource == Expression );
calculatedValueAction->setData( QVariant::fromValue( Qgis::ProcessingModelChildParameterSource::Expression ) );
}

QAction *inputValueAction = mSourceMenu->addAction( tr( "Model Input" ) );
inputValueAction->setCheckable( currentSource == ModelParameter );
inputValueAction->setChecked( currentSource == ModelParameter );
inputValueAction->setData( QVariant::fromValue( Qgis::ProcessingModelChildParameterSource::ModelParameter ) );
if ( mLimitedSources.empty() || mLimitedSources.contains( Qgis::ProcessingModelChildParameterSource::ModelParameter ) )
{
QAction *inputValueAction = mSourceMenu->addAction( tr( "Model Input" ) );
inputValueAction->setCheckable( currentSource == ModelParameter );
inputValueAction->setChecked( currentSource == ModelParameter );
inputValueAction->setData( QVariant::fromValue( Qgis::ProcessingModelChildParameterSource::ModelParameter ) );
}

QAction *childOutputValueAction = mSourceMenu->addAction( tr( "Algorithm Output" ) );
childOutputValueAction->setCheckable( currentSource == ChildOutput );
childOutputValueAction->setChecked( currentSource == ChildOutput );
childOutputValueAction->setData( QVariant::fromValue( Qgis::ProcessingModelChildParameterSource::ChildOutput ) );
if ( mLimitedSources.empty() || mLimitedSources.contains( Qgis::ProcessingModelChildParameterSource::ChildOutput ) )
{
QAction *childOutputValueAction = mSourceMenu->addAction( tr( "Algorithm Output" ) );
childOutputValueAction->setCheckable( currentSource == ChildOutput );
childOutputValueAction->setChecked( currentSource == ChildOutput );
childOutputValueAction->setData( QVariant::fromValue( Qgis::ProcessingModelChildParameterSource::ChildOutput ) );
}

// TODO - expression text item?
}
Expand All @@ -326,6 +345,13 @@ QgsProcessingModelerParameterWidget::SourceType QgsProcessingModelerParameterWid

void QgsProcessingModelerParameterWidget::setSourceType( Qgis::ProcessingModelChildParameterSource type )
{
if ( !mLimitedSources.empty() && !mLimitedSources.contains( type ) )
{
// specified type is not acceptable for this parameter, so override with the first acceptable
// type
type = mLimitedSources.at( 0 );
}

switch ( type )
{
case Qgis::ProcessingModelChildParameterSource::StaticValue:
Expand Down
2 changes: 2 additions & 0 deletions src/gui/processing/qgsprocessingmodelerparameterwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class GUI_EXPORT QgsProcessingModelerParameterWidget : public QWidget, public Qg
QComboBox *mChildOutputCombo = nullptr;
QgsFilterLineEdit *mModelOutputName = nullptr;

QList< Qgis::ProcessingModelChildParameterSource > mLimitedSources;

friend class TestProcessingGui;
};

Expand Down

0 comments on commit 32abc34

Please sign in to comment.