Skip to content

Commit da3ca92

Browse files
committed
[composer] Fix editing of map item variables
On behalf of Faunalia, sponsored by ENEL (cherry-picked from 5384e20)
1 parent b172a92 commit da3ca92

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

python/core/qgsexpressioncontext.sip

+7
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ class QgsExpressionContext
289289
*/
290290
int indexOfScope( QgsExpressionContextScope* scope ) const;
291291

292+
/** Returns the index of the first scope with a matching name within the context.
293+
* @param scopeName name of scope to find
294+
* @returns index of scope, or -1 if scope was not found within the context.
295+
* @note added in QGIS 3.0
296+
*/
297+
int indexOfScope( const QString& scopeName ) const;
298+
292299
/** Returns a list of variables names set by all scopes in the context.
293300
* @returns list of unique variable names
294301
* @see filteredVariableNames

src/app/composer/qgscomposeritemwidget.cpp

+3-1
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

+13
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@ int QgsExpressionContext::indexOfScope( QgsExpressionContextScope* scope ) const
307307
return mStack.indexOf( scope );
308308
}
309309

310+
int QgsExpressionContext::indexOfScope( const QString& scopeName ) const
311+
{
312+
int index = 0;
313+
Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
314+
{
315+
if ( scope->name() == scopeName )
316+
return index;
317+
318+
index++;
319+
}
320+
return -1;
321+
}
322+
310323
QStringList QgsExpressionContext::variableNames() const
311324
{
312325
QStringList names;

src/core/qgsexpressioncontext.h

+7
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,13 @@ class CORE_EXPORT QgsExpressionContext
324324
*/
325325
int indexOfScope( QgsExpressionContextScope* scope ) const;
326326

327+
/** Returns the index of the first scope with a matching name within the context.
328+
* @param scopeName name of scope to find
329+
* @returns index of scope, or -1 if scope was not found within the context.
330+
* @note added in QGIS 3.0
331+
*/
332+
int indexOfScope( const QString& scopeName ) const;
333+
327334
/** Returns a list of variables names set by all scopes in the context.
328335
* @returns list of unique variable names
329336
* @see filteredVariableNames

tests/src/core/testqgsexpressioncontext.cpp

+12
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();
@@ -300,6 +301,17 @@ void TestQgsExpressionContext::contextStack()
300301
QCOMPARE( scopes.at( 0 ), scope1 );
301302
}
302303

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

0 commit comments

Comments
 (0)