23 #include "models/qgsprocessingmodelalgorithm.h" 27 #include <QHBoxLayout> 28 #include <QToolButton> 29 #include <QStackedWidget> 35 const QString &childId,
41 , mParameterDefinition( parameter )
44 setFocusPolicy( Qt::StrongFocus );
49 QHBoxLayout *hLayout =
new QHBoxLayout();
51 mSourceButton =
new QToolButton();
52 mSourceButton->setFocusPolicy( Qt::StrongFocus );
55 mSourceButton->setFixedSize( 2 * static_cast< int >( 1.25 * iconSize / 2.0 ), 2 * static_cast< int >( iconSize * 1.1 / 2.0 ) );
56 mSourceButton->setIconSize( QSize( iconSize, iconSize ) );
57 mSourceButton->setPopupMode( QToolButton::InstantPopup );
59 mSourceMenu =
new QMenu(
this );
60 connect( mSourceMenu, &QMenu::aboutToShow,
this, &QgsProcessingModelerParameterWidget::sourceMenuAboutToShow );
61 connect( mSourceMenu, &QMenu::triggered,
this, &QgsProcessingModelerParameterWidget::sourceMenuActionTriggered );
62 mSourceButton->setMenu( mSourceMenu );
64 hLayout->addWidget( mSourceButton );
66 mStackedWidget =
new QStackedWidget();
69 if ( mStaticWidgetWrapper )
71 QWidget *widget = mStaticWidgetWrapper->createWrappedWidget( context );
74 mHasStaticWrapper =
true;
75 mStackedWidget->addWidget( widget );
78 mStackedWidget->addWidget(
new QWidget() );
82 mStackedWidget->addWidget(
new QWidget() );
87 mStackedWidget->addWidget( mExpressionWidget );
89 mModelInputCombo =
new QComboBox();
90 QHBoxLayout *hLayout2 =
new QHBoxLayout();
91 hLayout2->setMargin( 0 );
92 hLayout2->setContentsMargins( 0, 0, 0, 0 );
93 hLayout2->addWidget(
new QLabel( tr(
"Using model input" ) ) );
94 hLayout2->addWidget( mModelInputCombo, 1 );
95 QWidget *hWidget2 =
new QWidget();
96 hWidget2->setLayout( hLayout2 );
97 mStackedWidget->addWidget( hWidget2 );
99 mChildOutputCombo =
new QComboBox();
100 QHBoxLayout *hLayout3 =
new QHBoxLayout();
101 hLayout3->setMargin( 0 );
102 hLayout3->setContentsMargins( 0, 0, 0, 0 );
103 hLayout3->addWidget(
new QLabel( tr(
"Using algorithm output" ) ) );
104 hLayout3->addWidget( mChildOutputCombo, 1 );
105 QWidget *hWidget3 =
new QWidget();
106 hWidget3->setLayout( hLayout3 );
107 mStackedWidget->addWidget( hWidget3 );
109 hLayout->setMargin( 0 );
110 hLayout->setContentsMargins( 0, 0, 0, 0 );
111 hLayout->addWidget( mStackedWidget, 1 );
113 setLayout( hLayout );
114 setSourceType( QgsProcessingModelChildParameterSource::StaticValue );
121 if ( mStaticWidgetWrapper )
122 mStaticWidgetWrapper->setWidgetContext( context );
127 if ( mStaticWidgetWrapper )
128 mStaticWidgetWrapper->registerProcessingContextGenerator( generator );
133 return mParameterDefinition;
138 if ( mStaticWidgetWrapper )
139 return mStaticWidgetWrapper->createWrappedLabel();
148 mStaticValue = value.staticValue();
149 mModelInputParameterName = value.parameterName();
150 mOutputChildId = value.outputChildId();
151 mOutputName = value.outputName();
152 mExpression = value.expression();
155 setSourceType( value.source() );
160 switch ( currentSourceType() )
163 return QgsProcessingModelChildParameterSource::fromStaticValue( mStaticWidgetWrapper->parameterValue() );
166 return QgsProcessingModelChildParameterSource::fromExpression( mExpressionWidget->
expression() );
169 return QgsProcessingModelChildParameterSource::fromModelParameter( mModelInputCombo->currentData().toString() );
173 const QStringList parts = mChildOutputCombo->currentData().toStringList();
174 return QgsProcessingModelChildParameterSource::fromChildOutput( parts.value( 0, QString() ), parts.value( 1, QString() ) );
178 return QgsProcessingModelChildParameterSource();
187 if ( mModel->childAlgorithms().contains( mChildId ) )
188 alg = mModel->childAlgorithm( mChildId ).algorithm();
191 QgsExpressionContextScope *childScope = mModel->createExpressionContextScopeForChildAlgorithm( mChildId, mContext, QVariantMap(), QVariantMap() );
194 QStringList highlightedVariables = childScope->
variableNames();
195 QStringList highlightedFunctions = childScope->functionNames();
196 highlightedVariables += algorithmScope->variableNames();
197 highlightedFunctions += algorithmScope->functionNames();
205 void QgsProcessingModelerParameterWidget::sourceMenuAboutToShow()
207 mSourceMenu->clear();
209 const SourceType currentSource = currentSourceType();
211 if ( mHasStaticWrapper )
213 QAction *fixedValueAction = mSourceMenu->addAction( tr(
"Value" ) );
214 fixedValueAction->setCheckable( currentSource == StaticValue );
215 fixedValueAction->setChecked( currentSource == StaticValue );
216 fixedValueAction->setData( QgsProcessingModelChildParameterSource::StaticValue );
219 QAction *calculatedValueAction = mSourceMenu->addAction( tr(
"Pre-calculated Value" ) );
220 calculatedValueAction->setCheckable( currentSource == Expression );
221 calculatedValueAction->setChecked( currentSource == Expression );
222 calculatedValueAction->setData( QgsProcessingModelChildParameterSource::Expression );
224 QAction *inputValueAction = mSourceMenu->addAction( tr(
"Model Input" ) );
225 inputValueAction->setCheckable( currentSource == ModelParameter );
226 inputValueAction->setChecked( currentSource == ModelParameter );
227 inputValueAction->setData( QgsProcessingModelChildParameterSource::ModelParameter );
229 QAction *childOutputValueAction = mSourceMenu->addAction( tr(
"Algorithm Output" ) );
230 childOutputValueAction->setCheckable( currentSource == ChildOutput );
231 childOutputValueAction->setChecked( currentSource == ChildOutput );
232 childOutputValueAction->setData( QgsProcessingModelChildParameterSource::ChildOutput );
237 void QgsProcessingModelerParameterWidget::sourceMenuActionTriggered( QAction *action )
239 QgsProcessingModelChildParameterSource::Source sourceType =
static_cast< QgsProcessingModelChildParameterSource::Source
>( action->data().toInt() );
240 setSourceType( sourceType );
243 QgsProcessingModelerParameterWidget::SourceType QgsProcessingModelerParameterWidget::currentSourceType()
const 245 return static_cast< SourceType
>( mStackedWidget->currentIndex() );
248 void QgsProcessingModelerParameterWidget::setSourceType( QgsProcessingModelChildParameterSource::Source type )
252 case QgsProcessingModelChildParameterSource::StaticValue:
253 mStackedWidget->setCurrentIndex( static_cast< int >( StaticValue ) );
255 mSourceButton->setToolTip( tr(
"Value" ) );
258 case QgsProcessingModelChildParameterSource::Expression:
260 mStackedWidget->setCurrentIndex( static_cast< int >( Expression ) );
261 mSourceButton->setToolTip( tr(
"Pre-calculated Value" ) );
264 case QgsProcessingModelChildParameterSource::ModelParameter:
267 mStackedWidget->setCurrentIndex( static_cast< int >( ModelParameter ) );
268 mSourceButton->setToolTip( tr(
"Model Input" ) );
272 case QgsProcessingModelChildParameterSource::ChildOutput:
275 mStackedWidget->setCurrentIndex( static_cast< int >( ChildOutput ) );
276 mSourceButton->setToolTip( tr(
"Algorithm Output" ) );
280 case QgsProcessingModelChildParameterSource::ExpressionText:
285 void QgsProcessingModelerParameterWidget::updateUi()
287 mStaticWidgetWrapper->setParameterValue( mStaticValue, mContext );
291 int currentIndex = mModelInputCombo->findData( mModelInputParameterName );
292 if ( currentIndex == -1 && mModelInputCombo->count() > 0 )
294 mModelInputCombo->setCurrentIndex( currentIndex );
296 const QStringList parts = QStringList() << mOutputChildId << mOutputName;
297 currentIndex = mChildOutputCombo->findData( parts );
298 if ( currentIndex == -1 && mChildOutputCombo->count() > 0 )
300 mChildOutputCombo->setCurrentIndex( currentIndex );
305 const QList< QgsProcessingModelChildParameterSource > sources = mModel->availableSourcesForChild( mChildId,
306 compatibleParameterTypes, compatibleOutputTypes, compatibleDataTypes );
308 for (
const QgsProcessingModelChildParameterSource &source : sources )
310 switch ( source.source() )
312 case QgsProcessingModelChildParameterSource::ModelParameter:
313 mModelInputCombo->addItem( mModel->parameterDefinition( source.parameterName() )->description(), source.parameterName() );
316 case QgsProcessingModelChildParameterSource::ChildOutput:
318 if ( !mModel->childAlgorithms().contains( source.outputChildId() ) )
321 const QgsProcessingModelChildAlgorithm &alg = mModel->childAlgorithm( source.outputChildId() );
322 if ( !alg.algorithm() )
324 const QString outputDescription = alg.algorithm()->outputDefinition( source.outputName() )->description();
325 const QString childDescription = alg.description();
327 mChildOutputCombo->addItem( tr(
"“%1” from algorithm “%2”" ).arg( outputDescription, childDescription ), QStringList() << source.outputChildId() << source.outputName() );
331 case QgsProcessingModelChildParameterSource::StaticValue:
332 case QgsProcessingModelChildParameterSource::Expression:
333 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...
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
QString expression() const
Returns the current expression shown in the widget.
void setExpression(const QString &expression)
Sets the current expression to show 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.
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.
QStringList variableNames() const
Returns a list of variable names contained within the scope.