QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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 "qgsinterval.h"
31#include "qgsexpressionnode.h"
32
33class QgsFeature;
34class QgsGeometry;
35class QgsOgcUtils;
36class QgsVectorLayer;
38class QgsField;
39class QgsFields;
40class QgsDistanceArea;
41class QDomElement;
43class QgsExpressionPrivate;
45
46#ifndef SIP_RUN
48struct HelpArg
49{
50 HelpArg( const QString &arg, const QString &desc, bool descOnly = false, bool syntaxOnly = false,
51 bool optional = false, const QString &defaultVal = QString() )
52 : mArg( arg )
53 , mDescription( desc )
54 , mDescOnly( descOnly )
55 , mSyntaxOnly( syntaxOnly )
56 , mOptional( optional )
57 , mDefaultVal( defaultVal )
58 {}
59
60 QString mArg;
61 QString mDescription;
62 bool mDescOnly;
63 bool mSyntaxOnly;
64 bool mOptional;
65 QString mDefaultVal;
66};
67
68struct HelpExample
69{
70 HelpExample( const QString &expression, const QString &returns, const QString &note = QString() )
71 : mExpression( expression )
72 , mReturns( returns )
73 , mNote( note )
74 {}
75
76 QString mExpression;
77 QString mReturns;
78 QString mNote;
79};
80
81
82struct HelpVariant
83{
84 HelpVariant( const QString &name, const QString &description,
85 const QList<HelpArg> &arguments = QList<HelpArg>(),
86 bool variableLenArguments = false,
87 const QList<HelpExample> &examples = QList<HelpExample>(),
88 const QString &notes = QString(),
89 const QStringList &tags = QStringList() )
90 : mName( name )
91 , mDescription( description )
92 , mArguments( arguments )
93 , mVariableLenArguments( variableLenArguments )
94 , mExamples( examples )
95 , mNotes( notes )
96 , mTags( tags )
97 {}
98
99 QString mName;
100 QString mDescription;
101 QList<HelpArg> mArguments;
102 bool mVariableLenArguments;
103 QList<HelpExample> mExamples;
104 QString mNotes;
105 QStringList mTags;
106};
107
108
109struct Help
110{
112 Help() = default;
113
114 Help( const QString &name, const QString &type, const QString &description, const QList<HelpVariant> &variants )
115 : mName( name )
116 , mType( type )
117 , mDescription( description )
118 , mVariants( variants )
119 {}
120
121 QString mName;
122 QString mType;
123 QString mDescription;
124 QList<HelpVariant> mVariants;
125};
126
127typedef QHash<QString, Help> HelpTextHash;
128
130#endif
131
187class CORE_EXPORT QgsExpression
188{
189 Q_DECLARE_TR_FUNCTIONS( QgsExpression )
190 public:
191
196 struct CORE_EXPORT ParserError
197 {
199 {
200 Unknown = 0,
201 FunctionUnknown = 1,
202 FunctionWrongArgs = 2,
203 FunctionInvalidParams = 3,
204 FunctionNamedArgsError = 4
205 };
206
210 ParserErrorType errorType = ParserErrorType::Unknown;
211
215 QString errorMsg;
216
221 int firstLine = 0;
222
227 int firstColumn = 0;
228
232 int lastLine = 0;
233
237 int lastColumn = 0;
238 };
239
246 QgsExpression( const QString &expr );
247
253 QgsExpression( const QgsExpression &other );
254
260 QgsExpression &operator=( const QgsExpression &other );
261
267 operator QString() const SIP_SKIP;
268
275
276 ~QgsExpression();
277
284 bool operator==( const QgsExpression &other ) const;
285
292 bool isValid() const;
293
295 bool hasParserError() const;
297 QString parserErrorString() const;
298
303 QList<QgsExpression::ParserError> parserErrors() const;
304
310 const QgsExpressionNode *rootNode() const;
311
317 bool prepare( const QgsExpressionContext *context );
318
336 QSet<QString> referencedColumns() const;
337
349 QSet<QString> referencedVariables() const;
350
360 QSet<QString> referencedFunctions() const;
361
362#ifndef SIP_RUN
363
370 QList<const QgsExpressionNode *> nodes( ) const;
371
378 template <class T>
379 QList<const T *> findNodes( ) const
380 {
381 QList<const T *> lst;
382 const QList<const QgsExpressionNode *> allNodes( nodes() );
383 for ( const auto &node : allNodes )
384 {
385 const T *n = dynamic_cast<const T *>( node );
386 if ( n )
387 lst << n;
388 }
389 return lst;
390 }
391#endif
392
406 QSet<int> referencedAttributeIndexes( const QgsFields &fields ) const;
407
409 bool needsGeometry() const;
410
411 // evaluation
412
418 QVariant evaluate();
419
426 QVariant evaluate( const QgsExpressionContext *context );
427
429 bool hasEvalError() const;
431 QString evalErrorString() const;
433 void setEvalErrorString( const QString &str );
434
441 bool isField() const;
442
455 static int expressionToLayerFieldIndex( const QString &expression, const QgsVectorLayer *layer );
456
469 static QString quoteFieldExpression( const QString &expression, const QgsVectorLayer *layer );
470
479 static bool checkExpression( const QString &text, const QgsExpressionContext *context, QString &errorMessage SIP_OUT );
480
486 void setExpression( const QString &expression );
487
493 QString expression() const;
494
501 QString dump() const;
502
510 QgsDistanceArea *geomCalculator();
511
525 void setGeomCalculator( const QgsDistanceArea *calc );
526
534 Qgis::DistanceUnit distanceUnits() const;
535
545 void setDistanceUnits( Qgis::DistanceUnit unit );
546
554 Qgis::AreaUnit areaUnits() const;
555
565 void setAreaUnits( Qgis::AreaUnit unit );
566
578 static QString replaceExpressionText( const QString &action, const QgsExpressionContext *context,
579 const QgsDistanceArea *distanceArea = nullptr );
580
588 static QSet<QString> referencedVariables( const QString &text );
589
600 static double evaluateToDouble( const QString &text, double fallbackValue );
601
603 {
613 };
614
615 static const QList<QgsExpressionFunction *> &Functions();
616
617 static const QStringList &BuiltinFunctions();
618
626 static bool registerFunction( QgsExpressionFunction *function, bool transferOwnership = false );
627
633 static bool unregisterFunction( const QString &name );
634
639 static void cleanRegisteredFunctions();
640
642 static bool isFunctionName( const QString &name );
643
645 static int functionIndex( const QString &name );
646
651 static int functionCount();
652
658 static QString quotedColumnRef( QString name );
659
665 static QString quotedString( QString text );
666
675 static QString quotedValue( const QVariant &value );
676
686 static QString quotedValue( const QVariant &value, QVariant::Type type );
687
689
690#ifndef SIP_RUN
692 static HelpTextHash &functionHelpTexts();
694#endif
695
702 static QString helpText( QString name );
703
709 static QStringList tags( const QString &name );
710
723 static bool addVariableHelpText( const QString name, const QString &description );
724
732 static QString variableHelpText( const QString &variableName );
733
743 static QString formatVariableHelp( const QString &description, bool showValue = true, const QVariant &value = QVariant() );
744
749 static QString group( const QString &group );
750
760 static QString formatPreviewString( const QVariant &value, bool htmlOutput = true, int maximumPreviewLength = 60 );
761
771 static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value, QVariant::Type fieldType = QVariant::Type::Invalid );
772
783 static bool isFieldEqualityExpression( const QString &expression, QString &field SIP_OUT, QVariant &value SIP_OUT );
784
799 static bool attemptReduceToInClause( const QStringList &expressions, QString &result SIP_OUT );
800
801#ifdef SIP_RUN
802 SIP_PYOBJECT __repr__();
803 % MethodCode
804 QString str = QStringLiteral( "<QgsExpression: '%1'>" ).arg( sipCpp->expression() );
805 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
806 % End
807#endif
808
809 private:
810 void initGeomCalculator( const QgsExpressionContext *context );
811
818 void detach() SIP_SKIP;
819
820 QgsExpressionPrivate *d = nullptr;
821
822 static HelpTextHash sFunctionHelpTexts;
823
825 static void initFunctionHelp() SIP_SKIP;
827 static void initVariableHelp() SIP_SKIP;
828
829 friend class QgsOgcUtils;
830};
831
833
834#endif // QGSEXPRESSION_H
DistanceUnit
Units of distance.
Definition: qgis.h:3310
AreaUnit
Units of area.
Definition: qgis.h:3348
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 unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:53
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
The QgsOgcUtils class provides various utility functions for conversion between OGC (Open Geospatial ...
Definition: qgsogcutils.h:54
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
#define str(x)
Definition: qgis.cpp:38
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
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:554
Details about any parser errors that were found when parsing the expression.
QString errorMsg
The message for the error at this location.