Skip to content

Commit d06c11f

Browse files
committed
Public default constructor and comparison operator for QgsExpression
1 parent 699e9f7 commit d06c11f

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

python/core/qgsexpression.sip

Lines changed: 8 additions & 0 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

src/core/qgsexpression.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3696,6 +3696,18 @@ QgsExpression::~QgsExpression()
36963696
delete d;
36973697
}
36983698

3699+
bool QgsExpression::operator==( const QgsExpression& other ) const
3700+
{
3701+
if ( d == other.d || d->mExp == other.d->mExp )
3702+
return true;
3703+
return false;
3704+
}
3705+
3706+
bool QgsExpression::isValid() const
3707+
{
3708+
return d->mRootNode;
3709+
}
3710+
36993711
bool QgsExpression::hasParserError() const { return !d->mParserErrorString.isNull(); }
37003712

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

38793891
return d->mRootNode->dump();
38803892
}

src/core/qgsexpression.h

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

141+
/**
142+
* Compares two expressions. The operator returns true
143+
* if the expression string is equal.
144+
*
145+
* @note Added in QGIS 2.18
146+
*/
147+
bool operator==( const QgsExpression& other ) const;
148+
149+
/**
150+
* Checks if this expression is valid.
151+
* A valid expression could be parsed but does not necessarily evaluate properly.
152+
*
153+
* @note Added in QGIS 2.18
154+
*/
155+
bool isValid() const;
156+
135157
//! Returns true if an error occurred when parsing the input expression
136158
bool hasParserError() const;
137159
//! Returns parser error
@@ -251,6 +273,13 @@ class CORE_EXPORT QgsExpression
251273
*/
252274
static bool isValid( const QString& text, const QgsExpressionContext* context, QString &errorMessage );
253275

276+
/**
277+
* Set the expression string, will reset the whole internal structure.
278+
*
279+
* @note Added in QGIS 2.18
280+
*/
281+
void setExpression( const QString& expression );
282+
254283
void setScale( double scale );
255284

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

13731402
protected:
1374-
/**
1375-
* Used by QgsOgcUtils to create an empty
1376-
*/
1377-
QgsExpression();
1378-
13791403
void initGeomCalculator();
13801404

13811405
static QMap<QString, QVariant> gmSpecialColumns;

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)