Skip to content
Permalink
Browse files

Public default constructor and comparison operator for QgsExpression

  • Loading branch information
m-kuhn committed Sep 9, 2016
1 parent 699e9f7 commit d06c11f9a87008713a712ab57811c2797f1d2c9d
Showing with 59 additions and 7 deletions.
  1. +8 −0 python/core/qgsexpression.sip
  2. +13 −1 src/core/qgsexpression.cpp
  3. +30 −6 src/core/qgsexpression.h
  4. +8 −0 tests/src/python/test_qgsexpression.py
@@ -14,6 +14,14 @@ class QgsExpression
QgsExpression( const QString& expr );
~QgsExpression();

/**
* Checks if this expression is valid.
* A valid expression could be parsed but does not necessarily evaluate properly.
*
* @note Added in QGIS 3.0
*/
bool isValid() const;

//! Returns true if an error occurred when parsing the input expression
bool hasParserError() const;
//! Returns parser error
@@ -3696,6 +3696,18 @@ QgsExpression::~QgsExpression()
delete d;
}

bool QgsExpression::operator==( const QgsExpression& other ) const
{
if ( d == other.d || d->mExp == other.d->mExp )
return true;
return false;
}

bool QgsExpression::isValid() const
{
return d->mRootNode;
}

bool QgsExpression::hasParserError() const { return !d->mParserErrorString.isNull(); }

QString QgsExpression::parserErrorString() const { return d->mParserErrorString; }
@@ -3874,7 +3886,7 @@ int QgsExpression::currentRowNumber() { return d->mRowNumber; }
QString QgsExpression::dump() const
{
if ( !d->mRootNode )
return tr( "(no root)" );
return QString();

return d->mRootNode->dump();
}
@@ -113,7 +113,7 @@ class CORE_EXPORT QgsExpression
/**
* Creates a new expression based on the provided string.
* The string will immediately be parsed. For optimization
* {@link prepare()} should alwys be called before every
* {@link prepare()} should always be called before every
* loop in which this expression is used.
*/
QgsExpression( const QString& expr );
@@ -130,8 +130,30 @@ class CORE_EXPORT QgsExpression
* it does not need to be re-parsed.
*/
QgsExpression& operator=( const QgsExpression& other );

/**
* Create an empty expression
*/
QgsExpression();

~QgsExpression();

/**
* Compares two expressions. The operator returns true
* if the expression string is equal.
*
* @note Added in QGIS 2.18
*/
bool operator==( const QgsExpression& other ) const;

/**
* Checks if this expression is valid.
* A valid expression could be parsed but does not necessarily evaluate properly.
*
* @note Added in QGIS 2.18
*/
bool isValid() const;

//! Returns true if an error occurred when parsing the input expression
bool hasParserError() const;
//! Returns parser error
@@ -251,6 +273,13 @@ class CORE_EXPORT QgsExpression
*/
static bool isValid( const QString& text, const QgsExpressionContext* context, QString &errorMessage );

/**
* Set the expression string, will reset the whole internal structure.
*
* @note Added in QGIS 2.18
*/
void setExpression( const QString& expression );

void setScale( double scale );

double scale();
@@ -1371,11 +1400,6 @@ class CORE_EXPORT QgsExpression
static QString formatPreviewString( const QVariant& value );

protected:
/**
* Used by QgsOgcUtils to create an empty
*/
QgsExpression();

void initGeomCalculator();

static QMap<QString, QVariant> gmSpecialColumns;
@@ -187,5 +187,13 @@ def testComment(self):
result = exp.evaluate()
self.assertEqual(exp_res, result)

def testValid(self):
e = QgsExpression()
self.assertFalse(e.isValid())
e.setExpression('asdf||#@¼')
self.assertFalse(e.isValid())
e.setExpression('1')
self.assertTrue(e.isValid())

if __name__ == "__main__":
unittest.main()

0 comments on commit d06c11f

Please sign in to comment.
You can’t perform that action at this time.