File tree Expand file tree Collapse file tree 5 files changed +42
-1
lines changed Expand file tree Collapse file tree 5 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -296,6 +296,13 @@ class QgsExpressionContext
296
296
*/
297
297
int indexOfScope( QgsExpressionContextScope* scope ) const;
298
298
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
+
299
306
/** Returns a list of variables names set by all scopes in the context.
300
307
* @returns list of unique variable names
301
308
* @see filteredVariableNames
Original file line number Diff line number Diff line change @@ -114,7 +114,9 @@ void QgsComposerItemWidget::updateVariables()
114
114
{
115
115
QgsExpressionContext* context = mItem ->createExpressionContext ();
116
116
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 );
118
120
delete context;
119
121
}
120
122
Original file line number Diff line number Diff line change @@ -311,6 +311,19 @@ int QgsExpressionContext::indexOfScope( QgsExpressionContextScope* scope ) const
311
311
return mStack .indexOf ( scope );
312
312
}
313
313
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
+
314
327
QStringList QgsExpressionContext::variableNames () const
315
328
{
316
329
QStringList names;
Original file line number Diff line number Diff line change @@ -334,6 +334,13 @@ class CORE_EXPORT QgsExpressionContext
334
334
*/
335
335
int indexOfScope ( QgsExpressionContextScope* scope ) const ;
336
336
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
+
337
344
/* * Returns a list of variables names set by all scopes in the context.
338
345
* @returns list of unique variable names
339
346
* @see filteredVariableNames
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ class TestQgsExpressionContext : public QObject
37
37
void contextScopeCopy ();
38
38
void contextScopeFunctions ();
39
39
void contextStack ();
40
+ void scopeByName ();
40
41
void contextCopy ();
41
42
void contextStackFunctions ();
42
43
void evaluate ();
@@ -302,6 +303,17 @@ void TestQgsExpressionContext::contextStack()
302
303
QCOMPARE ( scopes.at ( 0 ), scope1 );
303
304
}
304
305
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
+
305
317
void TestQgsExpressionContext::contextCopy ()
306
318
{
307
319
QgsExpressionContext context;
You can’t perform that action at this time.
0 commit comments