QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  const 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  const 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->pushWarning( warning );
177  return QVariantMap();
178 }
179 
180 
181 //
182 // QgsRaiseMessageAlgorithm
183 //
184 
185 QString QgsRaiseMessageAlgorithm::name() const
186 {
187  return QStringLiteral( "raisemessage" );
188 }
189 
190 QgsProcessingAlgorithm::Flags QgsRaiseMessageAlgorithm::flags() const
191 {
192  return QgsProcessingAlgorithm::flags() | FlagHideFromToolbox | FlagSkipGenericModelLogging;
193 }
194 
195 QString QgsRaiseMessageAlgorithm::displayName() const
196 {
197  return QObject::tr( "Raise message" );
198 }
199 
200 QStringList QgsRaiseMessageAlgorithm::tags() const
201 {
202  return QObject::tr( "information" ).split( ',' );
203 }
204 
205 QString QgsRaiseMessageAlgorithm::group() const
206 {
207  return QObject::tr( "Modeler tools" );
208 }
209 
210 QString QgsRaiseMessageAlgorithm::groupId() const
211 {
212  return QStringLiteral( "modelertools" );
213 }
214 
215 QString QgsRaiseMessageAlgorithm::shortHelpString() const
216 {
217  return QObject::tr( "This algorithm raises an information message in the log.\n\n"
218  "The message can be customized, and optionally an expression based condition "
219  "can be specified. If an expression condition is used, then the message will only "
220  "be logged if the expression result is true. A false result indicates that no message "
221  "will be logged." );
222 }
223 
224 QString QgsRaiseMessageAlgorithm::shortDescription() const
225 {
226  return QObject::tr( "Raises an information message." );
227 }
228 
229 QgsRaiseMessageAlgorithm *QgsRaiseMessageAlgorithm::createInstance() const
230 {
231  return new QgsRaiseMessageAlgorithm();
232 }
233 
234 void QgsRaiseMessageAlgorithm::initAlgorithm( const QVariantMap & )
235 {
236  addParameter( new QgsProcessingParameterString( QStringLiteral( "MESSAGE" ), QObject::tr( "Information message" ) ) );
237  addParameter( new QgsProcessingParameterExpression( QStringLiteral( "CONDITION" ), QObject::tr( "Condition" ), QVariant(), QString(), true ) );
238 }
239 
240 QVariantMap QgsRaiseMessageAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
241 {
242  const QString expression = parameterAsExpression( parameters, QStringLiteral( "CONDITION" ), context );
243  if ( !expression.isEmpty() )
244  {
245  const QgsExpressionContext expContext = createExpressionContext( parameters, context );
246  QgsExpression exp( expression );
247  if ( exp.hasParserError() )
248  {
249  throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
250  }
251  if ( !exp.evaluate( &expContext ).toBool() )
252  return QVariantMap();
253  }
254 
255  const QString info = parameterAsString( parameters, QStringLiteral( "MESSAGE" ), context );
256  feedback->pushInfo( info );
257  return QVariantMap();
258 }
259 
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
qgsalgorithmraiseexception.h
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingParameterString
A string parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2647
QgsProcessingParameterExpression
An expression parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2739
QgsProcessingAlgorithm::flags
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Definition: qgsprocessingalgorithm.cpp:90
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsProcessingFeedback::pushWarning
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:68