QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26//
27// QgsRaiseExceptionAlgorithm
28//
29
30QString QgsRaiseExceptionAlgorithm::name() const
31{
32 return u"raiseexception"_s;
33}
34
35Qgis::ProcessingAlgorithmFlags QgsRaiseExceptionAlgorithm::flags() const
36{
38}
39
40QString QgsRaiseExceptionAlgorithm::displayName() const
41{
42 return QObject::tr( "Raise exception" );
43}
44
45QStringList QgsRaiseExceptionAlgorithm::tags() const
46{
47 return QObject::tr( "abort,warn,error,cancel" ).split( ',' );
48}
49
50QString QgsRaiseExceptionAlgorithm::group() const
51{
52 return QObject::tr( "Modeler tools" );
53}
54
55QString QgsRaiseExceptionAlgorithm::groupId() const
56{
57 return u"modelertools"_s;
58}
59
60QString QgsRaiseExceptionAlgorithm::shortHelpString() const
61{
62 return QObject::tr(
63 "This algorithm raises an exception and cancels a model's execution.\n\n"
64 "The exception message can be customized, and optionally an expression based condition "
65 "can be specified. If an expression condition is used, then the exception will only "
66 "be raised if the expression result is true. A false result indicates that no exception "
67 "will be raised, and the model execution can continue uninterrupted."
68 );
69}
70
71QString QgsRaiseExceptionAlgorithm::shortDescription() const
72{
73 return QObject::tr( "Raises an exception and cancels a model's execution." );
74}
75
76QgsRaiseExceptionAlgorithm *QgsRaiseExceptionAlgorithm::createInstance() const
77{
78 return new QgsRaiseExceptionAlgorithm();
79}
80
81void QgsRaiseExceptionAlgorithm::initAlgorithm( const QVariantMap & )
82{
83 addParameter( new QgsProcessingParameterString( u"MESSAGE"_s, QObject::tr( "Error message" ) ) );
84 addParameter( new QgsProcessingParameterExpression( u"CONDITION"_s, QObject::tr( "Condition" ), QVariant(), QString(), true ) );
85}
86
87QVariantMap QgsRaiseExceptionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
88{
89 QGS_MARK_ALGORITHM_SOURCE
90
91 const QString expression = parameterAsExpression( parameters, u"CONDITION"_s, context );
92 if ( !expression.isEmpty() )
93 {
94 const QgsExpressionContext expContext = createExpressionContext( parameters, context );
95 QgsExpression exp( expression );
96 if ( exp.hasParserError() )
97 {
98 throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
99 }
100 if ( !exp.evaluate( &expContext ).toBool() )
101 return QVariantMap();
102 }
103
104 const QString error = parameterAsString( parameters, u"MESSAGE"_s, context );
105 throw QgsProcessingException( error );
106}
107
108
109//
110// QgsRaiseWarningAlgorithm
111//
112
113QString QgsRaiseWarningAlgorithm::name() const
114{
115 return u"raisewarning"_s;
116}
117
118Qgis::ProcessingAlgorithmFlags QgsRaiseWarningAlgorithm::flags() const
119{
121}
122
123QString QgsRaiseWarningAlgorithm::displayName() const
124{
125 return QObject::tr( "Raise warning" );
126}
127
128QStringList QgsRaiseWarningAlgorithm::tags() const
129{
130 return QObject::tr( "abort,warn,error,cancel" ).split( ',' );
131}
132
133QString QgsRaiseWarningAlgorithm::group() const
134{
135 return QObject::tr( "Modeler tools" );
136}
137
138QString QgsRaiseWarningAlgorithm::groupId() const
139{
140 return u"modelertools"_s;
141}
142
143QString QgsRaiseWarningAlgorithm::shortHelpString() const
144{
145 return QObject::tr(
146 "This algorithm raises a warning message in the log.\n\n"
147 "The warning message can be customized, and optionally an expression based condition "
148 "can be specified. If an expression condition is used, then the warning will only "
149 "be logged if the expression result is true. A false result indicates that no warning "
150 "will be logged."
151 );
152}
153
154QString QgsRaiseWarningAlgorithm::shortDescription() const
155{
156 return QObject::tr( "Raises an warning message." );
157}
158
159QgsRaiseWarningAlgorithm *QgsRaiseWarningAlgorithm::createInstance() const
160{
161 return new QgsRaiseWarningAlgorithm();
162}
163
164void QgsRaiseWarningAlgorithm::initAlgorithm( const QVariantMap & )
165{
166 addParameter( new QgsProcessingParameterString( u"MESSAGE"_s, QObject::tr( "Warning message" ) ) );
167 addParameter( new QgsProcessingParameterExpression( u"CONDITION"_s, QObject::tr( "Condition" ), QVariant(), QString(), true ) );
168}
169
170QVariantMap QgsRaiseWarningAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
171{
172 const QString expression = parameterAsExpression( parameters, u"CONDITION"_s, context );
173 if ( !expression.isEmpty() )
174 {
175 const QgsExpressionContext expContext = createExpressionContext( parameters, context );
176 QgsExpression exp( expression );
177 if ( exp.hasParserError() )
178 {
179 throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
180 }
181 if ( !exp.evaluate( &expContext ).toBool() )
182 return QVariantMap();
183 }
184
185 const QString warning = parameterAsString( parameters, u"MESSAGE"_s, context );
186 feedback->pushWarning( warning );
187 return QVariantMap();
188}
189
190
191//
192// QgsRaiseMessageAlgorithm
193//
194
195QString QgsRaiseMessageAlgorithm::name() const
196{
197 return u"raisemessage"_s;
198}
199
200Qgis::ProcessingAlgorithmFlags QgsRaiseMessageAlgorithm::flags() const
201{
203}
204
205QString QgsRaiseMessageAlgorithm::displayName() const
206{
207 return QObject::tr( "Raise message" );
208}
209
210QStringList QgsRaiseMessageAlgorithm::tags() const
211{
212 return QObject::tr( "information" ).split( ',' );
213}
214
215QString QgsRaiseMessageAlgorithm::group() const
216{
217 return QObject::tr( "Modeler tools" );
218}
219
220QString QgsRaiseMessageAlgorithm::groupId() const
221{
222 return u"modelertools"_s;
223}
224
225QString QgsRaiseMessageAlgorithm::shortHelpString() const
226{
227 return QObject::tr(
228 "This algorithm raises an information message in the log.\n\n"
229 "The message can be customized, and optionally an expression based condition "
230 "can be specified. If an expression condition is used, then the message will only "
231 "be logged if the expression result is true. A false result indicates that no message "
232 "will be logged."
233 );
234}
235
236QString QgsRaiseMessageAlgorithm::shortDescription() const
237{
238 return QObject::tr( "Raises an information message." );
239}
240
241QgsRaiseMessageAlgorithm *QgsRaiseMessageAlgorithm::createInstance() const
242{
243 return new QgsRaiseMessageAlgorithm();
244}
245
246void QgsRaiseMessageAlgorithm::initAlgorithm( const QVariantMap & )
247{
248 addParameter( new QgsProcessingParameterString( u"MESSAGE"_s, QObject::tr( "Information message" ) ) );
249 addParameter( new QgsProcessingParameterExpression( u"CONDITION"_s, QObject::tr( "Condition" ), QVariant(), QString(), true ) );
250}
251
252QVariantMap QgsRaiseMessageAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
253{
254 const QString expression = parameterAsExpression( parameters, u"CONDITION"_s, context );
255 if ( !expression.isEmpty() )
256 {
257 const QgsExpressionContext expContext = createExpressionContext( parameters, context );
258 QgsExpression exp( expression );
259 if ( exp.hasParserError() )
260 {
261 throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
262 }
263 if ( !exp.evaluate( &expContext ).toBool() )
264 return QVariantMap();
265 }
266
267 const QString info = parameterAsString( parameters, u"MESSAGE"_s, context );
268 feedback->pushInfo( info );
269 return QVariantMap();
270}
271
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3826
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
Definition qgis.h:3800
@ SkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
Definition qgis.h:3811
@ CustomException
Algorithm raises custom exception notices, don't use the standard ones.
Definition qgis.h:3809
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Handles 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.