Skip to content
Permalink
Browse files

[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 b172a92 commit da3ca926042bdd9e4f84d624c16e7a276a6cbb17
@@ -289,6 +289,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
@@ -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;
}

@@ -307,6 +307,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;
@@ -324,6 +324,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
@@ -37,6 +37,7 @@ class TestQgsExpressionContext : public QObject
void contextScopeCopy();
void contextScopeFunctions();
void contextStack();
void scopeByName();
void contextCopy();
void contextStackFunctions();
void evaluate();
@@ -300,6 +301,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;

0 comments on commit da3ca92

Please sign in to comment.
You can’t perform that action at this time.