QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsprocessingmodelalgorithm.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingmodelalgorithm.h
3  -----------------------------
4  begin : June 2017
5  copyright : (C) 2017 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 #ifndef QGSPROCESSINGMODELALGORITHM_H
19 #define QGSPROCESSINGMODELALGORITHM_H
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
24 #include "qgsprocessingalgorithm.h"
29 
31 
38 class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
39 {
40  public:
41 
45  QgsProcessingModelAlgorithm( const QString &name = QString(), const QString &group = QString(), const QString &groupId = QString() );
46 
47  void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override; //#spellok
48 
49  QString name() const override;
50  QString displayName() const override;
51  QString group() const override;
52  QString groupId() const override;
53  QIcon icon() const override;
54  QString svgIconPath() const override;
55  QString shortHelpString() const override;
56  QString shortDescription() const override;
57  QString helpUrl() const override;
58  Flags flags() const override;
59 
60  bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const override;
61  QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const override;
62  QgsExpressionContext createExpressionContext( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const override;
63 
68  void setName( const QString &name );
69 
74  void setGroup( const QString &group );
75 
83  bool validate( QStringList &issues SIP_OUT ) const;
84 
92  QMap<QString, QgsProcessingModelChildAlgorithm> childAlgorithms() const;
93 
103  void setChildAlgorithms( const QMap<QString, QgsProcessingModelChildAlgorithm> &childAlgorithms );
104 
111  void setChildAlgorithm( const QgsProcessingModelChildAlgorithm &algorithm );
112 
123  QString addChildAlgorithm( QgsProcessingModelChildAlgorithm &algorithm );
124 
131  QgsProcessingModelChildAlgorithm &childAlgorithm( const QString &id );
132 
140  bool removeChildAlgorithm( const QString &id );
141 
149  void deactivateChildAlgorithm( const QString &id );
150 
157  bool activateChildAlgorithm( const QString &id );
158 
169  QSet< QString > dependentChildAlgorithms( const QString &childId, const QString &conditionalBranch = QString() ) const;
170 
176  QSet< QString > dependsOnChildAlgorithms( const QString &childId ) const;
177 
183  QList< QgsProcessingModelChildDependency > availableDependenciesForChildAlgorithm( const QString &childId ) const;
184 
194  bool validateChildAlgorithm( const QString &childId, QStringList &issues SIP_OUT ) const;
195 
202  void addModelParameter( QgsProcessingParameterDefinition *definition SIP_TRANSFER, const QgsProcessingModelParameter &component );
203 
210  void updateModelParameter( QgsProcessingParameterDefinition *definition SIP_TRANSFER );
211 
218  void removeModelParameter( const QString &name );
219 
225  bool childAlgorithmsDependOnParameter( const QString &name ) const;
226 
233  bool otherParametersDependOnParameter( const QString &name ) const;
234 
241  QMap<QString, QgsProcessingModelParameter> parameterComponents() const;
242 
251  void setParameterComponents( const QMap<QString, QgsProcessingModelParameter> &parameterComponents );
252 
260  void setParameterComponent( const QgsProcessingModelParameter &component );
261 
269  QgsProcessingModelParameter &parameterComponent( const QString &name );
270 
277  QList< QgsProcessingModelParameter > orderedParameters() const;
278 
288  void setParameterOrder( const QStringList &order );
289 
295  void updateDestinationParameters();
296 
305  void addGroupBox( const QgsProcessingModelGroupBox &groupBox );
306 
313  QList< QgsProcessingModelGroupBox > groupBoxes() const;
314 
322  void removeGroupBox( const QString &uuid );
323 
328  bool toFile( const QString &path ) const;
329 
334  bool fromFile( const QString &path );
335 
343  QVariant toVariant() const;
344 
352  bool loadVariant( const QVariant &variant );
353 
359  QVariantMap &helpContent() { return mHelpContent; }
360 
366  SIP_SKIP QVariantMap helpContent() const;
367 
373  void setHelpContent( const QVariantMap &contents );
374 
379  QString sourceFilePath() const;
380 
385  void setSourceFilePath( const QString &path );
386 
395  bool modelNameMatchesFilePath() const;
396 
404  QStringList asPythonCode( QgsProcessing::PythonOutputType outputType, int indentSize ) const;
405 
414  QList< QgsProcessingModelChildParameterSource > availableSourcesForChild( const QString &childId, const QStringList &parameterTypes = QStringList(),
415  const QStringList &outputTypes = QStringList(), const QList< int > &dataTypes = QList< int >() ) const;
416 
422  class CORE_EXPORT VariableDefinition
423  {
424  public:
425 
430  VariableDefinition( const QVariant &value = QVariant(), const QgsProcessingModelChildParameterSource &source = QgsProcessingModelChildParameterSource::fromStaticValue( QVariant() ), const QString &description = QString() )
431  : value( value )
432  , source( source )
433  , description( description )
434  {}
435 
437  QVariant value;
438 
440  QgsProcessingModelChildParameterSource source;
441 
443  QString description;
444  };
445 
459  QMap< QString, QgsProcessingModelAlgorithm::VariableDefinition > variablesForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
460  const QVariantMap &results = QVariantMap() ) const;
461 
466  QgsExpressionContextScope *createExpressionContextScopeForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
467  const QVariantMap &results = QVariantMap() ) const SIP_FACTORY;
468 
478  QVariantMap variables() const;
479 
489  void setVariables( const QVariantMap &variables );
490 
502  QVariantMap designerParameterValues() const;
503 
515  void setDesignerParameterValues( const QVariantMap &values ) { mDesignerParameterValues = values; }
516 
517  protected:
518 
520 
521  QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );
522 
523  private:
524 
525  QString mModelName;
526  QString mModelGroup;
527  QString mModelGroupId;
528 
529  QMap< QString, QgsProcessingModelChildAlgorithm > mChildAlgorithms;
530 
532  QMap< QString, QgsProcessingModelParameter > mParameterComponents;
533 
534  QVariantMap mHelpContent;
535 
537  QString mSourceFile;
538 
539  QVariantMap mResults;
540 
541  QVariantMap mVariables;
542 
543  QVariantMap mDesignerParameterValues;
544 
545  QMap< QString, QgsProcessingModelGroupBox > mGroupBoxes;
546 
547  QStringList mParameterOrder;
548 
549  void dependsOnChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;
550  void dependentChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends, const QString &branch ) const;
551 
552  QVariantMap parametersForChildAlgorithm( const QgsProcessingModelChildAlgorithm &child, const QVariantMap &modelParameters, const QVariantMap &results, const QgsExpressionContext &expressionContext, QString &error ) const;
553 
558  bool childOutputIsRequired( const QString &childId, const QString &outputName ) const;
559 
569  static bool vectorOutputIsCompatibleType( const QList<int> &acceptableDataTypes, QgsProcessing::SourceType outputType );
570 
574  void reattachAlgorithms() const;
575 
576  friend class TestQgsProcessingModelAlgorithm;
577 };
578 
580 
581 #endif // QGSPROCESSINGMODELALGORITHM_H
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract base class for processing algorithms.
virtual QString group() const
Returns the name of the group this algorithm belongs to.
virtual QString groupId() const
Returns the unique ID of the group this algorithm belongs to.
virtual QString helpUrl() const
Returns a url pointing to the algorithm's help page.
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
virtual QIcon icon() const
Returns an icon for the algorithm.
virtual QString shortHelpString() const
Returns a localised short helper string for the algorithm.
virtual QString shortDescription() const
Returns an optional translated short description of the algorithm.
virtual QString displayName() const =0
Returns the translated algorithm name, which should be used for any user-visible display of the algor...
virtual QgsExpressionContext createExpressionContext(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeatureSource *source=nullptr) const
Creates an expression context relating to the algorithm.
virtual QgsProcessingAlgorithm * createInstance() const =0
Creates a new instance of the algorithm class.
virtual QString svgIconPath() const
Returns a path to an SVG version of the algorithm's icon.
virtual void initAlgorithm(const QVariantMap &configuration=QVariantMap())=0
Initializes the algorithm using the specified configuration.
virtual QString asPythonCommand(const QVariantMap &parameters, QgsProcessingContext &context) const
Returns a Python command string which can be executed to run the algorithm using the specified parame...
virtual bool canExecute(QString *errorMessage=nullptr) const
Returns true if the algorithm can execute.
virtual QString name() const =0
Returns the algorithm name, used for identifying the algorithm.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
Base class for providing feedback from a processing algorithm.
Base class for the definition of processing parameters.
Contains enumerations and other constants for use in processing algorithms and parameters.
Definition: qgsprocessing.h:40
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:63
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
#define SIP_THROW(name)
Definition: qgis_sip.h:189
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76