QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsalgorithmraiseexception.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmraiseexception.cpp
3  ---------------------
4  begin : February 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 
21 
22 //
23 // QgsRaiseExceptionAlgorithm
24 //
25 
26 QString QgsRaiseExceptionAlgorithm::name() const
27 {
28  return QStringLiteral( "raiseexception" );
29 }
30 
31 QgsProcessingAlgorithm::Flags QgsRaiseExceptionAlgorithm::flags() const
32 {
33  return QgsProcessingAlgorithm::flags() | FlagHideFromToolbox | FlagCustomException | FlagSkipGenericModelLogging;
34 }
35 
36 QString QgsRaiseExceptionAlgorithm::displayName() const
37 {
38  return QObject::tr( "Raise exception" );
39 }
40 
41 QStringList QgsRaiseExceptionAlgorithm::tags() const
42 {
43  return QObject::tr( "abort,warn,error,cancel" ).split( ',' );
44 }
45 
46 QString QgsRaiseExceptionAlgorithm::group() const
47 {
48  return QObject::tr( "Modeler tools" );
49 }
50 
51 QString QgsRaiseExceptionAlgorithm::groupId() const
52 {
53  return QStringLiteral( "modelertools" );
54 }
55 
56 QString QgsRaiseExceptionAlgorithm::shortHelpString() const
57 {
58  return QObject::tr( "This algorithm raises an exception and cancels a model's execution.\n\n"
59  "The exception message can be customized, and optionally an expression based condition "
60  "can be specified. If an expression condition is used, then the exception will only "
61  "be raised if the expression result is true. A false result indicates that no exception "
62  "will be raised, and the model execution can continue uninterrupted." );
63 }
64 
65 QString QgsRaiseExceptionAlgorithm::shortDescription() const
66 {
67  return QObject::tr( "Raises an exception and cancels a model's execution." );
68 }
69 
70 QgsRaiseExceptionAlgorithm *QgsRaiseExceptionAlgorithm::createInstance() const
71 {
72  return new QgsRaiseExceptionAlgorithm();
73 }
74 
75 void QgsRaiseExceptionAlgorithm::initAlgorithm( const QVariantMap & )
76 {
77  addParameter( new QgsProcessingParameterString( QStringLiteral( "MESSAGE" ), QObject::tr( "Error message" ) ) );
78  addParameter( new QgsProcessingParameterExpression( QStringLiteral( "CONDITION" ), QObject::tr( "Condition" ), QVariant(), QString(), true ) );
79 }
80 
81 QVariantMap QgsRaiseExceptionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
82 {
83  const QString expression = parameterAsExpression( parameters, QStringLiteral( "CONDITION" ), context );
84  if ( !expression.isEmpty() )
85  {
86  QgsExpressionContext expContext = createExpressionContext( parameters, context );
87  QgsExpression exp( expression );
88  if ( exp.hasParserError() )
89  {
90  throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
91  }
92  if ( !exp.evaluate( &expContext ).toBool() )
93  return QVariantMap();
94  }
95 
96  const QString error = parameterAsString( parameters, QStringLiteral( "MESSAGE" ), context );
97  throw QgsProcessingException( error );
98 }
99 
100 
101 //
102 // QgsRaiseWarningAlgorithm
103 //
104 
105 QString QgsRaiseWarningAlgorithm::name() const
106 {
107  return QStringLiteral( "raisewarning" );
108 }
109 
110 QgsProcessingAlgorithm::Flags QgsRaiseWarningAlgorithm::flags() const
111 {
112  return QgsProcessingAlgorithm::flags() | FlagHideFromToolbox | FlagSkipGenericModelLogging;
113 }
114 
115 QString QgsRaiseWarningAlgorithm::displayName() const
116 {
117  return QObject::tr( "Raise warning" );
118 }
119 
120 QStringList QgsRaiseWarningAlgorithm::tags() const
121 {
122  return QObject::tr( "abort,warn,error,cancel" ).split( ',' );
123 }
124 
125 QString QgsRaiseWarningAlgorithm::group() const
126 {
127  return QObject::tr( "Modeler tools" );
128 }
129 
130 QString QgsRaiseWarningAlgorithm::groupId() const
131 {
132  return QStringLiteral( "modelertools" );
133 }
134 
135 QString QgsRaiseWarningAlgorithm::shortHelpString() const
136 {
137  return QObject::tr( "This algorithm raises a warning message in the log.\n\n"
138  "The warning message can be customized, and optionally an expression based condition "
139  "can be specified. If an expression condition is used, then the warning will only "
140  "be logged if the expression result is true. A false result indicates that no warning "
141  "will be logged." );
142 }
143 
144 QString QgsRaiseWarningAlgorithm::shortDescription() const
145 {
146  return QObject::tr( "Raises an warning message." );
147 }
148 
149 QgsRaiseWarningAlgorithm *QgsRaiseWarningAlgorithm::createInstance() const
150 {
151  return new QgsRaiseWarningAlgorithm();
152 }
153 
154 void QgsRaiseWarningAlgorithm::initAlgorithm( const QVariantMap & )
155 {
156  addParameter( new QgsProcessingParameterString( QStringLiteral( "MESSAGE" ), QObject::tr( "Warning message" ) ) );
157  addParameter( new QgsProcessingParameterExpression( QStringLiteral( "CONDITION" ), QObject::tr( "Condition" ), QVariant(), QString(), true ) );
158 }
159 
160 QVariantMap QgsRaiseWarningAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
161 {
162  const QString expression = parameterAsExpression( parameters, QStringLiteral( "CONDITION" ), context );
163  if ( !expression.isEmpty() )
164  {
165  QgsExpressionContext expContext = createExpressionContext( parameters, context );
166  QgsExpression exp( expression );
167  if ( exp.hasParserError() )
168  {
169  throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
170  }
171  if ( !exp.evaluate( &expContext ).toBool() )
172  return QVariantMap();
173  }
174 
175  const QString warning = parameterAsString( parameters, QStringLiteral( "MESSAGE" ), context );
176  feedback->reportError( warning );
177  return QVariantMap();
178 }
179 
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
An expression parameter for processing algorithms.
A string parameter for processing algorithms.