16 #ifndef QGSEXPRESSION_H 17 #define QGSEXPRESSION_H 20 #include <QStringList> 23 #include <QDomDocument> 24 #include <QCoreApplication> 41 class QgsExpressionPrivate;
158 bool isValid()
const;
161 bool hasParserError()
const;
163 QString parserErrorString()
const;
168 const Node* rootNode()
const;
172 Q_DECL_DEPRECATED
bool prepare(
const QgsFields &fields );
192 bool needsGeometry()
const;
232 bool hasEvalError()
const;
234 QString evalErrorString()
const;
236 void setEvalErrorString(
const QString& str );
240 Q_DECL_DEPRECATED
void setCurrentRowNumber(
int rowNumber );
243 Q_DECL_DEPRECATED
int currentRowNumber();
248 Q_DECL_DEPRECATED
static void setSpecialColumn(
const QString& name,
const QVariant& value );
252 Q_DECL_DEPRECATED
static void unsetSpecialColumn(
const QString& name );
256 Q_DECL_DEPRECATED
static QVariant specialColumn(
const QString& name );
261 static bool hasSpecialColumn(
const QString& name );
285 void setExpression(
const QString& expression );
287 void setScale(
double scale );
393 static double evaluateToDouble(
const QString& text,
const double fallbackValue );
457 static const char* BinaryOperatorText[];
460 static const char* UnaryOperatorText[];
476 bool optional =
false,
479 , mOptional( optional )
480 , mDefaultValue( defaultValue )
494 return (
QString::compare( mName, other.mName, Qt::CaseInsensitive ) == 0 );
525 bool usesGeometry =
false,
527 bool lazyEval =
false,
528 bool handlesNull =
false,
529 bool isContextual =
false )
532 , mUsesGeometry( usesGeometry )
534 , mHelpText( helpText )
535 , mReferencedColumns( referencedColumns )
536 , mLazyEval( lazyEval )
537 , mHandlesNull( handlesNull )
538 , mIsContextual( isContextual )
549 bool usesGeometry =
false,
551 bool lazyEval =
false,
552 bool handlesNull =
false,
553 bool isContextual =
false )
556 , mUsesGeometry( usesGeometry )
558 , mHelpText( helpText )
559 , mReferencedColumns( referencedColumns )
560 , mLazyEval( lazyEval )
561 , mHandlesNull( handlesNull )
562 , mIsContextual( isContextual )
570 const ParameterList& params,
573 bool usesGeometry =
false,
575 bool lazyEval =
false,
576 bool handlesNull =
false,
577 bool isContextual =
false )
580 , mParameterList( params )
581 , mUsesGeometry( usesGeometry )
583 , mHelpText( helpText )
584 , mReferencedColumns( referencedColumns )
585 , mLazyEval( lazyEval )
586 , mHandlesNull( handlesNull )
587 , mIsContextual( isContextual )
594 const ParameterList& params,
597 bool usesGeometry =
false,
599 bool lazyEval =
false,
600 bool handlesNull =
false,
601 bool isContextual =
false )
604 , mParameterList( params )
605 , mUsesGeometry( usesGeometry )
607 , mHelpText( helpText )
608 , mReferencedColumns( referencedColumns )
609 , mLazyEval( lazyEval )
610 , mHandlesNull( handlesNull )
611 , mIsContextual( isContextual )
621 int params()
const {
return mParameterList.isEmpty() ? mParams : mParameterList.count(); }
626 if ( mParameterList.isEmpty() )
630 Q_FOREACH (
const Parameter& param, mParameterList )
641 const ParameterList&
parameters()
const {
return mParameterList; }
671 virtual bool isDeprecated()
const {
return mGroups.isEmpty() ? false : mGroups.contains(
"deprecated" ); }
714 ParameterList mParameterList;
737 bool usesGeometry =
false,
739 bool lazyEval =
false,
741 bool handlesNull =
false )
742 :
Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval, handlesNull )
744 , mContextFnc( nullptr )
745 , mAliases( aliases )
757 bool usesGeometry =
false,
759 bool lazyEval =
false,
761 bool handlesNull =
false )
762 :
Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval, handlesNull )
765 , mAliases( aliases )
771 const ParameterList& params,
775 bool usesGeometry =
false,
777 bool lazyEval =
false,
779 bool handlesNull =
false )
780 :
Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval, handlesNull )
783 , mAliases( aliases )
790 const ParameterList& params,
794 bool usesGeometry =
false,
796 bool lazyEval =
false,
798 bool handlesNull =
false )
799 :
Function( fnname, params, groups, helpText, usesGeometry, referencedColumns, lazyEval, handlesNull )
802 , mAliases( aliases )
816 return mContextFnc ? mContextFnc( values, context, parent ) :
QVariant();
823 FcnEvalContext mContextFnc;
841 static bool registerFunction(
Function*
function,
bool transferOwnership =
false );
847 static bool unregisterFunction(
const QString& name );
856 static void cleanRegisteredFunctions();
859 static bool isFunctionName(
const QString& name );
862 static int functionIndex(
const QString& name );
867 static int functionCount();
903 static QString quotedValue(
const QVariant& value, QVariant::Type type );
932 virtual NodeType nodeType()
const = 0;
967 virtual QString dump()
const = 0;
977 virtual Node* clone()
const = 0;
999 virtual bool needsGeometry()
const = 0;
1016 virtual void accept(
Visitor& v )
const = 0;
1059 int count()
const {
return mList.count(); }
1082 bool mHasNamedNodes;
1097 , mOperand( operand )
1107 virtual QString dump()
const override;
1110 virtual bool needsGeometry()
const override {
return mOperand->needsGeometry(); }
1112 virtual Node* clone()
const override;
1127 , mOpRight( opRight )
1138 virtual QString dump()
const override;
1141 virtual bool needsGeometry()
const override {
return mOpLeft->needsGeometry() || mOpRight->needsGeometry(); }
1143 virtual Node* clone()
const override;
1145 int precedence()
const;
1146 bool leftAssociative()
const;
1149 bool compare(
double diff );
1150 int computeInt(
int x,
int y );
1151 double computeDouble(
double x,
double y );
1183 virtual QString dump()
const override;
1188 virtual Node* clone()
const override;
1203 const ParameterList& functionParams = Functions()[mFnIndex]->parameters();
1215 while ( args->
names().
at( idx ).isEmpty() )
1217 mArgs->append( args->
list().at( idx )->clone() );
1222 for ( ; idx < functionParams.
count(); ++idx )
1224 int nodeIdx = args->
names().
indexOf( functionParams.
at( idx ).name().toLower() );
1228 mArgs->append(
new NodeLiteral( functionParams.
at( idx ).defaultValue() ) );
1232 mArgs->append( args->
list().at( nodeIdx )->clone() );
1248 virtual QString dump()
const override;
1250 virtual QStringList referencedColumns()
const override;
1251 virtual bool needsGeometry()
const override {
bool needs = Functions()[mFnIndex]->usesgeometry();
if ( mArgs ) { Q_FOREACH (
Node* n, mArgs->list() ) needs |= n->
needsGeometry(); }
return needs; }
1253 virtual Node* clone()
const override;
1261 const ParameterList& functionParams = Functions()[fnIndex]->parameters();
1262 if ( functionParams.
isEmpty() )
1264 error =
QString(
"%1 does not supported named parameters" ).
arg( Functions()[fnIndex]->name() );
1273 while ( args->
names().
at( idx ).isEmpty() )
1275 providedArgs << idx;
1281 for ( ; idx < functionParams.
count(); ++idx )
1283 int nodeIdx = args->
names().
indexOf( functionParams.
at( idx ).name().toLower() );
1286 if ( !functionParams.
at( idx ).optional() )
1288 error =
QString(
"No value specified for parameter '%1' for %2" ).
arg( functionParams.
at( idx ).name(), Functions()[fnIndex]->name() );
1294 if ( providedArgs.
contains( idx ) )
1296 error =
QString(
"Duplicate parameter specified for '%1' for %2" ).
arg( functionParams.
at( idx ).name(), Functions()[fnIndex]->name() );
1300 providedArgs << idx;
1301 handledArgs << nodeIdx;
1310 error =
QString(
"Invalid parameter name '%1' for %2" ).
arg( name, Functions()[fnIndex]->name() );
1315 int functionIdx = functionParams.
indexOf( name );
1316 if ( providedArgs.
contains( functionIdx ) )
1318 error =
QString(
"Duplicate parameter specified for '%1' for %2" ).
arg( functionParams.
at( functionIdx ).name(), Functions()[fnIndex]->name() );
1350 virtual QString dump()
const override;
1355 virtual Node* clone()
const override;
1377 virtual QString dump()
const override;
1383 virtual Node* clone()
const override;
1396 : mWhenExp( whenExp )
1397 , mThenExp( thenExp )
1417 : mConditions( *conditions )
1418 , mElseExp( elseExp )
1419 {
delete conditions; }
1421 : mConditions( conditions )
1422 , mElseExp( elseExp )
1429 virtual QString dump()
const override;
1431 virtual QStringList referencedColumns()
const override;
1432 virtual bool needsGeometry()
const override;
1434 virtual Node* clone()
const override;
1461 void acceptVisitor(
Visitor& v )
const;
1501 void initGeomCalculator();
1511 , mDescription( desc )
1512 , mDescOnly( descOnly )
1513 , mSyntaxOnly( syntaxOnly )
1514 , mOptional( optional )
1515 , mDefaultVal( defaultVal )
1529 : mExpression( expression )
1530 , mReturns( returns )
1543 bool variableLenArguments =
false,
1545 const QString& notes = QString::null )
1547 , mDescription( description )
1548 , mArguments( arguments )
1549 , mVariableLenArguments( variableLenArguments )
1550 , mExamples( examples )
1569 , mDescription( description )
1570 , mVariants( variants )
1587 QgsExpressionPrivate*
d;
1594 static void initFunctionHelp();
1596 static void initVariableHelp();
1606 #endif // QGSEXPRESSION_H
Class for parsing and evaluation of expressions (formerly called "search strings").
Function(const QString &fnname, const ParameterList ¶ms, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QStringList &referencedColumns=QStringList(), bool lazyEval=false, bool handlesNull=false, bool isContextual=false)
Constructor for function which uses named parameter list.
virtual QStringList referencedColumns() const =0
Abstract virtual method which returns a list of columns required to evaluate this node...
UnaryOperator
list of unary operators
virtual bool needsGeometry() const override
Abstract virtual method which returns if the geometry is required to evaluate this expression...
const QString helptext() const
The help text for the function.
virtual QStringList referencedColumns() const override
Abstract virtual method which returns a list of columns required to evaluate this node...
Parameter(const QString &name, bool optional=false, const QVariant &defaultValue=QVariant())
Constructor for Parameter.
Function(const QString &fnname, int params, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QStringList &referencedColumns=QStringList(), bool lazyEval=false, bool handlesNull=false, bool isContextual=false)
Constructor for function which uses unnamed parameters.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
A abstract base class for defining QgsExpression functions.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Function(const QString &fnname, int params, const QStringList &groups, const QString &helpText=QString(), bool usesGeometry=false, const QStringList &referencedColumns=QStringList(), bool lazyEval=false, bool handlesNull=false, bool isContextual=false)
Constructor for function which uses unnamed parameters and group list.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeColumnRef(const QString &name)
static QString helptext(QString name)
Returns the help text for a specified function.
HelpArg(const QString &arg, const QString &desc, bool descOnly=false, bool syntaxOnly=false, bool optional=false, const QString &defaultVal=QString())
const T & at(int i) const
#define Q_NOWARN_DEPRECATED_PUSH
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
Q_DECL_DEPRECATED StaticFunction(const QString &fnname, int params, FcnEval fcn, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QStringList &referencedColumns=QStringList(), bool lazyEval=false, const QStringList &aliases=QStringList(), bool handlesNull=false)
Container of fields for a vector layer.
virtual ~StaticFunction()
A geometry is the spatial representation of a feature.
int count() const
Returns the number of nodes in the list.
virtual QStringList referencedColumns() const override
Abstract virtual method which returns a list of columns required to evaluate this node...
c++ helper class for defining QgsExpression functions.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QList< HelpVariant > mVariants
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
bool lazyEval() const
True if this function should use lazy evaluation.
QString name() const
Returns the name of the parameter.
QString group() const
Returns the first group which the function belongs to.
virtual bool needsGeometry() const override
Abstract virtual method which returns if the geometry is required to evaluate this expression...
virtual bool handlesNull() const
NodeBinaryOperator(BinaryOperator op, Node *opLeft, Node *opRight)
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
int indexOf(const T &value, int from) const
virtual QStringList referencedColumns() const
bool operator==(const Parameter &other) const
NodeCondition(WhenThenList *conditions, Node *elseExp=nullptr)
QVariant defaultValue() const
Returns the default value for the parameter.
HelpVariant(const QString &name, const QString &description, const QList< HelpArg > &arguments=QList< HelpArg >(), bool variableLenArguments=false, const QList< HelpExample > &examples=QList< HelpExample >(), const QString ¬es=QString::null)
static bool validateParams(int fnIndex, NodeList *args, QString &error)
Tests whether the provided argument list is valid for the matching function.
int count(const T &value) const
virtual QStringList referencedColumns() const override
Abstract virtual method which returns a list of columns required to evaluate this node...
void append(const T &value)
QList< Parameter > ParameterList
List of parameters, used for function definition.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
bool hasNamedNodes() const
Returns true if list contains any named nodes.
QStringList names() const
Returns a list of names for nodes.
static QHash< QString, QString > gGroups
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent) override
Returns result of evaluating the function.
bool operator==(const Function &other) const
Represents a single parameter passed to a function.
NodeInOperator(Node *node, NodeList *list, bool notin=false)
QString name() const
The name of the function.
Help(const QString &name, const QString &type, const QString &description, const QList< HelpVariant > &variants)
static QHash< QString, QString > gVariableHelpTexts
bool isContextual() const
Returns whether the function is only available if provided by a QgsExpressionContext object...
QList< HelpArg > mArguments
static QList< Function * > gmFunctions
Encapsulate a field in an attribute table or data source.
HelpExample(const QString &expression, const QString &returns, const QString ¬e=QString::null)
virtual void accept(Visitor &v) const override
Support the visitor pattern.
WhenThen(Node *whenExp, Node *thenExp)
QList< HelpExample > mExamples
StaticFunction(const QString &fnname, const ParameterList ¶ms, FcnEvalContext fcn, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QStringList &referencedColumns=QStringList(), bool lazyEval=false, const QStringList &aliases=QStringList(), bool handlesNull=false)
Static function for evaluation against a QgsExpressionContext, using a named list of parameter values...
NodeUnaryOperator(UnaryOperator op, Node *operand)
BinaryOperator op() const
bool usesgeometry() const
Does this function use a geometry object.
StaticFunction(const QString &fnname, const ParameterList ¶ms, FcnEvalContext fcn, const QStringList &groups, const QString &helpText=QString(), bool usesGeometry=false, const QStringList &referencedColumns=QStringList(), bool lazyEval=false, const QStringList &aliases=QStringList(), bool handlesNull=false)
Static function for evaluation against a QgsExpressionContext, using a named list of parameter values...
virtual void accept(Visitor &v) const override
Support the visitor pattern.
bool contains(const T &value) const
A representation of the interval between two datetime values.
bool optional() const
Returns true if the parameter is optional.
bool contains(const T &value) const
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
virtual QStringList referencedColumns() const override
Abstract virtual method which returns a list of columns required to evaluate this node...
virtual void accept(Visitor &v) const override
Support the visitor pattern.
#define Q_NOWARN_DEPRECATED_POP
NodeFunction(int fnIndex, NodeList *args)
General purpose distance and area calculator.
static QMap< QString, QString > gmSpecialColumnGroups
QString name() const
The name of the column.
virtual bool needsGeometry() const override
Abstract virtual method which returns if the geometry is required to evaluate this expression...
virtual void accept(Visitor &v) const override
Support the visitor pattern.
static QHash< QString, Help > gFunctionHelpTexts
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
virtual bool needsGeometry() const override
Abstract virtual method which returns if the geometry is required to evaluate this expression...
BinaryOperator
list of binary operators
QList< WhenThen * > WhenThenList
virtual void visit(const NodeUnaryOperator &n)=0
virtual QStringList aliases() const
Returns a list of possible aliases for the function.
QVariant value() const
The value of the literal.
NodeLiteral(const QVariant &value)
bool isField() const
Checks whether an expression consists only of a single field reference.
Support for visitor pattern - algorithms dealing with the expressions may be implemented without modi...
UnitType
Map units that qgis supports.
virtual bool needsGeometry() const =0
Abstract virtual method which returns if the geometry is required to evaluate this expression...
NamedNode(const QString &name, Node *node)
Constructor for NamedNode.
static QStringList gmBuiltinFunctions
virtual bool needsGeometry() const override
Abstract virtual method which returns if the geometry is required to evaluate this expression...
int indexOf(const QRegExp &rx, int from) const
int params() const
The number of parameters this function takes.
void append(Node *node)
Takes ownership of the provided node.
virtual void accept(Visitor &v) const override
Support the visitor pattern.
int minParams() const
The mininum number of parameters this function takes.
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
static QList< Function * > gmOwnedFunctions
List of functions owned by the expression engine.
This is the base class for vector data providers.
Function(const QString &fnname, const ParameterList ¶ms, const QStringList &groups, const QString &helpText=QString(), bool usesGeometry=false, const QStringList &referencedColumns=QStringList(), bool lazyEval=false, bool handlesNull=false, bool isContextual=false)
Constructor for function which uses named parameter list and group list.
QStringList groups() const
Returns a list of the groups the function belongs to.
Represents a vector layer which manages a vector based data sets.
int compare(const QString &other) const
virtual QStringList aliases() const override
Returns a list of possible aliases for the function.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
The QgsOgcUtils class provides various utility functions for conversion between OGC (Open Geospatial ...
NodeCondition(const WhenThenList &conditions, Node *elseExp=nullptr)
void append(NamedNode *node)
Adds a named node.
const ParameterList & parameters() const
Returns the list of named parameters for the function, if set.
StaticFunction(const QString &fnname, int params, FcnEvalContext fcn, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QStringList &referencedColumns=QStringList(), bool lazyEval=false, const QStringList &aliases=QStringList(), bool handlesNull=false)
Static function for evaluation against a QgsExpressionContext, using an unnamed list of parameter val...
virtual ~NodeInOperator()
virtual bool needsGeometry() const override
Abstract virtual method which returns if the geometry is required to evaluate this expression...
virtual bool isDeprecated() const
Returns true if the function is deprecated and should not be presented as a valid option to users in ...
virtual QStringList referencedColumns() const override
Abstract virtual method which returns a list of columns required to evaluate this node...
static QMap< QString, QVariant > gmSpecialColumns
bool mVariableLenArguments