Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[composer] Fix editing of map item variables
On behalf of Faunalia, sponsored by ENEL

(cherry-picked from 5384e20)
  • Loading branch information
nyalldawson committed Jul 26, 2016
1 parent d68025c commit c08b6d7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/core/qgsexpressioncontext.sip
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/app/composer/qgscomposeritemwidget.cpp
Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -311,6 +311,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;
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsexpressioncontext.h
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/src/core/testqgsexpressioncontext.cpp
Expand Up @@ -37,6 +37,7 @@ class TestQgsExpressionContext : public QObject
void contextScopeCopy();
void contextScopeFunctions();
void contextStack();
void scopeByName();
void contextCopy();
void contextStackFunctions();
void evaluate();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c08b6d7

Please sign in to comment.