QGIS API Documentation 3.43.0-Master (56aa1fd18d7)
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
19#include "qgsapplication.h"
28#include "moc_qgsprocessingwidgetwrapper.cpp"
30#include <QLabel>
31#include <QHBoxLayout>
32
33//
34// QgsProcessingParameterWidgetContext
35//
36
38{
39 mMapCanvas = canvas;
40}
41
43{
44 return mMapCanvas;
45}
46
48{
49 mMessageBar = bar;
50}
51
53{
54 return mMessageBar;
55}
56
61
66
71
73{
74 return mProject;
75}
76
78{
79 return mModelChildAlgorithmId;
80}
81
82void QgsProcessingParameterWidgetContext::setModelChildAlgorithmId( const QString &modelChildAlgorithmId )
83{
84 mModelChildAlgorithmId = modelChildAlgorithmId;
85}
86
88{
89 return mActiveLayer;
90}
91
93{
94 mActiveLayer = activeLayer;
95}
96
97QgsProcessingModelAlgorithm *QgsProcessingParameterWidgetContext::model() const
98{
99 return mModel;
100}
101
102void QgsProcessingParameterWidgetContext::setModel( QgsProcessingModelAlgorithm *model )
103{
104 mModel = model;
105}
106
107
108//
109// QgsAbstractProcessingParameterWidgetWrapper
110//
111
113 : QObject( parent )
114 , mType( type )
115 , mParameterDefinition( parameter )
116{
117}
118
123
128
133
135{
136 if ( mWidget )
137 return mWidget;
138
139 mWidget = createWidget();
140 QWidget *wrappedWidget = mWidget;
141 if ( mParameterDefinition->isDynamic() )
142 {
143 QHBoxLayout *hLayout = new QHBoxLayout();
144 hLayout->setContentsMargins( 0, 0, 0, 0 );
145 hLayout->addWidget( mWidget, 1 );
146 mPropertyButton = new QgsPropertyOverrideButton();
147 hLayout->addWidget( mPropertyButton );
148 mPropertyButton->init( 0, QgsProperty(), mParameterDefinition->dynamicPropertyDefinition() );
149 mPropertyButton->registerEnabledWidget( mWidget, false );
150 mPropertyButton->registerExpressionContextGenerator( this );
151
152 wrappedWidget = new QWidget();
153 wrappedWidget->setLayout( hLayout );
154 }
155
156 if ( !dynamic_cast<const QgsProcessingDestinationParameter *>( mParameterDefinition ) )
157 {
158 // an exception -- output widgets handle this themselves
159 setWidgetValue( mParameterDefinition->defaultValueForGui(), context );
160 }
161
162 return wrappedWidget;
163}
164
166{
167 if ( mLabel )
168 return mLabel;
169
170 mLabel = createLabel();
171 return mLabel;
172}
173
175{
176 return mWidget;
177}
178
183
188
190{
191 if ( mPropertyButton && value.userType() == qMetaTypeId<QgsProperty>() )
192 {
193 mPropertyButton->setToProperty( value.value<QgsProperty>() );
194 }
195 else
196 {
197 if ( mPropertyButton )
198 mPropertyButton->setToProperty( QgsProperty() );
199
200 setWidgetValue( value, context );
201 }
202}
203
205{
206 if ( mPropertyButton && mPropertyButton->isActive() )
207 return mPropertyButton->toProperty();
208 else
209 return widgetValue();
210}
211
213{
214 return QVariantMap();
215}
216
221
226
228{
229 switch ( mType )
230 {
232 return nullptr;
233
236 {
237 QString description = mParameterDefinition->description();
239 description = QObject::tr( "%1 [optional]" ).arg( description );
240 auto label = std::make_unique<QLabel>( description );
241 label->setToolTip( mParameterDefinition->toolTip() );
242 label->setWordWrap( true );
243 return label.release();
244 }
245 }
246 return nullptr;
247}
248
250{
251 if ( mPropertyButton )
252 return mPropertyButton->vectorLayer();
253 return nullptr;
254}
255
256void QgsAbstractProcessingParameterWidgetWrapper::postInitialize( const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
257{
258 switch ( mType )
259 {
262 {
263 if ( parameterDefinition()->isDynamic() )
264 {
265 for ( const QgsAbstractProcessingParameterWidgetWrapper *wrapper : wrappers )
266 {
267 if ( wrapper->parameterDefinition()->name() == parameterDefinition()->dynamicLayerParameterName() )
268 {
269 setDynamicParentLayerParameter( wrapper );
270 connect( wrapper, &QgsAbstractProcessingParameterWidgetWrapper::widgetValueHasChanged, this, &QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged );
271 break;
272 }
273 }
274 }
275 break;
276 }
277
279 break;
280 }
281}
282
287
289{
290 QgsExpressionContext context = QgsProcessingGuiUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
291 if ( mParameterDefinition && !mParameterDefinition->additionalExpressionContextVariables().isEmpty() )
292 {
293 auto paramScope = std::make_unique<QgsExpressionContextScope>();
294 const QStringList additional = mParameterDefinition->additionalExpressionContextVariables();
295 for ( const QString &var : additional )
296 {
297 paramScope->setVariable( var, QVariant() );
298 }
299 context.appendScope( paramScope.release() );
300
301 // we always highlight additional variables for visibility
302 QStringList highlighted = context.highlightedVariables();
303 highlighted.append( additional );
304 context.setHighlightedVariables( highlighted );
305 }
306 return context;
307}
308
312
313void QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged( QgsAbstractProcessingParameterWidgetWrapper *wrapper )
314{
315 if ( wrapper )
316 {
317 setDynamicParentLayerParameter( wrapper );
318 }
319}
320
321void QgsAbstractProcessingParameterWidgetWrapper::setDynamicParentLayerParameter( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper )
322{
323 if ( mPropertyButton )
324 {
325 // evaluate value to layer
326 QgsProcessingContext *context = nullptr;
327 std::unique_ptr<QgsProcessingContext> tmpContext;
330
331 if ( !context )
332 {
333 tmpContext = std::make_unique<QgsProcessingContext>();
334 context = tmpContext.get();
335 }
336
337 QVariant val = parentWrapper->parameterValue();
338 if ( val.userType() == qMetaTypeId<QgsProcessingFeatureSourceDefinition>() )
339 {
340 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
341 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
342 val = fromVar.source;
343 }
344
346 if ( !layer )
347 {
348 mPropertyButton->setVectorLayer( nullptr );
349 return;
350 }
351
352 // need to grab ownership of layer if required - otherwise layer may be deleted when context
353 // goes out of scope
354 std::unique_ptr<QgsMapLayer> ownedLayer( context->takeResultLayer( layer->id() ) );
355 if ( ownedLayer && ownedLayer->type() == Qgis::LayerType::Vector )
356 {
357 mDynamicLayer.reset( qobject_cast<QgsVectorLayer *>( ownedLayer.release() ) );
358 layer = mDynamicLayer.get();
359 }
360 else
361 {
362 // don't need ownership of this layer - it wasn't owned by context (so e.g. is owned by the project)
363 }
364
365 mPropertyButton->setVectorLayer( layer );
366 }
367}
368
370{
371 auto widget = std::make_unique<QgsProcessingModelerParameterWidget>( model, childId, parameter, context );
372 widget->populateSources( compatibleParameterTypes(), compatibleOutputTypes(), compatibleDataTypes( parameter ) );
373 widget->setExpressionHelpText( modelerExpressionFormatString() );
374
375 if ( parameter->isDestination() )
377 else
378 widget->setSourceType( defaultModelSource( parameter ) );
379
380 return widget.release();
381}
382
387
389{
391 if ( !paramType )
392 return QStringList();
393 return paramType->acceptedParameterTypes();
394}
395
397{
399 if ( !paramType )
400 return QStringList();
401 return paramType->acceptedOutputTypes();
402}
403
405{
407 if ( !paramType )
408 return QList<int>();
409 return paramType->acceptedDataTypes( parameter );
410}
411
416
421
422//
423// QgsProcessingGuiUtils
424//
425
427QgsExpressionContext QgsProcessingGuiUtils::createExpressionContext( QgsProcessingContextGenerator *processingContextGenerator, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingAlgorithm *algorithm, const QgsVectorLayer *linkedLayer )
428{
429 // Get a processing context to start with
430 QgsProcessingContext *context = nullptr;
431 std::unique_ptr<QgsProcessingContext> tmpContext;
432 if ( processingContextGenerator )
433 context = processingContextGenerator->processingContext();
434
435 if ( !context )
436 {
437 tmpContext = std::make_unique<QgsProcessingContext>();
438 context = tmpContext.get();
439 }
440
442
443 if ( auto *lModel = widgetContext.model() )
444 {
445 c << QgsExpressionContextUtils::processingModelAlgorithmScope( lModel, QVariantMap(), *context );
446
447 const QgsProcessingAlgorithm *alg = nullptr;
448 if ( lModel->childAlgorithms().contains( widgetContext.modelChildAlgorithmId() ) )
449 alg = lModel->childAlgorithm( widgetContext.modelChildAlgorithmId() ).algorithm();
450
451 QgsExpressionContextScope *algorithmScope = QgsExpressionContextUtils::processingAlgorithmScope( alg ? alg : algorithm, QVariantMap(), *context );
452 c << algorithmScope;
453 QgsExpressionContextScope *childScope = lModel->createExpressionContextScopeForChildAlgorithm( widgetContext.modelChildAlgorithmId(), *context, QVariantMap(), QVariantMap() );
454 c << childScope;
455
456 QStringList highlightedVariables = childScope->variableNames();
457 QStringList highlightedFunctions = childScope->functionNames();
458 highlightedVariables += algorithmScope->variableNames();
459 highlightedVariables += lModel->variables().keys();
460 highlightedFunctions += algorithmScope->functionNames();
461 c.setHighlightedVariables( highlightedVariables );
462 c.setHighlightedFunctions( highlightedFunctions );
463 }
464 else
465 {
466 if ( algorithm )
468 }
469
470 if ( linkedLayer )
472
473 return c;
474}
476
481
483{
484 if ( mValue == value )
485 return;
486
487 mValue = value;
488 emit widgetValueHasChanged( this );
489}
490
492{
493 return mValue;
494}
495
497{
498 return mLayer;
499}
500
502{
503 mLayer = layer;
504}
505
507{
508 return nullptr;
509}
510
512{
513 return nullptr;
514}
ProcessingMode
Types of modes which Processing widgets can be created for.
Definition qgis.h:3537
@ Batch
Batch processing mode.
@ Modeler
Modeler mode.
@ Standard
Standard (single-run) algorithm mode.
@ Vector
Vector layer.
ProcessingModelChildParameterSource
Processing model child parameter sources.
Definition qgis.h:3713
@ ModelOutput
Parameter value is linked to an output parameter for the model.
@ StaticValue
Parameter value is a static value.
@ Optional
Parameter is optional.
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...
Qgis::ProcessingMode type() const
Returns the dialog type for which widgets and labels will be created by this wrapper.
virtual QVariantMap customProperties() const
Returns any custom properties set by the wrapper.
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
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.
QgsAbstractProcessingParameterWidgetWrapper(const QgsProcessingParameterDefinition *parameter=nullptr, Qgis::ProcessingMode type=Qgis::ProcessingMode::Standard, QObject *parent=nullptr)
Constructor for QgsAbstractProcessingParameterWidgetWrapper, for the specified parameter definition a...
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.
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
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.
Base class for all map layer types.
Definition qgsmaplayer.h:77
QString id
Definition qgsmaplayer.h:80
A bar for displaying non-blocking messages to the user.
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.
QgsProcessingHiddenWidgetWrapper(const QgsProcessingParameterDefinition *parameter=nullptr, Qgis::ProcessingMode type=Qgis::ProcessingMode::Standard, QObject *parent=nullptr)
Constructor for QgsProcessingHiddenWidgetWrapper, for the specified parameter definition and dialog t...
QVariant widgetValue() const override
Returns the current value of the parameter.
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 parameters 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.
QString dynamicLayerParameterName() const
Returns the name of the parameter for a layer linked to a dynamic parameter, or an empty string if th...
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.
Makes metadata of processing parameters available.
virtual QStringList acceptedOutputTypes() const =0
Returns a list of compatible Processing output types for inputs for this parameter type.
virtual QList< int > acceptedDataTypes(const QgsProcessingParameterDefinition *parameter) const
Returns a list of compatible Processing data types for inputs for this parameter type for the specifi...
virtual QStringList acceptedParameterTypes() const =0
Returns a list of compatible Processing parameter types for inputs for this parameter type.
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 QStringList compatibleParameterTypes() const
Returns a list of compatible Processing parameter types for inputs for this parameter.
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 QString parameterType() const =0
Returns the type string for the parameter type the factory is associated with.
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 compatibleOutputTypes() const
Returns a list of compatible Processing output types for inputs for this parameter.
virtual Qgis::ProcessingModelChildParameterSource defaultModelSource(const QgsProcessingParameterDefinition *parameter) const
Returns the default source type to use for the widget for the specified 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.
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
A button for controlling property overrides which may apply to a widget.
A store for object properties.
Represents a vector layer which manages a vector based dataset.
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