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 5e2a251 commit 760816bb4142d42dd04e690fd39628930fe687f2
@@ -531,6 +531,7 @@ version instead.</li>
<li>QgsExpression::Interval has been removed. Use QgsInterval instead.</li>
<li>replaceExpressionText() no longer accepts a substitution map parameter. Use expression context variables instead.</li>
<li>helptext() has been renamed to helpText()</li>
<li>isValid() has been renamed to checkExpression()</li>
</ul>

\subsection qgis_api_break_3_0_QgsFeature QgsFeature
@@ -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
@@ -73,9 +81,9 @@ class QgsExpression
* @param context optional expression context
* @param errorMessage will be filled with any error message from the validation
* @returns true if string is a valid expression
* @note added in QGIS 2.12
* @note added in QGIS 3.0
*/
static bool isValid( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
static bool checkExpression( const QString& text, const QgsExpressionContext* context, QString &errorMessage );

//! Return the original, unmodified expression string.
//! If there was none supplied because it was constructed by sole
@@ -3494,14 +3494,22 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
return gmFunctions;
}

bool QgsExpression::isValid( const QString &text, const QgsExpressionContext *context, QString &errorMessage )
bool QgsExpression::checkExpression( const QString &text, const QgsExpressionContext *context, QString &errorMessage )
{
QgsExpression exp( text );
exp.prepare( context );
errorMessage = exp.parserErrorString();
return !exp.hasParserError();
}

void QgsExpression::setExpression( const QString& expression )
{
detach();
d->mRootNode = ::parseExpression( expression, d->mParserErrorString );
d->mEvalErrorString = QString();
d->mExp = expression;
}

QString QgsExpression::expression() const
{
if ( !d->mExp.isNull() )
@@ -3611,6 +3619,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; }
@@ -3740,7 +3760,7 @@ void QgsExpression::setEvalErrorString( const QString& str )
QString QgsExpression::dump() const
{
if ( !d->mRootNode )
return tr( "(no root)" );
return QString();

return d->mRootNode->dump();
}
@@ -112,7 +112,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 );
@@ -129,8 +129,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 3.0
*/
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 3.0
*/
bool isValid() const;

//! Returns true if an error occurred when parsing the input expression
bool hasParserError() const;
//! Returns parser error
@@ -194,7 +216,14 @@ class CORE_EXPORT QgsExpression
* @returns true if string is a valid expression
* @note added in QGIS 2.12
*/
static bool isValid( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
static bool checkExpression( const QString& text, const QgsExpressionContext* context, QString &errorMessage );

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

//! Return the original, unmodified expression string.
//! If there was none supplied because it was constructed by sole
@@ -1238,11 +1267,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;
@@ -129,7 +129,7 @@ QString QgsExpressionLineEdit::expression() const
bool QgsExpressionLineEdit::isValidExpression( QString *expressionError ) const
{
QString temp;
return QgsExpression::isValid( expression(), &mExpressionContext, expressionError ? *expressionError : temp );
return QgsExpression::checkExpression( expression(), &mExpressionContext, expressionError ? *expressionError : temp );
}

void QgsExpressionLineEdit::registerExpressionContextGenerator( const QgsExpressionContextGenerator* generator )
@@ -113,7 +113,7 @@ QString QgsFieldExpressionWidget::asExpression() const
bool QgsFieldExpressionWidget::isValidExpression( QString *expressionError ) const
{
QString temp;
return QgsExpression::isValid( currentText(), &mExpressionContext, expressionError ? *expressionError : temp );
return QgsExpression::checkExpression( currentText(), &mExpressionContext, expressionError ? *expressionError : temp );
}

bool QgsFieldExpressionWidget::isExpression() const
@@ -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 760816b

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