QGIS API Documentation 4.3.0-Master (45633be667c)
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 QGS_MARK_ALGORITHM_SOURCE
173
174 const QString expression = parameterAsExpression( parameters, u"CONDITION"_s, context );
175 if ( !expression.isEmpty() )
176 {
177 const QgsExpressionContext expContext = createExpressionContext( parameters, context );
178 QgsExpression exp( expression );
179 if ( exp.hasParserError() )
180 {
181 throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
182 }
183 if ( !exp.evaluate( &expContext ).toBool() )
184 return QVariantMap();
185 }
186
187 const QString warning = parameterAsString( parameters, u"MESSAGE"_s, context );
188 feedback->pushWarning( warning );
189 return QVariantMap();
190}
191
192
193//
194// QgsRaiseMessageAlgorithm
195//
196
197QString QgsRaiseMessageAlgorithm::name() const
198{
199 return u"raisemessage"_s;
200}
201
202Qgis::ProcessingAlgorithmFlags QgsRaiseMessageAlgorithm::flags() const
203{
205}
206
207QString QgsRaiseMessageAlgorithm::displayName() const
208{
209 return QObject::tr( "Raise message" );
210}
211
212QStringList QgsRaiseMessageAlgorithm::tags() const
213{
214 return QObject::tr( "information" ).split( ',' );
215}
216
217QString QgsRaiseMessageAlgorithm::group() const
218{
219 return QObject::tr( "Modeler tools" );
220}
221
222QString QgsRaiseMessageAlgorithm::groupId() const
223{
224 return u"modelertools"_s;
225}
226
227QString QgsRaiseMessageAlgorithm::shortHelpString() const
228{
229 return QObject::tr(
230 "This algorithm raises an information message in the log.\n\n"
231 "The message can be customized, and optionally an expression based condition "
232 "can be specified. If an expression condition is used, then the message will only "
233 "be logged if the expression result is true. A false result indicates that no message "
234 "will be logged."
235 );
236}
237
238QString QgsRaiseMessageAlgorithm::shortDescription() const
239{
240 return QObject::tr( "Raises an information message." );
241}
242
243QgsRaiseMessageAlgorithm *QgsRaiseMessageAlgorithm::createInstance() const
244{
245 return new QgsRaiseMessageAlgorithm();
246}
247
248void QgsRaiseMessageAlgorithm::initAlgorithm( const QVariantMap & )
249{
250 addParameter( new QgsProcessingParameterString( u"MESSAGE"_s, QObject::tr( "Information message" ) ) );
251 addParameter( new QgsProcessingParameterExpression( u"CONDITION"_s, QObject::tr( "Condition" ), QVariant(), QString(), true ) );
252}
253
254QVariantMap QgsRaiseMessageAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
255{
256 QGS_MARK_ALGORITHM_SOURCE
257
258 const QString expression = parameterAsExpression( parameters, u"CONDITION"_s, context );
259 if ( !expression.isEmpty() )
260 {
261 const QgsExpressionContext expContext = createExpressionContext( parameters, context );
262 QgsExpression exp( expression );
263 if ( exp.hasParserError() )
264 {
265 throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
266 }
267 if ( !exp.evaluate( &expContext ).toBool() )
268 return QVariantMap();
269 }
270
271 const QString info = parameterAsString( parameters, u"MESSAGE"_s, context );
272 feedback->pushInfo( info );
273 return QVariantMap();
274}
275
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3828
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
Definition qgis.h:3802
@ SkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
Definition qgis.h:3813
@ CustomException
Algorithm raises custom exception notices, don't use the standard ones.
Definition qgis.h:3811
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.