Skip to content

Commit d25fcec

Browse files
committedJan 11, 2017
Add method to retrieve variables from a QgsExpressionContext as a QVariantMap
1 parent a7806d1 commit d25fcec

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
 

‎python/core/qgsexpressioncontext.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ class QgsExpressionContext
247247
*/
248248
QVariant variable( const QString& name ) const;
249249

250+
/**
251+
* Returns a map of variable name to value representing all the expression variables
252+
* contained by the context.
253+
* @note added in QGIS 3.0
254+
*/
255+
QVariantMap variablesToMap() const;
256+
250257
/** Returns true if the specified variable name is intended to be highlighted to the
251258
* user. This is used by the expression builder to more prominently display the
252259
* variable.

‎src/core/qgsexpressioncontext.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,17 @@ QVariant QgsExpressionContext::variable( const QString& name ) const
274274
return scope ? scope->variable( name ) : QVariant();
275275
}
276276

277+
QVariantMap QgsExpressionContext::variablesToMap() const
278+
{
279+
QStringList names = variableNames();
280+
QVariantMap m;
281+
Q_FOREACH ( const QString& name, names )
282+
{
283+
m.insert( name, variable( name ) );
284+
}
285+
return m;
286+
}
287+
277288
bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const
278289
{
279290
return mHighlightedVariables.contains( name );

‎src/core/qgsexpressioncontext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,13 @@ class CORE_EXPORT QgsExpressionContext
303303
*/
304304
QVariant variable( const QString& name ) const;
305305

306+
/**
307+
* Returns a map of variable name to value representing all the expression variables
308+
* contained by the context.
309+
* @note added in QGIS 3.0
310+
*/
311+
QVariantMap variablesToMap() const;
312+
306313
/** Returns true if the specified variable name is intended to be highlighted to the
307314
* user. This is used by the expression builder to more prominently display the
308315
* variable.

‎tests/src/core/testqgsexpressioncontext.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class TestQgsExpressionContext : public QObject
5151

5252
void cache();
5353

54+
void valuesAsMap();
55+
5456
private:
5557

5658
class GetTestValueFunction : public QgsScopedExpressionFunction
@@ -688,5 +690,35 @@ void TestQgsExpressionContext::cache()
688690
QVERIFY( !c.cachedValue( "test" ).isValid() );
689691
}
690692

693+
void TestQgsExpressionContext::valuesAsMap()
694+
{
695+
QgsExpressionContext context;
696+
697+
//test retrieving from empty context
698+
QVERIFY( context.variablesToMap().isEmpty() );
699+
700+
//add a scope to the context
701+
QgsExpressionContextScope* s1 = new QgsExpressionContextScope();
702+
s1->setVariable( "v1", "t1" );
703+
s1->setVariable( "v2", "t2" );
704+
context << s1;
705+
706+
QVariantMap m = context.variablesToMap();
707+
QCOMPARE( m.size(), 2 );
708+
QCOMPARE( m.value( "v1" ).toString(), QString( "t1" ) );
709+
QCOMPARE( m.value( "v2" ).toString(), QString( "t2" ) );
710+
711+
QgsExpressionContextScope* s2 = new QgsExpressionContextScope();
712+
s2->setVariable( "v2", "t2a" );
713+
s2->setVariable( "v3", "t3" );
714+
context << s2;
715+
716+
m = context.variablesToMap();
717+
QCOMPARE( m.size(), 3 );
718+
QCOMPARE( m.value( "v1" ).toString(), QString( "t1" ) );
719+
QCOMPARE( m.value( "v2" ).toString(), QString( "t2a" ) );
720+
QCOMPARE( m.value( "v3" ).toString(), QString( "t3" ) );
721+
}
722+
691723
QGSTEST_MAIN( TestQgsExpressionContext )
692724
#include "testqgsexpressioncontext.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.