QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 #include "qgsexpressionnode.h"
33 
34 class QgsFeature;
35 class QgsGeometry;
36 class QgsOgcUtils;
37 class QgsVectorLayer;
39 class QgsField;
40 class QgsFields;
41 class QgsDistanceArea;
42 class QDomElement;
44 class QgsExpressionPrivate;
46 
104 class CORE_EXPORT QgsExpression
105 {
106  Q_DECLARE_TR_FUNCTIONS( QgsExpression )
107  public:
108 
113  struct CORE_EXPORT ParserError
114  {
116  {
117  Unknown = 0,
118  FunctionUnknown = 1,
119  FunctionWrongArgs = 2,
120  FunctionInvalidParams = 3,
121  FunctionNamedArgsError = 4
122  };
123 
127  ParserErrorType errorType = ParserErrorType::Unknown;
128 
132  QString errorMsg;
133 
138  int firstLine = 0;
139 
144  int firstColumn = 0;
145 
149  int lastLine = 0;
150 
154  int lastColumn = 0;
155  };
156 
163  QgsExpression( const QString &expr );
164 
170  QgsExpression( const QgsExpression &other );
171 
177  QgsExpression &operator=( const QgsExpression &other );
178 
184  operator QString() const SIP_SKIP;
185 
191  QgsExpression();
192 
193  ~QgsExpression();
194 
201  bool operator==( const QgsExpression &other ) const;
202 
209  bool isValid() const;
210 
212  bool hasParserError() const;
214  QString parserErrorString() const;
215 
220  QList<QgsExpression::ParserError> parserErrors() const;
221 
227  const QgsExpressionNode *rootNode() const;
228 
234  bool prepare( const QgsExpressionContext *context );
235 
253  QSet<QString> referencedColumns() const;
254 
266  QSet<QString> referencedVariables() const;
267 
277  QSet<QString> referencedFunctions() const;
278 
279 #ifndef SIP_RUN
280 
287  QList<const QgsExpressionNode *> nodes( ) const;
288 
295  template <class T>
296  QList<const T *> findNodes( ) const
297  {
298  QList<const T *> lst;
299  const QList<const QgsExpressionNode *> allNodes( nodes() );
300  for ( const auto &node : allNodes )
301  {
302  const T *n = dynamic_cast<const T *>( node );
303  if ( n )
304  lst << n;
305  }
306  return lst;
307  }
308 #endif
309 
323  QSet<int> referencedAttributeIndexes( const QgsFields &fields ) const;
324 
326  bool needsGeometry() const;
327 
328  // evaluation
329 
335  QVariant evaluate();
336 
343  QVariant evaluate( const QgsExpressionContext *context );
344 
346  bool hasEvalError() const;
348  QString evalErrorString() const;
350  void setEvalErrorString( const QString &str );
351 
356  bool isField() const;
357 
366  static bool checkExpression( const QString &text, const QgsExpressionContext *context, QString &errorMessage SIP_OUT );
367 
373  void setExpression( const QString &expression );
374 
380  QString expression() const;
381 
388  QString dump() const;
389 
397  QgsDistanceArea *geomCalculator();
398 
412  void setGeomCalculator( const QgsDistanceArea *calc );
413 
421  QgsUnitTypes::DistanceUnit distanceUnits() const;
422 
432  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
433 
441  QgsUnitTypes::AreaUnit areaUnits() const;
442 
452  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
453 
465  static QString replaceExpressionText( const QString &action, const QgsExpressionContext *context,
466  const QgsDistanceArea *distanceArea = nullptr );
467 
475  static QSet<QString> referencedVariables( const QString &text );
476 
487  static double evaluateToDouble( const QString &text, double fallbackValue );
488 
490  {
500  };
501 
502  static const QList<QgsExpressionFunction *> &Functions();
503 
504  static const QStringList &BuiltinFunctions();
505 
513  static bool registerFunction( QgsExpressionFunction *function, bool transferOwnership = false );
514 
520  static bool unregisterFunction( const QString &name );
521 
526  static void cleanRegisteredFunctions();
527 
529  static bool isFunctionName( const QString &name );
530 
532  static int functionIndex( const QString &name );
533 
538  static int functionCount();
539 
545  static QString quotedColumnRef( QString name );
546 
552  static QString quotedString( QString text );
553 
562  static QString quotedValue( const QVariant &value );
563 
573  static QString quotedValue( const QVariant &value, QVariant::Type type );
574 
576 
583  static QString helpText( QString name );
584 
590  static QStringList tags( const QString &name );
591 
598  static QString variableHelpText( const QString &variableName );
599 
609  static QString formatVariableHelp( const QString &description, bool showValue = true, const QVariant &value = QVariant() );
610 
615  static QString group( const QString &group );
616 
626  static QString formatPreviewString( const QVariant &value, bool htmlOutput = true, int maximumPreviewLength = 60 );
627 
636  static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value );
637 
648  static bool isFieldEqualityExpression( const QString &expression, QString &field SIP_OUT, QVariant &value SIP_OUT );
649 
664  static bool attemptReduceToInClause( const QStringList &expressions, QString &result SIP_OUT );
665 
666 #ifdef SIP_RUN
667  SIP_PYOBJECT __repr__();
668  % MethodCode
669  QString str = QStringLiteral( "<QgsExpression: '%1'>" ).arg( sipCpp->expression() );
670  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
671  % End
672 #endif
673 
674  private:
675  void initGeomCalculator( const QgsExpressionContext *context );
676 
683  void detach() SIP_SKIP;
684 
685  QgsExpressionPrivate *d = nullptr;
686 
688  static void initFunctionHelp() SIP_SKIP;
690  static void initVariableHelp() SIP_SKIP;
691 
692  friend class QgsOgcUtils;
693 };
694 
696 
697 #endif // QGSEXPRESSION_H
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A abstract base class for defining QgsExpression functions.
Abstract base class for all nodes that can appear in an expression.
Class for parsing and evaluation of expressions (formerly called "search strings").
QList< const T * > findNodes() const
Returns a list of all nodes of the given class which are used in this expression.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
The QgsOgcUtils class provides various utility functions for conversion between OGC (Open Geospatial ...
Definition: qgsogcutils.h:51
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
AreaUnit
Units of area.
Definition: qgsunittypes.h:94
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
QString formatVariableHelp(const QString &variable, const QString &description, bool showValue, const QVariant &value)
Returns a HTML formatted string for use as a variable item help.
const QgsField & field
Definition: qgsfield.h:472
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
Details about any parser errors that were found when parsing the expression.
QString errorMsg
The message for the error at this location.