diff --git a/python/core/qgsexpressioncontext.sip b/python/core/qgsexpressioncontext.sip index 538dfadbddd..77cfce72cd3 100644 --- a/python/core/qgsexpressioncontext.sip +++ b/python/core/qgsexpressioncontext.sip @@ -247,6 +247,13 @@ class QgsExpressionContext */ QVariant variable( const QString& name ) const; + /** + * Returns a map of variable name to value representing all the expression variables + * contained by the context. + * @note added in QGIS 3.0 + */ + QVariantMap variablesToMap() const; + /** Returns true if the specified variable name is intended to be highlighted to the * user. This is used by the expression builder to more prominently display the * variable. diff --git a/src/core/qgsexpressioncontext.cpp b/src/core/qgsexpressioncontext.cpp index 244b6a80548..4ffb3d6b24f 100644 --- a/src/core/qgsexpressioncontext.cpp +++ b/src/core/qgsexpressioncontext.cpp @@ -274,6 +274,17 @@ QVariant QgsExpressionContext::variable( const QString& name ) const return scope ? scope->variable( name ) : QVariant(); } +QVariantMap QgsExpressionContext::variablesToMap() const +{ + QStringList names = variableNames(); + QVariantMap m; + Q_FOREACH ( const QString& name, names ) + { + m.insert( name, variable( name ) ); + } + return m; +} + bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const { return mHighlightedVariables.contains( name ); diff --git a/src/core/qgsexpressioncontext.h b/src/core/qgsexpressioncontext.h index a2367b2991f..33c3dc96e55 100644 --- a/src/core/qgsexpressioncontext.h +++ b/src/core/qgsexpressioncontext.h @@ -303,6 +303,13 @@ class CORE_EXPORT QgsExpressionContext */ QVariant variable( const QString& name ) const; + /** + * Returns a map of variable name to value representing all the expression variables + * contained by the context. + * @note added in QGIS 3.0 + */ + QVariantMap variablesToMap() const; + /** Returns true if the specified variable name is intended to be highlighted to the * user. This is used by the expression builder to more prominently display the * variable. diff --git a/tests/src/core/testqgsexpressioncontext.cpp b/tests/src/core/testqgsexpressioncontext.cpp index 58f81196537..014b0ec4101 100644 --- a/tests/src/core/testqgsexpressioncontext.cpp +++ b/tests/src/core/testqgsexpressioncontext.cpp @@ -51,6 +51,8 @@ class TestQgsExpressionContext : public QObject void cache(); + void valuesAsMap(); + private: class GetTestValueFunction : public QgsScopedExpressionFunction @@ -688,5 +690,35 @@ void TestQgsExpressionContext::cache() QVERIFY( !c.cachedValue( "test" ).isValid() ); } +void TestQgsExpressionContext::valuesAsMap() +{ + QgsExpressionContext context; + + //test retrieving from empty context + QVERIFY( context.variablesToMap().isEmpty() ); + + //add a scope to the context + QgsExpressionContextScope* s1 = new QgsExpressionContextScope(); + s1->setVariable( "v1", "t1" ); + s1->setVariable( "v2", "t2" ); + context << s1; + + QVariantMap m = context.variablesToMap(); + QCOMPARE( m.size(), 2 ); + QCOMPARE( m.value( "v1" ).toString(), QString( "t1" ) ); + QCOMPARE( m.value( "v2" ).toString(), QString( "t2" ) ); + + QgsExpressionContextScope* s2 = new QgsExpressionContextScope(); + s2->setVariable( "v2", "t2a" ); + s2->setVariable( "v3", "t3" ); + context << s2; + + m = context.variablesToMap(); + QCOMPARE( m.size(), 3 ); + QCOMPARE( m.value( "v1" ).toString(), QString( "t1" ) ); + QCOMPARE( m.value( "v2" ).toString(), QString( "t2a" ) ); + QCOMPARE( m.value( "v3" ).toString(), QString( "t3" ) ); +} + QGSTEST_MAIN( TestQgsExpressionContext ) #include "testqgsexpressioncontext.moc"