QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
20#include "moc_qgsprocessingwidgetwrapper.cpp"
27#include <QLabel>
28#include <QHBoxLayout>
29
30//
31// QgsProcessingParameterWidgetContext
32//
33
35{
36 mMapCanvas = canvas;
37}
38
40{
41 return mMapCanvas;
42}
43
45{
46 mMessageBar = bar;
47}
48
50{
51 return mMessageBar;
52}
53
58
63
68
70{
71 return mProject;
72}
73
75{
76 return mModelChildAlgorithmId;
77}
78
79void QgsProcessingParameterWidgetContext::setModelChildAlgorithmId( const QString &modelChildAlgorithmId )
80{
81 mModelChildAlgorithmId = modelChildAlgorithmId;
82}
83
85{
86 return mActiveLayer;
87}
88
90{
91 mActiveLayer = activeLayer;
92}
93
94QgsProcessingModelAlgorithm *QgsProcessingParameterWidgetContext::model() const
95{
96 return mModel;
97}
98
99void QgsProcessingParameterWidgetContext::setModel( QgsProcessingModelAlgorithm *model )
100{
101 mModel = model;
102}
103
104
105//
106// QgsAbstractProcessingParameterWidgetWrapper
107//
108
110 : QObject( parent )
111 , mType( type )
112 , mParameterDefinition( parameter )
113{
114}
115
120
125
130
132{
133 if ( mWidget )
134 return mWidget;
135
136 mWidget = createWidget();
137 QWidget *wrappedWidget = mWidget;
138 if ( mParameterDefinition->isDynamic() )
139 {
140 QHBoxLayout *hLayout = new QHBoxLayout();
141 hLayout->setContentsMargins( 0, 0, 0, 0 );
142 hLayout->addWidget( mWidget, 1 );
143 mPropertyButton = new QgsPropertyOverrideButton();
144 hLayout->addWidget( mPropertyButton );
145 mPropertyButton->init( 0, QgsProperty(), mParameterDefinition->dynamicPropertyDefinition() );
146 mPropertyButton->registerEnabledWidget( mWidget, false );
147 mPropertyButton->registerExpressionContextGenerator( this );
148
149 wrappedWidget = new QWidget();
150 wrappedWidget->setLayout( hLayout );
151 }
152
153 if ( !dynamic_cast<const QgsProcessingDestinationParameter *>( mParameterDefinition ) )
154 {
155 // an exception -- output widgets handle this themselves
156 setWidgetValue( mParameterDefinition->defaultValueForGui(), context );
157 }
158
159 return wrappedWidget;
160}
161
163{
164 if ( mLabel )
165 return mLabel;
166
167 mLabel = createLabel();
168 return mLabel;
169}
170
172{
173 return mWidget;
174}
175
180
185
187{
188 if ( mPropertyButton && value.userType() == qMetaTypeId<QgsProperty>() )
189 {
190 mPropertyButton->setToProperty( value.value<QgsProperty>() );
191 }
192 else
193 {
194 if ( mPropertyButton )
195 mPropertyButton->setToProperty( QgsProperty() );
196
197 setWidgetValue( value, context );
198 }
199}
200
202{
203 if ( mPropertyButton && mPropertyButton->isActive() )
204 return mPropertyButton->toProperty();
205 else
206 return widgetValue();
207}
208
210{
211 return QVariantMap();
212}
213
218
223
225{
226 switch ( mType )
227 {
229 return nullptr;
230
233 {
234 QString description = mParameterDefinition->description();
236 description = QObject::tr( "%1 [optional]" ).arg( description );
237 std::unique_ptr<QLabel> label = std::make_unique<QLabel>( description );
238 label->setToolTip( mParameterDefinition->toolTip() );
239 label->setWordWrap( true );
240 return label.release();
241 }
242 }
243 return nullptr;
244}
245
247{
248 if ( mPropertyButton )
249 return mPropertyButton->vectorLayer();
250 return nullptr;
251}
252
253void QgsAbstractProcessingParameterWidgetWrapper::postInitialize( const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
254{
255 switch ( mType )
256 {
259 {
260 if ( parameterDefinition()->isDynamic() )
261 {
262 for ( const QgsAbstractProcessingParameterWidgetWrapper *wrapper : wrappers )
263 {
264 if ( wrapper->parameterDefinition()->name() == parameterDefinition()->dynamicLayerParameterName() )
265 {
266 setDynamicParentLayerParameter( wrapper );
267 connect( wrapper, &QgsAbstractProcessingParameterWidgetWrapper::widgetValueHasChanged, this, &QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged );
268 break;
269 }
270 }
271 }
272 break;
273 }
274
276 break;
277 }
278}
279
284
286{
287 QgsExpressionContext context = QgsProcessingGuiUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
288 if ( mParameterDefinition && !mParameterDefinition->additionalExpressionContextVariables().isEmpty() )
289 {
290 std::unique_ptr<QgsExpressionContextScope> paramScope = std::make_unique<QgsExpressionContextScope>();
291 const QStringList additional = mParameterDefinition->additionalExpressionContextVariables();
292 for ( const QString &var : additional )
293 {
294 paramScope->setVariable( var, QVariant() );
295 }
296 context.appendScope( paramScope.release() );
297
298 // we always highlight additional variables for visibility
299 QStringList highlighted = context.highlightedVariables();
300 highlighted.append( additional );
301 context.setHighlightedVariables( highlighted );
302 }
303 return context;
304}
305
309
310void QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged( QgsAbstractProcessingParameterWidgetWrapper *wrapper )
311{
312 if ( wrapper )
313 {
314 setDynamicParentLayerParameter( wrapper );
315 }
316}
317
318void QgsAbstractProcessingParameterWidgetWrapper::setDynamicParentLayerParameter( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper )
319{
320 if ( mPropertyButton )
321 {
322 // evaluate value to layer
323 QgsProcessingContext *context = nullptr;
324 std::unique_ptr<QgsProcessingContext> tmpContext;
327
328 if ( !context )
329 {
330 tmpContext = std::make_unique<QgsProcessingContext>();
331 context = tmpContext.get();
332 }
333
334 QVariant val = parentWrapper->parameterValue();
335 if ( val.userType() == qMetaTypeId<QgsProcessingFeatureSourceDefinition>() )
336 {
337 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
338 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
339 val = fromVar.source;
340 }
341
343 if ( !layer )
344 {
345 mPropertyButton->setVectorLayer( nullptr );
346 return;
347 }
348
349 // need to grab ownership of layer if required - otherwise layer may be deleted when context
350 // goes out of scope
351 std::unique_ptr<QgsMapLayer> ownedLayer( context->takeResultLayer( layer->id() ) );
352 if ( ownedLayer && ownedLayer->type() == Qgis::LayerType::Vector )
353 {
354 mDynamicLayer.reset( qobject_cast<QgsVectorLayer *>( ownedLayer.release() ) );
355 layer = mDynamicLayer.get();
356 }
357 else
358 {
359 // don't need ownership of this layer - it wasn't owned by context (so e.g. is owned by the project)
360 }
361
362 mPropertyButton->setVectorLayer( layer );
363 }
364}
365
367{
368 std::unique_ptr<QgsProcessingModelerParameterWidget> widget = std::make_unique<QgsProcessingModelerParameterWidget>( model, childId, parameter, context );
369 widget->populateSources( compatibleParameterTypes(), compatibleOutputTypes(), compatibleDataTypes( parameter ) );
370 widget->setExpressionHelpText( modelerExpressionFormatString() );
371
372 if ( parameter->isDestination() )
374 else
375 widget->setSourceType( defaultModelSource( parameter ) );
376
377 return widget.release();
378}
379
384
389
394
399
400//
401// QgsProcessingGuiUtils
402//
403
405QgsExpressionContext QgsProcessingGuiUtils::createExpressionContext( QgsProcessingContextGenerator *processingContextGenerator, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingAlgorithm *algorithm, const QgsVectorLayer *linkedLayer )
406{
407 // Get a processing context to start with
408 QgsProcessingContext *context = nullptr;
409 std::unique_ptr<QgsProcessingContext> tmpContext;
410 if ( processingContextGenerator )
411 context = processingContextGenerator->processingContext();
412
413 if ( !context )
414 {
415 tmpContext = std::make_unique<QgsProcessingContext>();
416 context = tmpContext.get();
417 }
418
420
421 if ( auto *lModel = widgetContext.model() )
422 {
423 c << QgsExpressionContextUtils::processingModelAlgorithmScope( lModel, QVariantMap(), *context );
424
425 const QgsProcessingAlgorithm *alg = nullptr;
426 if ( lModel->childAlgorithms().contains( widgetContext.modelChildAlgorithmId() ) )
427 alg = lModel->childAlgorithm( widgetContext.modelChildAlgorithmId() ).algorithm();
428
429 QgsExpressionContextScope *algorithmScope = QgsExpressionContextUtils::processingAlgorithmScope( alg ? alg : algorithm, QVariantMap(), *context );
430 c << algorithmScope;
431 QgsExpressionContextScope *childScope = lModel->createExpressionContextScopeForChildAlgorithm( widgetContext.modelChildAlgorithmId(), *context, QVariantMap(), QVariantMap() );
432 c << childScope;
433
434 QStringList highlightedVariables = childScope->variableNames();
435 QStringList highlightedFunctions = childScope->functionNames();
436 highlightedVariables += algorithmScope->variableNames();
437 highlightedVariables += lModel->variables().keys();
438 highlightedFunctions += algorithmScope->functionNames();
439 c.setHighlightedVariables( highlightedVariables );
440 c.setHighlightedFunctions( highlightedFunctions );
441 }
442 else
443 {
444 if ( algorithm )
446 }
447
448 if ( linkedLayer )
450
451 return c;
452}
454
459
461{
462 if ( mValue == value )
463 return;
464
465 mValue = value;
466 emit widgetValueHasChanged( this );
467}
468
470{
471 return mValue;
472}
473
475{
476 return mLayer;
477}
478
480{
481 mLayer = layer;
482}
483
485{
486 return nullptr;
487}
488
490{
491 return nullptr;
492}
@ Vector
Vector layer.
ProcessingModelChildParameterSource
Processing model child parameter sources.
Definition qgis.h:3632
@ 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...
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.
Base class for all map layer types.
Definition qgsmaplayer.h:76
QString id
Definition qgsmaplayer.h:79
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.
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.
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.
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 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 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.
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 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