Skip to content

Commit c08b6d7

Browse files
committedJul 26, 2016
[composer] Fix editing of map item variables
On behalf of Faunalia, sponsored by ENEL (cherry-picked from 5384e20)
1 parent d68025c commit c08b6d7

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed
 

‎python/core/qgsexpressioncontext.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@ class QgsExpressionContext
296296
*/
297297
int indexOfScope( QgsExpressionContextScope* scope ) const;
298298

299+
/** Returns the index of the first scope with a matching name within the context.
300+
* @param scopeName name of scope to find
301+
* @returns index of scope, or -1 if scope was not found within the context.
302+
* @note added in QGIS 3.0
303+
*/
304+
int indexOfScope( const QString& scopeName ) const;
305+
299306
/** Returns a list of variables names set by all scopes in the context.
300307
* @returns list of unique variable names
301308
* @see filteredVariableNames

‎src/app/composer/qgscomposeritemwidget.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ void QgsComposerItemWidget::updateVariables()
114114
{
115115
QgsExpressionContext* context = mItem->createExpressionContext();
116116
mVariableEditor->setContext( context );
117-
mVariableEditor->setEditableScopeIndex( context->scopeCount() - 1 );
117+
int editableIndex = context->indexOfScope( tr( "Composer Item" ) );
118+
if ( editableIndex >= 0 )
119+
mVariableEditor->setEditableScopeIndex( editableIndex );
118120
delete context;
119121
}
120122

‎src/core/qgsexpressioncontext.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,19 @@ int QgsExpressionContext::indexOfScope( QgsExpressionContextScope* scope ) const
311311
return mStack.indexOf( scope );
312312
}
313313

314+
int QgsExpressionContext::indexOfScope( const QString& scopeName ) const
315+
{
316+
int index = 0;
317+
Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
318+
{
319+
if ( scope->name() == scopeName )
320+
return index;
321+
322+
index++;
323+
}
324+
return -1;
325+
}
326+
314327
QStringList QgsExpressionContext::variableNames() const
315328
{
316329
QStringList names;

‎src/core/qgsexpressioncontext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ class CORE_EXPORT QgsExpressionContext
334334
*/
335335
int indexOfScope( QgsExpressionContextScope* scope ) const;
336336

337+
/** Returns the index of the first scope with a matching name within the context.
338+
* @param scopeName name of scope to find
339+
* @returns index of scope, or -1 if scope was not found within the context.
340+
* @note added in QGIS 3.0
341+
*/
342+
int indexOfScope( const QString& scopeName ) const;
343+
337344
/** Returns a list of variables names set by all scopes in the context.
338345
* @returns list of unique variable names
339346
* @see filteredVariableNames

‎tests/src/core/testqgsexpressioncontext.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class TestQgsExpressionContext : public QObject
3737
void contextScopeCopy();
3838
void contextScopeFunctions();
3939
void contextStack();
40+
void scopeByName();
4041
void contextCopy();
4142
void contextStackFunctions();
4243
void evaluate();
@@ -302,6 +303,17 @@ void TestQgsExpressionContext::contextStack()
302303
QCOMPARE( scopes.at( 0 ), scope1 );
303304
}
304305

306+
void TestQgsExpressionContext::scopeByName()
307+
{
308+
QgsExpressionContext context;
309+
QCOMPARE( context.indexOfScope( "test1" ), -1 );
310+
context << new QgsExpressionContextScope( "test1" );
311+
context << new QgsExpressionContextScope( "test2" );
312+
QCOMPARE( context.indexOfScope( "test1" ), 0 );
313+
QCOMPARE( context.indexOfScope( "test2" ), 1 );
314+
QCOMPARE( context.indexOfScope( "not in context" ), -1 );
315+
}
316+
305317
void TestQgsExpressionContext::contextCopy()
306318
{
307319
QgsExpressionContext context;

0 commit comments

Comments
 (0)
Please sign in to comment.