32#include <QStackedWidget>
38 const QString &childId,
44 , mParameterDefinition( parameter )
47 setFocusPolicy( Qt::StrongFocus );
52 QHBoxLayout *hLayout =
new QHBoxLayout();
55 const QVariantList acceptedSourcesMetadata = mParameterDefinition->
metadata().value( QStringLiteral(
"model_widget" ) ).toMap().value( QStringLiteral(
"accepted_sources" ) ).toList();
56 for (
const QVariant &acceptedSource : acceptedSourcesMetadata )
62 mSourceButton =
new QToolButton();
63 mSourceButton->setFocusPolicy( Qt::StrongFocus );
66 mSourceButton->setFixedSize( 2 *
static_cast< int >( 1.25 * iconSize / 2.0 ), 2 *
static_cast< int >( iconSize * 1.1 / 2.0 ) );
67 mSourceButton->setIconSize( QSize( iconSize, iconSize ) );
68 mSourceButton->setPopupMode( QToolButton::InstantPopup );
70 mSourceMenu =
new QMenu(
this );
71 connect( mSourceMenu, &QMenu::aboutToShow,
this, &QgsProcessingModelerParameterWidget::sourceMenuAboutToShow );
72 connect( mSourceMenu, &QMenu::triggered,
this, &QgsProcessingModelerParameterWidget::sourceMenuActionTriggered );
73 mSourceButton->setMenu( mSourceMenu );
75 hLayout->addWidget( mSourceButton );
77 mStackedWidget =
new QStackedWidget();
80 if ( mStaticWidgetWrapper )
82 QWidget *widget = mStaticWidgetWrapper->createWrappedWidget( context );
85 mHasStaticWrapper =
true;
86 mStackedWidget->addWidget( widget );
89 mStackedWidget->addWidget(
new QWidget() );
93 mStackedWidget->addWidget(
new QWidget() );
98 mStackedWidget->addWidget( mExpressionWidget );
100 mModelInputCombo =
new QComboBox();
101 QHBoxLayout *hLayout2 =
new QHBoxLayout();
102 hLayout2->setContentsMargins( 0, 0, 0, 0 );
103 hLayout2->addWidget(
new QLabel( tr(
"Using model input" ) ) );
104 hLayout2->addWidget( mModelInputCombo, 1 );
105 QWidget *hWidget2 =
new QWidget();
106 hWidget2->setLayout( hLayout2 );
107 mStackedWidget->addWidget( hWidget2 );
109 mChildOutputCombo =
new QComboBox();
110 QHBoxLayout *hLayout3 =
new QHBoxLayout();
111 hLayout3->setContentsMargins( 0, 0, 0, 0 );
112 hLayout3->addWidget(
new QLabel( tr(
"Using algorithm output" ) ) );
113 hLayout3->addWidget( mChildOutputCombo, 1 );
114 QWidget *hWidget3 =
new QWidget();
115 hWidget3->setLayout( hLayout3 );
116 mStackedWidget->addWidget( hWidget3 );
121 mModelOutputName->setPlaceholderText( tr(
"[Enter name if this is a final result]" ) );
122 QHBoxLayout *hLayout4 =
new QHBoxLayout();
123 hLayout4->setContentsMargins( 0, 0, 0, 0 );
124 hLayout4->addWidget( mModelOutputName );
125 QWidget *hWidget4 =
new QWidget();
126 hWidget4->setLayout( hLayout4 );
127 mStackedWidget->addWidget( hWidget4 );
130 hLayout->setContentsMargins( 0, 0, 0, 0 );
131 hLayout->addWidget( mStackedWidget, 1 );
133 setLayout( hLayout );
141 if ( mStaticWidgetWrapper )
142 mStaticWidgetWrapper->setWidgetContext( context );
147 if ( mStaticWidgetWrapper )
148 mStaticWidgetWrapper->registerProcessingContextGenerator( generator );
153 return mParameterDefinition;
158 if ( mStaticWidgetWrapper )
159 return mStaticWidgetWrapper->createWrappedLabel();
168 mStaticValue =
value.staticValue();
169 mModelInputParameterName =
value.parameterName();
170 mOutputChildId =
value.outputChildId();
171 mOutputName =
value.outputName();
172 mExpression =
value.expression();
180 if ( values.size() == 1 )
185 for (
const QgsProcessingModelChildParameterSource &v : values )
186 r << QVariant::fromValue( v );
195 if ( mModelOutputName )
196 mModelOutputName->setText(
value );
202 return currentSourceType() == ModelOutput;
207 return mModelOutputName ? mModelOutputName->text().trimmed() : QString();
212 switch ( currentSourceType() )
216 const QVariant v = mStaticWidgetWrapper->parameterValue();
218 if ( v.userType() == QMetaType::Type::QVariantList )
220 const QVariantList vList = v.toList();
221 if ( std::all_of( vList.begin(), vList.end(), [](
const QVariant & val )
223 return val.userType() == QMetaType::type(
"QgsProcessingModelChildParameterSource" );
229 return QVariant::fromValue( QgsProcessingModelChildParameterSource::fromStaticValue( v ) );
233 return QVariant::fromValue( QgsProcessingModelChildParameterSource::fromExpression( mExpressionWidget->
expression() ) );
236 return QVariant::fromValue( QgsProcessingModelChildParameterSource::fromModelParameter( mModelInputCombo->currentData().toString() ) );
240 const QStringList parts = mChildOutputCombo->currentData().toStringList();
241 return QVariant::fromValue( QgsProcessingModelChildParameterSource::fromChildOutput( parts.value( 0, QString() ), parts.value( 1, QString() ) ) );
245 return mModelOutputName ? ( mModelOutputName->text().trimmed().isEmpty() ? QVariant() : mModelOutputName->text() ) : QVariant();
248 return QVariant::fromValue( QgsProcessingModelChildParameterSource() );
253 if ( mStaticWidgetWrapper )
254 mStaticWidgetWrapper->setDialog( dialog );
263 if ( mModel->childAlgorithms().contains( mChildId ) )
264 alg = mModel->childAlgorithm( mChildId ).algorithm();
269 QgsExpressionContextScope *childScope = mModel->createExpressionContextScopeForChildAlgorithm( mChildId, mContext, QVariantMap(), QVariantMap() );
272 QStringList highlightedVariables = childScope->
variableNames();
273 QStringList highlightedFunctions = childScope->
functionNames();
275 highlightedVariables += mModel->variables().keys();
277 c.setHighlightedVariables( highlightedVariables );
278 c.setHighlightedFunctions( highlightedFunctions );
284void QgsProcessingModelerParameterWidget::sourceMenuAboutToShow()
286 mSourceMenu->clear();
288 const SourceType currentSource = currentSourceType();
293 QAction *modelOutputAction = mSourceMenu->addAction( tr(
"Model Output" ) );
294 modelOutputAction->setCheckable( currentSource == ModelOutput );
295 modelOutputAction->setChecked( currentSource == ModelOutput );
299 if ( mHasStaticWrapper
302 QAction *fixedValueAction = mSourceMenu->addAction( tr(
"Value" ) );
303 fixedValueAction->setCheckable( currentSource == StaticValue );
304 fixedValueAction->setChecked( currentSource == StaticValue );
310 QAction *calculatedValueAction = mSourceMenu->addAction( tr(
"Pre-calculated Value" ) );
311 calculatedValueAction->setCheckable( currentSource == Expression );
312 calculatedValueAction->setChecked( currentSource == Expression );
318 QAction *inputValueAction = mSourceMenu->addAction( tr(
"Model Input" ) );
319 inputValueAction->setCheckable( currentSource == ModelParameter );
320 inputValueAction->setChecked( currentSource == ModelParameter );
326 QAction *childOutputValueAction = mSourceMenu->addAction( tr(
"Algorithm Output" ) );
327 childOutputValueAction->setCheckable( currentSource == ChildOutput );
328 childOutputValueAction->setChecked( currentSource == ChildOutput );
335void QgsProcessingModelerParameterWidget::sourceMenuActionTriggered( QAction *action )
341QgsProcessingModelerParameterWidget::SourceType QgsProcessingModelerParameterWidget::currentSourceType()
const
343 return static_cast< SourceType
>( mStackedWidget->currentIndex() );
348 if ( !mLimitedSources.empty() && !mLimitedSources.contains( type ) )
352 type = mLimitedSources.at( 0 );
358 mStackedWidget->setCurrentIndex(
static_cast< int >( StaticValue ) );
360 mSourceButton->setToolTip( tr(
"Value" ) );
365 mStackedWidget->setCurrentIndex(
static_cast< int >( Expression ) );
366 mSourceButton->setToolTip( tr(
"Pre-calculated Value" ) );
372 mStackedWidget->setCurrentIndex(
static_cast< int >( ModelParameter ) );
373 mSourceButton->setToolTip( tr(
"Model Input" ) );
380 mStackedWidget->setCurrentIndex(
static_cast< int >( ChildOutput ) );
381 mSourceButton->setToolTip( tr(
"Algorithm Output" ) );
388 mStackedWidget->setCurrentIndex(
static_cast< int >( ModelOutput ) );
389 mSourceButton->setToolTip( tr(
"Model Output" ) );
398void QgsProcessingModelerParameterWidget::updateUi()
400 mStaticWidgetWrapper->setParameterValue( mStaticValue, mContext );
404 int currentIndex = mModelInputCombo->findData( mModelInputParameterName );
405 if ( currentIndex == -1 && mModelInputCombo->count() > 0 )
407 mModelInputCombo->setCurrentIndex( currentIndex );
409 const QStringList parts = QStringList() << mOutputChildId << mOutputName;
410 currentIndex = mChildOutputCombo->findData( parts );
411 if ( currentIndex == -1 && mChildOutputCombo->count() > 0 )
413 mChildOutputCombo->setCurrentIndex( currentIndex );
418 const QList< QgsProcessingModelChildParameterSource > sources = mModel->availableSourcesForChild( mChildId,
419 compatibleParameterTypes, compatibleOutputTypes, compatibleDataTypes );
421 for (
const QgsProcessingModelChildParameterSource &source : sources )
423 switch ( source.source() )
426 mModelInputCombo->addItem( mModel->parameterDefinition( source.parameterName() )->description(), source.parameterName() );
431 if ( !mModel->childAlgorithms().contains( source.outputChildId() ) )
434 const QgsProcessingModelChildAlgorithm &alg = mModel->childAlgorithm( source.outputChildId() );
435 if ( !alg.algorithm() )
437 const QString outputDescription = alg.algorithm()->outputDefinition( source.outputName() )->description();
438 const QString childDescription = alg.description();
440 mChildOutputCombo->addItem( tr(
"“%1” from algorithm “%2”" ).arg( outputDescription, childDescription ), QStringList() << source.outputChildId() << source.outputName() );
ProcessingModelChildParameterSource
Processing model child parameter sources.
@ ExpressionText
Parameter value is taken from a text with expressions, evaluated just before the algorithm runs.
@ ModelOutput
Parameter value is linked to an output parameter for the model.
@ ChildOutput
Parameter value is taken from an output generated by a child algorithm.
@ ModelParameter
Parameter value is taken from a parent model parameter.
@ StaticValue
Parameter value is a static value.
@ Expression
Parameter value is taken from an expression, evaluated just before the algorithm runs.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QStringList functionNames() const
Retrieves a list of names of functions contained in the scope.
QStringList variableNames() const
Returns a list of variable names contained within the scope.
static QgsExpressionContextScope * processingModelAlgorithmScope(const QgsProcessingModelAlgorithm *model, const QVariantMap ¶meters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing model algorithm,...
static QgsExpressionContextScope * processingAlgorithmScope(const QgsProcessingAlgorithm *algorithm, const QVariantMap ¶meters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing algorithm,...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The QgsExpressionLineEdit widget includes a line edit for entering expressions together with a button...
QString expression() const
Returns the current expression shown in the widget.
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
void setExpectedOutputFormat(const QString &expected)
Set the expected format string, which is shown in the expression builder dialog for the widget.
void setExpression(const QString &expression)
Sets the current expression to show in the widget.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Abstract base class for processing algorithms.
An interface for objects which can create Processing contexts.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for the definition of processing parameters.
QVariantMap metadata() const
Returns the parameter's freeform metadata.
virtual bool isDestination() const
Returns true if this parameter represents a file or layer destination, e.g.
Contains settings which reflect the context in which a Processing parameter widget is shown,...
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c