QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsprocessingwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingwidgetwrapper.cpp
3 ---------------------
4 begin : August 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
26#include <QLabel>
27#include <QHBoxLayout>
28
29//
30// QgsProcessingParameterWidgetContext
31//
32
34{
35 mMapCanvas = canvas;
36}
37
39{
40 return mMapCanvas;
41}
42
44{
45 mMessageBar = bar;
46}
47
49{
50 return mMessageBar;
51}
52
54{
55 mBrowserModel = model;
56}
57
59{
60 return mBrowserModel;
61}
62
64{
65 mProject = project;
66}
67
69{
70 return mProject;
71}
72
74{
75 return mModelChildAlgorithmId;
76}
77
78void QgsProcessingParameterWidgetContext::setModelChildAlgorithmId( const QString &modelChildAlgorithmId )
79{
80 mModelChildAlgorithmId = modelChildAlgorithmId;
81}
82
84{
85 return mActiveLayer;
86}
87
89{
90 mActiveLayer = activeLayer;
91}
92
93QgsProcessingModelAlgorithm *QgsProcessingParameterWidgetContext::model() const
94{
95 return mModel;
96}
97
98void QgsProcessingParameterWidgetContext::setModel( QgsProcessingModelAlgorithm *model )
99{
100 mModel = model;
101}
102
103
104//
105// QgsAbstractProcessingParameterWidgetWrapper
106//
107
109 : QObject( parent )
110 , mType( type )
111 , mParameterDefinition( parameter )
112{
113}
114
116{
117 return mType;
118}
119
121{
122 mWidgetContext = context;
123}
124
126{
127 return mWidgetContext;
128}
129
131{
132 if ( mWidget )
133 return mWidget;
134
135 mWidget = createWidget();
136 QWidget *wrappedWidget = mWidget;
137 if ( mParameterDefinition->isDynamic() )
138 {
139 QHBoxLayout *hLayout = new QHBoxLayout();
140 hLayout->setContentsMargins( 0, 0, 0, 0 );
141 hLayout->addWidget( mWidget, 1 );
142 mPropertyButton = new QgsPropertyOverrideButton();
143 hLayout->addWidget( mPropertyButton );
144 mPropertyButton->init( 0, QgsProperty(), mParameterDefinition->dynamicPropertyDefinition() );
145 mPropertyButton->registerEnabledWidget( mWidget, false );
146 mPropertyButton->registerExpressionContextGenerator( this );
147
148 wrappedWidget = new QWidget();
149 wrappedWidget->setLayout( hLayout );
150 }
151
152 if ( !dynamic_cast<const QgsProcessingDestinationParameter * >( mParameterDefinition ) )
153 {
154 // an exception -- output widgets handle this themselves
155 setWidgetValue( mParameterDefinition->defaultValueForGui(), context );
156 }
157
158 return wrappedWidget;
159}
160
162{
163 if ( mLabel )
164 return mLabel;
165
166 mLabel = createLabel();
167 return mLabel;
168}
169
171{
172 return mWidget;
173}
174
176{
177 return mLabel;
178}
179
181{
182 return mParameterDefinition;
183}
184
186{
187 if ( mPropertyButton && value.userType() == QMetaType::type( "QgsProperty" ) )
188 {
189 mPropertyButton->setToProperty( value.value< QgsProperty >() );
190 }
191 else
192 {
193 if ( mPropertyButton )
194 mPropertyButton->setToProperty( QgsProperty() );
195
196 setWidgetValue( value, context );
197 }
198}
199
201{
202 if ( mPropertyButton && mPropertyButton->isActive() )
203 return mPropertyButton->toProperty();
204 else
205 return widgetValue();
206}
207
209{
210 return QVariantMap();
211}
212
214{
215 mProcessingContextGenerator = generator;
216}
217
219{
220 mParametersGenerator = generator;
221}
222
224{
225 switch ( mType )
226 {
228 return nullptr;
229
232 {
233 QString description = mParameterDefinition->description();
235 description = QObject::tr( "%1 [optional]" ).arg( description );
236 std::unique_ptr< QLabel > label = std::make_unique< QLabel >( description );
237 label->setToolTip( mParameterDefinition->toolTip() );
238 return label.release();
239 }
240 }
241 return nullptr;
242}
243
245{
246 if ( mPropertyButton )
247 return mPropertyButton->vectorLayer();
248 return nullptr;
249}
250
251void QgsAbstractProcessingParameterWidgetWrapper::postInitialize( const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
252{
253 switch ( mType )
254 {
257 {
258 if ( parameterDefinition()->isDynamic() )
259 {
260 for ( const QgsAbstractProcessingParameterWidgetWrapper *wrapper : wrappers )
261 {
262 if ( wrapper->parameterDefinition()->name() == parameterDefinition()->dynamicLayerParameterName() )
263 {
264 setDynamicParentLayerParameter( wrapper );
265 connect( wrapper, &QgsAbstractProcessingParameterWidgetWrapper::widgetValueHasChanged, this, &QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged );
266 break;
267 }
268 }
269 }
270 break;
271 }
272
274 break;
275 }
276}
277
279{
280 return 0;
281}
282
284{
285 QgsExpressionContext context = QgsProcessingGuiUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
286 if ( mParameterDefinition && !mParameterDefinition->additionalExpressionContextVariables().isEmpty() )
287 {
288 std::unique_ptr< QgsExpressionContextScope > paramScope = std::make_unique< QgsExpressionContextScope >();
289 const QStringList additional = mParameterDefinition->additionalExpressionContextVariables();
290 for ( const QString &var : additional )
291 {
292 paramScope->setVariable( var, QVariant() );
293 }
294 context.appendScope( paramScope.release() );
295
296 // we always highlight additional variables for visibility
297 QStringList highlighted = context.highlightedVariables();
298 highlighted.append( additional );
299 context.setHighlightedVariables( highlighted );
300 }
301 return context;
302}
303
305{
306
307}
308
309void QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged( QgsAbstractProcessingParameterWidgetWrapper *wrapper )
310{
311 if ( wrapper )
312 {
313 setDynamicParentLayerParameter( wrapper );
314 }
315}
316
317void QgsAbstractProcessingParameterWidgetWrapper::setDynamicParentLayerParameter( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper )
318{
319 if ( mPropertyButton )
320 {
321 // evaluate value to layer
322 QgsProcessingContext *context = nullptr;
323 std::unique_ptr< QgsProcessingContext > tmpContext;
326
327 if ( !context )
328 {
329 tmpContext = std::make_unique< QgsProcessingContext >();
330 context = tmpContext.get();
331 }
332
333 QVariant val = parentWrapper->parameterValue();
334 if ( val.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
335 {
336 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
337 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
338 val = fromVar.source;
339 }
340
342 if ( !layer )
343 {
344 mPropertyButton->setVectorLayer( nullptr );
345 return;
346 }
347
348 // need to grab ownership of layer if required - otherwise layer may be deleted when context
349 // goes out of scope
350 std::unique_ptr< QgsMapLayer > ownedLayer( context->takeResultLayer( layer->id() ) );
351 if ( ownedLayer && ownedLayer->type() == Qgis::LayerType::Vector )
352 {
353 mDynamicLayer.reset( qobject_cast< QgsVectorLayer * >( ownedLayer.release() ) );
354 layer = mDynamicLayer.get();
355 }
356 else
357 {
358 // don't need ownership of this layer - it wasn't owned by context (so e.g. is owned by the project)
359 }
360
361 mPropertyButton->setVectorLayer( layer );
362 }
363}
364
366{
367 std::unique_ptr< QgsProcessingModelerParameterWidget > widget = std::make_unique< QgsProcessingModelerParameterWidget >( model, childId, parameter, context );
368 widget->populateSources( compatibleParameterTypes(), compatibleOutputTypes(), compatibleDataTypes( parameter ) );
369 widget->setExpressionHelpText( modelerExpressionFormatString() );
370
371 if ( parameter->isDestination() )
372 widget->setSourceType( QgsProcessingModelChildParameterSource::ModelOutput );
373 else
374 widget->setSourceType( defaultModelSource( parameter ) );
375
376 return widget.release();
377}
378
381 const QgsProcessingAlgorithm * )
382{
383 return nullptr;
384}
385
387{
388 return QList< int >();
389}
390
392{
393 return QString();
394}
395
397{
398 return QgsProcessingModelChildParameterSource::StaticValue;
399}
400
401//
402// QgsProcessingGuiUtils
403//
404
406QgsExpressionContext QgsProcessingGuiUtils::createExpressionContext( QgsProcessingContextGenerator *processingContextGenerator, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingAlgorithm *algorithm, const QgsVectorLayer *linkedLayer )
407{
408 // Get a processing context to start with
409 QgsProcessingContext *context = nullptr;
410 std::unique_ptr< QgsProcessingContext > tmpContext;
411 if ( processingContextGenerator )
412 context = processingContextGenerator->processingContext();
413
414 if ( !context )
415 {
416 tmpContext = std::make_unique< QgsProcessingContext >();
417 context = tmpContext.get();
418 }
419
421
422 if ( auto *lModel = widgetContext.model() )
423 {
424 c << QgsExpressionContextUtils::processingModelAlgorithmScope( lModel, QVariantMap(), *context );
425
426 const QgsProcessingAlgorithm *alg = nullptr;
427 if ( lModel->childAlgorithms().contains( widgetContext.modelChildAlgorithmId() ) )
428 alg = lModel->childAlgorithm( widgetContext.modelChildAlgorithmId() ).algorithm();
429
430 QgsExpressionContextScope *algorithmScope = QgsExpressionContextUtils::processingAlgorithmScope( alg ? alg : algorithm, QVariantMap(), *context );
431 c << algorithmScope;
432 QgsExpressionContextScope *childScope = lModel->createExpressionContextScopeForChildAlgorithm( widgetContext.modelChildAlgorithmId(), *context, QVariantMap(), QVariantMap() );
433 c << childScope;
434
435 QStringList highlightedVariables = childScope->variableNames();
436 QStringList highlightedFunctions = childScope->functionNames();
437 highlightedVariables += algorithmScope->variableNames();
438 highlightedVariables += lModel->variables().keys();
439 highlightedFunctions += algorithmScope->functionNames();
440 c.setHighlightedVariables( highlightedVariables );
441 c.setHighlightedFunctions( highlightedFunctions );
442 }
443 else
444 {
445 if ( algorithm )
447 }
448
449 if ( linkedLayer )
451
452 return c;
453}
455
457 : QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
458{
459
460}
461
463{
464 if ( mValue == value )
465 return;
466
467 mValue = value;
468 emit widgetValueHasChanged( this );
469}
470
472{
473 return mValue;
474}
475
477{
478 return mLayer;
479}
480
482{
483 mLayer = layer;
484}
485
487{
488 return nullptr;
489
490}
491
493{
494 return nullptr;
495}
A widget wrapper for Processing parameter value widgets.
QVariant parameterValue() const
Returns the current value of the parameter.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual QVariantMap customProperties() const
Returns any custom properties set by the wrapper.
QgsAbstractProcessingParameterWidgetWrapper(const QgsProcessingParameterDefinition *parameter=nullptr, QgsProcessingGui::WidgetType type=QgsProcessingGui::Standard, QObject *parent=nullptr)
Constructor for QgsAbstractProcessingParameterWidgetWrapper, for the specified parameter definition a...
virtual void setWidgetValue(const QVariant &value, QgsProcessingContext &context)=0
Sets the current value for the parameter to show in the widget.
virtual QLabel * createLabel()
Creates a new label to accompany widgets created by the wrapper.
void registerProcessingParametersGenerator(QgsProcessingParametersGenerator *generator)
Registers a Processing parameters generator class that will be used to retrieve algorithm parameters ...
QgsProcessingParameterWidgetContext mWidgetContext
QgsProcessingGui::WidgetType type() const
Returns the dialog type for which widgets and labels will be created by this wrapper.
QgsProcessingParametersGenerator * mParametersGenerator
virtual void registerProcessingContextGenerator(QgsProcessingContextGenerator *generator)
Registers a Processing context generator class that will be used to retrieve a Processing context for...
const QgsProcessingParameterWidgetContext & widgetContext() const
Returns the context in which the Processing parameter widget is shown, e.g., the parent model algorit...
QLabel * createWrappedLabel()
Creates and returns a new label to accompany widgets created by the wrapper.
QWidget * createWrappedWidget(QgsProcessingContext &context)
Creates and return a new wrapped widget which allows customization of the parameter's value.
QgsProcessingContextGenerator * mProcessingContextGenerator
void widgetValueHasChanged(QgsAbstractProcessingParameterWidgetWrapper *wrapper)
Emitted whenever the parameter value (as defined by the wrapped widget) is changed.
virtual void postInitialize(const QList< QgsAbstractProcessingParameterWidgetWrapper * > &wrappers)
Called after all wrappers have been created within a particular dialog or context,...
virtual const QgsVectorLayer * linkedVectorLayer() const
Returns the optional vector layer associated with this widget wrapper, or nullptr if no vector layer ...
QWidget * wrappedWidget()
Returns the current wrapped widget, if any.
const QgsProcessingParameterDefinition * parameterDefinition() const
Returns the parameter definition associated with this wrapper.
QLabel * wrappedLabel()
Returns the current wrapped label, if any.
virtual QVariant widgetValue() const =0
Returns the current value of the parameter.
virtual QWidget * createWidget()=0
Creates a new widget which allows customization of the parameter's value.
virtual int stretch() const
Returns the Qt layout "stretch" factor to use when adding this widget to a layout.
virtual void setDialog(QDialog *dialog)
Sets the parent dialog in which the wrapper is shown.
virtual void setWidgetContext(const QgsProcessingParameterWidgetContext &context)
Sets the context in which the Processing parameter widget is shown, e.g., the parent model algorithm,...
void setParameterValue(const QVariant &value, QgsProcessingContext &context)
Sets the current value for the parameter.
A model for showing available data sources and other items in a structured tree.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QStringList variableNames() const
Returns a list of variable names contained within the scope.
static QgsExpressionContextScope * processingModelAlgorithmScope(const QgsProcessingModelAlgorithm *model, const QVariantMap &parameters, 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 &parameters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing algorithm,...
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QStringList highlightedVariables() const
Returns the current list of variables highlighted within the context.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
Base class for all map layer types.
Definition: qgsmaplayer.h:73
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
Abstract base class for widgets which allow users to specify the properties of a Processing parameter...
Abstract base class for processing algorithms.
An interface for objects which can create Processing contexts.
virtual QgsProcessingContext * processingContext()=0
This method needs to be reimplemented in all classes which implement this interface and return a Proc...
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
QgsMapLayer * takeResultLayer(const QString &id)
Takes the result map layer with matching id from the context and transfers ownership of it back to th...
Base class for all parameter definitions which represent file or layer destinations,...
Encapsulates settings relating to a feature source input to a processing algorithm.
WidgetType
Types of dialogs which Processing widgets can be created for.
@ Modeler
Modeler dialog.
@ Standard
Standard algorithm dialog.
@ Batch
Batch processing dialog.
QVariant widgetValue() const override
Returns the current value of the parameter.
QgsProcessingHiddenWidgetWrapper(const QgsProcessingParameterDefinition *parameter=nullptr, QgsProcessingGui::WidgetType type=QgsProcessingGui::Standard, QObject *parent=nullptr)
Constructor for QgsProcessingHiddenWidgetWrapper, for the specified parameter definition and dialog t...
QWidget * createWidget() override
Creates a new widget which allows customization of the parameter's value.
void setWidgetValue(const QVariant &value, QgsProcessingContext &context) override
Sets the current value for the parameter to show in the widget.
void setLinkedVectorLayer(const QgsVectorLayer *layer)
Sets the vector layer linked to the wrapper.
QLabel * createLabel() override
Creates a new label to accompany widgets created by the wrapper.
const QgsVectorLayer * linkedVectorLayer() const override
Returns the optional vector layer associated with this widget wrapper, or nullptr if no vector layer ...
A widget for customising the value of Processing algorithm parameter inside a Processing model.
Base class for the definition of processing parameters.
QStringList additionalExpressionContextVariables() const
Returns a list of additional expression context variables which are available for use when evaluating...
virtual QString toolTip() const
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
QgsProcessingAlgorithm * algorithm() const
Returns a pointer to the algorithm which owns this parameter.
QString description() const
Returns the description for the parameter.
QVariant defaultValueForGui() const
Returns the default value to use for the parameter in a GUI.
virtual bool isDestination() const
Returns true if this parameter represents a file or layer destination, e.g.
bool isDynamic() const
Returns true if the parameter supports is dynamic, and can support data-defined values (i....
QgsPropertyDefinition dynamicPropertyDefinition() const
Returns the property definition for dynamic properties.
Contains settings which reflect the context in which a Processing parameter widget is shown,...
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
QgsProject * project() const
Returns the project associated with the widget.
QgsBrowserGuiModel * browserModel() const
Returns the browser model associated with the widget.
void setActiveLayer(QgsMapLayer *layer)
Sets the current active layer.
void setModelChildAlgorithmId(const QString &id)
Sets the child algorithm id within the model which the parameter widget is associated with.
void setProject(QgsProject *project)
Sets the project associated with the widget.
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setBrowserModel(QgsBrowserGuiModel *model)
Sets the browser model associated with the widget.
QgsProcessingModelAlgorithm * model() const
Returns the model which the parameter widget is associated with.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
QgsMapLayer * activeLayer() const
Returns the current active layer.
void setModel(QgsProcessingModelAlgorithm *model)
Sets the model which the parameter widget is associated with.
QString modelChildAlgorithmId() const
Returns the child algorithm ID within the model which the parameter widget is associated with.
virtual QList< int > compatibleDataTypes(const QgsProcessingParameterDefinition *parameter) const
Returns a list of compatible Processing data types for inputs for this widget for the specified param...
virtual QStringList compatibleOutputTypes() const =0
Returns a list of compatible Processing output types for inputs for this parameter.
virtual QgsProcessingModelChildParameterSource::Source defaultModelSource(const QgsProcessingParameterDefinition *parameter) const
Returns the default source type to use for the widget for the specified parameter.
virtual QgsProcessingAbstractParameterDefinitionWidget * createParameterDefinitionWidget(QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition=nullptr, const QgsProcessingAlgorithm *algorithm=nullptr)
Creates a new parameter definition widget allowing for configuration of an instance of the parameter ...
virtual QgsProcessingModelerParameterWidget * createModelerWidgetWrapper(QgsProcessingModelAlgorithm *model, const QString &childId, const QgsProcessingParameterDefinition *parameter, QgsProcessingContext &context)
Creates a new modeler parameter widget for the given parameter.
virtual QStringList compatibleParameterTypes() const =0
Returns a list of compatible Processing parameter types for inputs for this parameter.
virtual QString modelerExpressionFormatString() const
Returns the expected expression format string for expression results for the parameter within model c...
An interface for objects which can create sets of parameter values for processing algorithms.
static QgsVectorLayer * parameterAsVectorLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a vector layer.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:105
A button for controlling property overrides which may apply to a widget.
A store for object properties.
Definition: qgsproperty.h:230
Represents a vector layer which manages a vector based data sets.
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 allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
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