QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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 <QRecursiveMutex>
28
29#include <functional>
30
31#include "qgis.h"
32#include "qgsinterval.h"
33#include "qgsexpressionnode.h"
34
35class QgsFeature;
36class QgsGeometry;
37class QgsOgcUtils;
38class QgsVectorLayer;
40class QgsField;
41class QgsFields;
42class QgsDistanceArea;
43class QDomElement;
45class QgsExpressionPrivate;
47
48#ifndef SIP_RUN
50struct HelpArg
51{
52 HelpArg( const QString &arg, const QString &desc, bool descOnly = false, bool syntaxOnly = false,
53 bool optional = false, const QString &defaultVal = QString() )
54 : mArg( arg )
55 , mDescription( desc )
56 , mDescOnly( descOnly )
57 , mSyntaxOnly( syntaxOnly )
58 , mOptional( optional )
59 , mDefaultVal( defaultVal )
60 {}
61
62 QString mArg;
63 QString mDescription;
64 bool mDescOnly;
65 bool mSyntaxOnly;
66 bool mOptional;
67 QString mDefaultVal;
68};
69
70struct HelpExample
71{
72 HelpExample( const QString &expression, const QString &returns, const QString &note = QString() )
73 : mExpression( expression )
74 , mReturns( returns )
75 , mNote( note )
76 {}
77
78 QString mExpression;
79 QString mReturns;
80 QString mNote;
81};
82
83
84struct HelpVariant
85{
86 HelpVariant( const QString &name, const QString &description,
87 const QList<HelpArg> &arguments = QList<HelpArg>(),
88 bool variableLenArguments = false,
89 const QList<HelpExample> &examples = QList<HelpExample>(),
90 const QString &notes = QString(),
91 const QStringList &tags = QStringList() )
92 : mName( name )
93 , mDescription( description )
94 , mArguments( arguments )
95 , mVariableLenArguments( variableLenArguments )
96 , mExamples( examples )
97 , mNotes( notes )
98 , mTags( tags )
99 {}
100
101 QString mName;
102 QString mDescription;
103 QList<HelpArg> mArguments;
104 bool mVariableLenArguments;
105 QList<HelpExample> mExamples;
106 QString mNotes;
107 QStringList mTags;
108};
109
110
111struct Help
112{
114 Help() = default;
115
116 Help( const QString &name, const QString &type, const QString &description, const QList<HelpVariant> &variants )
117 : mName( name )
118 , mType( type )
119 , mDescription( description )
120 , mVariants( variants )
121 {}
122
123 QString mName;
124 QString mType;
125 QString mDescription;
126 QList<HelpVariant> mVariants;
127};
128
129typedef QHash<QString, Help> HelpTextHash;
130
132#endif
133
189class CORE_EXPORT QgsExpression
190{
191 Q_DECLARE_TR_FUNCTIONS( QgsExpression )
192 public:
193
198 struct CORE_EXPORT ParserError
199 {
201 {
202 Unknown = 0,
203 FunctionUnknown = 1,
204 FunctionWrongArgs = 2,
205 FunctionInvalidParams = 3,
206 FunctionNamedArgsError = 4
207 };
208
212 ParserErrorType errorType = ParserErrorType::Unknown;
213
217 QString errorMsg;
218
223 int firstLine = 0;
224
229 int firstColumn = 0;
230
234 int lastLine = 0;
235
239 int lastColumn = 0;
240 };
241
248 QgsExpression( const QString &expr );
249
255 QgsExpression( const QgsExpression &other );
256
262 QgsExpression &operator=( const QgsExpression &other );
263
269 operator QString() const SIP_SKIP;
270
277
278 ~QgsExpression();
279
286 bool operator==( const QgsExpression &other ) const;
287
294 bool isValid() const;
295
297 bool hasParserError() const;
299 QString parserErrorString() const;
300
305 QList<QgsExpression::ParserError> parserErrors() const;
306
312 const QgsExpressionNode *rootNode() const;
313
319 bool prepare( const QgsExpressionContext *context );
320
338 QSet<QString> referencedColumns() const;
339
351 QSet<QString> referencedVariables() const;
352
362 QSet<QString> referencedFunctions() const;
363
364#ifndef SIP_RUN
365
372 QList<const QgsExpressionNode *> nodes( ) const;
373
380 template <class T>
381 QList<const T *> findNodes( ) const
382 {
383 QList<const T *> lst;
384 const QList<const QgsExpressionNode *> allNodes( nodes() );
385 for ( const auto &node : allNodes )
386 {
387 const T *n = dynamic_cast<const T *>( node );
388 if ( n )
389 lst << n;
390 }
391 return lst;
392 }
393#endif
394
408 QSet<int> referencedAttributeIndexes( const QgsFields &fields ) const;
409
411 bool needsGeometry() const;
412
413 // evaluation
414
420 QVariant evaluate();
421
428 QVariant evaluate( const QgsExpressionContext *context );
429
431 bool hasEvalError() const;
433 QString evalErrorString() const;
435 void setEvalErrorString( const QString &str );
436
443 bool isField() const;
444
457 static int expressionToLayerFieldIndex( const QString &expression, const QgsVectorLayer *layer );
458
471 static QString quoteFieldExpression( const QString &expression, const QgsVectorLayer *layer );
472
481 static bool checkExpression( const QString &text, const QgsExpressionContext *context, QString &errorMessage SIP_OUT );
482
488 void setExpression( const QString &expression );
489
495 QString expression() const;
496
503 QString dump() const;
504
512 QgsDistanceArea *geomCalculator();
513
527 void setGeomCalculator( const QgsDistanceArea *calc );
528
536 Qgis::DistanceUnit distanceUnits() const;
537
547 void setDistanceUnits( Qgis::DistanceUnit unit );
548
556 Qgis::AreaUnit areaUnits() const;
557
567 void setAreaUnits( Qgis::AreaUnit unit );
568
580 static QString replaceExpressionText( const QString &action, const QgsExpressionContext *context,
581 const QgsDistanceArea *distanceArea = nullptr );
582
590 static QSet<QString> referencedVariables( const QString &text );
591
602 static double evaluateToDouble( const QString &text, double fallbackValue );
603
616
617 static const QList<QgsExpressionFunction *> &Functions();
618
619 static const QStringList &BuiltinFunctions();
620
628 static bool registerFunction( QgsExpressionFunction *function, bool transferOwnership = false );
629
635 static bool unregisterFunction( const QString &name );
636
641 static void cleanRegisteredFunctions();
642
644 static bool isFunctionName( const QString &name );
645
647 static int functionIndex( const QString &name );
648
653 static int functionCount();
654
660 static QString quotedColumnRef( QString name );
661
667 static QString quotedString( QString text );
668
677 static QString quotedValue( const QVariant &value );
678
688 static QString quotedValue( const QVariant &value, QVariant::Type type );
689
691
692#ifndef SIP_RUN
694 static HelpTextHash &functionHelpTexts();
696#endif
697
704 static QString helpText( QString name );
705
711 static QStringList tags( const QString &name );
712
725 static bool addVariableHelpText( const QString name, const QString &description );
726
734 static QString variableHelpText( const QString &variableName );
735
745 static QString formatVariableHelp( const QString &description, bool showValue = true, const QVariant &value = QVariant() );
746
751 static QString group( const QString &group );
752
762 static QString formatPreviewString( const QVariant &value, bool htmlOutput = true, int maximumPreviewLength = 60 );
763
773 static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value, QVariant::Type fieldType = QVariant::Type::Invalid );
774
785 static bool isFieldEqualityExpression( const QString &expression, QString &field SIP_OUT, QVariant &value SIP_OUT );
786
801 static bool attemptReduceToInClause( const QStringList &expressions, QString &result SIP_OUT );
802
803#ifdef SIP_RUN
804 SIP_PYOBJECT __repr__();
805 % MethodCode
806 QString str = QStringLiteral( "<QgsExpression: '%1'>" ).arg( sipCpp->expression() );
807 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
808 % End
809#endif
810
811 private:
812 void initGeomCalculator( const QgsExpressionContext *context );
813
820 void detach() SIP_SKIP;
821
822 QgsExpressionPrivate *d = nullptr;
823
824 static HelpTextHash sFunctionHelpTexts;
825
827 static void initFunctionHelp() SIP_SKIP;
829 static void initVariableHelp() SIP_SKIP;
830
832 static QRecursiveMutex sFunctionsMutex;
833 static QMap< QString, int> sFunctionIndexMap;
834
835 friend class QgsOgcUtils;
836};
837
839
840#endif // QGSEXPRESSION_H
DistanceUnit
Units of distance.
Definition qgis.h:3496
AreaUnit
Units of area.
Definition qgis.h:3534
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.
An expression node for expression 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.
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.
Details about any parser errors that were found when parsing the expression.
QString errorMsg
The message for the error at this location.