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