File tree 5 files changed +42
-1
lines changed
5 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -289,6 +289,13 @@ class QgsExpressionContext
289
289
*/
290
290
int indexOfScope( QgsExpressionContextScope* scope ) const;
291
291
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
+
292
299
/** Returns a list of variables names set by all scopes in the context.
293
300
* @returns list of unique variable names
294
301
* @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 @@ -307,6 +307,19 @@ int QgsExpressionContext::indexOfScope( QgsExpressionContextScope* scope ) const
307
307
return mStack .indexOf ( scope );
308
308
}
309
309
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
+
310
323
QStringList QgsExpressionContext::variableNames () const
311
324
{
312
325
QStringList names;
Original file line number Diff line number Diff line change @@ -324,6 +324,13 @@ class CORE_EXPORT QgsExpressionContext
324
324
*/
325
325
int indexOfScope ( QgsExpressionContextScope* scope ) const ;
326
326
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
+
327
334
/* * Returns a list of variables names set by all scopes in the context.
328
335
* @returns list of unique variable names
329
336
* @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 ();
@@ -300,6 +301,17 @@ void TestQgsExpressionContext::contextStack()
300
301
QCOMPARE ( scopes.at ( 0 ), scope1 );
301
302
}
302
303
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
+
303
315
void TestQgsExpressionContext::contextCopy ()
304
316
{
305
317
QgsExpressionContext context;
You can’t perform that action at this time.
0 commit comments