QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
qgsexpressionfunction.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsexpressionfunction.h
3 -------------------
4 begin : May 2017
5 copyright : (C) 2017 Matthias Kuhn
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSEXPRESSIONFUNCTION_H
17#define QGSEXPRESSIONFUNCTION_H
18
19#include <functional>
20
21#include "qgis.h"
22#include "qgis_core.h"
23#include "qgsexpressionnode.h"
24
25#include <QJsonDocument>
26#include <QJsonObject>
27#include <QSet>
28#include <QString>
29#include <QVariant>
30
32class QgsExpression;
35
40class CORE_EXPORT QgsExpressionFunction
41{
42 public:
46 typedef QVariant ( *FcnEval )( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) SIP_SKIP;
47
52 class CORE_EXPORT Parameter
53 {
54 public:
62 Parameter( const QString &name, bool optional = false, const QVariant &defaultValue = QVariant(), bool isSubExpression = false )
63 : mName( name )
64 , mOptional( optional )
65 , mDefaultValue( defaultValue )
66 , mIsSubExpression( isSubExpression )
67 {}
68
70 QString name() const { return mName; }
71
73 bool optional() const { return mOptional; }
74
76 QVariant defaultValue() const { return mDefaultValue; }
77
83 bool isSubExpression() const { return mIsSubExpression; }
84
85 bool operator==( const QgsExpressionFunction::Parameter &other ) const { return ( QString::compare( mName, other.mName, Qt::CaseInsensitive ) == 0 ); }
86
87 private:
88 QString mName;
89 bool mOptional = false;
90 QVariant mDefaultValue;
91 bool mIsSubExpression = false;
92 };
93
95 typedef QList< QgsExpressionFunction::Parameter > ParameterList;
96
98 QgsExpressionFunction( const QString &fnname, int params, const QString &group, const QString &helpText = QString(), bool lazyEval = false, bool handlesNull = false, bool isContextual = false )
99 : mName( fnname )
100 , mParams( params )
101 , mGroups( group.isEmpty() ? QStringList() : QStringList() << group )
102 , mHelpText( helpText )
103 , mLazyEval( lazyEval )
104 , mHandlesNull( handlesNull )
105 , mIsContextual( isContextual )
106 {}
107
111 QgsExpressionFunction( const QString &fnname, int params, const QStringList &groups, const QString &helpText = QString(), bool lazyEval = false, bool handlesNull = false, bool isContextual = false )
112 : mName( fnname )
113 , mParams( params )
114 , mGroups( groups )
115 , mHelpText( helpText )
116 , mLazyEval( lazyEval )
117 , mHandlesNull( handlesNull )
118 , mIsContextual( isContextual )
119 {}
120
125 const QString &fnname,
127 const QString &group,
128 const QString &helpText = QString(),
129 bool lazyEval = false,
130 bool handlesNull = false,
131 bool isContextual = false
132 )
133 : mName( fnname )
134 , mParams( 0 )
135 , mParameterList( params )
136 , mGroups( group.isEmpty() ? QStringList() : QStringList() << group )
137 , mHelpText( helpText )
138 , mLazyEval( lazyEval )
139 , mHandlesNull( handlesNull )
140 , mIsContextual( isContextual )
141 {}
142
147 const QString &fnname,
149 const QStringList &groups,
150 const QString &helpText = QString(),
151 bool lazyEval = false,
152 bool handlesNull = false,
153 bool isContextual = false
154 )
155 : mName( fnname )
156 , mParams( 0 )
157 , mParameterList( params )
158 , mGroups( groups )
159 , mHelpText( helpText )
160 , mLazyEval( lazyEval )
161 , mHandlesNull( handlesNull )
162 , mIsContextual( isContextual )
163 {}
164
165 virtual ~QgsExpressionFunction() = default;
166
168 QString name() const { return mName; }
169
171 int params() const { return mParameterList.isEmpty() ? mParams : mParameterList.count(); }
172
174 int minParams() const
175 {
176 if ( mParameterList.isEmpty() )
177 return mParams;
178
179 int min = 0;
180 for ( const Parameter &param : mParameterList )
181 {
182 if ( !param.optional() )
183 min++;
184 }
185 return min;
186 }
187
191 const QgsExpressionFunction::ParameterList &parameters() const { return mParameterList; }
192
194 virtual bool usesGeometry( const QgsExpressionNodeFunction *node ) const;
195
201 virtual QStringList aliases() const;
202
208 bool lazyEval() const { return mLazyEval; }
209
219 virtual bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const;
220
228 virtual bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const;
229
237 virtual QSet<QString> referencedColumns( const QgsExpressionNodeFunction *node ) const;
238
242 bool isContextual() const { return mIsContextual; }
243
248 virtual bool isDeprecated() const;
249
254 QString group() const { return mGroups.isEmpty() ? QString() : mGroups.at( 0 ); }
255
260 QStringList groups() const { return mGroups; }
261
263 const QString helpText() const;
264
273 virtual QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) = 0;
274
279 virtual QVariant run( QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node );
280
281 bool operator==( const QgsExpressionFunction &other ) const;
282
287 virtual bool handlesNull() const;
288
289 protected:
298 static bool allParamsStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context );
299
300 private:
301 QString mName;
302 int mParams;
304 QStringList mGroups;
305 QString mHelpText;
306 bool mLazyEval;
307 bool mHandlesNull;
308 bool mIsContextual; //if true function is only available through an expression context
309};
310
316#ifndef SIP_RUN
318{
319 public:
324 const QString &fnname,
325 int params,
326 FcnEval fcn,
327 const QString &group,
328 const QString &helpText = QString(),
329 bool usesGeometry = false,
330 const QSet<QString> &referencedColumns = QSet<QString>(),
331 bool lazyEval = false,
332 const QStringList &aliases = QStringList(),
333 bool handlesNull = false
334 )
336 , mFnc( fcn )
337 , mAliases( aliases )
338 , mUsesGeometry( usesGeometry )
339 , mReferencedColumns( referencedColumns )
340 {}
341
347 const QString &fnname,
348 int params,
349 FcnEval fcn,
350 const QStringList &groups,
351 const QString &helpText = QString(),
352 bool usesGeometry = false,
353 const QSet<QString> &referencedColumns = QSet<QString>(),
354 bool lazyEval = false,
355 const QStringList &aliases = QStringList(),
356 bool handlesNull = false
357 )
359 , mFnc( fcn )
360 , mAliases( aliases )
361 , mUsesGeometry( usesGeometry )
362 , mReferencedColumns( referencedColumns )
363 {}
364
369 const QString &fnname,
371 FcnEval fcn,
372 const QString &group,
373 const QString &helpText = QString(),
374 bool usesGeometry = false,
375 const QSet<QString> &referencedColumns = QSet<QString>(),
376 bool lazyEval = false,
377 const QStringList &aliases = QStringList(),
378 bool handlesNull = false
379 )
381 , mFnc( fcn )
382 , mAliases( aliases )
383 , mUsesGeometry( usesGeometry )
384 , mReferencedColumns( referencedColumns )
385 {}
386
399 const QString &fnname,
401 FcnEval fcn,
402 const QString &group,
403 const QString &helpText,
404 const std::function< bool( const QgsExpressionNodeFunction *node )> &usesGeometry,
405 const std::function< QSet<QString>( const QgsExpressionNodeFunction *node )> &referencedColumns,
406 bool lazyEval = false,
407 const QStringList &aliases = QStringList(),
408 bool handlesNull = false
409 );
410
416 const QString &fnname,
418 FcnEval fcn,
419 const QStringList &groups,
420 const QString &helpText = QString(),
421 bool usesGeometry = false,
422 const QSet<QString> &referencedColumns = QSet<QString>(),
423 bool lazyEval = false,
424 const QStringList &aliases = QStringList(),
425 bool handlesNull = false
426 )
428 , mFnc( fcn )
429 , mAliases( aliases )
430 , mUsesGeometry( usesGeometry )
431 , mReferencedColumns( referencedColumns )
432 {}
433
442 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override
443 {
444 return mFnc ? mFnc( values, context, parent, node ) : QVariant();
445 }
446
447 QStringList aliases() const override;
448
449 bool usesGeometry( const QgsExpressionNodeFunction *node ) const override;
450
456 void setUsesGeometryFunction( const std::function< bool( const QgsExpressionNodeFunction *node )> &usesGeometry );
457
458 QSet<QString> referencedColumns( const QgsExpressionNodeFunction *node ) const override;
459
460 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
461
462 bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
463
470 void setIsStaticFunction( const std::function< bool( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) > &isStatic );
471
472
480 void setIsStatic( bool isStatic );
481
488 void setPrepareFunction( const std::function< bool( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * )> &prepareFunc );
489
493 static const QList<QgsExpressionFunction *> &functions();
494
495 private:
496 FcnEval mFnc;
497 QStringList mAliases;
498 bool mUsesGeometry;
499 std::function< bool( const QgsExpressionNodeFunction *node ) > mUsesGeometryFunc;
500 std::function< QSet<QString>( const QgsExpressionNodeFunction *node ) > mReferencedColumnsFunc;
501 std::function< bool( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) > mIsStaticFunc = allParamsStatic;
502 std::function< bool( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) > mPrepareFunc;
503 QSet<QString> mReferencedColumns;
504 bool mIsStatic = false;
505};
506
516{
517 public:
519
520 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
521
522 QVariant run( QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
523
524 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
525
526 bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
527};
528
538{
539 public:
541
542 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
543
544 QVariant run( QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
545
546 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
547
548 bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
549};
550
560{
561 public:
563
564 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
565
566 QVariant run( QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
567
568 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
569
570 bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
571
572 private:
576 void appendTemporaryVariable( const QgsExpressionContext *context, const QString &name, const QVariant &value ) const;
577
581 void popTemporaryVariable( const QgsExpressionContext *context ) const;
582};
583
584#endif
585
586#endif // QGSEXPRESSIONFUNCTION_H
QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override
Returns result of evaluating the function.
bool prepare(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
This will be called during the prepare step() of an expression if it is not static.
bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
Will be called during prepare to determine if the function is static.
QVariant run(QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override
Evaluates the function, first evaluating all required arguments before passing them to the function's...
QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override
Returns result of evaluating the function.
bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
Will be called during prepare to determine if the function is static.
QVariant run(QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override
Evaluates the function, first evaluating all required arguments before passing them to the function's...
bool prepare(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
This will be called during the prepare step() of an expression if it is not static.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Represents a single parameter passed to a function.
Parameter(const QString &name, bool optional=false, const QVariant &defaultValue=QVariant(), bool isSubExpression=false)
Constructor for Parameter.
bool operator==(const QgsExpressionFunction::Parameter &other) const
QVariant defaultValue() const
Returns the default value for the parameter.
QString name() const
Returns the name of the parameter.
bool isSubExpression() const
Returns true if parameter argument is a separate sub-expression, and should not be checked while dete...
bool optional() const
Returns true if the parameter is optional.
QList< QgsExpressionFunction::Parameter > ParameterList
List of parameters, used for function definition.
QgsExpressionFunction(const QString &fnname, const QgsExpressionFunction::ParameterList &params, const QStringList &groups, const QString &helpText=QString(), bool lazyEval=false, bool handlesNull=false, bool isContextual=false)
Constructor for function which uses named parameter list and group list.
bool isContextual() const
Returns whether the function is only available if provided by a QgsExpressionContext object.
int params() const
The number of parameters this function takes.
QStringList groups() const
Returns a list of the groups the function belongs to.
QgsExpressionFunction(const QString &fnname, int params, const QString &group, const QString &helpText=QString(), bool lazyEval=false, bool handlesNull=false, bool isContextual=false)
Constructor for function which uses unnamed parameters.
QVariant(* FcnEval)(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node)
Function definition for evaluation against an expression context, using a list of values as parameter...
bool lazyEval() const
true if this function should use lazy evaluation.
static bool allParamsStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context)
This will return true if all the params for the provided function node are static within the constrai...
virtual ~QgsExpressionFunction()=default
int minParams() const
The minimum number of parameters this function takes.
QgsExpressionFunction(const QString &fnname, int params, const QStringList &groups, const QString &helpText=QString(), bool lazyEval=false, bool handlesNull=false, bool isContextual=false)
Constructor for function which uses unnamed parameters and group list.
QString name() const
The name of the function.
virtual QVariant run(QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node)
Evaluates the function, first evaluating all required arguments before passing them to the function's...
QString group() const
Returns the first group which the function belongs to.
virtual QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node)=0
Returns result of evaluating the function.
const QgsExpressionFunction::ParameterList & parameters() const
Returns the list of named parameters for the function, if set.
virtual bool handlesNull() const
Returns true if the function handles NULL values in arguments by itself, and the default NULL value h...
const QString helpText() const
The help text for the function.
QgsExpressionFunction(const QString &fnname, const QgsExpressionFunction::ParameterList &params, const QString &group, const QString &helpText=QString(), bool lazyEval=false, bool handlesNull=false, bool isContextual=false)
Constructor for function which uses named parameter list.
An expression node for expression functions.
A list of expression nodes.
Handles parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
This will be called during the prepare step() of an expression if it is not static.
QgsStaticExpressionFunction(const QString &fnname, const QgsExpressionFunction::ParameterList &params, FcnEval fcn, const QStringList &groups, const QString &helpText=QString(), bool usesGeometry=false, const QSet< QString > &referencedColumns=QSet< QString >(), bool lazyEval=false, const QStringList &aliases=QStringList(), bool handlesNull=false)
Static function for evaluation against a QgsExpressionContext, using a named list of parameter values...
void setIsStaticFunction(const std::function< bool(const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext *) > &isStatic)
Set a function that will be called in the prepare step to determine if the function is static or not.
QStringList aliases() const override
Returns a list of possible aliases for the function.
void setPrepareFunction(const std::function< bool(const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext *)> &prepareFunc)
Set a function that will be called in the prepare step to determine if the function is static or not.
static const QList< QgsExpressionFunction * > & functions()
Returns a list of all registered expression functions.
void setUsesGeometryFunction(const std::function< bool(const QgsExpressionNodeFunction *node)> &usesGeometry)
Set a function that will be called when determining if the function requires feature geometry or not.
QgsStaticExpressionFunction(const QString &fnname, const QgsExpressionFunction::ParameterList &params, FcnEval fcn, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QSet< QString > &referencedColumns=QSet< QString >(), bool lazyEval=false, const QStringList &aliases=QStringList(), bool handlesNull=false)
Static function for evaluation against a QgsExpressionContext, using a named list of parameter values...
QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override
Returns result of evaluating the function.
bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
Will be called during prepare to determine if the function is static.
void setIsStatic(bool isStatic)
Tag this function as either static or not static.
QgsStaticExpressionFunction(const QString &fnname, const QgsExpressionFunction::ParameterList &params, FcnEval fcn, const QString &group, const QString &helpText, const std::function< bool(const QgsExpressionNodeFunction *node)> &usesGeometry, const std::function< QSet< QString >(const QgsExpressionNodeFunction *node)> &referencedColumns, bool lazyEval=false, const QStringList &aliases=QStringList(), bool handlesNull=false)
Static function for evaluation against a QgsExpressionContext, using a named list of parameter values...
QgsStaticExpressionFunction(const QString &fnname, int params, FcnEval fcn, const QStringList &groups, const QString &helpText=QString(), bool usesGeometry=false, const QSet< QString > &referencedColumns=QSet< QString >(), bool lazyEval=false, const QStringList &aliases=QStringList(), bool handlesNull=false)
Static function for evaluation against a QgsExpressionContext, using an unnamed list of parameter val...
QgsStaticExpressionFunction(const QString &fnname, int params, FcnEval fcn, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QSet< QString > &referencedColumns=QSet< QString >(), bool lazyEval=false, const QStringList &aliases=QStringList(), bool handlesNull=false)
Static function for evaluation against a QgsExpressionContext, using an unnamed list of parameter val...
QSet< QString > referencedColumns(const QgsExpressionNodeFunction *node) const override
Returns a set of field names which are required for this function.
bool usesGeometry(const QgsExpressionNodeFunction *node) const override
Does this function use a geometry object.
bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
Will be called during prepare to determine if the function is static.
QVariant run(QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override
Evaluates the function, first evaluating all required arguments before passing them to the function's...
QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override
Returns result of evaluating the function.
bool prepare(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
This will be called during the prepare step() of an expression if it is not static.
#define SIP_SKIP
Definition qgis_sip.h:133
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)