QGIS API Documentation 3.99.0-Master (c22de0620c0)
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
22#include "qgsapplication.h"
30
31#include <QHBoxLayout>
32#include <QLabel>
33
34#include "moc_qgsprocessingwidgetwrapper.cpp"
35
36//
37// QgsProcessingParameterWidgetContext
38//
39
41{
42 mMapCanvas = canvas;
43}
44
46{
47 return mMapCanvas;
48}
49
51{
52 mMessageBar = bar;
53}
54
56{
57 return mMessageBar;
58}
59
64
69
74
76{
77 return mProject;
78}
79
81{
82 return mModelChildAlgorithmId;
83}
84
89
91{
92 return mActiveLayer;
93}
94
99
101{
102 mProcessingContextGenerator = generator;
103}
104
109
111{
112 return mModelDialog;
113}
114
116{
117 mModelDialog = dialog;
118}
119
120QgsProcessingModelAlgorithm *QgsProcessingParameterWidgetContext::model() const
121{
122 return mModel;
123}
124
125void QgsProcessingParameterWidgetContext::setModel( QgsProcessingModelAlgorithm *model )
126{
127 mModel = model;
128}
129
130
131//
132// QgsAbstractProcessingParameterWidgetWrapper
133//
134
136 : QObject( parent )
137 , mType( type )
138 , mParameterDefinition( parameter )
139{
140}
141
146
151
156
158{
159 if ( mWidget )
160 return mWidget;
161
162 mWidget = createWidget();
163 QWidget *wrappedWidget = mWidget;
164 if ( mParameterDefinition->isDynamic() )
165 {
166 QHBoxLayout *hLayout = new QHBoxLayout();
167 hLayout->setContentsMargins( 0, 0, 0, 0 );
168 hLayout->addWidget( mWidget, 1 );
169 mPropertyButton = new QgsPropertyOverrideButton();
170 hLayout->addWidget( mPropertyButton );
171 mPropertyButton->init( 0, QgsProperty(), mParameterDefinition->dynamicPropertyDefinition() );
172 mPropertyButton->registerEnabledWidget( mWidget, false );
173 mPropertyButton->registerExpressionContextGenerator( this );
174
175 wrappedWidget = new QWidget();
176 wrappedWidget->setLayout( hLayout );
177 }
178
179 if ( !dynamic_cast<const QgsProcessingDestinationParameter *>( mParameterDefinition ) )
180 {
181 // an exception -- output widgets handle this themselves
182 setWidgetValue( mParameterDefinition->defaultValueForGui(), context );
183 }
184
185 return wrappedWidget;
186}
187
189{
190 if ( mLabel )
191 return mLabel;
192
193 mLabel = createLabel();
194 return mLabel;
195}
196
198{
199 return mWidget;
200}
201
206
211
213{
214 if ( mPropertyButton && value.userType() == qMetaTypeId<QgsProperty>() )
215 {
216 mPropertyButton->setToProperty( value.value<QgsProperty>() );
217 }
218 else
219 {
220 if ( mPropertyButton )
221 mPropertyButton->setToProperty( QgsProperty() );
222
223 setWidgetValue( value, context );
224 }
225}
226
228{
229 if ( mPropertyButton && mPropertyButton->isActive() )
230 return mPropertyButton->toProperty();
231 else
232 return widgetValue();
233}
234
236{
237 return QVariantMap();
238}
239
244
249
251{
252 switch ( mType )
253 {
255 return nullptr;
256
259 {
260 QString description = mParameterDefinition->description();
262 description = QObject::tr( "%1 [optional]" ).arg( description );
263 auto label = std::make_unique<QLabel>( description );
264 label->setToolTip( mParameterDefinition->toolTip() );
265 label->setWordWrap( true );
266 return label.release();
267 }
268 }
269 return nullptr;
270}
271
273{
274 if ( mPropertyButton )
275 return mPropertyButton->vectorLayer();
276 return nullptr;
277}
278
279void QgsAbstractProcessingParameterWidgetWrapper::postInitialize( const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
280{
281 switch ( mType )
282 {
285 {
286 if ( parameterDefinition()->isDynamic() )
287 {
288 for ( const QgsAbstractProcessingParameterWidgetWrapper *wrapper : wrappers )
289 {
290 if ( wrapper->parameterDefinition()->name() == parameterDefinition()->dynamicLayerParameterName() )
291 {
292 setDynamicParentLayerParameter( wrapper );
293 connect( wrapper, &QgsAbstractProcessingParameterWidgetWrapper::widgetValueHasChanged, this, &QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged );
294 break;
295 }
296 }
297 }
298 break;
299 }
300
302 break;
303 }
304}
305
310
312{
313 QgsExpressionContext context = QgsProcessingWidgetWrapperUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
314 if ( mParameterDefinition && !mParameterDefinition->additionalExpressionContextVariables().isEmpty() )
315 {
316 auto paramScope = std::make_unique<QgsExpressionContextScope>();
317 const QStringList additional = mParameterDefinition->additionalExpressionContextVariables();
318 for ( const QString &var : additional )
319 {
320 paramScope->setVariable( var, QVariant() );
321 }
322 context.appendScope( paramScope.release() );
323
324 // we always highlight additional variables for visibility
325 QStringList highlighted = context.highlightedVariables();
326 highlighted.append( additional );
327 context.setHighlightedVariables( highlighted );
328 }
329 return context;
330}
331
335
336void QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged( QgsAbstractProcessingParameterWidgetWrapper *wrapper )
337{
338 if ( wrapper )
339 {
340 setDynamicParentLayerParameter( wrapper );
341 }
342}
343
344void QgsAbstractProcessingParameterWidgetWrapper::setDynamicParentLayerParameter( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper )
345{
346 if ( mPropertyButton )
347 {
348 // evaluate value to layer
349 QgsProcessingContext *context = nullptr;
350 std::unique_ptr<QgsProcessingContext> tmpContext;
352 context = mProcessingContextGenerator->processingContext();
353
354 if ( !context )
355 {
356 tmpContext = std::make_unique<QgsProcessingContext>();
357 context = tmpContext.get();
358 }
359
360 QVariant val = parentWrapper->parameterValue();
361 if ( val.userType() == qMetaTypeId<QgsProcessingFeatureSourceDefinition>() )
362 {
363 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
364 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
365 val = fromVar.source;
366 }
367
368 QgsVectorLayer *layer = QgsProcessingParameters::parameterAsVectorLayer( parentWrapper->parameterDefinition(), val, *context );
369 if ( !layer )
370 {
371 mPropertyButton->setVectorLayer( nullptr );
372 return;
373 }
374
375 // need to grab ownership of layer if required - otherwise layer may be deleted when context
376 // goes out of scope
377 std::unique_ptr<QgsMapLayer> ownedLayer( context->takeResultLayer( layer->id() ) );
378 if ( ownedLayer && ownedLayer->type() == Qgis::LayerType::Vector )
379 {
380 mDynamicLayer.reset( qobject_cast<QgsVectorLayer *>( ownedLayer.release() ) );
381 layer = mDynamicLayer.get();
382 }
383 else
384 {
385 // don't need ownership of this layer - it wasn't owned by context (so e.g. is owned by the project)
386 }
387
388 mPropertyButton->setVectorLayer( layer );
389 }
390}
391
393{
394 auto widget = std::make_unique<QgsProcessingModelerParameterWidget>( model, childId, parameter, context );
395 widget->populateSources( compatibleParameterTypes(), compatibleOutputTypes(), compatibleDataTypes( parameter ) );
396 widget->setExpressionHelpText( modelerExpressionFormatString() );
397
398 if ( parameter->isDestination() )
400 else
401 widget->setSourceType( defaultModelSource( parameter ) );
402
403 return widget.release();
404}
405
410
412{
414 if ( !paramType )
415 return QStringList();
416 return paramType->acceptedParameterTypes();
417}
418
420{
422 if ( !paramType )
423 return QStringList();
424 return paramType->acceptedOutputTypes();
425}
426
428{
430 if ( !paramType )
431 return QList<int>();
432 return paramType->acceptedDataTypes( parameter );
433}
434
439
444
445//
446// QgsProcessingWidgetWrapperUtils
447//
448
450QgsExpressionContext QgsProcessingWidgetWrapperUtils::createExpressionContext( QgsProcessingContextGenerator *processingContextGenerator, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingAlgorithm *algorithm, const QgsVectorLayer *linkedLayer )
451{
452 // Get a processing context to start with
453 QgsProcessingContext *context = nullptr;
454 std::unique_ptr<QgsProcessingContext> tmpContext;
455 if ( processingContextGenerator )
456 context = processingContextGenerator->processingContext();
457
458 if ( !context )
459 {
460 tmpContext = std::make_unique<QgsProcessingContext>();
461 context = tmpContext.get();
462 }
463
465
466 if ( auto *lModel = widgetContext.model() )
467 {
468 c << QgsExpressionContextUtils::processingModelAlgorithmScope( lModel, QVariantMap(), *context );
469
470 const QgsProcessingAlgorithm *alg = nullptr;
471 if ( lModel->childAlgorithms().contains( widgetContext.modelChildAlgorithmId() ) )
472 alg = lModel->childAlgorithm( widgetContext.modelChildAlgorithmId() ).algorithm();
473
474 QgsExpressionContextScope *algorithmScope = QgsExpressionContextUtils::processingAlgorithmScope( alg ? alg : algorithm, QVariantMap(), *context );
475 c << algorithmScope;
476 QgsExpressionContextScope *childScope = lModel->createExpressionContextScopeForChildAlgorithm( widgetContext.modelChildAlgorithmId(), *context, QVariantMap(), QVariantMap() );
477 c << childScope;
478
479 QStringList highlightedVariables = childScope->variableNames();
480 QStringList highlightedFunctions = childScope->functionNames();
481 highlightedVariables += algorithmScope->variableNames();
482 highlightedVariables += lModel->variables().keys();
483 highlightedFunctions += algorithmScope->functionNames();
484 c.setHighlightedVariables( highlightedVariables );
485 c.setHighlightedFunctions( highlightedFunctions );
486 }
487 else
488 {
489 if ( algorithm )
491 }
492
493 if ( linkedLayer )
495
496 return c;
497}
499
504
506{
507 if ( mValue == value )
508 return;
509
510 mValue = value;
511 emit widgetValueHasChanged( this );
512}
513
515{
516 return mValue;
517}
518
520{
521 return mLayer;
522}
523
525{
526 mLayer = layer;
527}
528
530{
531 return nullptr;
532}
533
535{
536 return nullptr;
537}
ProcessingMode
Types of modes which Processing widgets can be created for.
Definition qgis.h:3757
@ Batch
Batch processing mode.
Definition qgis.h:3759
@ Modeler
Modeler mode.
Definition qgis.h:3760
@ Standard
Standard (single-run) algorithm mode.
Definition qgis.h:3758
@ Vector
Vector layer.
Definition qgis.h:205
ProcessingModelChildParameterSource
Processing model child parameter sources.
Definition qgis.h:3933
@ ModelOutput
Parameter value is linked to an output parameter for the model.
Definition qgis.h:3939
@ StaticValue
Parameter value is a static value.
Definition qgis.h:3936
@ Optional
Parameter is optional.
Definition qgis.h:3851
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:83
QString id
Definition qgsmaplayer.h:86
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,...
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.
virtual bool isDestination() const
Returns true if this parameter represents a file or layer destination, e.g.
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.
QgsModelDesignerDialog * modelDesignerDialog() const
Returns the associated model designer dialog, if applicable.
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 setModelDesignerDialog(QgsModelDesignerDialog *dialog)
Sets the associated model designer dialog, if applicable.
void setModel(QgsProcessingModelAlgorithm *model)
Sets the model which the parameter widget is associated with.
QgsProcessingContextGenerator * processingContextGenerator()
Returns the Processing context generator class that will be used to retrieve a Processing context for...
void registerProcessingContextGenerator(QgsProcessingContextGenerator *generator)
Registers a Processing context generator class that will be used to retrieve a Processing context for...
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:113
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