QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsalgorithmconditionalbranch.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmconditionalbranch.cpp
3 ---------------------
4 begin : March 2020
5 copyright : (C) 2020 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
19#include "qgsapplication.h"
20
22
23QString QgsConditionalBranchAlgorithm::name() const
24{
25 return QStringLiteral( "condition" );
26}
27
28QString QgsConditionalBranchAlgorithm::displayName() const
29{
30 return QObject::tr( "Conditional branch" );
31}
32
33QStringList QgsConditionalBranchAlgorithm::tags() const
34{
35 return QObject::tr( "if,logic,test" ).split( ',' );
36}
37
38QString QgsConditionalBranchAlgorithm::group() const
39{
40 return QObject::tr( "Modeler tools" );
41}
42
43QString QgsConditionalBranchAlgorithm::groupId() const
44{
45 return QStringLiteral( "modelertools" );
46}
47
48Qgis::ProcessingAlgorithmFlags QgsConditionalBranchAlgorithm::flags() const
49{
51}
52
53QString QgsConditionalBranchAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm adds a conditional branch into a model, allowing parts of the model to be executed based on the result of an expression evaluation." );
56}
57
58QString QgsConditionalBranchAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Adds a conditional branch into a model, allowing parts of the model to be selectively executed." );
61}
62
63QgsConditionalBranchAlgorithm *QgsConditionalBranchAlgorithm::createInstance() const
64{
65 return new QgsConditionalBranchAlgorithm();
66}
67
68QgsConditionalBranchAlgorithm::~QgsConditionalBranchAlgorithm()
69{
70 qDeleteAll( mOutputs );
71}
72
73void QgsConditionalBranchAlgorithm::initAlgorithm( const QVariantMap &configuration )
74{
75 const QVariantList branches = configuration.value( QStringLiteral( "conditions" ) ).toList();
76 for ( const QVariant &branch : branches )
77 {
78 const QVariantMap branchDef = branch.toMap();
79 const QString name = branchDef.value( QStringLiteral( "name" ) ).toString();
80 mOutputs.append( new Output( name, branchDef.value( QStringLiteral( "expression" ) ).toString() ) );
81 addOutput( new QgsProcessingOutputConditionalBranch( name, name ) );
82 }
83}
84
85QVariantMap QgsConditionalBranchAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
86{
87 QgsExpressionContext expressionContext = createExpressionContext( parameters, context );
88
89 QVariantMap results;
90 for ( Output *output : std::as_const( mOutputs ) )
91 {
92 output->expression.prepare( &expressionContext );
93 const QVariant res = output->expression.evaluate( &expressionContext );
94 results.insert( output->name, res );
95 if ( res.toBool() )
96 {
97 feedback->pushInfo( QObject::tr( "Condition %1 passed" ).arg( output->name ) );
98 }
99 else
100 {
101 feedback->pushInfo( QObject::tr( "Condition %1 failed" ).arg( output->name ) );
102 }
103 }
104 qDeleteAll( mOutputs );
105 mOutputs.clear();
106 return results;
107}
108
109
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3339
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
@ SkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A conditional branch output for processing algorithms, which represents a possible model logic flow w...