QGIS API Documentation 4.3.0-Master (52aa6503bed)
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
6 email : matthias@opengis.ch
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 <QMutex>
28#include <QSet>
29#include <QString>
30#include <QVariant>
31
33class QgsExpression;
36
41class CORE_EXPORT QgsExpressionFunction
42{
43 public:
47 typedef QVariant ( *FcnEval )( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) SIP_SKIP;
48
53 class CORE_EXPORT Parameter
54 {
55 public:
63 Parameter( const QString &name, bool optional = false, const QVariant &defaultValue = QVariant(), bool isSubExpression = false )
64 : mName( name )
65 , mOptional( optional )
66 , mDefaultValue( defaultValue )
67 , mIsSubExpression( isSubExpression )
68 {}
69
71 QString name() const { return mName; }
72
74 bool optional() const { return mOptional; }
75
77 QVariant defaultValue() const { return mDefaultValue; }
78
84 bool isSubExpression() const { return mIsSubExpression; }
85
86 bool operator==( const QgsExpressionFunction::Parameter &other ) const { return ( QString::compare( mName, other.mName, Qt::CaseInsensitive ) == 0 ); }
87
88 private:
89 QString mName;
90 bool mOptional = false;
91 QVariant mDefaultValue;
92 bool mIsSubExpression = false;
93 };
94
96 typedef QList< QgsExpressionFunction::Parameter > ParameterList;
97
99 QgsExpressionFunction( const QString &fnname, int params, const QString &group, const QString &helpText = QString(), bool lazyEval = false, bool handlesNull = false, bool isContextual = false )
100 : mName( fnname )
101 , mParams( params )
102 , mGroups( group.isEmpty() ? QStringList() : QStringList() << group )
103 , mHelpText( helpText )
104 , mLazyEval( lazyEval )
105 , mHandlesNull( handlesNull )
106 , mIsContextual( isContextual )
107 {}
108
112 QgsExpressionFunction( const QString &fnname, int params, const QStringList &groups, const QString &helpText = QString(), bool lazyEval = false, bool handlesNull = false, bool isContextual = false )
113 : mName( fnname )
114 , mParams( params )
115 , mGroups( groups )
116 , mHelpText( helpText )
117 , mLazyEval( lazyEval )
118 , mHandlesNull( handlesNull )
119 , mIsContextual( isContextual )
120 {}
121
126 const QString &fnname,
128 const QString &group,
129 const QString &helpText = QString(),
130 bool lazyEval = false,
131 bool handlesNull = false,
132 bool isContextual = false
133 )
134 : mName( fnname )
135 , mParams( 0 )
136 , mParameterList( params )
137 , mGroups( group.isEmpty() ? QStringList() : QStringList() << group )
138 , mHelpText( helpText )
139 , mLazyEval( lazyEval )
140 , mHandlesNull( handlesNull )
141 , mIsContextual( isContextual )
142 {}
143
148 const QString &fnname,
150 const QStringList &groups,
151 const QString &helpText = QString(),
152 bool lazyEval = false,
153 bool handlesNull = false,
154 bool isContextual = false
155 )
156 : mName( fnname )
157 , mParams( 0 )
158 , mParameterList( params )
159 , mGroups( groups )
160 , mHelpText( helpText )
161 , mLazyEval( lazyEval )
162 , mHandlesNull( handlesNull )
163 , mIsContextual( isContextual )
164 {}
165
166 virtual ~QgsExpressionFunction() = default;
167
169 QString name() const { return mName; }
170
172 int params() const { return mParameterList.isEmpty() ? mParams : mParameterList.count(); }
173
175 int minParams() const
176 {
177 if ( mParameterList.isEmpty() )
178 return mParams;
179
180 int min = 0;
181 for ( const Parameter &param : mParameterList )
182 {
183 if ( !param.optional() )
184 min++;
185 }
186 return min;
187 }
188
192 const QgsExpressionFunction::ParameterList &parameters() const { return mParameterList; }
193
195 virtual bool usesGeometry( const QgsExpressionNodeFunction *node ) const;
196
202 virtual QStringList aliases() const;
203
209 bool lazyEval() const { return mLazyEval; }
210
220 virtual bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const;
221
229 virtual bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const;
230
238 virtual QSet<QString> referencedColumns( const QgsExpressionNodeFunction *node ) const;
239
243 bool isContextual() const { return mIsContextual; }
244
249 virtual bool isDeprecated() const;
250
255 QString group() const { return mGroups.isEmpty() ? QString() : mGroups.at( 0 ); }
256
261 QStringList groups() const { return mGroups; }
262
264 const QString helpText() const;
265
274 virtual QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) = 0;
275
280 virtual QVariant run( QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node );
281
282 bool operator==( const QgsExpressionFunction &other ) const;
283
288 virtual bool handlesNull() const;
289
290 protected:
299 static bool allParamsStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context );
300
301 private:
302 QString mName;
303 int mParams;
305 QStringList mGroups;
306 QString mHelpText;
307 bool mLazyEval;
308 bool mHandlesNull;
309 bool mIsContextual; //if true function is only available through an expression context
310};
311
317#ifndef SIP_RUN
319{
320 public:
325 const QString &fnname,
326 int params,
327 FcnEval fcn,
328 const QString &group,
329 const QString &helpText = QString(),
330 bool usesGeometry = false,
331 const QSet<QString> &referencedColumns = QSet<QString>(),
332 bool lazyEval = false,
333 const QStringList &aliases = QStringList(),
334 bool handlesNull = false
335 )
337 , mFnc( fcn )
338 , mAliases( aliases )
339 , mUsesGeometry( usesGeometry )
340 , mReferencedColumns( referencedColumns )
341 {}
342
348 const QString &fnname,
349 int params,
350 FcnEval fcn,
351 const QStringList &groups,
352 const QString &helpText = QString(),
353 bool usesGeometry = false,
354 const QSet<QString> &referencedColumns = QSet<QString>(),
355 bool lazyEval = false,
356 const QStringList &aliases = QStringList(),
357 bool handlesNull = false
358 )
360 , mFnc( fcn )
361 , mAliases( aliases )
362 , mUsesGeometry( usesGeometry )
363 , mReferencedColumns( referencedColumns )
364 {}
365
370 const QString &fnname,
372 FcnEval fcn,
373 const QString &group,
374 const QString &helpText = QString(),
375 bool usesGeometry = false,
376 const QSet<QString> &referencedColumns = QSet<QString>(),
377 bool lazyEval = false,
378 const QStringList &aliases = QStringList(),
379 bool handlesNull = false
380 )
382 , mFnc( fcn )
383 , mAliases( aliases )
384 , mUsesGeometry( usesGeometry )
385 , mReferencedColumns( referencedColumns )
386 {}
387
400 const QString &fnname,
402 FcnEval fcn,
403 const QString &group,
404 const QString &helpText,
405 const std::function< bool( const QgsExpressionNodeFunction *node )> &usesGeometry,
406 const std::function< QSet<QString>( const QgsExpressionNodeFunction *node )> &referencedColumns,
407 bool lazyEval = false,
408 const QStringList &aliases = QStringList(),
409 bool handlesNull = false
410 );
411
417 const QString &fnname,
419 FcnEval fcn,
420 const QStringList &groups,
421 const QString &helpText = QString(),
422 bool usesGeometry = false,
423 const QSet<QString> &referencedColumns = QSet<QString>(),
424 bool lazyEval = false,
425 const QStringList &aliases = QStringList(),
426 bool handlesNull = false
427 )
429 , mFnc( fcn )
430 , mAliases( aliases )
431 , mUsesGeometry( usesGeometry )
432 , mReferencedColumns( referencedColumns )
433 {}
434
443 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override
444 {
445 return mFnc ? mFnc( values, context, parent, node ) : QVariant();
446 }
447
448 QStringList aliases() const override;
449
450 bool usesGeometry( const QgsExpressionNodeFunction *node ) const override;
451
457 void setUsesGeometryFunction( const std::function< bool( const QgsExpressionNodeFunction *node )> &usesGeometry );
458
459 QSet<QString> referencedColumns( const QgsExpressionNodeFunction *node ) const override;
460
461 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
462
463 bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
464
471 void setIsStaticFunction( const std::function< bool( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) > &isStatic );
472
473
481 void setIsStatic( bool isStatic );
482
489 void setPrepareFunction( const std::function< bool( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * )> &prepareFunc );
490
494 static const QList<QgsExpressionFunction *> &functions();
495
496 private:
497 FcnEval mFnc;
498 QStringList mAliases;
499 bool mUsesGeometry;
500 std::function< bool( const QgsExpressionNodeFunction *node ) > mUsesGeometryFunc;
501 std::function< QSet<QString>( const QgsExpressionNodeFunction *node ) > mReferencedColumnsFunc;
502 std::function< bool( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) > mIsStaticFunc = allParamsStatic;
503 std::function< bool( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) > mPrepareFunc;
504 QSet<QString> mReferencedColumns;
505 bool mIsStatic = false;
506};
507
517{
518 public:
520
521 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
522
523 QVariant run( QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
524
525 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
526
527 bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
528};
529
539{
540 public:
542
543 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
544
545 QVariant run( QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
546
547 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
548
549 bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
550};
551
561{
562 public:
564
565 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
566
567 QVariant run( QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
568
569 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override;
570
571 bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
572
573 private:
577 void appendTemporaryVariable( const QgsExpressionContext *context, const QString &name, const QVariant &value ) const;
578
582 void popTemporaryVariable( const QgsExpressionContext *context ) const;
583};
584
585#endif
586
587#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:138
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)