QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
23 QString QgsConditionalBranchAlgorithm::name() const
24 {
25  return QStringLiteral( "condition" );
26 }
27 
28 QString QgsConditionalBranchAlgorithm::displayName() const
29 {
30  return QObject::tr( "Conditional branch" );
31 }
32 
33 QStringList QgsConditionalBranchAlgorithm::tags() const
34 {
35  return QObject::tr( "if,logic,test" ).split( ',' );
36 }
37 
38 QString QgsConditionalBranchAlgorithm::group() const
39 {
40  return QObject::tr( "Modeler tools" );
41 }
42 
43 QString QgsConditionalBranchAlgorithm::groupId() const
44 {
45  return QStringLiteral( "modelertools" );
46 }
47 
48 QgsProcessingAlgorithm::Flags QgsConditionalBranchAlgorithm::flags() const
49 {
50  return FlagHideFromToolbox | FlagSkipGenericModelLogging;
51 }
52 
53 QString 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 
58 QString 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 
63 QgsConditionalBranchAlgorithm *QgsConditionalBranchAlgorithm::createInstance() const
64 {
65  return new QgsConditionalBranchAlgorithm();
66 }
67 
68 QgsConditionalBranchAlgorithm::~QgsConditionalBranchAlgorithm()
69 {
70  qDeleteAll( mOutputs );
71 }
72 
73 void 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 
85 QVariantMap 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 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:77
qgsalgorithmconditionalbranch.h
qgsapplication.h
QgsProcessingOutputConditionalBranch
A conditional branch output for processing algorithms, which represents a possible model logic flow w...
Definition: qgsprocessingoutputs.h:424
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46