QGIS API Documentation  2.99.0-Master (37c43df)
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 <QMetaType>
20 #include <QStringList>
21 #include <QVariant>
22 #include <QList>
23 #include <QDomDocument>
24 #include <QCoreApplication>
25 #include <QSet>
26 #include <functional>
27 
28 #include "qgis.h"
29 #include "qgsunittypes.h"
30 #include "qgsinterval.h"
31 
32 class QgsFeature;
33 class QgsGeometry;
34 class QgsOgcUtils;
35 class QgsVectorLayer;
37 class QgsField;
38 class QgsFields;
39 class QgsDistanceArea;
40 class QDomElement;
42 class QgsExpressionPrivate;
43 
109 class CORE_EXPORT QgsExpression
110 {
111  Q_DECLARE_TR_FUNCTIONS( QgsExpression )
112  public:
113 
120  QgsExpression( const QString& expr );
121 
127  QgsExpression( const QgsExpression& other );
128 
134  QgsExpression& operator=( const QgsExpression& other );
135 
141  QgsExpression();
142 
143  ~QgsExpression();
144 
151  bool operator==( const QgsExpression& other ) const;
152 
159  bool isValid() const;
160 
162  bool hasParserError() const;
164  QString parserErrorString() const;
165 
166  class Node;
167 
169  const Node* rootNode() const;
170 
175  bool prepare( const QgsExpressionContext *context );
176 
186  QSet<QString> referencedColumns() const;
187 
195  QSet<QString> referencedVariables() const;
196 
202  QSet<int> referencedAttributeIndexes( const QgsFields& fields ) const;
203 
205  bool needsGeometry() const;
206 
207  // evaluation
208 
213  QVariant evaluate();
214 
220  QVariant evaluate( const QgsExpressionContext* context );
221 
223  bool hasEvalError() const;
225  QString evalErrorString() const;
227  void setEvalErrorString( const QString& str );
228 
232  bool isField() const { return rootNode() && dynamic_cast<const NodeColumnRef*>( rootNode() ) ;}
233 
241  static bool checkExpression( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
242 
248  void setExpression( const QString& expression );
249 
253  QString expression() const;
254 
259  QString dump() const;
260 
267  QgsDistanceArea *geomCalculator();
268 
277  void setGeomCalculator( const QgsDistanceArea* calc );
278 
285  QgsUnitTypes::DistanceUnit distanceUnits() const;
286 
293  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
294 
301  QgsUnitTypes::AreaUnit areaUnits() const;
302 
309  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
310 
321  static QString replaceExpressionText( const QString& action, const QgsExpressionContext* context,
322  const QgsDistanceArea* distanceArea = nullptr );
323 
333  static double evaluateToDouble( const QString& text, const double fallbackValue );
334 
340  {
343  };
344 
350  {
351  // logical
354 
355  // comparison
369 
370  // math
378 
379  // strings
381  };
382 
384  {
394  };
395 
397  static const char* BinaryOperatorText[];
398 
400  static const char* UnaryOperatorText[];
401 
406  class CORE_EXPORT Parameter
407  {
408  public:
409 
415  Parameter( const QString& name,
416  bool optional = false,
417  const QVariant& defaultValue = QVariant() )
418  : mName( name )
419  , mOptional( optional )
420  , mDefaultValue( defaultValue )
421  {}
422 
424  QString name() const { return mName; }
425 
427  bool optional() const { return mOptional; }
428 
430  QVariant defaultValue() const { return mDefaultValue; }
431 
432  bool operator==( const Parameter& other ) const
433  {
434  return ( QString::compare( mName, other.mName, Qt::CaseInsensitive ) == 0 );
435  }
436 
437  private:
438  QString mName;
439  bool mOptional;
440  QVariant mDefaultValue;
441  };
442 
444  typedef QList< Parameter > ParameterList;
445 
448  typedef QVariant( *FcnEval )( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent );
449 
450  class NodeFunction;
451 
455  class CORE_EXPORT Function
456  {
457  public:
458 
460  Function( const QString& fnname,
461  int params,
462  const QString& group,
463  const QString& helpText = QString(),
464  bool lazyEval = false,
465  bool handlesNull = false,
466  bool isContextual = false )
467  : mName( fnname )
468  , mParams( params )
469  , mGroups( group.isEmpty() ? QStringList() : QStringList() << group )
470  , mHelpText( helpText )
471  , mLazyEval( lazyEval )
472  , mHandlesNull( handlesNull )
473  , mIsContextual( isContextual )
474  {
475  }
476 
480  Function( const QString& fnname,
481  int params,
482  const QStringList& groups,
483  const QString& helpText = QString(),
484  bool lazyEval = false,
485  bool handlesNull = false,
486  bool isContextual = false )
487  : mName( fnname )
488  , mParams( params )
489  , mGroups( groups )
490  , mHelpText( helpText )
491  , mLazyEval( lazyEval )
492  , mHandlesNull( handlesNull )
493  , mIsContextual( isContextual )
494  {
495  }
496 
500  Function( const QString& fnname,
501  const ParameterList& params,
502  const QString& group,
503  const QString& helpText = QString(),
504  bool lazyEval = false,
505  bool handlesNull = false,
506  bool isContextual = false )
507  : mName( fnname )
508  , mParams( 0 )
509  , mParameterList( params )
510  , mGroups( group.isEmpty() ? QStringList() : QStringList() << group )
511  , mHelpText( helpText )
512  , mLazyEval( lazyEval )
513  , mHandlesNull( handlesNull )
514  , mIsContextual( isContextual )
515  {}
516 
520  Function( const QString& fnname,
521  const ParameterList& params,
522  const QStringList& groups,
523  const QString& helpText = QString(),
524  bool lazyEval = false,
525  bool handlesNull = false,
526  bool isContextual = false )
527  : mName( fnname )
528  , mParams( 0 )
529  , mParameterList( params )
530  , mGroups( groups )
531  , mHelpText( helpText )
532  , mLazyEval( lazyEval )
533  , mHandlesNull( handlesNull )
534  , mIsContextual( isContextual )
535  {}
536 
537  virtual ~Function() {}
538 
540  QString name() const { return mName; }
541 
543  int params() const { return mParameterList.isEmpty() ? mParams : mParameterList.count(); }
544 
546  int minParams() const
547  {
548  if ( mParameterList.isEmpty() )
549  return mParams;
550 
551  int min = 0;
552  Q_FOREACH ( const Parameter& param, mParameterList )
553  {
554  if ( !param.optional() )
555  min++;
556  }
557  return min;
558  }
559 
563  const ParameterList& parameters() const { return mParameterList; }
564 
566  virtual bool usesGeometry( const NodeFunction* node ) const;
567 
573  virtual QStringList aliases() const { return QStringList(); }
574 
578  bool lazyEval() const { return mLazyEval; }
579 
588  virtual QSet<QString> referencedColumns( const NodeFunction* node ) const;
589 
593  bool isContextual() const { return mIsContextual; }
594 
599  virtual bool isDeprecated() const { return mGroups.isEmpty() ? false : mGroups.contains( QStringLiteral( "deprecated" ) ); }
600 
604  QString group() const { return mGroups.isEmpty() ? QString() : mGroups.at( 0 ); }
605 
610  QStringList groups() const { return mGroups; }
611 
613  const QString helpText() const { return mHelpText.isEmpty() ? QgsExpression::helpText( mName ) : mHelpText; }
614 
621  virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) = 0;
622 
623  bool operator==( const Function& other ) const;
624 
625  virtual bool handlesNull() const { return mHandlesNull; }
626 
627  private:
628  QString mName;
629  int mParams;
630  ParameterList mParameterList;
631  QStringList mGroups;
632  QString mHelpText;
633  bool mLazyEval;
634  bool mHandlesNull;
635  bool mIsContextual; //if true function is only available through an expression context
636  };
637 
642  class StaticFunction : public Function
643  {
644  public:
645 
648  StaticFunction( const QString& fnname,
649  int params,
650  FcnEval fcn,
651  const QString& group,
652  const QString& helpText = QString(),
653  bool usesGeometry = false,
654  const QSet<QString>& referencedColumns = QSet<QString>(),
655  bool lazyEval = false,
656  const QStringList& aliases = QStringList(),
657  bool handlesNull = false )
658  : Function( fnname, params, group, helpText, lazyEval, handlesNull )
659  , mFnc( fcn )
660  , mAliases( aliases )
661  , mUsesGeometry( usesGeometry )
662  , mReferencedColumns( referencedColumns )
663  {}
664 
667  StaticFunction( const QString& fnname,
668  const ParameterList& params,
669  FcnEval fcn,
670  const QString& group,
671  const QString& helpText = QString(),
672  bool usesGeometry = false,
673  const QSet<QString>& referencedColumns = QSet<QString>(),
674  bool lazyEval = false,
675  const QStringList& aliases = QStringList(),
676  bool handlesNull = false )
677  : Function( fnname, params, group, helpText, lazyEval, handlesNull )
678  , mFnc( fcn )
679  , mAliases( aliases )
680  , mUsesGeometry( usesGeometry )
681  , mReferencedColumns( referencedColumns )
682  {}
683 
695  StaticFunction( const QString& fnname,
696  const ParameterList& params,
697  FcnEval fcn,
698  const QString& group,
699  const QString& helpText,
700  std::function < bool ( const NodeFunction* node ) > usesGeometry,
701  std::function < QSet<QString>( const NodeFunction* node ) > referencedColumns,
702  bool lazyEval = false,
703  const QStringList& aliases = QStringList(),
704  bool handlesNull = false );
705 
709  StaticFunction( const QString& fnname,
710  const ParameterList& params,
711  FcnEval fcn,
712  const QStringList& groups,
713  const QString& helpText = QString(),
714  bool usesGeometry = false,
715  const QSet<QString>& referencedColumns = QSet<QString>(),
716  bool lazyEval = false,
717  const QStringList& aliases = QStringList(),
718  bool handlesNull = false )
719  : Function( fnname, params, groups, helpText, lazyEval, handlesNull )
720  , mFnc( fcn )
721  , mAliases( aliases )
722  , mUsesGeometry( usesGeometry )
723  , mReferencedColumns( referencedColumns )
724  {}
725 
726  virtual ~StaticFunction() {}
727 
734  virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) override
735  {
736  return mFnc ? mFnc( values, context, parent ) : QVariant();
737  }
738 
739  virtual QStringList aliases() const override { return mAliases; }
740 
741  virtual bool usesGeometry( const QgsExpression::NodeFunction* node ) const override;
742 
743  virtual QSet<QString> referencedColumns( const QgsExpression::NodeFunction* node ) const override;
744 
745  private:
746  FcnEval mFnc;
747  QStringList mAliases;
748  bool mUsesGeometry;
749  std::function < bool( const NodeFunction* node ) > mUsesGeometryFunc;
750  std::function < QSet<QString>( const NodeFunction* node ) > mReferencedColumnsFunc;
751  QSet<QString> mReferencedColumns;
752  };
753 
755  static QList<Function*> gmFunctions;
756  static const QList<Function*>& Functions();
757 
759  static QStringList gmBuiltinFunctions;
760  static const QStringList& BuiltinFunctions();
761 
768  static bool registerFunction( Function* function, bool transferOwnership = false );
769 
774  static bool unregisterFunction( const QString& name );
775 
778  static QList<Function*> gmOwnedFunctions;
779 
783  static void cleanRegisteredFunctions();
784 
786  static bool isFunctionName( const QString& name );
787 
789  static int functionIndex( const QString& name );
790 
794  static int functionCount();
795 
800  static QString quotedColumnRef( QString name );
801 
806  static QString quotedString( QString text );
807 
815  static QString quotedValue( const QVariant& value );
816 
825  static QString quotedValue( const QVariant& value, QVariant::Type type );
826 
828 
829  enum NodeType
830  {
837  ntCondition
838  };
839 
842  class CORE_EXPORT Node
843  {
844  public:
845  virtual ~Node() {}
846 
852  virtual NodeType nodeType() const = 0;
853 
859  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) = 0;
860 
866  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) = 0;
867 
873  virtual QString dump() const = 0;
874 
883  virtual Node* clone() const = 0;
884 
895  virtual QSet<QString> referencedColumns() const = 0;
896 
900  virtual QSet<QString> referencedVariables() const = 0;
901 
910  virtual bool needsGeometry() const = 0;
911  };
912 
916  class CORE_EXPORT NamedNode
917  {
918  public:
919 
924  NamedNode( const QString& name, Node* node )
925  : name( name )
926  , node( node )
927  {}
928 
930  QString name;
931 
934  };
935 
938  class CORE_EXPORT NodeList
939  {
940  public:
941  NodeList() : mHasNamedNodes( false ) {}
942  virtual ~NodeList() { qDeleteAll( mList ); }
944  void append( Node* node ) { mList.append( node ); mNameList.append( QString() ); }
945 
949  void append( NamedNode* node );
950 
953  int count() const { return mList.count(); }
954 
957  bool hasNamedNodes() const { return mHasNamedNodes; }
958 
962  QList<Node*> list() { return mList; }
963 
969  Node* at( int i ) { return mList.at( i ); }
970 
973  QStringList names() const { return mNameList; }
974 
976  NodeList* clone() const;
977 
978  virtual QString dump() const;
979 
980  protected:
981  QList<Node*> mList;
982  QStringList mNameList;
983 
984  private:
985 
986  bool mHasNamedNodes;
987  };
988 
991  class CORE_EXPORT NodeUnaryOperator : public Node
992  {
993  public:
995  : mOp( op )
996  , mOperand( operand )
997  {}
998  ~NodeUnaryOperator() { delete mOperand; }
999 
1000  UnaryOperator op() const { return mOp; }
1001  Node* operand() const { return mOperand; }
1002 
1003  virtual NodeType nodeType() const override { return ntUnaryOperator; }
1004  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1005  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1006  virtual QString dump() const override;
1007 
1008  virtual QSet<QString> referencedColumns() const override;
1009  virtual QSet<QString> referencedVariables() const override;
1010  virtual bool needsGeometry() const override { return mOperand->needsGeometry(); }
1011  virtual Node* clone() const override;
1012 
1013  protected:
1016  };
1017 
1020  class CORE_EXPORT NodeBinaryOperator : public Node
1021  {
1022  public:
1023  NodeBinaryOperator( BinaryOperator op, Node* opLeft, Node* opRight )
1024  : mOp( op )
1025  , mOpLeft( opLeft )
1026  , mOpRight( opRight )
1027  {}
1028  ~NodeBinaryOperator() { delete mOpLeft; delete mOpRight; }
1029 
1030  BinaryOperator op() const { return mOp; }
1031  Node* opLeft() const { return mOpLeft; }
1032  Node* opRight() const { return mOpRight; }
1033 
1034  virtual NodeType nodeType() const override { return ntBinaryOperator; }
1035  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1036  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1037  virtual QString dump() const override;
1038 
1039  virtual QSet<QString> referencedColumns() const override;
1040  virtual QSet<QString> referencedVariables() const override;
1041  virtual bool needsGeometry() const override;
1042  virtual Node* clone() const override;
1043 
1044  int precedence() const;
1045  bool leftAssociative() const;
1046 
1047  protected:
1048  bool compare( double diff );
1049  int computeInt( int x, int y );
1050  double computeDouble( double x, double y );
1051 
1056  QDateTime computeDateTimeFromInterval( const QDateTime& d, QgsInterval* i );
1057 
1061  };
1062 
1065  class CORE_EXPORT NodeInOperator : public Node
1066  {
1067  public:
1068  NodeInOperator( Node* node, NodeList* list, bool notin = false )
1069  : mNode( node )
1070  , mList( list )
1071  , mNotIn( notin )
1072  {}
1073  virtual ~NodeInOperator() { delete mNode; delete mList; }
1074 
1075  Node* node() const { return mNode; }
1076  bool isNotIn() const { return mNotIn; }
1077  NodeList* list() const { return mList; }
1078 
1079  virtual NodeType nodeType() const override { return ntInOperator; }
1080  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1081  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1082  virtual QString dump() const override;
1083 
1084  virtual QSet<QString> referencedColumns() const override;
1085  virtual QSet<QString> referencedVariables() const override;
1086  virtual bool needsGeometry() const override;
1087  virtual Node* clone() const override;
1088 
1089  protected:
1092  bool mNotIn;
1093  };
1094 
1097  class CORE_EXPORT NodeFunction : public Node
1098  {
1099  public:
1100  NodeFunction( int fnIndex, NodeList* args );
1101 
1102  virtual ~NodeFunction() { delete mArgs; }
1103 
1104  int fnIndex() const { return mFnIndex; }
1105  NodeList* args() const { return mArgs; }
1106 
1107  virtual NodeType nodeType() const override { return ntFunction; }
1108  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1109  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1110  virtual QString dump() const override;
1111 
1112  virtual QSet<QString> referencedColumns() const override;
1113  virtual QSet<QString> referencedVariables() const override;
1114  virtual bool needsGeometry() const override;
1115  virtual Node* clone() const override;
1116 
1118  static bool validateParams( int fnIndex, NodeList* args, QString& error );
1119 
1120  private:
1121  int mFnIndex;
1122  NodeList* mArgs;
1123 
1124  };
1125 
1128  class CORE_EXPORT NodeLiteral : public Node
1129  {
1130  public:
1131  NodeLiteral( const QVariant& value )
1132  : mValue( value )
1133  {}
1134 
1136  inline QVariant value() const { return mValue; }
1137 
1138  virtual NodeType nodeType() const override { return ntLiteral; }
1139  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1140  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1141  virtual QString dump() const override;
1142 
1143  virtual QSet<QString> referencedColumns() const override;
1144  virtual QSet<QString> referencedVariables() const override;
1145  virtual bool needsGeometry() const override { return false; }
1146  virtual Node* clone() const override;
1147 
1148  protected:
1149  QVariant mValue;
1150  };
1151 
1154  class CORE_EXPORT NodeColumnRef : public Node
1155  {
1156  public:
1157  NodeColumnRef( const QString& name )
1158  : mName( name )
1159  , mIndex( -1 )
1160  {}
1161 
1163  QString name() const { return mName; }
1164 
1165  virtual NodeType nodeType() const override { return ntColumnRef; }
1166  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1167  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1168  virtual QString dump() const override;
1169 
1170  virtual QSet<QString> referencedColumns() const override;
1171  virtual QSet<QString> referencedVariables() const override;
1172  virtual bool needsGeometry() const override { return false; }
1173 
1174  virtual Node* clone() const override;
1175 
1176  protected:
1177  QString mName;
1178  int mIndex;
1179  };
1180 
1183  class CORE_EXPORT WhenThen
1184  {
1185  public:
1186  WhenThen( Node* whenExp, Node* thenExp )
1187  : mWhenExp( whenExp )
1188  , mThenExp( thenExp )
1189  {}
1190  ~WhenThen() { delete mWhenExp; delete mThenExp; }
1191 
1192  // protected:
1195 
1196  private:
1197  WhenThen( const WhenThen& rh );
1198  WhenThen& operator=( const WhenThen& rh );
1199  };
1200  typedef QList<WhenThen*> WhenThenList;
1201 
1204  class CORE_EXPORT NodeCondition : public Node
1205  {
1206  public:
1207  NodeCondition( WhenThenList* conditions, Node* elseExp = nullptr )
1208  : mConditions( *conditions )
1209  , mElseExp( elseExp )
1210  { delete conditions; }
1211  NodeCondition( const WhenThenList& conditions, Node* elseExp = nullptr )
1212  : mConditions( conditions )
1213  , mElseExp( elseExp )
1214  {}
1215  ~NodeCondition() { delete mElseExp; qDeleteAll( mConditions ); }
1216 
1217  virtual NodeType nodeType() const override { return ntCondition; }
1218  virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
1219  virtual bool prepare( QgsExpression* parent, const QgsExpressionContext* context ) override;
1220  virtual QString dump() const override;
1221 
1222  virtual QSet<QString> referencedColumns() const override;
1223  virtual QSet<QString> referencedVariables() const override;
1224  virtual bool needsGeometry() const override;
1225  virtual Node* clone() const override;
1226 
1227  protected:
1228  WhenThenList mConditions;
1230  };
1231 
1236  static QString helpText( QString name );
1237 
1245  static QString variableHelpText( const QString& variableName, bool showValue = true, const QVariant& value = QVariant() );
1246 
1250  static QString group( const QString& group );
1251 
1258  static QString formatPreviewString( const QVariant& value );
1259 
1260  protected:
1261  void initGeomCalculator();
1262 
1263  static QMap<QString, QVariant> gmSpecialColumns;
1264  static QMap<QString, QString> gmSpecialColumnGroups;
1265 
1266  struct HelpArg
1267  {
1268  HelpArg( const QString& arg, const QString& desc, bool descOnly = false, bool syntaxOnly = false,
1269  bool optional = false, const QString& defaultVal = QString() )
1270  : mArg( arg )
1271  , mDescription( desc )
1272  , mDescOnly( descOnly )
1273  , mSyntaxOnly( syntaxOnly )
1274  , mOptional( optional )
1275  , mDefaultVal( defaultVal )
1276  {}
1277 
1278  QString mArg;
1279  QString mDescription;
1283  QString mDefaultVal;
1284  };
1285 
1287  {
1288  HelpExample( const QString& expression, const QString& returns, const QString& note = QString::null )
1289  : mExpression( expression )
1290  , mReturns( returns )
1291  , mNote( note )
1292  {}
1293 
1294  QString mExpression;
1295  QString mReturns;
1296  QString mNote;
1297  };
1298 
1300  {
1301  HelpVariant( const QString& name, const QString& description,
1302  const QList<HelpArg>& arguments = QList<HelpArg>(),
1303  bool variableLenArguments = false,
1304  const QList<HelpExample>& examples = QList<HelpExample>(),
1305  const QString& notes = QString::null )
1306  : mName( name )
1307  , mDescription( description )
1308  , mArguments( arguments )
1309  , mVariableLenArguments( variableLenArguments )
1310  , mExamples( examples )
1311  , mNotes( notes )
1312  {}
1313 
1314  QString mName;
1315  QString mDescription;
1316  QList<HelpArg> mArguments;
1318  QList<HelpExample> mExamples;
1319  QString mNotes;
1320  };
1321 
1322  struct Help
1323  {
1324  Help() {}
1325 
1326  Help( const QString& name, const QString& type, const QString& description, const QList<HelpVariant>& variants )
1327  : mName( name )
1328  , mType( type )
1329  , mDescription( description )
1330  , mVariants( variants )
1331  {}
1332 
1333  QString mName;
1334  QString mType;
1335  QString mDescription;
1336  QList<HelpVariant> mVariants;
1337  };
1338 
1345  void detach();
1346 
1347  QgsExpressionPrivate* d;
1348 
1349  static QHash<QString, Help> gFunctionHelpTexts;
1350  static QHash<QString, QString> gVariableHelpTexts;
1351  static QHash<QString, QString> gGroups;
1352 
1354  static void initFunctionHelp();
1356  static void initVariableHelp();
1357 
1358  friend class QgsOgcUtils;
1359 };
1360 
1361 
1362 
1364 
1365 #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.
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:36
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:78
int count() const
Returns the number of nodes in the list.
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:135
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.
QString group() const
Returns the first group which the function belongs to.
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)
QgsExpressionPrivate * d
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
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.
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 QMap< QString, QVariant > gmSpecialColumns
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.
Definition: qgsfield.h:47
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)
static QMap< QString, QString > gmSpecialColumnGroups
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:34
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:42
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.
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.
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 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.
static QStringList gmBuiltinFunctions
int params() const
The number of parameters this function takes.
void append(Node *node)
Takes ownership of the provided node.
int minParams() const
The mininum number of parameters this function takes.
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.
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:43
NodeCondition(const WhenThenList &conditions, Node *elseExp=nullptr)
AreaUnit
Units of area.
Definition: qgsunittypes.h:64
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 ...