QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  Q_GADGET
41 
42  public:
43 
47  QgsProcessingModelAlgorithm( const QString &name = QString(), const QString &group = QString(), const QString &groupId = QString() );
48 
49  void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override; //#spellok
50 
51  QString name() const override;
52  QString displayName() const override;
53  QString group() const override;
54  QString groupId() const override;
55  QIcon icon() const override;
56  QString svgIconPath() const override;
57  QString shortHelpString() const override;
58  QString shortDescription() const override;
59  QString helpUrl() const override;
60 
61  bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const override;
62  QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const override;
63  QgsExpressionContext createExpressionContext( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const override;
64 
69  void setName( const QString &name );
70 
75  void setGroup( const QString &group );
76 
84  bool validate( QStringList &issues SIP_OUT ) const;
85 
93  QMap<QString, QgsProcessingModelChildAlgorithm> childAlgorithms() const;
94 
104  void setChildAlgorithms( const QMap<QString, QgsProcessingModelChildAlgorithm> &childAlgorithms );
105 
112  void setChildAlgorithm( const QgsProcessingModelChildAlgorithm &algorithm );
113 
124  QString addChildAlgorithm( QgsProcessingModelChildAlgorithm &algorithm );
125 
132  QgsProcessingModelChildAlgorithm &childAlgorithm( const QString &id );
133 
141  bool removeChildAlgorithm( const QString &id );
142 
150  void deactivateChildAlgorithm( const QString &id );
151 
158  bool activateChildAlgorithm( const QString &id );
159 
170  QSet< QString > dependentChildAlgorithms( const QString &childId, const QString &conditionalBranch = QString() ) const;
171 
177  QSet< QString > dependsOnChildAlgorithms( const QString &childId ) const;
178 
184  QList< QgsProcessingModelChildDependency > availableDependenciesForChildAlgorithm( const QString &childId ) const;
185 
195  bool validateChildAlgorithm( const QString &childId, QStringList &issues SIP_OUT ) const;
196 
203  void addModelParameter( QgsProcessingParameterDefinition *definition SIP_TRANSFER, const QgsProcessingModelParameter &component );
204 
211  void updateModelParameter( QgsProcessingParameterDefinition *definition SIP_TRANSFER );
212 
219  void removeModelParameter( const QString &name );
220 
228  void changeParameterName( const QString &oldName, const QString &newName );
229 
235  bool childAlgorithmsDependOnParameter( const QString &name ) const;
236 
243  bool otherParametersDependOnParameter( const QString &name ) const;
244 
251  QMap<QString, QgsProcessingModelParameter> parameterComponents() const;
252 
261  void setParameterComponents( const QMap<QString, QgsProcessingModelParameter> &parameterComponents );
262 
270  void setParameterComponent( const QgsProcessingModelParameter &component );
271 
279  QgsProcessingModelParameter &parameterComponent( const QString &name );
280 
287  QList< QgsProcessingModelParameter > orderedParameters() const;
288 
298  void setParameterOrder( const QStringList &order );
299 
305  void updateDestinationParameters();
306 
315  void addGroupBox( const QgsProcessingModelGroupBox &groupBox );
316 
323  QList< QgsProcessingModelGroupBox > groupBoxes() const;
324 
332  void removeGroupBox( const QString &uuid );
333 
338  bool toFile( const QString &path ) const;
339 
344  bool fromFile( const QString &path );
345 
353  QVariant toVariant() const;
354 
362  bool loadVariant( const QVariant &variant );
363 
369  QVariantMap &helpContent() { return mHelpContent; }
370 
376  SIP_SKIP QVariantMap helpContent() const;
377 
383  void setHelpContent( const QVariantMap &contents );
384 
389  QString sourceFilePath() const;
390 
395  void setSourceFilePath( const QString &path );
396 
405  bool modelNameMatchesFilePath() const;
406 
414  QStringList asPythonCode( QgsProcessing::PythonOutputType outputType, int indentSize ) const;
415 
424  QList< QgsProcessingModelChildParameterSource > availableSourcesForChild( const QString &childId, const QStringList &parameterTypes = QStringList(),
425  const QStringList &outputTypes = QStringList(), const QList< int > &dataTypes = QList< int >() ) const;
426 
432  class CORE_EXPORT VariableDefinition
433  {
434  public:
435 
440  VariableDefinition( const QVariant &value = QVariant(), const QgsProcessingModelChildParameterSource &source = QgsProcessingModelChildParameterSource::fromStaticValue( QVariant() ), const QString &description = QString() )
441  : value( value )
442  , source( source )
443  , description( description )
444  {}
445 
447  QVariant value;
448 
450  QgsProcessingModelChildParameterSource source;
451 
453  QString description;
454  };
455 
469  QMap< QString, QgsProcessingModelAlgorithm::VariableDefinition > variablesForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
470  const QVariantMap &results = QVariantMap() ) const;
471 
476  QgsExpressionContextScope *createExpressionContextScopeForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
477  const QVariantMap &results = QVariantMap() ) const SIP_FACTORY;
478 
488  QVariantMap variables() const;
489 
499  void setVariables( const QVariantMap &variables );
500 
512  QVariantMap designerParameterValues() const;
513 
525  void setDesignerParameterValues( const QVariantMap &values ) { mDesignerParameterValues = values; }
526 
532  const QgsProcessingParameterDefinition *modelParameterFromChildIdAndOutputName( const QString &childId, const QString &childOutputName ) const;
533 
541  static QString safeName( const QString &name, bool capitalize = false );
542 
543 #ifndef SIP_RUN
544 
546  enum class InternalVersion
547  {
548  Version1,
549  Version2,
550  };
551  Q_ENUM( InternalVersion )
552 
553 #endif
554 
555  protected:
556 
557  QgsProcessingAlgorithm *createInstance() const override SIP_FACTORY;
558 
559  QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );
560 
561  private:
562 
563  InternalVersion mInternalVersion = InternalVersion::Version2;
564 
565  QString mModelName;
566  QString mModelGroup;
567  QString mModelGroupId;
568 
569  QMap< QString, QgsProcessingModelChildAlgorithm > mChildAlgorithms;
570 
572  QMap< QString, QgsProcessingModelParameter > mParameterComponents;
573 
574  QVariantMap mHelpContent;
575 
577  QString mSourceFile;
578 
579  QVariantMap mResults;
580 
581  QVariantMap mVariables;
582 
583  QVariantMap mDesignerParameterValues;
584 
585  QMap< QString, QgsProcessingModelGroupBox > mGroupBoxes;
586 
587  QStringList mParameterOrder;
588 
589  void dependsOnChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;
590  void dependentChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends, const QString &branch ) const;
591 
592  QVariantMap parametersForChildAlgorithm( const QgsProcessingModelChildAlgorithm &child, const QVariantMap &modelParameters, const QVariantMap &results, const QgsExpressionContext &expressionContext, QString &error ) const;
593 
598  bool childOutputIsRequired( const QString &childId, const QString &outputName ) const;
599 
609  static bool vectorOutputIsCompatibleType( const QList<int> &acceptableDataTypes, QgsProcessing::SourceType outputType );
610 
614  void reattachAlgorithms() const;
615 
616  friend class TestQgsProcessingModelAlgorithm;
617 };
618 
620 
621 #endif // QGSPROCESSINGMODELALGORITHM_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsProcessingAlgorithm::shortDescription
virtual QString shortDescription() const
Returns an optional translated short description of the algorithm.
Definition: qgsprocessingalgorithm.cpp:60
QgsProcessingAlgorithm::canExecute
virtual bool canExecute(QString *errorMessage=nullptr) const
Returns true if the algorithm can execute.
Definition: qgsprocessingalgorithm.cpp:95
QgsProcessingAlgorithm::displayName
virtual QString displayName() const =0
Returns the translated algorithm name, which should be used for any user-visible display of the algor...
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
algorithm
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
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
qgis.h
qgsprocessingmodelgroupbox.h
QgsProcessingAlgorithm::groupId
virtual QString groupId() const
Returns the unique ID of the group this algorithm belongs to.
Definition: qgsprocessingalgorithm.h:234
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:334
QgsProcessingAlgorithm::svgIconPath
virtual QString svgIconPath() const
Returns a path to an SVG version of the algorithm's icon.
Definition: qgsprocessingalgorithm.cpp:85
qgsprocessingmodelchilddependency.h
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:198
QgsProcessingAlgorithm::name
virtual QString name() const =0
Returns the algorithm name, used for identifying the algorithm.
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgsprocessingalgorithm.h
qgsprocessingmodelchildalgorithm.h
QgsProcessingAlgorithm::asPythonCommand
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...
Definition: qgsprocessingalgorithm.cpp:290
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsProcessingAlgorithm::shortHelpString
virtual QString shortHelpString() const
Returns a localised short helper string for the algorithm.
Definition: qgsprocessingalgorithm.cpp:65
QgsProcessing
Contains enumerations and other constants for use in processing algorithms and parameters.
Definition: qgsprocessing.h:39
qgsprocessingmodelparameter.h
QgsProcessingAlgorithm::QgsProcessingModelAlgorithm
friend class QgsProcessingModelAlgorithm
Definition: qgsprocessingalgorithm.h:1108
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
QgsProcessingAlgorithm::helpUrl
virtual QString helpUrl() const
Returns a url pointing to the algorithm's help page.
Definition: qgsprocessingalgorithm.cpp:75
QgsProcessingAlgorithm
Abstract base class for processing algorithms.
Definition: qgsprocessingalgorithm.h:52
QgsProcessingAlgorithm::icon
virtual QIcon icon() const
Returns an icon for the algorithm.
Definition: qgsprocessingalgorithm.cpp:80
QgsProcessingFeatureSource
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
Definition: qgsprocessingutils.h:577
QgsProcessingAlgorithm::createExpressionContext
virtual QgsExpressionContext createExpressionContext(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeatureSource *source=nullptr) const
Creates an expression context relating to the algorithm.
Definition: qgsprocessingalgorithm.cpp:158
qgsprocessingmodelchildparametersource.h
QgsProcessingAlgorithm::group
virtual QString group() const
Returns the name of the group this algorithm belongs to.
Definition: qgsprocessingalgorithm.h:225
QgsProcessing::PythonOutputType
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:62
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsProcessingAlgorithm::initAlgorithm
virtual void initAlgorithm(const QVariantMap &configuration=QVariantMap())=0
Initializes the algorithm using the specified configuration.