Skip to content

Commit 1c079ea

Browse files
committedSep 5, 2015
Expression context fixes:
- Fix python API break in QgsExpression::Function - Add convenience methods for retrieving feature/fields from a context
1 parent 8611543 commit 1c079ea

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed
 

‎python/core/qgsexpression.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ class QgsExpression
290290
* @param context context expression is being evaluated against
291291
* @param parent parent expression
292292
* @returns result of function
293+
* @note named funcV2 in Python bindings. Will be renamed to func to replace deprecated method in QGIS 3.0.
293294
*/
294-
virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent );
295+
virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) /PyName=funcV2/;
295296

296297
virtual bool handlesNull() const;
297298
};

‎python/core/qgsexpressioncontext.sip

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,28 @@ class QgsExpressionContext
346346
* will be set within the last scope of the context, so will override any
347347
* existing features within the context.
348348
* @param feature feature for context
349+
* @see feature()
349350
*/
350351
void setFeature( const QgsFeature& feature );
351352

353+
/** Convenience function for retrieving the feature for the context, if set.
354+
* @see setFeature
355+
*/
356+
QgsFeature feature() const;
357+
352358
/** Convenience function for setting a fields for the context. The fields
353359
* will be set within the last scope of the context, so will override any
354360
* existing fields within the context.
355361
* @param fields fields for context
362+
* @see fields()
356363
*/
357364
void setFields( const QgsFields& fields );
358365

366+
/** Convenience function for retrieving the fields for the context, if set.
367+
* @see setFields
368+
*/
369+
QgsFields fields() const;
370+
359371
};
360372

361373
/** \ingroup core

‎src/core/qgsexpression.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,9 @@ class CORE_EXPORT QgsExpression
399399
* @param context context expression is being evaluated against
400400
* @param parent parent expression
401401
* @returns result of function
402+
* @note named funcV2 in Python bindings. Will be renamed to func to replace deprecated method in QGIS 3.0.
402403
*/
404+
//TODO QGIS 3.0 - rename python method
403405
virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent );
404406

405407
bool operator==( const Function& other ) const
@@ -752,10 +754,10 @@ class CORE_EXPORT QgsExpression
752754

753755
virtual QStringList referencedColumns() const override { QStringList lst( mNode->referencedColumns() ); foreach ( Node* n, mList->list() ) lst.append( n->referencedColumns() ); return lst; }
754756
virtual bool needsGeometry() const override { bool needs = false; foreach ( Node* n, mList->list() ) needs |= n->needsGeometry(); return needs; }
755-
virtual void accept( Visitor& v ) const override { v.visit( *this ); }
757+
virtual void accept( Visitor& v ) const override { v.visit( *this ); }
756758

757-
protected:
758-
Node* mNode;
759+
protected:
760+
Node* mNode;
759761
NodeList* mList;
760762
bool mNotIn;
761763
};

‎src/core/qgsexpressioncontext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ void QgsExpressionContext::setFeature( const QgsFeature &feature )
350350
mStack.last()->setFeature( feature );
351351
}
352352

353+
QgsFeature QgsExpressionContext::feature() const
354+
{
355+
return qvariant_cast<QgsFeature>( variable( QgsExpressionContext::EXPR_FEATURE ) );
356+
}
357+
353358
void QgsExpressionContext::setFields( const QgsFields &fields )
354359
{
355360
if ( mStack.isEmpty() )
@@ -358,6 +363,11 @@ void QgsExpressionContext::setFields( const QgsFields &fields )
358363
mStack.last()->setFields( fields );
359364
}
360365

366+
QgsFields QgsExpressionContext::fields() const
367+
{
368+
return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
369+
}
370+
361371

362372
//
363373
// QgsExpressionContextUtils

‎src/core/qgsexpressioncontext.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,28 @@ class CORE_EXPORT QgsExpressionContext
378378
* will be set within the last scope of the context, so will override any
379379
* existing features within the context.
380380
* @param feature feature for context
381+
* @see feature()
381382
*/
382383
void setFeature( const QgsFeature& feature );
383384

385+
/** Convenience function for retrieving the feature for the context, if set.
386+
* @see setFeature
387+
*/
388+
QgsFeature feature() const;
389+
384390
/** Convenience function for setting a fields for the context. The fields
385391
* will be set within the last scope of the context, so will override any
386392
* existing fields within the context.
387393
* @param fields fields for context
394+
* @see fields()
388395
*/
389396
void setFields( const QgsFields& fields );
390397

398+
/** Convenience function for retrieving the fields for the context, if set.
399+
* @see setFields
400+
*/
401+
QgsFields fields() const;
402+
391403
static const QString EXPR_FIELDS;
392404
static const QString EXPR_FEATURE;
393405

‎tests/src/core/testqgsexpressioncontext.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,21 @@ void TestQgsExpressionContext::setFeature()
411411

412412
//test setting a feature in a context with no scopes
413413
QgsExpressionContext emptyContext;
414+
QVERIFY( !emptyContext.feature().isValid() );
414415
emptyContext.setFeature( feature );
415416
//setFeature should have created a scope
416417
QCOMPARE( emptyContext.scopeCount(), 1 );
417418
QVERIFY( emptyContext.hasVariable( QgsExpressionContext::EXPR_FEATURE ) );
418419
QCOMPARE(( qvariant_cast<QgsFeature>( emptyContext.variable( QgsExpressionContext::EXPR_FEATURE ) ) ).id(), 50LL );
420+
QCOMPARE( emptyContext.feature(), feature() );
419421

420422
QgsExpressionContext contextWithScope;
421423
contextWithScope << new QgsExpressionContextScope();
422424
contextWithScope.setFeature( feature );
423425
QCOMPARE( contextWithScope.scopeCount(), 1 );
424426
QVERIFY( contextWithScope.hasVariable( QgsExpressionContext::EXPR_FEATURE ) );
425427
QCOMPARE(( qvariant_cast<QgsFeature>( contextWithScope.variable( QgsExpressionContext::EXPR_FEATURE ) ) ).id(), 50LL );
428+
QCOMPARE( contextWithScope.feature(), feature() );
426429
}
427430

428431
void TestQgsExpressionContext::setFields()
@@ -438,18 +441,21 @@ void TestQgsExpressionContext::setFields()
438441

439442
//test setting a fields in a context with no scopes
440443
QgsExpressionContext emptyContext;
444+
QVERIFY( emptyContext.fields().isEmpty() );
441445
emptyContext.setFields( fields );
442446
//setFeature should have created a scope
443447
QCOMPARE( emptyContext.scopeCount(), 1 );
444448
QVERIFY( emptyContext.hasVariable( QgsExpressionContext::EXPR_FIELDS ) );
445449
QCOMPARE(( qvariant_cast<QgsFields>( emptyContext.variable( QgsExpressionContext::EXPR_FIELDS ) ) ).at( 0 ).name(), QString( "testfield" ) );
450+
QCOMPARE( emptyContext.fields().at( 0 ).name(), QString( "testfield" ) );
446451

447452
QgsExpressionContext contextWithScope;
448453
contextWithScope << new QgsExpressionContextScope();
449454
contextWithScope.setFields( fields );
450455
QCOMPARE( contextWithScope.scopeCount(), 1 );
451456
QVERIFY( contextWithScope.hasVariable( QgsExpressionContext::EXPR_FIELDS ) );
452457
QCOMPARE(( qvariant_cast<QgsFields>( contextWithScope.variable( QgsExpressionContext::EXPR_FIELDS ) ) ).at( 0 ).name(), QString( "testfield" ) );
458+
QCOMPARE( contextWithScope.fields().at( 0 ).name(), QString( "testfield" ) );
453459
}
454460

455461
void TestQgsExpressionContext::globalScope()

0 commit comments

Comments
 (0)
Please sign in to comment.