From 5384e203fb58a7402ed8ff5598257a953171830d Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 20 Jul 2016 11:01:00 +1000 Subject: [PATCH] [composer] Fix editing of map item variables On behalf of Faunalia, sponsored by ENEL --- python/core/qgsexpressioncontext.sip | 7 +++++++ src/app/composer/qgscomposeritemwidget.cpp | 4 +++- src/core/qgsexpressioncontext.cpp | 13 +++++++++++++ src/core/qgsexpressioncontext.h | 7 +++++++ tests/src/core/testqgsexpressioncontext.cpp | 12 ++++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/python/core/qgsexpressioncontext.sip b/python/core/qgsexpressioncontext.sip index 0b1a242b633..57f89a8b013 100644 --- a/python/core/qgsexpressioncontext.sip +++ b/python/core/qgsexpressioncontext.sip @@ -296,6 +296,13 @@ class QgsExpressionContext */ int indexOfScope( QgsExpressionContextScope* scope ) const; + /** Returns the index of the first scope with a matching name within the context. + * @param scopeName name of scope to find + * @returns index of scope, or -1 if scope was not found within the context. + * @note added in QGIS 3.0 + */ + int indexOfScope( const QString& scopeName ) const; + /** Returns a list of variables names set by all scopes in the context. * @returns list of unique variable names * @see filteredVariableNames diff --git a/src/app/composer/qgscomposeritemwidget.cpp b/src/app/composer/qgscomposeritemwidget.cpp index 09c012e9b58..280edc3bf63 100644 --- a/src/app/composer/qgscomposeritemwidget.cpp +++ b/src/app/composer/qgscomposeritemwidget.cpp @@ -114,7 +114,9 @@ void QgsComposerItemWidget::updateVariables() { QgsExpressionContext* context = mItem->createExpressionContext(); mVariableEditor->setContext( context ); - mVariableEditor->setEditableScopeIndex( context->scopeCount() - 1 ); + int editableIndex = context->indexOfScope( tr( "Composer Item" ) ); + if ( editableIndex >= 0 ) + mVariableEditor->setEditableScopeIndex( editableIndex ); delete context; } diff --git a/src/core/qgsexpressioncontext.cpp b/src/core/qgsexpressioncontext.cpp index 15daa00c1ab..be13327b7c0 100644 --- a/src/core/qgsexpressioncontext.cpp +++ b/src/core/qgsexpressioncontext.cpp @@ -312,6 +312,19 @@ int QgsExpressionContext::indexOfScope( QgsExpressionContextScope* scope ) const return mStack.indexOf( scope ); } +int QgsExpressionContext::indexOfScope( const QString& scopeName ) const +{ + int index = 0; + Q_FOREACH ( const QgsExpressionContextScope* scope, mStack ) + { + if ( scope->name() == scopeName ) + return index; + + index++; + } + return -1; +} + QStringList QgsExpressionContext::variableNames() const { QStringList names; diff --git a/src/core/qgsexpressioncontext.h b/src/core/qgsexpressioncontext.h index 56e845fa2f4..b59698b4ca6 100644 --- a/src/core/qgsexpressioncontext.h +++ b/src/core/qgsexpressioncontext.h @@ -334,6 +334,13 @@ class CORE_EXPORT QgsExpressionContext */ int indexOfScope( QgsExpressionContextScope* scope ) const; + /** Returns the index of the first scope with a matching name within the context. + * @param scopeName name of scope to find + * @returns index of scope, or -1 if scope was not found within the context. + * @note added in QGIS 3.0 + */ + int indexOfScope( const QString& scopeName ) const; + /** Returns a list of variables names set by all scopes in the context. * @returns list of unique variable names * @see filteredVariableNames diff --git a/tests/src/core/testqgsexpressioncontext.cpp b/tests/src/core/testqgsexpressioncontext.cpp index ffe628d7ca3..6e2f8334307 100644 --- a/tests/src/core/testqgsexpressioncontext.cpp +++ b/tests/src/core/testqgsexpressioncontext.cpp @@ -37,6 +37,7 @@ class TestQgsExpressionContext : public QObject void contextScopeCopy(); void contextScopeFunctions(); void contextStack(); + void scopeByName(); void contextCopy(); void contextStackFunctions(); void evaluate(); @@ -302,6 +303,17 @@ void TestQgsExpressionContext::contextStack() QCOMPARE( scopes.at( 0 ), scope1 ); } +void TestQgsExpressionContext::scopeByName() +{ + QgsExpressionContext context; + QCOMPARE( context.indexOfScope( "test1" ), -1 ); + context << new QgsExpressionContextScope( "test1" ); + context << new QgsExpressionContextScope( "test2" ); + QCOMPARE( context.indexOfScope( "test1" ), 0 ); + QCOMPARE( context.indexOfScope( "test2" ), 1 ); + QCOMPARE( context.indexOfScope( "not in context" ), -1 ); +} + void TestQgsExpressionContext::contextCopy() { QgsExpressionContext context;