Skip to content

Commit 1e13d73

Browse files
committedJul 10, 2017
Move declaration of algorithm parameters/outputs to a new virtual
initAlgorithm() method This allows 2 benefits: - algorithms can be subclassed and have subclasses add additional parameters/outputs to the algorithm. With the previous approach of declaring parameters/outputs in the constructor, it's not possible to call virtual methods to add additional parameters/ outputs (since you can't call virtual methods from a constructor). - initAlgorithm takes a variant map argument, allowing the algorithm to dynamically adjust its declared parameters and outputs according to this configuration map. This potentially allows model algorithms which can be configured to have variable numbers of parameters and outputs at run time. E.g. a "router" algorithm which directs features to one of any number of output sinks depending on some user configured criteria.
1 parent fc221a6 commit 1e13d73

File tree

161 files changed

+409
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+409
-58
lines changed
 

‎python/core/processing/models/qgsprocessingmodelalgorithm.sip

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
2727
Constructor for QgsProcessingModelAlgorithm.
2828
%End
2929

30+
virtual void initAlgorithm( const QVariantMap &configuration = QVariantMap() ); //#spellok
31+
32+
3033
virtual QString name() const;
3134

3235
virtual QString displayName() const;
@@ -46,8 +49,6 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
4649

4750
virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;
4851

49-
virtual QgsProcessingModelAlgorithm *createInstance() const /Factory/;
50-
5152

5253
void setName( const QString &name );
5354
%Docstring
@@ -364,6 +365,9 @@ Translated description of variable
364365

365366
protected:
366367

368+
virtual QgsProcessingAlgorithm *createInstance() const /Factory/;
369+
370+
367371
virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
368372

369373

‎python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class QgsProcessingAlgorithm
3838
QgsProcessingAlgorithm();
3939
%Docstring
4040
Constructor for QgsProcessingAlgorithm.
41+
42+
initAlgorithm() should be called after creating an algorithm to ensure it can correctly configure
43+
its parameterDefinitions() and outputDefinitions(). Alternatively, calling create() will return
44+
a pre-initialized copy of the algorithm.
4145
%End
4246

4347
virtual ~QgsProcessingAlgorithm();
@@ -46,6 +50,11 @@ class QgsProcessingAlgorithm
4650
QgsProcessingAlgorithm *create() const /Factory/;
4751
%Docstring
4852
Creates a copy of the algorithm, ready for execution.
53+
54+
This method returns a new, preinitialized copy of the algorithm, ready for
55+
executing.
56+
57+
.. seealso:: initAlgorithm()
4958
:rtype: QgsProcessingAlgorithm
5059
%End
5160

@@ -345,11 +354,35 @@ class QgsProcessingAlgorithm
345354
sipTransferTo( resObj, Py_None );
346355
%End
347356

357+
virtual void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) = 0;
358+
%Docstring
359+
Initializes the algorithm using the specified ``configuration``.
360+
361+
This should be called directly after creating algorithms and before retrieving
362+
any parameterDefinitions() or outputDefinitions().
363+
364+
Subclasses should use their implementations to add all required input parameter and output
365+
definitions (which can be dynamically adjusted according to ``configuration``).
366+
367+
Dynamic configuration can be used by algorithms which alter their behavior
368+
when used inside processing models. For instance, a "feature router" type
369+
algorithm which sends input features to one of any number of outputs sinks
370+
based on some preconfigured filter parameters can use the init method to
371+
create these outputs based on the specified ``configuration``.
372+
373+
.. seealso:: addParameter()
374+
.. seealso:: addOutput()
375+
%End
376+
348377
bool addParameter( QgsProcessingParameterDefinition *parameterDefinition /Transfer/ );
349378
%Docstring
350379
Adds a parameter ``definition`` to the algorithm. Ownership of the definition is transferred to the algorithm.
351380
Returns true if parameter could be successfully added, or false if the parameter could not be added (e.g.
352381
as a result of a duplicate name).
382+
383+
This should usually be called from a subclass' initAlgorithm() implementation.
384+
385+
.. seealso:: initAlgorithm()
353386
.. seealso:: addOutput()
354387
:rtype: bool
355388
%End
@@ -365,7 +398,11 @@ class QgsProcessingAlgorithm
365398
Adds an output ``definition`` to the algorithm. Ownership of the definition is transferred to the algorithm.
366399
Returns true if the output could be successfully added, or false if the output could not be added (e.g.
367400
as a result of a duplicate name).
401+
402+
This should usually be called from a subclass' initAlgorithm() implementation.
403+
368404
.. seealso:: addParameter()
405+
.. seealso:: initAlgorithm()
369406
:rtype: bool
370407
%End
371408

0 commit comments

Comments
 (0)
Please sign in to comment.