QGIS API Documentation  2.99.0-Master (90ae728)
qgsexpression.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpression.h
3  -------------------
4  begin : August 2011
5  copyright : (C) 2011 Martin Dobias
6  email : wonder.sk at gmail dot com
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 QGSEXPRESSION_H
17 #define QGSEXPRESSION_H
18 
19 #include "qgis_core.h"
20 #include <QMetaType>
21 #include <QStringList>
22 #include <QVariant>
23 #include <QList>
24 #include <QDomDocument>
25 #include <QCoreApplication>
26 #include <QSet>
27 #include <functional>
28 
29 #include "qgis.h"
30 #include "qgsunittypes.h"
31 #include "qgsinterval.h"
32 
33 class QgsFeature;
34 class QgsGeometry;
35 class QgsOgcUtils;
36 class QgsVectorLayer;
38 class QgsField;
39 class QgsFields;
40 class QgsDistanceArea;
41 class QDomElement;
43 class QgsExpressionPrivate;
44 
110 class CORE_EXPORT QgsExpression
111 {
112  Q_DECLARE_TR_FUNCTIONS( QgsExpression )
113  public:
114 
121  QgsExpression( const QString& expr );
122 
128  QgsExpression( const QgsExpression& other );
129 
135  QgsExpression& operator=( const QgsExpression& other );
136 
142  QgsExpression();
143 
144  ~QgsExpression();
145 
152  bool operator==( const QgsExpression& other ) const;
153 
160  bool isValid() const;
161 
163  bool hasParserError() const;
165  QString parserErrorString() const;
166 
167  class Node;
168 
170  const Node* rootNode() const;
171 
176  bool prepare( const QgsExpressionContext *context );
177 
187  QSet<QString> referencedColumns() const;
188 
196  QSet<QString> referencedVariables() const;
197 
203  QSet<int> referencedAttributeIndexes( const QgsFields& fields ) const;
204 
206  bool needsGeometry() const;
207 
208  // evaluation
209 
214  QVariant evaluate();
215 
221  QVariant evaluate( const QgsExpressionContext* context );
222 
224  bool hasEvalError() const;
226  QString evalErrorString() const;
228  void setEvalErrorString( const QString& str );
229 
233  bool isField() const { return rootNode() && dynamic_cast<const NodeColumnRef*>( rootNode() ) ;}
234 
242  static bool checkExpression( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
243 
249  void setExpression( const QString& expression );
250 
254  QString expression() const;
255 
260  QString dump() const;
261 
268  QgsDistanceArea *geomCalculator();
269 
278  void setGeomCalculator( const QgsDistanceArea* calc );
279 
286  QgsUnitTypes::DistanceUnit distanceUnits() const;
287 
294  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
295 
302  QgsUnitTypes::AreaUnit areaUnits() const;
303 
310  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
311 
322  static QString replaceExpressionText( const QString& action, const QgsExpressionContext* context,
323  const QgsDistanceArea* distanceArea = nullptr );
324 
334  static double evaluateToDouble( const QString& text, const double fallbackValue );
335 
341  {
344  };
345 
351  {
352  // logical
355 
356  // comparison
370 
371  // math
379 
380  // strings
382  };
383 
385  {
395  };
396 
398  static const char* BINARY_OPERATOR_TEXT[];
399 
401  static const char* UNARY_OPERATOR_TEXT[];
402 
407  class CORE_EXPORT Parameter
408  {
409  public:
410 
416  Parameter( const QString& name,
417  bool optional = false,
418  const QVariant& defaultValue = QVariant() )
419  : mName( name )
420  , mOptional( optional )
421  , mDefaultValue( defaultValue )
422  {}
423 
425  QString name() const { return mName; }
426 
428  bool optional() const { return mOptional; }
429 
431  QVariant defaultValue() const { return mDefaultValue; }
432 
433  bool operator==( const Parameter& other ) const
434  {
435  return ( QString::compare( mName, other.mName, Qt::CaseInsensitive ) == 0 );
436  }
437 
438  private:
439  QString mName;
440  bool mOptional;
441  QVariant mDefaultValue;
442  };
443 
445  typedef QList< Parameter > ParameterList;
446 
449  typedef QVariant( *FcnEval )( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent );
450 
451  class NodeFunction;
452 
456  class CORE_EXPORT Function
457  {
458  public:
459 
461  Function( const QString& fnname,
462  int params,
463  const QString& group,
464  const QString& helpText = QString(),
465  bool lazyEval = false,
466  bool handlesNull = false,
467  bool isContextual = false )
468  : mName( fnname )
469  , mParams( params )
470  , mGroups( group.isEmpty() ? QStringList() : QStringList() << group )
471  , mHelpText( helpText )
472  , mLazyEval( lazyEval )
473  , mHandlesNull( handlesNull )
474  , mIsContextual( isContextual )
475  {
476  }
477 
481  Function( const QString& fnname,
482  int params,
483  const QStringList& groups,
484  const QString& helpText = QString(),
485  bool lazyEval = false,
486  bool handlesNull = false,
487  bool isContextual = false )
488  : mName( fnname )
489  , mParams( params )
490  , mGroups( groups )
491  , mHelpText( helpText )
492  , mLazyEval( lazyEval )
493  , mHandlesNull( handlesNull )
494  , mIsContextual( isContextual )
495  {
496  }
497 
501  Function( const QString& fnname,
502  const ParameterList& params,
503  const QString& group,
504  const QString& helpText = QString(),
505  bool lazyEval = false,
506  bool handlesNull = false,
507  bool isContextual = false )
508  : mName( fnname )
509  , mParams( 0 )
510  , mParameterList( params )
511  , mGroups( group.isEmpty() ? QStringList() : QStringList() << group )
512  , mHelpText( helpText )
513  , mLazyEval( lazyEval )
514  , mHandlesNull( handlesNull )
515  , mIsContextual( isContextual )
516  {}
517 
521  Function( const QString& fnname,
522  const ParameterList& params,
523  const QStringList& groups,
524  const QString& helpText = QString(),
525  bool lazyEval = false,
526  bool handlesNull = false,
527  bool isContextual = false )
528  : mName( fnname )
529  , mParams( 0 )
530  , mParameterList( params )
531  , mGroups( groups )
532  , mHelpText( helpText )
533  , mLazyEval( lazyEval )
534  , mHandlesNull( handlesNull )
535  , mIsContextual( isContextual )
536  {}
537 
538  virtual ~Function() = default;
539 
541  QString name() const { return mName; }
542 
544  int params() const { return mParameterList.isEmpty() ? mParams : mParameterList.count(); }
545 
547  int minParams() const
548  {
549  if ( mParameterList.isEmpty() )
550  return mParams;
551 
552  int min = 0;
553  Q_FOREACH ( const Parameter& param, mParameterList )
554  {
555  if ( !param.optional() )
556  min++;
557  }
558  return min;
559  }
560 
564  const ParameterList& parameters() const { return mParameterList; }
565 
567  virtual bool usesGeometry( const NodeFunction* node ) const;
568 
574  virtual QStringList aliases() const { return QStringList(); }
575 
579  bool lazyEval() const { return mLazyEval; }
580 
589  virtual QSet<QString> referencedColumns( const NodeFunction* node ) const;
590 
594  bool isContextual() const { return mIsContextual; }
595 
600  virtual bool isDeprecated() const { return mGroups.isEmpty() ? false : mGroups.contains( QStringLiteral( "deprecated" ) ); }
601 
605  QString group() const { return mGroups.isEmpty() ? QString() : mGroups.at( 0 ); }
606 
611  QStringList groups() const { return mGroups; }
612 
614  const QString helpText() const { return mHelpText.isEmpty() ? QgsExpression::helpText( mName ) : mHelpText; }
615 
622  virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) = 0;
623 
624  bool operator==( const Function& other ) const;
625 
626  virtual bool handlesNull() const { return mHandlesNull; }
627 
628  private:
629  QString mName;
630  int mParams;
631  ParameterList mParameterList;
632  QStringList mGroups;
633  QString mHelpText;
634  bool mLazyEval;
635  bool mHandlesNull;
636  bool mIsContextual; //if true function is only available through an expression context
637  };
638 
643  class StaticFunction : public Function
644  {
645  public:
646 
649  StaticFunction( const QString& fnname,
650  int params,
651  FcnEval fcn,
652  const QString& group,
653  const QString& helpText = QString(),
654  bool usesGeometry = false,
655  const QSet<QString>& referencedColumns = QSet<QString>(),
656  bool lazyEval = false,
657  const QStringList& aliases = QStringList(),
658  bool handlesNull = false )
659  : Function( fnname, params, group, helpText, lazyEval, handlesNull )
660  , mFnc( fcn )
661  , mAliases( aliases )
662  , mUsesGeometry( usesGeometry )
663  , mReferencedColumns( referencedColumns )
664  {}
665 
668  StaticFunction( const QString& fnname,
669  const ParameterList& params,
670  FcnEval fcn,
671  const QString& group,
672  const QString& helpText = QString(),
673  bool usesGeometry = false,
674  const QSet<QString>& referencedColumns = QSet<QString>(),
675  bool lazyEval = false,
676  const QStringList& aliases = QStringList(),
677  bool handlesNull = false )
678  : Function( fnname, params, group, helpText, lazyEval, handlesNull )
679  , mFnc( fcn )
680  , mAliases( aliases )
681  , mUsesGeometry( usesGeometry )
682  , mReferencedColumns( referencedColumns )
683  {}
684 
696  StaticFunction( const QString& fnname,
697  const ParameterList& params,
698  FcnEval fcn,
699  const QString& group,
700  const QString& helpText,
701  std::function < bool ( const NodeFunction* node ) > usesGeometry,
702  std::function < QSet<QString>( const NodeFunction* node ) > referencedColumns,
703  bool lazyEval = false,
704  const QStringList& aliases = QStringList(),
705  bool handlesNull = false );
706 
710  StaticFunction( const QString& fnname,
711  const ParameterList& params,
712  FcnEval fcn,
713  const QStringList& groups,
714  const QString& helpText = QString(),
715  bool usesGeometry = false,
716  const QSet<QString>& referencedColumns = QSet<QString>(),
717  bool lazyEval = false,
718  const QStringList& aliases = QStringList(),
719  bool handlesNull = false )
720  : Function( fnname, params, groups, helpText, lazyEval, handlesNull )
721  , mFnc( fcn )
722  , mAliases( aliases )
723  , mUsesGeometry( usesGeometry )
724  , mReferencedColumns( referencedColumns )
725  {}
726 
733  virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) override
734  {
735  return mFnc ? mFnc( values, context, parent ) : QVariant();
736  }
737 
738  virtual QStringList aliases() const override { return mAliases; }
739 
740  virtual bool usesGeometry( const QgsExpression::NodeFunction* node ) const override;
741 
742  virtual QSet<QString> referencedColumns( const QgsExpression::NodeFunction* node ) const override;
743 
744  private:
745  FcnEval mFnc;
746  QStringList mAliases;
747  bool mUsesGeometry;
748  std::function < bool( const NodeFunction* node ) > mUsesGeometryFunc;
749  std::function < QSet<QString>( const NodeFunction* node ) > mReferencedColumnsFunc;
750  QSet<QString> mReferencedColumns;
751  };
752 
754  static QList<Function*> sFunctions;
755  static const QList<Function*>& Functions();
756 
758  static QStringList sBuiltinFunctions;
759  static const QStringList& BuiltinFunctions();
760 
767  static bool registerFunction( Function* function, bool transferOwnership = false );
768 
773  static bool unregisterFunction( const QString& name );
774 
777  static QList<Function*> sOwnedFunctions;
778 
782  static void cleanRegisteredFunctions();
783 
785  static bool isFunctionName( const QString& name );
786 
788  static int functionIndex( const QString& name );
789 
793  static int functionCount();
794 
799  static QString quotedColumnRef( QString name );
800 
805  static QString quotedString( QString text );
806 
814  static QString quotedValue( const QVariant& value );
815 
824  static QString quotedValue( const QVariant& value, QVariant::Type type );
825 
827 
828  enum NodeType
829  {
836  ntCondition
837  };
838 
841  class CORE_EXPORT Node
842  {
843  public:
844  virtual ~Node() = default;
845 
851  virtual NodeType nodeType() const = 0;
852 
858  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) = 0;
859 
865  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) = 0;
866 
872  virtual QString dump() const = 0;
873 
882  virtual Node* clone() const = 0;
883 
894  virtual QSet<QString> referencedColumns() const = 0;
895 
899  virtual QSet<QString> referencedVariables() const = 0;
900 
909  virtual bool needsGeometry() const = 0;
910  };
911 
915  class CORE_EXPORT NamedNode
916  {
917  public:
918 
923  NamedNode( const QString& name, Node* node )
924  : name( name )
925  , node( node )
926  {}
927 
929  QString name;
930 
932  Node* node = nullptr;
933  };
934 
937  class CORE_EXPORT NodeList
938  {
939  public:
940  NodeList() : mHasNamedNodes( false ) {}
941  virtual ~NodeList() { qDeleteAll( mList ); }
943  void append( Node* node ) { mList.append( node ); mNameList.append( QString() ); }
944 
948  void append( NamedNode* node );
949 
952  int count() const { return mList.count(); }
953 
956  bool hasNamedNodes() const { return mHasNamedNodes; }
957 
961  QList<Node*> list() { return mList; }
962 
968  Node* at( int i ) { return mList.at( i ); }
969 
972  QStringList names() const { return mNameList; }
973 
975  NodeList* clone() const;
976 
977  virtual QString dump() const;
978 
979  protected:
980  QList<Node*> mList;
981  QStringList mNameList;
982 
983  private:
984 
985  bool mHasNamedNodes;
986  };
987 
990  class CORE_EXPORT NodeUnaryOperator : public Node
991  {
992  public:
994  : mOp( op )
995  , mOperand( operand )
996  {}
997  ~NodeUnaryOperator() { delete mOperand; }
998 
999  UnaryOperator op() const { return mOp; }
1000  Node* operand() const { return mOperand; }
1001 
1002  virtual NodeType nodeType() const override { return ntUnaryOperator; }
1003  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1004  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1005  virtual QString dump() const override;
1006 
1007  virtual QSet<QString> referencedColumns() const override;
1008  virtual QSet<QString> referencedVariables() const override;
1009  virtual bool needsGeometry() const override { return mOperand->needsGeometry(); }
1010  virtual Node* clone() const override;
1011 
1012  protected:
1014  Node* mOperand = nullptr;
1015  };
1016 
1019  class CORE_EXPORT NodeBinaryOperator : public Node
1020  {
1021  public:
1022  NodeBinaryOperator( BinaryOperator op, Node* opLeft, Node* opRight )
1023  : mOp( op )
1024  , mOpLeft( opLeft )
1025  , mOpRight( opRight )
1026  {}
1027  ~NodeBinaryOperator() { delete mOpLeft; delete mOpRight; }
1028 
1029  BinaryOperator op() const { return mOp; }
1030  Node* opLeft() const { return mOpLeft; }
1031  Node* opRight() const { return mOpRight; }
1032 
1033  virtual NodeType nodeType() const override { return ntBinaryOperator; }
1034  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1035  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1036  virtual QString dump() const override;
1037 
1038  virtual QSet<QString> referencedColumns() const override;
1039  virtual QSet<QString> referencedVariables() const override;
1040  virtual bool needsGeometry() const override;
1041  virtual Node* clone() const override;
1042 
1043  int precedence() const;
1044  bool leftAssociative() const;
1045 
1046  protected:
1047  bool compare( double diff );
1048  int computeInt( int x, int y );
1049  double computeDouble( double x, double y );
1050 
1055  QDateTime computeDateTimeFromInterval( const QDateTime& d, QgsInterval* i );
1056 
1058  Node* mOpLeft = nullptr;
1059  Node* mOpRight = nullptr;
1060  };
1061 
1064  class CORE_EXPORT NodeInOperator : public Node
1065  {
1066  public:
1067  NodeInOperator( Node* node, NodeList* list, bool notin = false )
1068  : mNode( node )
1069  , mList( list )
1070  , mNotIn( notin )
1071  {}
1072  virtual ~NodeInOperator() { delete mNode; delete mList; }
1073 
1074  Node* node() const { return mNode; }
1075  bool isNotIn() const { return mNotIn; }
1076  NodeList* list() const { return mList; }
1077 
1078  virtual NodeType nodeType() const override { return ntInOperator; }
1079  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1080  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1081  virtual QString dump() const override;
1082 
1083  virtual QSet<QString> referencedColumns() const override;
1084  virtual QSet<QString> referencedVariables() const override;
1085  virtual bool needsGeometry() const override;
1086  virtual Node* clone() const override;
1087 
1088  protected:
1089  Node* mNode = nullptr;
1090  NodeList* mList = nullptr;
1091  bool mNotIn;
1092  };
1093 
1096  class CORE_EXPORT NodeFunction : public Node
1097  {
1098  public:
1099  NodeFunction( int fnIndex, NodeList* args );
1100 
1101  virtual ~NodeFunction() { delete mArgs; }
1102 
1103  int fnIndex() const { return mFnIndex; }
1104  NodeList* args() const { return mArgs; }
1105 
1106  virtual NodeType nodeType() const override { return ntFunction; }
1107  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1108  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1109  virtual QString dump() const override;
1110 
1111  virtual QSet<QString> referencedColumns() const override;
1112  virtual QSet<QString> referencedVariables() const override;
1113  virtual bool needsGeometry() const override;
1114  virtual Node* clone() const override;
1115 
1117  static bool validateParams( int fnIndex, NodeList* args, QString& error );
1118 
1119  private:
1120  int mFnIndex;
1121  NodeList* mArgs = nullptr;
1122 
1123  };
1124 
1127  class CORE_EXPORT NodeLiteral : public Node
1128  {
1129  public:
1130  NodeLiteral( const QVariant& value )
1131  : mValue( value )
1132  {}
1133 
1135  inline QVariant value() const { return mValue; }
1136 
1137  virtual NodeType nodeType() const override { return ntLiteral; }
1138  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1139  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1140  virtual QString dump() const override;
1141 
1142  virtual QSet<QString> referencedColumns() const override;
1143  virtual QSet<QString> referencedVariables() const override;
1144  virtual bool needsGeometry() const override { return false; }
1145  virtual Node* clone() const override;
1146 
1147  protected:
1148  QVariant mValue;
1149  };
1150 
1153  class CORE_EXPORT NodeColumnRef : public Node
1154  {
1155  public:
1156  NodeColumnRef( const QString& name )
1157  : mName( name )
1158  , mIndex( -1 )
1159  {}
1160 
1162  QString name() const { return mName; }
1163 
1164  virtual NodeType nodeType() const override { return ntColumnRef; }
1165  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1166  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1167  virtual QString dump() const override;
1168 
1169  virtual QSet<QString> referencedColumns() const override;
1170  virtual QSet<QString> referencedVariables() const override;
1171  virtual bool needsGeometry() const override { return false; }
1172 
1173  virtual Node* clone() const override;
1174 
1175  protected:
1176  QString mName;
1177  int mIndex;
1178  };
1179 
1182  class CORE_EXPORT WhenThen
1183  {
1184  public:
1185  WhenThen( Node* whenExp, Node* thenExp )
1186  : mWhenExp( whenExp )
1187  , mThenExp( thenExp )
1188  {}
1189  ~WhenThen() { delete mWhenExp; delete mThenExp; }
1190 
1192  WhenThen( const WhenThen& rh ) = delete;
1194  WhenThen& operator=( const WhenThen& rh ) = delete;
1195 
1196  // protected:
1197  Node* mWhenExp = nullptr;
1198  Node* mThenExp = nullptr;
1199 
1200  };
1201  typedef QList<WhenThen*> WhenThenList;
1202 
1205  class CORE_EXPORT NodeCondition : public Node
1206  {
1207  public:
1208  NodeCondition( WhenThenList* conditions, Node* elseExp = nullptr )
1209  : mConditions( *conditions )
1210  , mElseExp( elseExp )
1211  { delete conditions; }
1212  NodeCondition( const WhenThenList& conditions, Node* elseExp = nullptr )
1213  : mConditions( conditions )
1214  , mElseExp( elseExp )
1215  {}
1216  ~NodeCondition() { delete mElseExp; qDeleteAll( mConditions ); }
1217 
1218  virtual NodeType nodeType() const override { return ntCondition; }
1219  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1220  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1221  virtual QString dump() const override;
1222 
1223  virtual QSet<QString> referencedColumns() const override;
1224  virtual QSet<QString> referencedVariables() const override;
1225  virtual bool needsGeometry() const override;
1226  virtual Node* clone() const override;
1227 
1228  protected:
1229  WhenThenList mConditions;
1230  Node* mElseExp = nullptr;
1231  };
1232 
1237  static QString helpText( QString name );
1238 
1246  static QString variableHelpText( const QString& variableName, bool showValue = true, const QVariant& value = QVariant() );
1247 
1251  static QString group( const QString& group );
1252 
1259  static QString formatPreviewString( const QVariant& value );
1260 
1261  protected:
1262  void initGeomCalculator();
1263 
1264  struct HelpArg
1265  {
1266  HelpArg( const QString& arg, const QString& desc, bool descOnly = false, bool syntaxOnly = false,
1267  bool optional = false, const QString& defaultVal = QString() )
1268  : mArg( arg )
1269  , mDescription( desc )
1270  , mDescOnly( descOnly )
1271  , mSyntaxOnly( syntaxOnly )
1272  , mOptional( optional )
1273  , mDefaultVal( defaultVal )
1274  {}
1275 
1276  QString mArg;
1277  QString mDescription;
1281  QString mDefaultVal;
1282  };
1283 
1285  {
1286  HelpExample( const QString& expression, const QString& returns, const QString& note = QString::null )
1287  : mExpression( expression )
1288  , mReturns( returns )
1289  , mNote( note )
1290  {}
1291 
1292  QString mExpression;
1293  QString mReturns;
1294  QString mNote;
1295  };
1296 
1298  {
1299  HelpVariant( const QString& name, const QString& description,
1300  const QList<HelpArg>& arguments = QList<HelpArg>(),
1301  bool variableLenArguments = false,
1302  const QList<HelpExample>& examples = QList<HelpExample>(),
1303  const QString& notes = QString::null )
1304  : mName( name )
1305  , mDescription( description )
1306  , mArguments( arguments )
1307  , mVariableLenArguments( variableLenArguments )
1308  , mExamples( examples )
1309  , mNotes( notes )
1310  {}
1311 
1312  QString mName;
1313  QString mDescription;
1314  QList<HelpArg> mArguments;
1316  QList<HelpExample> mExamples;
1317  QString mNotes;
1318  };
1319 
1320  struct Help
1321  {
1322  Help() {}
1323 
1324  Help( const QString& name, const QString& type, const QString& description, const QList<HelpVariant>& variants )
1325  : mName( name )
1326  , mType( type )
1327  , mDescription( description )
1328  , mVariants( variants )
1329  {}
1330 
1331  QString mName;
1332  QString mType;
1333  QString mDescription;
1334  QList<HelpVariant> mVariants;
1335  };
1336 
1343  void detach();
1344 
1345  QgsExpressionPrivate* d = nullptr;
1346 
1347  static QHash<QString, Help> sFunctionHelpTexts;
1348  static QHash<QString, QString> sVariableHelpTexts;
1349  static QHash<QString, QString> sGroups;
1350 
1352  static void initFunctionHelp();
1354  static void initVariableHelp();
1355 
1356  friend class QgsOgcUtils;
1357 };
1358 
1359 
1360 
1362 
1363 #endif // QGSEXPRESSION_H
Class for parsing and evaluation of expressions (formerly called "search strings").
StaticFunction(const QString &fnname, const 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...
UnaryOperator
list of unary operators
virtual bool needsGeometry() const override
Abstract virtual method which returns if the geometry is required to evaluate this expression...
Parameter(const QString &name, bool optional=false, const QVariant &defaultValue=QVariant())
Constructor for Parameter.
static QHash< QString, QString > sGroups
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
const QString helpText() const
The help text for the function.
A abstract base class for defining QgsExpression functions.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeColumnRef(const QString &name)
HelpArg(const QString &arg, const QString &desc, bool descOnly=false, bool syntaxOnly=false, bool optional=false, const QString &defaultVal=QString())
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
Container of fields for a vector layer.
Definition: qgsfields.h:39
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:79
int count() const
Returns the number of nodes in the list.
static QList< Function * > sFunctions
c++ helper class for defining QgsExpression functions.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:136
QList< HelpVariant > mVariants
QString name
Node name.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
StaticFunction(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...
Function(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.
bool lazyEval() const
True if this function should use lazy evaluation.
QString name() const
Returns the name of the parameter.
static QStringList sBuiltinFunctions
QString group() const
Returns the first group which the function belongs to.
static QHash< QString, Help > sFunctionHelpTexts
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.
StaticFunction(const QString &fnname, const 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...
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 &notes=QString::null)
QList< Parameter > ParameterList
List of parameters, used for function definition.
Function(const QString &fnname, const 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.
bool hasNamedNodes() const
Returns true if list contains any named nodes.
QStringList names() const
Returns a list of names for nodes.
QList< Node * > mList
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.
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)
bool isContextual() const
Returns whether the function is only available if provided by a QgsExpressionContext object...
QList< HelpArg > mArguments
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:45
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
Definition: MathUtils.cc:452
HelpExample(const QString &expression, const QString &returns, const QString &note=QString::null)
WhenThen(Node *whenExp, Node *thenExp)
QList< HelpExample > mExamples
Function(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.
NodeUnaryOperator(UnaryOperator op, Node *operand)
Function(const QString &fnname, const 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.
A representation of the interval between two datetime values.
Definition: qgsinterval.h:37
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
bool optional() const
Returns true if the parameter is optional.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
General purpose distance and area calculator.
QString name() const
The name of the column.
static QHash< QString, QString > sVariableHelpTexts
virtual bool needsGeometry() const override
Abstract virtual method which returns if the geometry is required to evaluate this expression...
Node * at(int i)
Get the node at position i in the list.
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
static QList< Function * > sOwnedFunctions
List of functions owned by the expression engine.
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.
static QString helpText(QString name)
Returns the help text for a specified function.
NamedNode(const QString &name, Node *node)
Constructor for NamedNode.
int params() const
The number of parameters this function takes.
void append(Node *node)
Takes ownership of the provided node.
int minParams() const
The minimum number of parameters this function takes.
virtual NodeType nodeType() const override
Abstract virtual that returns the type of this node.
This is the base class for vector data providers.
QStringList groups() const
Returns a list of the groups the function belongs to.
Represents a vector layer which manages a vector based data sets.
virtual QStringList aliases() const override
Returns a list of possible aliases for the function.
The QgsOgcUtils class provides various utility functions for conversion between OGC (Open Geospatial ...
Definition: qgsogcutils.h:44
NodeCondition(const WhenThenList &conditions, Node *elseExp=nullptr)
AreaUnit
Units of area.
Definition: qgsunittypes.h:65
const ParameterList & parameters() const
Returns the list of named parameters for the function, if set.
QList< Node * > list()
Get a list of all the nodes.
virtual bool isDeprecated() const
Returns true if the function is deprecated and should not be presented as a valid option to users in ...