Skip to content

Commit

Permalink
Public default constructor and comparison operator for QgsExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Sep 9, 2016
1 parent 699e9f7 commit d06c11f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
8 changes: 8 additions & 0 deletions python/core/qgsexpression.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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();
}
Expand Down
36 changes: 30 additions & 6 deletions src/core/qgsexpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_qgsexpression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.