23 #include "models/qgsprocessingmodelalgorithm.h" 29 #include <QHBoxLayout> 30 #include <QToolButton> 31 #include <QStackedWidget> 37 const QString &childId,
43 , mParameterDefinition( parameter )
46 setFocusPolicy( Qt::StrongFocus );
51 QHBoxLayout *hLayout =
new QHBoxLayout();
53 mSourceButton =
new QToolButton();
54 mSourceButton->setFocusPolicy( Qt::StrongFocus );
57 mSourceButton->setFixedSize( 2 * static_cast< int >( 1.25 * iconSize / 2.0 ), 2 * static_cast< int >( iconSize * 1.1 / 2.0 ) );
58 mSourceButton->setIconSize( QSize( iconSize, iconSize ) );
59 mSourceButton->setPopupMode( QToolButton::InstantPopup );
61 mSourceMenu =
new QMenu(
this );
62 connect( mSourceMenu, &QMenu::aboutToShow,
this, &QgsProcessingModelerParameterWidget::sourceMenuAboutToShow );
63 connect( mSourceMenu, &QMenu::triggered,
this, &QgsProcessingModelerParameterWidget::sourceMenuActionTriggered );
64 mSourceButton->setMenu( mSourceMenu );
66 hLayout->addWidget( mSourceButton );
68 mStackedWidget =
new QStackedWidget();
71 if ( mStaticWidgetWrapper )
73 QWidget *widget = mStaticWidgetWrapper->createWrappedWidget( context );
76 mHasStaticWrapper =
true;
77 mStackedWidget->addWidget( widget );
80 mStackedWidget->addWidget(
new QWidget() );
84 mStackedWidget->addWidget(
new QWidget() );
89 mStackedWidget->addWidget( mExpressionWidget );
91 mModelInputCombo =
new QComboBox();
92 QHBoxLayout *hLayout2 =
new QHBoxLayout();
93 hLayout2->setMargin( 0 );
94 hLayout2->setContentsMargins( 0, 0, 0, 0 );
95 hLayout2->addWidget(
new QLabel( tr(
"Using model input" ) ) );
96 hLayout2->addWidget( mModelInputCombo, 1 );
97 QWidget *hWidget2 =
new QWidget();
98 hWidget2->setLayout( hLayout2 );
99 mStackedWidget->addWidget( hWidget2 );
101 mChildOutputCombo =
new QComboBox();
102 QHBoxLayout *hLayout3 =
new QHBoxLayout();
103 hLayout3->setMargin( 0 );
104 hLayout3->setContentsMargins( 0, 0, 0, 0 );
105 hLayout3->addWidget(
new QLabel( tr(
"Using algorithm output" ) ) );
106 hLayout3->addWidget( mChildOutputCombo, 1 );
107 QWidget *hWidget3 =
new QWidget();
108 hWidget3->setLayout( hLayout3 );
109 mStackedWidget->addWidget( hWidget3 );
111 hLayout->setMargin( 0 );
112 hLayout->setContentsMargins( 0, 0, 0, 0 );
113 hLayout->addWidget( mStackedWidget, 1 );
115 setLayout( hLayout );
116 setSourceType( QgsProcessingModelChildParameterSource::StaticValue );
123 if ( mStaticWidgetWrapper )
124 mStaticWidgetWrapper->setWidgetContext( context );
129 if ( mStaticWidgetWrapper )
130 mStaticWidgetWrapper->registerProcessingContextGenerator( generator );
135 return mParameterDefinition;
140 if ( mStaticWidgetWrapper )
141 return mStaticWidgetWrapper->createWrappedLabel();
150 mStaticValue = value.staticValue();
151 mModelInputParameterName = value.parameterName();
152 mOutputChildId = value.outputChildId();
153 mOutputName = value.outputName();
154 mExpression = value.expression();
157 setSourceType( value.source() );
162 switch ( currentSourceType() )
165 return QgsProcessingModelChildParameterSource::fromStaticValue( mStaticWidgetWrapper->parameterValue() );
168 return QgsProcessingModelChildParameterSource::fromExpression( mExpressionWidget->
expression() );
171 return QgsProcessingModelChildParameterSource::fromModelParameter( mModelInputCombo->currentData().toString() );
175 const QStringList parts = mChildOutputCombo->currentData().toStringList();
176 return QgsProcessingModelChildParameterSource::fromChildOutput( parts.value( 0, QString() ), parts.value( 1, QString() ) );
180 return QgsProcessingModelChildParameterSource();
185 if ( mStaticWidgetWrapper )
186 mStaticWidgetWrapper->setDialog( dialog );
195 if ( mModel->childAlgorithms().contains( mChildId ) )
196 alg = mModel->childAlgorithm( mChildId ).algorithm();
201 QgsExpressionContextScope *childScope = mModel->createExpressionContextScopeForChildAlgorithm( mChildId, mContext, QVariantMap(), QVariantMap() );
204 QStringList highlightedVariables = childScope->
variableNames();
205 QStringList highlightedFunctions = childScope->functionNames();
206 highlightedVariables += algorithmScope->variableNames();
207 highlightedVariables += mModel->variables().keys();
208 highlightedFunctions += algorithmScope->functionNames();
216 void QgsProcessingModelerParameterWidget::sourceMenuAboutToShow()
218 mSourceMenu->clear();
220 const SourceType currentSource = currentSourceType();
222 if ( mHasStaticWrapper )
224 QAction *fixedValueAction = mSourceMenu->addAction( tr(
"Value" ) );
225 fixedValueAction->setCheckable( currentSource == StaticValue );
226 fixedValueAction->setChecked( currentSource == StaticValue );
227 fixedValueAction->setData( QgsProcessingModelChildParameterSource::StaticValue );
230 QAction *calculatedValueAction = mSourceMenu->addAction( tr(
"Pre-calculated Value" ) );
231 calculatedValueAction->setCheckable( currentSource == Expression );
232 calculatedValueAction->setChecked( currentSource == Expression );
233 calculatedValueAction->setData( QgsProcessingModelChildParameterSource::Expression );
235 QAction *inputValueAction = mSourceMenu->addAction( tr(
"Model Input" ) );
236 inputValueAction->setCheckable( currentSource == ModelParameter );
237 inputValueAction->setChecked( currentSource == ModelParameter );
238 inputValueAction->setData( QgsProcessingModelChildParameterSource::ModelParameter );
240 QAction *childOutputValueAction = mSourceMenu->addAction( tr(
"Algorithm Output" ) );
241 childOutputValueAction->setCheckable( currentSource == ChildOutput );
242 childOutputValueAction->setChecked( currentSource == ChildOutput );
243 childOutputValueAction->setData( QgsProcessingModelChildParameterSource::ChildOutput );
248 void QgsProcessingModelerParameterWidget::sourceMenuActionTriggered( QAction *action )
250 QgsProcessingModelChildParameterSource::Source sourceType =
static_cast< QgsProcessingModelChildParameterSource::Source
>( action->data().toInt() );
251 setSourceType( sourceType );
254 QgsProcessingModelerParameterWidget::SourceType QgsProcessingModelerParameterWidget::currentSourceType()
const 256 return static_cast< SourceType
>( mStackedWidget->currentIndex() );
259 void QgsProcessingModelerParameterWidget::setSourceType( QgsProcessingModelChildParameterSource::Source type )
263 case QgsProcessingModelChildParameterSource::StaticValue:
264 mStackedWidget->setCurrentIndex( static_cast< int >( StaticValue ) );
266 mSourceButton->setToolTip( tr(
"Value" ) );
269 case QgsProcessingModelChildParameterSource::Expression:
271 mStackedWidget->setCurrentIndex( static_cast< int >( Expression ) );
272 mSourceButton->setToolTip( tr(
"Pre-calculated Value" ) );
275 case QgsProcessingModelChildParameterSource::ModelParameter:
278 mStackedWidget->setCurrentIndex( static_cast< int >( ModelParameter ) );
279 mSourceButton->setToolTip( tr(
"Model Input" ) );
283 case QgsProcessingModelChildParameterSource::ChildOutput:
286 mStackedWidget->setCurrentIndex( static_cast< int >( ChildOutput ) );
287 mSourceButton->setToolTip( tr(
"Algorithm Output" ) );
291 case QgsProcessingModelChildParameterSource::ExpressionText:
296 void QgsProcessingModelerParameterWidget::updateUi()
298 mStaticWidgetWrapper->setParameterValue( mStaticValue, mContext );
302 int currentIndex = mModelInputCombo->findData( mModelInputParameterName );
303 if ( currentIndex == -1 && mModelInputCombo->count() > 0 )
305 mModelInputCombo->setCurrentIndex( currentIndex );
307 const QStringList parts = QStringList() << mOutputChildId << mOutputName;
308 currentIndex = mChildOutputCombo->findData( parts );
309 if ( currentIndex == -1 && mChildOutputCombo->count() > 0 )
311 mChildOutputCombo->setCurrentIndex( currentIndex );
316 const QList< QgsProcessingModelChildParameterSource > sources = mModel->availableSourcesForChild( mChildId,
317 compatibleParameterTypes, compatibleOutputTypes, compatibleDataTypes );
319 for (
const QgsProcessingModelChildParameterSource &source : sources )
321 switch ( source.source() )
323 case QgsProcessingModelChildParameterSource::ModelParameter:
324 mModelInputCombo->addItem( mModel->parameterDefinition( source.parameterName() )->description(), source.parameterName() );
327 case QgsProcessingModelChildParameterSource::ChildOutput:
329 if ( !mModel->childAlgorithms().contains( source.outputChildId() ) )
332 const QgsProcessingModelChildAlgorithm &alg = mModel->childAlgorithm( source.outputChildId() );
333 if ( !alg.algorithm() )
335 const QString outputDescription = alg.algorithm()->outputDefinition( source.outputName() )->description();
336 const QString childDescription = alg.description();
338 mChildOutputCombo->addItem( tr(
"“%1” from algorithm “%2”" ).arg( outputDescription, childDescription ), QStringList() << source.outputChildId() << source.outputName() );
342 case QgsProcessingModelChildParameterSource::StaticValue:
343 case QgsProcessingModelChildParameterSource::Expression:
344 case QgsProcessingModelChildParameterSource::ExpressionText:
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...
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
QStringList variableNames() const
Returns a list of variable names contained within the scope.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Abstract base class for processing algorithms.
void setHighlightedFunctions(const QStringList &names)
Sets the list of function names intended to be highlighted to the user.
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
void setExpression(const QString &expression)
Sets the current expression to show in the widget.
QString expression() const
Returns the current expression shown in the widget.
An interface for objects which can create Processing contexts.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Single scope for storing variables and functions for use within a QgsExpressionContext.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
QgsExpressionContext & expressionContext()
Returns the expression context.
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...
Contains settings which reflect the context in which a Processing parameter widget is shown...
The QgsExpressionLineEdit widget includes a line edit for entering expressions together with a button...
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
Base class for the definition of processing parameters.
void setExpectedOutputFormat(const QString &expected)
Set the expected format string, which is shown in the expression builder dialog for the widget...
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user...
Contains information about the context in which a processing algorithm is executed.