QGIS API Documentation 3.99.0-Master (357b655ed83)
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
20#include "qgsapplication.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsConditionalBranchAlgorithm::name() const
29{
30 return u"condition"_s;
31}
32
33QString QgsConditionalBranchAlgorithm::displayName() const
34{
35 return QObject::tr( "Conditional branch" );
36}
37
38QStringList QgsConditionalBranchAlgorithm::tags() const
39{
40 return QObject::tr( "if,logic,test" ).split( ',' );
41}
42
43QString QgsConditionalBranchAlgorithm::group() const
44{
45 return QObject::tr( "Modeler tools" );
46}
47
48QString QgsConditionalBranchAlgorithm::groupId() const
49{
50 return u"modelertools"_s;
51}
52
53Qgis::ProcessingAlgorithmFlags QgsConditionalBranchAlgorithm::flags() const
54{
56}
57
58QString QgsConditionalBranchAlgorithm::shortHelpString() const
59{
60 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." );
61}
62
63QString QgsConditionalBranchAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Adds a conditional branch into a model, allowing parts of the model to be selectively executed." );
66}
67
68QgsConditionalBranchAlgorithm *QgsConditionalBranchAlgorithm::createInstance() const
69{
70 return new QgsConditionalBranchAlgorithm();
71}
72
73QgsConditionalBranchAlgorithm::~QgsConditionalBranchAlgorithm()
74{
75 qDeleteAll( mOutputs );
76}
77
78void QgsConditionalBranchAlgorithm::initAlgorithm( const QVariantMap &configuration )
79{
80 const QVariantList branches = configuration.value( u"conditions"_s ).toList();
81 for ( const QVariant &branch : branches )
82 {
83 const QVariantMap branchDef = branch.toMap();
84 const QString name = branchDef.value( u"name"_s ).toString();
85 mOutputs.append( new Output( name, branchDef.value( u"expression"_s ).toString() ) );
86 addOutput( new QgsProcessingOutputConditionalBranch( name, name ) );
87 }
88}
89
90QVariantMap QgsConditionalBranchAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
91{
92 QgsExpressionContext expressionContext = createExpressionContext( parameters, context );
93
94 QVariantMap results;
95 for ( Output *output : std::as_const( mOutputs ) )
96 {
97 output->expression.prepare( &expressionContext );
98 const QVariant res = output->expression.evaluate( &expressionContext );
99 results.insert( output->name, res );
100 if ( res.toBool() )
101 {
102 feedback->pushInfo( QObject::tr( "Condition %1 passed" ).arg( output->name ) );
103 }
104 else
105 {
106 feedback->pushInfo( QObject::tr( "Condition %1 failed" ).arg( output->name ) );
107 }
108 }
109 qDeleteAll( mOutputs );
110 mOutputs.clear();
111 return results;
112}
113
114
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3680
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
Definition qgis.h:3654
@ SkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
Definition qgis.h:3665
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...