From 98a1c233af2d5294854925ff03075c1e70f69331 Mon Sep 17 00:00:00 2001 From: Martin Dobias Date: Tue, 9 Aug 2011 20:48:13 +0200 Subject: [PATCH] Added python bindings for QgsExpression --- python/core/core.sip | 1 + python/core/qgsexpression.sip | 64 +++++++++++++++++++++++++++++++++++ src/core/qgsexpression.h | 5 ++- 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 python/core/qgsexpression.sip diff --git a/python/core/core.sip b/python/core/core.sip index 7a4b3563b7d..d775dfbfdd9 100644 --- a/python/core/core.sip +++ b/python/core/core.sip @@ -35,6 +35,7 @@ %Include qgsdataprovider.sip %Include qgsdatasourceuri.sip %Include qgsdistancearea.sip +%Include qgsexpression.sip %Include qgsfeature.sip %Include qgsfield.sip %Include qgsgeometry.sip diff --git a/python/core/qgsexpression.sip b/python/core/qgsexpression.sip new file mode 100644 index 00000000000..63539292c29 --- /dev/null +++ b/python/core/qgsexpression.sip @@ -0,0 +1,64 @@ + +class QgsExpression +{ +%TypeHeaderCode +#include "qgsexpression.h" +%End + +public: + QgsExpression( const QString& expr ); + ~QgsExpression(); + + //! Returns true if an error occurred when parsing the input expression + bool hasParserError() const; + //! Returns parser error + QString parserErrorString() const; + + //! Get the expression ready for evaluation - find out column indexes. + bool prepare( const QgsFieldMap& fields ); + + //! Get list of columns referenced by the expression + QStringList referencedColumns(); + //! Returns true if the expression uses feature geometry for some computation + bool needsGeometry(); + + // evaluation + + //! Evaluate the feature and return the result + //! @note prepare() should be called before calling this method + QVariant evaluate( QgsFeature* f = NULL ); + + //! Evaluate the feature and return the result + //! @note this method does not expect that prepare() has been called on this instance + QVariant evaluate( QgsFeature* f, const QgsFieldMap& fields ); + + //! Returns true if an error occurred when evaluating last input + bool hasEvalError() const; + //! Returns evaluation error + QString evalErrorString() const; + //! Set evaluation error (used internally by evaluation functions) + void setEvalErrorString( QString str ); + + //! Set the number for $rownum special column + void setCurrentRowNumber( int rowNumber ); + //! Return the number used for $rownum special column + int currentRowNumber(); + + //! Return the parsed expression as a string - useful for debugging + QString dump() const; + + //! Return calculator used for distance and area calculations + //! (used by internal functions) + QgsDistanceArea* geomCalculator(); + + // + + // tells whether the identifier is a name of existing function + static bool isFunctionName( QString name ); + + // return index of the function in BuiltinFunctions array + static int functionIndex( QString name ); + + //! return quoted column reference (in double quotes) + static QString quotedColumnRef( QString name ); +}; diff --git a/src/core/qgsexpression.h b/src/core/qgsexpression.h index 8f574288bf2..475e8479139 100644 --- a/src/core/qgsexpression.h +++ b/src/core/qgsexpression.h @@ -55,7 +55,7 @@ to find out indices of columns and then repeatedly call evaluate(feature). @note added in 2.0 */ -class QgsExpression +class CORE_EXPORT QgsExpression { public: QgsExpression( const QString& expr ); @@ -165,6 +165,9 @@ class QgsExpression // return index of the function in BuiltinFunctions array static int functionIndex( QString name ); + //! return quoted column reference (in double quotes) + static QString quotedColumnRef( QString name ) { return QString( "\"%1\"" ).arg( name.replace( "\"", "\"\"" ) ); } + ////// class Node