20#include "moc_qgsprocessingmodelerparameterwidget.cpp"
33#include <QStackedWidget>
39 const QString &childId,
45 , mParameterDefinition( parameter )
48 setFocusPolicy( Qt::StrongFocus );
53 QHBoxLayout *hLayout =
new QHBoxLayout();
56 const QVariantList acceptedSourcesMetadata = mParameterDefinition->
metadata().value( QStringLiteral(
"model_widget" ) ).toMap().value( QStringLiteral(
"accepted_sources" ) ).toList();
57 for (
const QVariant &acceptedSource : acceptedSourcesMetadata )
63 mSourceButton =
new QToolButton();
64 mSourceButton->setFocusPolicy( Qt::StrongFocus );
67 mSourceButton->setFixedSize( 2 *
static_cast< int >( 1.25 * iconSize / 2.0 ), 2 *
static_cast< int >( iconSize * 1.1 / 2.0 ) );
68 mSourceButton->setIconSize( QSize( iconSize, iconSize ) );
69 mSourceButton->setPopupMode( QToolButton::InstantPopup );
71 mSourceMenu =
new QMenu(
this );
72 connect( mSourceMenu, &QMenu::aboutToShow,
this, &QgsProcessingModelerParameterWidget::sourceMenuAboutToShow );
73 connect( mSourceMenu, &QMenu::triggered,
this, &QgsProcessingModelerParameterWidget::sourceMenuActionTriggered );
74 mSourceButton->setMenu( mSourceMenu );
76 hLayout->addWidget( mSourceButton );
78 mStackedWidget =
new QStackedWidget();
81 if ( mStaticWidgetWrapper )
83 QWidget *widget = mStaticWidgetWrapper->createWrappedWidget( context );
86 mHasStaticWrapper =
true;
87 mStackedWidget->addWidget( widget );
90 mStackedWidget->addWidget(
new QWidget() );
94 mStackedWidget->addWidget(
new QWidget() );
99 mStackedWidget->addWidget( mExpressionWidget );
101 mModelInputCombo =
new QComboBox();
102 QHBoxLayout *hLayout2 =
new QHBoxLayout();
103 hLayout2->setContentsMargins( 0, 0, 0, 0 );
104 hLayout2->addWidget(
new QLabel( tr(
"Using model input" ) ) );
105 hLayout2->addWidget( mModelInputCombo, 1 );
106 QWidget *hWidget2 =
new QWidget();
107 hWidget2->setLayout( hLayout2 );
108 mStackedWidget->addWidget( hWidget2 );
110 mChildOutputCombo =
new QComboBox();
111 QHBoxLayout *hLayout3 =
new QHBoxLayout();
112 hLayout3->setContentsMargins( 0, 0, 0, 0 );
113 hLayout3->addWidget(
new QLabel( tr(
"Using algorithm output" ) ) );
114 hLayout3->addWidget( mChildOutputCombo, 1 );
115 QWidget *hWidget3 =
new QWidget();
116 hWidget3->setLayout( hLayout3 );
117 mStackedWidget->addWidget( hWidget3 );
122 mModelOutputName->setPlaceholderText( tr(
"[Enter name if this is a final result]" ) );
123 QHBoxLayout *hLayout4 =
new QHBoxLayout();
124 hLayout4->setContentsMargins( 0, 0, 0, 0 );
125 hLayout4->addWidget( mModelOutputName );
126 QWidget *hWidget4 =
new QWidget();
127 hWidget4->setLayout( hLayout4 );
128 mStackedWidget->addWidget( hWidget4 );
131 hLayout->setContentsMargins( 0, 0, 0, 0 );
132 hLayout->addWidget( mStackedWidget, 1 );
134 setLayout( hLayout );
142 if ( mStaticWidgetWrapper )
143 mStaticWidgetWrapper->setWidgetContext( context );
148 if ( mStaticWidgetWrapper )
149 mStaticWidgetWrapper->registerProcessingContextGenerator( generator );
154 return mParameterDefinition;
159 if ( mStaticWidgetWrapper )
160 return mStaticWidgetWrapper->createWrappedLabel();
169 mStaticValue =
value.staticValue();
170 mModelInputParameterName =
value.parameterName();
171 mOutputChildId =
value.outputChildId();
172 mOutputName =
value.outputName();
173 mExpression =
value.expression();
181 if ( values.size() == 1 )
186 for (
const QgsProcessingModelChildParameterSource &v : values )
187 r << QVariant::fromValue( v );
196 if ( mModelOutputName )
197 mModelOutputName->setText(
value );
203 return currentSourceType() == ModelOutput;
208 return mModelOutputName ? mModelOutputName->text().trimmed() : QString();
213 switch ( currentSourceType() )
217 const QVariant v = mStaticWidgetWrapper->parameterValue();
219 if ( v.userType() == QMetaType::Type::QVariantList )
221 const QVariantList vList = v.toList();
222 if ( std::all_of( vList.begin(), vList.end(), [](
const QVariant & val )
224 return val.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>();
230 return QVariant::fromValue( QgsProcessingModelChildParameterSource::fromStaticValue( v ) );
234 return QVariant::fromValue( QgsProcessingModelChildParameterSource::fromExpression( mExpressionWidget->
expression() ) );
237 return QVariant::fromValue( QgsProcessingModelChildParameterSource::fromModelParameter( mModelInputCombo->currentData().toString() ) );
241 const QStringList parts = mChildOutputCombo->currentData().toStringList();
242 return QVariant::fromValue( QgsProcessingModelChildParameterSource::fromChildOutput( parts.value( 0, QString() ), parts.value( 1, QString() ) ) );
246 return mModelOutputName ? ( mModelOutputName->text().trimmed().isEmpty() ? QVariant() : mModelOutputName->text() ) : QVariant();
249 return QVariant::fromValue( QgsProcessingModelChildParameterSource() );
254 if ( mStaticWidgetWrapper )
255 mStaticWidgetWrapper->setDialog( dialog );
264 if ( mModel->childAlgorithms().contains( mChildId ) )
265 alg = mModel->childAlgorithm( mChildId ).algorithm();
270 QgsExpressionContextScope *childScope = mModel->createExpressionContextScopeForChildAlgorithm( mChildId, mContext, QVariantMap(), QVariantMap() );
273 QStringList highlightedVariables = childScope->
variableNames();
274 QStringList highlightedFunctions = childScope->
functionNames();
276 highlightedVariables += mModel->variables().keys();
278 c.setHighlightedVariables( highlightedVariables );
279 c.setHighlightedFunctions( highlightedFunctions );
285void QgsProcessingModelerParameterWidget::sourceMenuAboutToShow()
287 mSourceMenu->clear();
289 const SourceType currentSource = currentSourceType();
294 QAction *modelOutputAction = mSourceMenu->addAction( tr(
"Model Output" ) );
295 modelOutputAction->setCheckable( currentSource == ModelOutput );
296 modelOutputAction->setChecked( currentSource == ModelOutput );
300 if ( mHasStaticWrapper
303 QAction *fixedValueAction = mSourceMenu->addAction( tr(
"Value" ) );
304 fixedValueAction->setCheckable( currentSource == StaticValue );
305 fixedValueAction->setChecked( currentSource == StaticValue );
311 QAction *calculatedValueAction = mSourceMenu->addAction( tr(
"Pre-calculated Value" ) );
312 calculatedValueAction->setCheckable( currentSource == Expression );
313 calculatedValueAction->setChecked( currentSource == Expression );
319 QAction *inputValueAction = mSourceMenu->addAction( tr(
"Model Input" ) );
320 inputValueAction->setCheckable( currentSource == ModelParameter );
321 inputValueAction->setChecked( currentSource == ModelParameter );
327 QAction *childOutputValueAction = mSourceMenu->addAction( tr(
"Algorithm Output" ) );
328 childOutputValueAction->setCheckable( currentSource == ChildOutput );
329 childOutputValueAction->setChecked( currentSource == ChildOutput );
336void QgsProcessingModelerParameterWidget::sourceMenuActionTriggered( QAction *action )
342QgsProcessingModelerParameterWidget::SourceType QgsProcessingModelerParameterWidget::currentSourceType()
const
344 return static_cast< SourceType
>( mStackedWidget->currentIndex() );
349 if ( !mLimitedSources.empty() && !mLimitedSources.contains( type ) )
353 type = mLimitedSources.at( 0 );
359 mStackedWidget->setCurrentIndex(
static_cast< int >( StaticValue ) );
361 mSourceButton->setToolTip( tr(
"Value" ) );
366 mStackedWidget->setCurrentIndex(
static_cast< int >( Expression ) );
367 mSourceButton->setToolTip( tr(
"Pre-calculated Value" ) );
373 mStackedWidget->setCurrentIndex(
static_cast< int >( ModelParameter ) );
374 mSourceButton->setToolTip( tr(
"Model Input" ) );
381 mStackedWidget->setCurrentIndex(
static_cast< int >( ChildOutput ) );
382 mSourceButton->setToolTip( tr(
"Algorithm Output" ) );
389 mStackedWidget->setCurrentIndex(
static_cast< int >( ModelOutput ) );
390 mSourceButton->setToolTip( tr(
"Model Output" ) );
399void QgsProcessingModelerParameterWidget::updateUi()
401 mStaticWidgetWrapper->setParameterValue( mStaticValue, mContext );
405 int currentIndex = mModelInputCombo->findData( mModelInputParameterName );
406 if ( currentIndex == -1 && mModelInputCombo->count() > 0 )
408 mModelInputCombo->setCurrentIndex( currentIndex );
410 const QStringList parts = QStringList() << mOutputChildId << mOutputName;
411 currentIndex = mChildOutputCombo->findData( parts );
412 if ( currentIndex == -1 && mChildOutputCombo->count() > 0 )
414 mChildOutputCombo->setCurrentIndex( currentIndex );
419 const QList< QgsProcessingModelChildParameterSource > sources = mModel->availableSourcesForChild( mChildId,
420 compatibleParameterTypes, compatibleOutputTypes, compatibleDataTypes );
422 for (
const QgsProcessingModelChildParameterSource &source : sources )
424 switch ( source.source() )
427 mModelInputCombo->addItem( mModel->parameterDefinition( source.parameterName() )->description(), source.parameterName() );
432 if ( !mModel->childAlgorithms().contains( source.outputChildId() ) )
435 const QgsProcessingModelChildAlgorithm &alg = mModel->childAlgorithm( source.outputChildId() );
436 if ( !alg.algorithm() )
438 const QString outputDescription = alg.algorithm()->outputDefinition( source.outputName() )->description();
439 const QString childDescription = alg.description();
441 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