QGIS API Documentation 4.3.0-Master (01f22706ec5)
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// QgsAbstractProcessingParameterWidgetWrapper
38//
39
41 : QObject( parent )
42 , mType( type )
43 , mParameterDefinition( parameter )
44{}
45
50
55
60
62{
63 if ( mWidget )
64 return mWidget;
65
66 mWidget = createWidget();
67 QWidget *wrappedWidget = mWidget;
68 if ( mParameterDefinition->isDynamic() )
69 {
70 QHBoxLayout *hLayout = new QHBoxLayout();
71 hLayout->setContentsMargins( 0, 0, 0, 0 );
72 hLayout->addWidget( mWidget, 1 );
73 mPropertyButton = new QgsPropertyOverrideButton();
74 hLayout->addWidget( mPropertyButton );
75 mPropertyButton->init( 0, QgsProperty(), mParameterDefinition->dynamicPropertyDefinition() );
76 mPropertyButton->registerEnabledWidget( mWidget, false );
77 mPropertyButton->registerExpressionContextGenerator( this );
78
79 wrappedWidget = new QWidget();
80 wrappedWidget->setLayout( hLayout );
81 }
82
83 if ( !dynamic_cast<const QgsProcessingDestinationParameter *>( mParameterDefinition ) )
84 {
85 // an exception -- output widgets handle this themselves
86 setWidgetValue( mParameterDefinition->defaultValueForGui(), context );
87 }
88
89 return wrappedWidget;
90}
91
93{
94 if ( mLabel )
95 return mLabel;
96
97 mLabel = createLabel();
98 return mLabel;
99}
100
102{
103 return mWidget;
104}
105
110
115
117{
118 if ( mPropertyButton && value.userType() == qMetaTypeId<QgsProperty>() )
119 {
120 mPropertyButton->setToProperty( value.value<QgsProperty>() );
121 }
122 else
123 {
124 if ( mPropertyButton )
125 mPropertyButton->setToProperty( QgsProperty() );
126
127 setWidgetValue( value, context );
128 }
129}
130
132{
133 if ( mPropertyButton && mPropertyButton->isActive() )
134 return mPropertyButton->toProperty();
135 else
136 return widgetValue();
137}
138
140{
141 return QVariantMap();
142}
143
148
153
155{
156 switch ( mType )
157 {
159 return nullptr;
160
163 {
164 QString description = mParameterDefinition->description();
166 description = QObject::tr( "%1 [optional]" ).arg( description );
167 auto label = std::make_unique<QLabel>( description );
168 label->setToolTip( mParameterDefinition->toolTip() );
169 label->setWordWrap( true );
170 return label.release();
171 }
172 }
173 return nullptr;
174}
175
177{
178 if ( mPropertyButton )
179 return mPropertyButton->vectorLayer();
180 return nullptr;
181}
182
183void QgsAbstractProcessingParameterWidgetWrapper::postInitialize( const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
184{
185 switch ( mType )
186 {
189 {
190 if ( parameterDefinition()->isDynamic() )
191 {
192 for ( const QgsAbstractProcessingParameterWidgetWrapper *wrapper : wrappers )
193 {
194 if ( wrapper->parameterDefinition()->name() == parameterDefinition()->dynamicLayerParameterName() )
195 {
196 setDynamicParentLayerParameter( wrapper );
197 connect( wrapper, &QgsAbstractProcessingParameterWidgetWrapper::widgetValueHasChanged, this, &QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged );
198 break;
199 }
200 }
201 }
202 break;
203 }
204
206 break;
207 }
208}
209
214
216{
218 = QgsProcessingWidgetWrapperUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
219 if ( mParameterDefinition && !mParameterDefinition->additionalExpressionContextVariables().isEmpty() )
220 {
221 auto paramScope = std::make_unique<QgsExpressionContextScope>();
222 const QStringList additional = mParameterDefinition->additionalExpressionContextVariables();
223 for ( const QString &var : additional )
224 {
225 paramScope->setVariable( var, QVariant() );
226 }
227 context.appendScope( paramScope.release() );
228
229 // we always highlight additional variables for visibility
230 QStringList highlighted = context.highlightedVariables();
231 highlighted.append( additional );
232 context.setHighlightedVariables( highlighted );
233 }
234 return context;
235}
236
239
240void QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged( QgsAbstractProcessingParameterWidgetWrapper *wrapper )
241{
242 if ( wrapper )
243 {
244 setDynamicParentLayerParameter( wrapper );
245 }
246}
247
248void QgsAbstractProcessingParameterWidgetWrapper::setDynamicParentLayerParameter( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper )
249{
250 if ( mPropertyButton )
251 {
252 // evaluate value to layer
253 QgsProcessingContext *context = nullptr;
254 std::unique_ptr<QgsProcessingContext> tmpContext;
256 context = mProcessingContextGenerator->processingContext();
257
258 if ( !context )
259 {
260 tmpContext = std::make_unique<QgsProcessingContext>();
261 context = tmpContext.get();
262 }
263
264 QVariant val = parentWrapper->parameterValue();
265 if ( val.userType() == qMetaTypeId<QgsProcessingFeatureSourceDefinition>() )
266 {
267 // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
268 const QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val );
269 val = fromVar.source;
270 }
271
272 QgsVectorLayer *layer = QgsProcessingParameters::parameterAsVectorLayer( parentWrapper->parameterDefinition(), val, *context );
273 if ( !layer )
274 {
275 mPropertyButton->setVectorLayer( nullptr );
276 return;
277 }
278
279 // need to grab ownership of layer if required - otherwise layer may be deleted when context
280 // goes out of scope
281 std::unique_ptr<QgsMapLayer> ownedLayer( context->takeResultLayer( layer->id() ) );
282 if ( ownedLayer && ownedLayer->type() == Qgis::LayerType::Vector )
283 {
284 mDynamicLayer.reset( qobject_cast<QgsVectorLayer *>( ownedLayer.release() ) );
285 layer = mDynamicLayer.get();
286 }
287 else
288 {
289 // don't need ownership of this layer - it wasn't owned by context (so e.g. is owned by the project)
290 }
291
292 mPropertyButton->setVectorLayer( layer );
293 }
294}
295
297 QgsProcessingModelAlgorithm *model, const QString &childId, const QgsProcessingParameterDefinition *parameter, QgsProcessingContext &context
298)
299{
300 auto widget = std::make_unique<QgsProcessingModelerParameterWidget>( model, childId, parameter, context );
301 widget->populateSources( compatibleParameterTypes(), compatibleOutputTypes(), compatibleDataTypes( parameter ) );
302 widget->setExpressionHelpText( modelerExpressionFormatString() );
303
304 if ( parameter->isDestination() )
306 else
307 widget->setSourceType( defaultModelSource( parameter ) );
308
309 return widget.release();
310}
311
318
320{
322 if ( !paramType )
323 return QStringList();
324 return paramType->acceptedParameterTypes();
325}
326
328{
330 if ( !paramType )
331 return QStringList();
332 return paramType->acceptedOutputTypes();
333}
334
336{
338 if ( !paramType )
339 return QList<int>();
340 return paramType->acceptedDataTypes( parameter );
341}
342
347
352
353//
354// QgsProcessingWidgetWrapperUtils
355//
356
358QgsExpressionContext QgsProcessingWidgetWrapperUtils::createExpressionContext(
359 QgsProcessingContextGenerator *processingContextGenerator, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingAlgorithm *algorithm, const QgsVectorLayer *linkedLayer
360)
361{
362 // Get a processing context to start with
363 QgsProcessingContext *context = nullptr;
364 std::unique_ptr<QgsProcessingContext> tmpContext;
365 if ( processingContextGenerator )
366 context = processingContextGenerator->processingContext();
367
368 if ( !context )
369 {
370 tmpContext = std::make_unique<QgsProcessingContext>();
371 context = tmpContext.get();
372 }
373
375
376 if ( auto *lModel = widgetContext.model() )
377 {
378 c << QgsExpressionContextUtils::processingModelAlgorithmScope( lModel, QVariantMap(), *context );
379
380 const QgsProcessingAlgorithm *alg = nullptr;
381 if ( lModel->childAlgorithms().contains( widgetContext.modelChildAlgorithmId() ) )
382 alg = lModel->childAlgorithm( widgetContext.modelChildAlgorithmId() ).algorithm();
383
384 QgsExpressionContextScope *algorithmScope = QgsExpressionContextUtils::processingAlgorithmScope( alg ? alg : algorithm, QVariantMap(), *context );
385 c << algorithmScope;
386 QgsExpressionContextScope *childScope = lModel->createExpressionContextScopeForChildAlgorithm( widgetContext.modelChildAlgorithmId(), *context, QVariantMap(), QVariantMap() ).release();
387 c << childScope;
388
389 QStringList highlightedVariables = childScope->variableNames();
390 QStringList highlightedFunctions = childScope->functionNames();
391 highlightedVariables += algorithmScope->variableNames();
392 highlightedVariables += lModel->variables().keys();
393 highlightedFunctions += algorithmScope->functionNames();
394 c.setHighlightedVariables( highlightedVariables );
395 c.setHighlightedFunctions( highlightedFunctions );
396 }
397 else
398 {
399 if ( algorithm )
401 }
402
403 if ( linkedLayer )
405
406 return c;
407}
409
413
415{
416 if ( mValue == value )
417 return;
418
419 mValue = value;
420 emit widgetValueHasChanged( this );
421}
422
424{
425 return mValue;
426}
427
429{
430 return mLayer;
431}
432
434{
435 mLayer = layer;
436}
437
439{
440 return nullptr;
441}
442
444{
445 return nullptr;
446}
ProcessingMode
Types of modes which Processing widgets can be created for.
Definition qgis.h:3888
@ Batch
Batch processing mode.
Definition qgis.h:3890
@ Modeler
Modeler mode.
Definition qgis.h:3891
@ Standard
Standard (single-run) algorithm mode.
Definition qgis.h:3889
@ Vector
Vector layer.
Definition qgis.h:207
ProcessingModelChildParameterSource
Processing model child parameter sources.
Definition qgis.h:4066
@ ModelOutput
Parameter value is linked to an output parameter for the model.
Definition qgis.h:4072
@ StaticValue
Parameter value is a static value.
Definition qgis.h:4069
@ Optional
Parameter is optional.
Definition qgis.h:3984
A widget wrapper for Processing parameter value widgets.
QVariant parameterValue() const
Returns the current value of the parameter.
virtual void setDialog(QWidget *dialog)
Sets the parent dialog (or widget) in which the wrapper is shown.
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 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,...
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.
QString id
Definition qgsmaplayer.h:86
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() const =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.
QgsProcessingModelAlgorithm * model() const
Returns 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.
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