QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 const QString expression = parameterAsExpression( parameters, u"CONDITION"_s, context );
90 if ( !expression.isEmpty() )
91 {
92 const QgsExpressionContext expContext = createExpressionContext( parameters, context );
93 QgsExpression exp( expression );
94 if ( exp.hasParserError() )
95 {
96 throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
97 }
98 if ( !exp.evaluate( &expContext ).toBool() )
99 return QVariantMap();
100 }
101
102 const QString error = parameterAsString( parameters, u"MESSAGE"_s, context );
103 throw QgsProcessingException( error );
104}
105
106
107//
108// QgsRaiseWarningAlgorithm
109//
110
111QString QgsRaiseWarningAlgorithm::name() const
112{
113 return u"raisewarning"_s;
114}
115
116Qgis::ProcessingAlgorithmFlags QgsRaiseWarningAlgorithm::flags() const
117{
119}
120
121QString QgsRaiseWarningAlgorithm::displayName() const
122{
123 return QObject::tr( "Raise warning" );
124}
125
126QStringList QgsRaiseWarningAlgorithm::tags() const
127{
128 return QObject::tr( "abort,warn,error,cancel" ).split( ',' );
129}
130
131QString QgsRaiseWarningAlgorithm::group() const
132{
133 return QObject::tr( "Modeler tools" );
134}
135
136QString QgsRaiseWarningAlgorithm::groupId() const
137{
138 return u"modelertools"_s;
139}
140
141QString QgsRaiseWarningAlgorithm::shortHelpString() const
142{
143 return QObject::tr(
144 "This algorithm raises a warning message in the log.\n\n"
145 "The warning message can be customized, and optionally an expression based condition "
146 "can be specified. If an expression condition is used, then the warning will only "
147 "be logged if the expression result is true. A false result indicates that no warning "
148 "will be logged."
149 );
150}
151
152QString QgsRaiseWarningAlgorithm::shortDescription() const
153{
154 return QObject::tr( "Raises an warning message." );
155}
156
157QgsRaiseWarningAlgorithm *QgsRaiseWarningAlgorithm::createInstance() const
158{
159 return new QgsRaiseWarningAlgorithm();
160}
161
162void QgsRaiseWarningAlgorithm::initAlgorithm( const QVariantMap & )
163{
164 addParameter( new QgsProcessingParameterString( u"MESSAGE"_s, QObject::tr( "Warning message" ) ) );
165 addParameter( new QgsProcessingParameterExpression( u"CONDITION"_s, QObject::tr( "Condition" ), QVariant(), QString(), true ) );
166}
167
168QVariantMap QgsRaiseWarningAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
169{
170 const QString expression = parameterAsExpression( parameters, u"CONDITION"_s, context );
171 if ( !expression.isEmpty() )
172 {
173 const QgsExpressionContext expContext = createExpressionContext( parameters, context );
174 QgsExpression exp( expression );
175 if ( exp.hasParserError() )
176 {
177 throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
178 }
179 if ( !exp.evaluate( &expContext ).toBool() )
180 return QVariantMap();
181 }
182
183 const QString warning = parameterAsString( parameters, u"MESSAGE"_s, context );
184 feedback->pushWarning( warning );
185 return QVariantMap();
186}
187
188
189//
190// QgsRaiseMessageAlgorithm
191//
192
193QString QgsRaiseMessageAlgorithm::name() const
194{
195 return u"raisemessage"_s;
196}
197
198Qgis::ProcessingAlgorithmFlags QgsRaiseMessageAlgorithm::flags() const
199{
201}
202
203QString QgsRaiseMessageAlgorithm::displayName() const
204{
205 return QObject::tr( "Raise message" );
206}
207
208QStringList QgsRaiseMessageAlgorithm::tags() const
209{
210 return QObject::tr( "information" ).split( ',' );
211}
212
213QString QgsRaiseMessageAlgorithm::group() const
214{
215 return QObject::tr( "Modeler tools" );
216}
217
218QString QgsRaiseMessageAlgorithm::groupId() const
219{
220 return u"modelertools"_s;
221}
222
223QString QgsRaiseMessageAlgorithm::shortHelpString() const
224{
225 return QObject::tr(
226 "This algorithm raises an information message in the log.\n\n"
227 "The message can be customized, and optionally an expression based condition "
228 "can be specified. If an expression condition is used, then the message will only "
229 "be logged if the expression result is true. A false result indicates that no message "
230 "will be logged."
231 );
232}
233
234QString QgsRaiseMessageAlgorithm::shortDescription() const
235{
236 return QObject::tr( "Raises an information message." );
237}
238
239QgsRaiseMessageAlgorithm *QgsRaiseMessageAlgorithm::createInstance() const
240{
241 return new QgsRaiseMessageAlgorithm();
242}
243
244void QgsRaiseMessageAlgorithm::initAlgorithm( const QVariantMap & )
245{
246 addParameter( new QgsProcessingParameterString( u"MESSAGE"_s, QObject::tr( "Information message" ) ) );
247 addParameter( new QgsProcessingParameterExpression( u"CONDITION"_s, QObject::tr( "Condition" ), QVariant(), QString(), true ) );
248}
249
250QVariantMap QgsRaiseMessageAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
251{
252 const QString expression = parameterAsExpression( parameters, u"CONDITION"_s, context );
253 if ( !expression.isEmpty() )
254 {
255 const QgsExpressionContext expContext = createExpressionContext( parameters, context );
256 QgsExpression exp( expression );
257 if ( exp.hasParserError() )
258 {
259 throw QgsProcessingException( QObject::tr( "Error parsing condition expression: %1" ).arg( exp.parserErrorString() ) );
260 }
261 if ( !exp.evaluate( &expContext ).toBool() )
262 return QVariantMap();
263 }
264
265 const QString info = parameterAsString( parameters, u"MESSAGE"_s, context );
266 feedback->pushInfo( info );
267 return QVariantMap();
268}
269
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3724
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
Definition qgis.h:3698
@ SkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
Definition qgis.h:3709
@ CustomException
Algorithm raises custom exception notices, don't use the standard ones.
Definition qgis.h:3707
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.