Skip to content

Commit 760816b

Browse files
committed
Public default constructor and comparison operator for QgsExpression
1 parent 5e2a251 commit 760816b

File tree

7 files changed

+74
-13
lines changed

7 files changed

+74
-13
lines changed

doc/api_break.dox

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ version instead.</li>
531531
<li>QgsExpression::Interval has been removed. Use QgsInterval instead.</li>
532532
<li>replaceExpressionText() no longer accepts a substitution map parameter. Use expression context variables instead.</li>
533533
<li>helptext() has been renamed to helpText()</li>
534+
<li>isValid() has been renamed to checkExpression()</li>
534535
</ul>
535536

536537
\subsection qgis_api_break_3_0_QgsFeature QgsFeature

python/core/qgsexpression.sip

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ class QgsExpression
1414
QgsExpression( const QString& expr );
1515
~QgsExpression();
1616

17+
/**
18+
* Checks if this expression is valid.
19+
* A valid expression could be parsed but does not necessarily evaluate properly.
20+
*
21+
* @note Added in QGIS 3.0
22+
*/
23+
bool isValid() const;
24+
1725
//! Returns true if an error occurred when parsing the input expression
1826
bool hasParserError() const;
1927
//! Returns parser error
@@ -73,9 +81,9 @@ class QgsExpression
7381
* @param context optional expression context
7482
* @param errorMessage will be filled with any error message from the validation
7583
* @returns true if string is a valid expression
76-
* @note added in QGIS 2.12
84+
* @note added in QGIS 3.0
7785
*/
78-
static bool isValid( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
86+
static bool checkExpression( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
7987

8088
//! Return the original, unmodified expression string.
8189
//! If there was none supplied because it was constructed by sole

src/core/qgsexpression.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,14 +3494,22 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
34943494
return gmFunctions;
34953495
}
34963496

3497-
bool QgsExpression::isValid( const QString &text, const QgsExpressionContext *context, QString &errorMessage )
3497+
bool QgsExpression::checkExpression( const QString &text, const QgsExpressionContext *context, QString &errorMessage )
34983498
{
34993499
QgsExpression exp( text );
35003500
exp.prepare( context );
35013501
errorMessage = exp.parserErrorString();
35023502
return !exp.hasParserError();
35033503
}
35043504

3505+
void QgsExpression::setExpression( const QString& expression )
3506+
{
3507+
detach();
3508+
d->mRootNode = ::parseExpression( expression, d->mParserErrorString );
3509+
d->mEvalErrorString = QString();
3510+
d->mExp = expression;
3511+
}
3512+
35053513
QString QgsExpression::expression() const
35063514
{
35073515
if ( !d->mExp.isNull() )
@@ -3611,6 +3619,18 @@ QgsExpression::~QgsExpression()
36113619
delete d;
36123620
}
36133621

3622+
bool QgsExpression::operator==( const QgsExpression& other ) const
3623+
{
3624+
if ( d == other.d || d->mExp == other.d->mExp )
3625+
return true;
3626+
return false;
3627+
}
3628+
3629+
bool QgsExpression::isValid() const
3630+
{
3631+
return d->mRootNode;
3632+
}
3633+
36143634
bool QgsExpression::hasParserError() const { return !d->mParserErrorString.isNull(); }
36153635

36163636
QString QgsExpression::parserErrorString() const { return d->mParserErrorString; }
@@ -3740,7 +3760,7 @@ void QgsExpression::setEvalErrorString( const QString& str )
37403760
QString QgsExpression::dump() const
37413761
{
37423762
if ( !d->mRootNode )
3743-
return tr( "(no root)" );
3763+
return QString();
37443764

37453765
return d->mRootNode->dump();
37463766
}

src/core/qgsexpression.h

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class CORE_EXPORT QgsExpression
112112
/**
113113
* Creates a new expression based on the provided string.
114114
* The string will immediately be parsed. For optimization
115-
* {@link prepare()} should alwys be called before every
115+
* {@link prepare()} should always be called before every
116116
* loop in which this expression is used.
117117
*/
118118
QgsExpression( const QString& expr );
@@ -129,8 +129,30 @@ class CORE_EXPORT QgsExpression
129129
* it does not need to be re-parsed.
130130
*/
131131
QgsExpression& operator=( const QgsExpression& other );
132+
133+
/**
134+
* Create an empty expression
135+
*/
136+
QgsExpression();
137+
132138
~QgsExpression();
133139

140+
/**
141+
* Compares two expressions. The operator returns true
142+
* if the expression string is equal.
143+
*
144+
* @note Added in QGIS 3.0
145+
*/
146+
bool operator==( const QgsExpression& other ) const;
147+
148+
/**
149+
* Checks if this expression is valid.
150+
* A valid expression could be parsed but does not necessarily evaluate properly.
151+
*
152+
* @note Added in QGIS 3.0
153+
*/
154+
bool isValid() const;
155+
134156
//! Returns true if an error occurred when parsing the input expression
135157
bool hasParserError() const;
136158
//! Returns parser error
@@ -194,7 +216,14 @@ class CORE_EXPORT QgsExpression
194216
* @returns true if string is a valid expression
195217
* @note added in QGIS 2.12
196218
*/
197-
static bool isValid( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
219+
static bool checkExpression( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
220+
221+
/**
222+
* Set the expression string, will reset the whole internal structure.
223+
*
224+
* @note Added in QGIS 3.0
225+
*/
226+
void setExpression( const QString& expression );
198227

199228
//! Return the original, unmodified expression string.
200229
//! If there was none supplied because it was constructed by sole
@@ -1238,11 +1267,6 @@ class CORE_EXPORT QgsExpression
12381267
static QString formatPreviewString( const QVariant& value );
12391268

12401269
protected:
1241-
/**
1242-
* Used by QgsOgcUtils to create an empty
1243-
*/
1244-
QgsExpression();
1245-
12461270
void initGeomCalculator();
12471271

12481272
static QMap<QString, QVariant> gmSpecialColumns;

src/gui/qgsexpressionlineedit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ QString QgsExpressionLineEdit::expression() const
129129
bool QgsExpressionLineEdit::isValidExpression( QString *expressionError ) const
130130
{
131131
QString temp;
132-
return QgsExpression::isValid( expression(), &mExpressionContext, expressionError ? *expressionError : temp );
132+
return QgsExpression::checkExpression( expression(), &mExpressionContext, expressionError ? *expressionError : temp );
133133
}
134134

135135
void QgsExpressionLineEdit::registerExpressionContextGenerator( const QgsExpressionContextGenerator* generator )

src/gui/qgsfieldexpressionwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ QString QgsFieldExpressionWidget::asExpression() const
113113
bool QgsFieldExpressionWidget::isValidExpression( QString *expressionError ) const
114114
{
115115
QString temp;
116-
return QgsExpression::isValid( currentText(), &mExpressionContext, expressionError ? *expressionError : temp );
116+
return QgsExpression::checkExpression( currentText(), &mExpressionContext, expressionError ? *expressionError : temp );
117117
}
118118

119119
bool QgsFieldExpressionWidget::isExpression() const

tests/src/python/test_qgsexpression.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,13 @@ def testComment(self):
187187
result = exp.evaluate()
188188
self.assertEqual(exp_res, result)
189189

190+
def testValid(self):
191+
e = QgsExpression()
192+
self.assertFalse(e.isValid())
193+
e.setExpression('asdf||#@¼')
194+
self.assertFalse(e.isValid())
195+
e.setExpression('1')
196+
self.assertTrue(e.isValid())
197+
190198
if __name__ == "__main__":
191199
unittest.main()

0 commit comments

Comments
 (0)