QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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
26QString QgsRaiseExceptionAlgorithm::name() const
27{
28 return QStringLiteral( "raiseexception" );
29}
30
31Qgis::ProcessingAlgorithmFlags QgsRaiseExceptionAlgorithm::flags() const
32{
34}
35
36QString QgsRaiseExceptionAlgorithm::displayName() const
37{
38 return QObject::tr( "Raise exception" );
39}
40
41QStringList QgsRaiseExceptionAlgorithm::tags() const
42{
43 return QObject::tr( "abort,warn,error,cancel" ).split( ',' );
44}
45
46QString QgsRaiseExceptionAlgorithm::group() const
47{
48 return QObject::tr( "Modeler tools" );
49}
50
51QString QgsRaiseExceptionAlgorithm::groupId() const
52{
53 return QStringLiteral( "modelertools" );
54}
55
56QString 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
65QString QgsRaiseExceptionAlgorithm::shortDescription() const
66{
67 return QObject::tr( "Raises an exception and cancels a model's execution." );
68}
69
70QgsRaiseExceptionAlgorithm *QgsRaiseExceptionAlgorithm::createInstance() const
71{
72 return new QgsRaiseExceptionAlgorithm();
73}
74
75void 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
81QVariantMap 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
105QString QgsRaiseWarningAlgorithm::name() const
106{
107 return QStringLiteral( "raisewarning" );
108}
109
110Qgis::ProcessingAlgorithmFlags QgsRaiseWarningAlgorithm::flags() const
111{
113}
114
115QString QgsRaiseWarningAlgorithm::displayName() const
116{
117 return QObject::tr( "Raise warning" );
118}
119
120QStringList QgsRaiseWarningAlgorithm::tags() const
121{
122 return QObject::tr( "abort,warn,error,cancel" ).split( ',' );
123}
124
125QString QgsRaiseWarningAlgorithm::group() const
126{
127 return QObject::tr( "Modeler tools" );
128}
129
130QString QgsRaiseWarningAlgorithm::groupId() const
131{
132 return QStringLiteral( "modelertools" );
133}
134
135QString 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
144QString QgsRaiseWarningAlgorithm::shortDescription() const
145{
146 return QObject::tr( "Raises an warning message." );
147}
148
149QgsRaiseWarningAlgorithm *QgsRaiseWarningAlgorithm::createInstance() const
150{
151 return new QgsRaiseWarningAlgorithm();
152}
153
154void 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
160QVariantMap 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
185QString QgsRaiseMessageAlgorithm::name() const
186{
187 return QStringLiteral( "raisemessage" );
188}
189
190Qgis::ProcessingAlgorithmFlags QgsRaiseMessageAlgorithm::flags() const
191{
193}
194
195QString QgsRaiseMessageAlgorithm::displayName() const
196{
197 return QObject::tr( "Raise message" );
198}
199
200QStringList QgsRaiseMessageAlgorithm::tags() const
201{
202 return QObject::tr( "information" ).split( ',' );
203}
204
205QString QgsRaiseMessageAlgorithm::group() const
206{
207 return QObject::tr( "Modeler tools" );
208}
209
210QString QgsRaiseMessageAlgorithm::groupId() const
211{
212 return QStringLiteral( "modelertools" );
213}
214
215QString 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
224QString QgsRaiseMessageAlgorithm::shortDescription() const
225{
226 return QObject::tr( "Raises an information message." );
227}
228
229QgsRaiseMessageAlgorithm *QgsRaiseMessageAlgorithm::createInstance() const
230{
231 return new QgsRaiseMessageAlgorithm();
232}
233
234void 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
240QVariantMap 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
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3339
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
@ SkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
@ CustomException
Algorithm raises custom exception notices, don't use the standard ones.
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 Qgis::ProcessingAlgorithmFlags 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.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
An expression parameter for processing algorithms.
A string parameter for processing algorithms.