-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsprocessingalgorithm.h
305 lines (252 loc) · 11.6 KB
/
qgsprocessingalgorithm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/***************************************************************************
qgsprocessingalgorithm.h
------------------------
begin : December 2016
copyright : (C) 2016 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSPROCESSINGALGORITHM_H
#define QGSPROCESSINGALGORITHM_H
#include "qgis_core.h"
#include "qgis.h"
#include "qgsprocessingparameters.h"
#include <QString>
#include <QVariant>
#include <QIcon>
class QgsProcessingProvider;
class QgsProcessingContext;
class QgsProcessingFeedback;
/**
* \class QgsProcessingAlgorithm
* \ingroup core
* Abstract base class for processing algorithms.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingAlgorithm
{
public:
//! Flags indicating how and when an algorithm operates and should be exposed to users
enum Flag
{
FlagHideFromToolbox = 1 << 1, //!< Algorithm should be hidden from the toolbox
FlagHideFromModeler = 1 << 2, //!< Algorithm should be hidden from the modeler
FlagSupportsBatch = 1 << 3, //!< Algorithm supports batch mode
FlagDeprecated = FlagHideFromToolbox | FlagHideFromModeler, //!< Algorithm is deprecated
};
Q_DECLARE_FLAGS( Flags, Flag )
/**
* Constructor for QgsProcessingAlgorithm.
*/
QgsProcessingAlgorithm() = default;
virtual ~QgsProcessingAlgorithm();
//! Algorithms cannot be copied
QgsProcessingAlgorithm( const QgsProcessingAlgorithm &other ) = delete;
//! Algorithms cannot be copied
QgsProcessingAlgorithm &operator=( const QgsProcessingAlgorithm &other ) = delete;
/**
* Returns the algorithm name, used for identifying the algorithm. This string
* should be fixed for the algorithm, and must not be localised. The name should
* be unique within each provider. Names should contain lowercase alphanumeric characters
* only and no spaces or other formatting characters.
* \see displayName()
* \see group()
* \see tags()
*/
virtual QString name() const = 0;
/**
* Returns the unique ID for the algorithm, which is a combination of the algorithm
* provider's ID and the algorithms unique name (e.g. "qgis:mergelayers" ).
* \see name()
* \see provider()
*/
QString id() const;
/**
* Returns the translated algorithm name, which should be used for any user-visible display
* of the algorithm name.
* \see name()
*/
virtual QString displayName() const = 0;
/**
* Returns a list of tags which relate to the algorithm, and are used to assist users in searching
* for suitable algorithms. These tags should be localised.
*/
virtual QStringList tags() const { return QStringList(); }
/**
* Returns an icon for the algorithm.
* \see svgIconPath()
*/
virtual QIcon icon() const;
/**
* Returns a path to an SVG version of the algorithm's icon.
* \see icon()
*/
virtual QString svgIconPath() const;
/**
* Returns the name of the group this algorithm belongs to. This string
* should be localised.
* \see tags()
*/
virtual QString group() const { return QString(); }
/**
* Returns the flags indicating how and when the algorithm operates and should be exposed to users.
*/
virtual Flags flags() const;
/**
* Returns true if the algorithm can execute. Algorithm subclasses can return false
* here to indicate that they are not able to execute, e.g. as a result of unmet
* external dependencies. If specified, the \a errorMessage argument will be filled
* with a localised error message describing why the algorithm cannot execute.
*/
virtual bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const;
/**
* Returns the provider to which this algorithm belongs.
*/
QgsProcessingProvider *provider() const;
/**
* Returns an ordered list of parameter definitions utilized by the algorithm.
* \see addParameter()
* \see parameterDefinition()
*/
QgsProcessingParameterDefinitions parameterDefinitions() const { return mParameters; }
/**
* Returns a matching parameter by \a name. Matching is done in a case-insensitive
* manner.
* \see parameterDefinitions()
*/
const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;
/**
* Returns the number of visible (non-hidden) parameters defined by this
* algorithm.
*/
int countVisibleParameters() const;
/**
* Runs the algorithm using the specified \a parameters. Algorithms should implement
* their custom processing logic here.
*
* The \a context argument specifies the context in which the algorithm is being run.
*
* Algorithm progress should be reported using the supplied \a feedback object. Additionally,
* well-behaved algorithms should periodically check \a feedback to determine whether the
* algorithm should be canceled and exited early.
*
* \returns A map of algorithm outputs. These may be output layer references, or calculated
* values such as statistical calculations.
*/
virtual QVariantMap run( const QVariantMap ¶meters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
protected:
/**
* Adds a parameter \a definition to the algorithm. Ownership of the definition is transferred to the algorithm.
* Returns true if parameter could be successfully added, or false if the parameter could not be added (e.g.
* as a result of a duplicate name).
*/
bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER );
/**
* Evaluates the parameter with matching \a name to a static string value.
*/
QString parameterAsString( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to an expression.
*/
QString parameterAsExpression( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a static double value.
*/
double parameterAsDouble( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a static integer value.
*/
int parameterAsInt( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a enum value.
*/
int parameterAsEnum( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to list of enum values.
*/
QList<int> parameterAsEnums( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a static boolean value.
*/
bool parameterAsBool( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a map layer.
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*/
QgsMapLayer *parameterAsLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a raster layer.
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*/
QgsRasterLayer *parameterAsRasterLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a vector layer.
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*/
QgsVectorLayer *parameterAsVectorLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a coordinate reference system.
*/
QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a rectangular extent.
*/
QgsRectangle parameterAsExtent( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a point.
*/
QgsPointXY parameterAsPoint( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a file/folder name.
*/
QString parameterAsFile( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a matrix/table of values.
* Tables are collapsed to a 1 dimensional list.
*/
QVariantList parameterAsMatrix( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a list of map layers.
*/
QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a range of values.
*/
QList<double> parameterAsRange( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a list of fields.
*/
QStringList parameterAsFields( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
private:
QgsProcessingProvider *mProvider = nullptr;
QgsProcessingParameterDefinitions mParameters;
/**
* Associates this algorithm with its provider. No transfer of ownership is involved.
*/
void setProvider( QgsProcessingProvider *provider );
// friend class to access setProvider() - we do not want this public!
friend class QgsProcessingProvider;
friend class TestQgsProcessing;
#ifdef SIP_RUN
QgsProcessingAlgorithm( const QgsProcessingAlgorithm &other );
#endif
};
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingAlgorithm::Flags )
#endif // QGSPROCESSINGALGORITHM_H