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();
189 if ( mModel->childAlgorithms().contains( mChildId ) )
190 alg = mModel->childAlgorithm( mChildId ).algorithm();
195 QgsExpressionContextScope *childScope = mModel->createExpressionContextScopeForChildAlgorithm( mChildId, mContext, QVariantMap(), QVariantMap() );
198 QStringList highlightedVariables = childScope->
variableNames();
199 QStringList highlightedFunctions = childScope->functionNames();
200 highlightedVariables += algorithmScope->variableNames();
201 highlightedFunctions += algorithmScope->functionNames();
209 void QgsProcessingModelerParameterWidget::sourceMenuAboutToShow()
211 mSourceMenu->clear();
213 const SourceType currentSource = currentSourceType();
215 if ( mHasStaticWrapper )
217 QAction *fixedValueAction = mSourceMenu->addAction( tr(
"Value" ) );
218 fixedValueAction->setCheckable( currentSource == StaticValue );
219 fixedValueAction->setChecked( currentSource == StaticValue );
220 fixedValueAction->setData( QgsProcessingModelChildParameterSource::StaticValue );
223 QAction *calculatedValueAction = mSourceMenu->addAction( tr(
"Pre-calculated Value" ) );
224 calculatedValueAction->setCheckable( currentSource == Expression );
225 calculatedValueAction->setChecked( currentSource == Expression );
226 calculatedValueAction->setData( QgsProcessingModelChildParameterSource::Expression );
228 QAction *inputValueAction = mSourceMenu->addAction( tr(
"Model Input" ) );
229 inputValueAction->setCheckable( currentSource == ModelParameter );
230 inputValueAction->setChecked( currentSource == ModelParameter );
231 inputValueAction->setData( QgsProcessingModelChildParameterSource::ModelParameter );
233 QAction *childOutputValueAction = mSourceMenu->addAction( tr(
"Algorithm Output" ) );
234 childOutputValueAction->setCheckable( currentSource == ChildOutput );
235 childOutputValueAction->setChecked( currentSource == ChildOutput );
236 childOutputValueAction->setData( QgsProcessingModelChildParameterSource::ChildOutput );
241 void QgsProcessingModelerParameterWidget::sourceMenuActionTriggered( QAction *action )
243 QgsProcessingModelChildParameterSource::Source sourceType =
static_cast< QgsProcessingModelChildParameterSource::Source
>( action->data().toInt() );
244 setSourceType( sourceType );
247 QgsProcessingModelerParameterWidget::SourceType QgsProcessingModelerParameterWidget::currentSourceType()
const 249 return static_cast< SourceType
>( mStackedWidget->currentIndex() );
252 void QgsProcessingModelerParameterWidget::setSourceType( QgsProcessingModelChildParameterSource::Source type )
256 case QgsProcessingModelChildParameterSource::StaticValue:
257 mStackedWidget->setCurrentIndex( static_cast< int >( StaticValue ) );
259 mSourceButton->setToolTip( tr(
"Value" ) );
262 case QgsProcessingModelChildParameterSource::Expression:
264 mStackedWidget->setCurrentIndex( static_cast< int >( Expression ) );
265 mSourceButton->setToolTip( tr(
"Pre-calculated Value" ) );
268 case QgsProcessingModelChildParameterSource::ModelParameter:
271 mStackedWidget->setCurrentIndex( static_cast< int >( ModelParameter ) );
272 mSourceButton->setToolTip( tr(
"Model Input" ) );
276 case QgsProcessingModelChildParameterSource::ChildOutput:
279 mStackedWidget->setCurrentIndex( static_cast< int >( ChildOutput ) );
280 mSourceButton->setToolTip( tr(
"Algorithm Output" ) );
284 case QgsProcessingModelChildParameterSource::ExpressionText:
289 void QgsProcessingModelerParameterWidget::updateUi()
291 mStaticWidgetWrapper->setParameterValue( mStaticValue, mContext );
295 int currentIndex = mModelInputCombo->findData( mModelInputParameterName );
296 if ( currentIndex == -1 && mModelInputCombo->count() > 0 )
298 mModelInputCombo->setCurrentIndex( currentIndex );
300 const QStringList parts = QStringList() << mOutputChildId << mOutputName;
301 currentIndex = mChildOutputCombo->findData( parts );
302 if ( currentIndex == -1 && mChildOutputCombo->count() > 0 )
304 mChildOutputCombo->setCurrentIndex( currentIndex );
309 const QList< QgsProcessingModelChildParameterSource > sources = mModel->availableSourcesForChild( mChildId,
310 compatibleParameterTypes, compatibleOutputTypes, compatibleDataTypes );
312 for (
const QgsProcessingModelChildParameterSource &source : sources )
314 switch ( source.source() )
316 case QgsProcessingModelChildParameterSource::ModelParameter:
317 mModelInputCombo->addItem( mModel->parameterDefinition( source.parameterName() )->description(), source.parameterName() );
320 case QgsProcessingModelChildParameterSource::ChildOutput:
322 if ( !mModel->childAlgorithms().contains( source.outputChildId() ) )
325 const QgsProcessingModelChildAlgorithm &alg = mModel->childAlgorithm( source.outputChildId() );
326 if ( !alg.algorithm() )
328 const QString outputDescription = alg.algorithm()->outputDefinition( source.outputName() )->description();
329 const QString childDescription = alg.description();
331 mChildOutputCombo->addItem( tr(
"“%1” from algorithm “%2”" ).arg( outputDescription, childDescription ), QStringList() << source.outputChildId() << source.outputName() );
335 case QgsProcessingModelChildParameterSource::StaticValue:
336 case QgsProcessingModelChildParameterSource::Expression:
337 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.
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.