@@ -69,11 +69,14 @@ QgsDataDefinedButton::QgsDataDefinedButton( QWidget* parent,
69
69
setMenu ( mDefineMenu );
70
70
71
71
mFieldsMenu = new QMenu ( this );
72
-
73
72
mActionDataTypes = new QAction ( this );
74
73
// list fields and types in submenu, since there may be many
75
74
mActionDataTypes ->setMenu ( mFieldsMenu );
76
75
76
+ mActionVariables = new QAction ( tr ( " Variable" ), this );
77
+ mVariablesMenu = new QMenu ( this );
78
+ mActionVariables ->setMenu ( mVariablesMenu );
79
+
77
80
mActionActive = new QAction ( this );
78
81
QFont f = mActionActive ->font ();
79
82
f.setBold ( true );
@@ -318,6 +321,39 @@ void QgsDataDefinedButton::aboutToShowMenu()
318
321
exprTitleAct->setFont ( titlefont );
319
322
exprTitleAct->setEnabled ( false );
320
323
324
+ mVariablesMenu ->clear ();
325
+ bool variableActive = false ;
326
+ if ( mExpressionContextCallback )
327
+ {
328
+ QgsExpressionContext context = mExpressionContextCallback ( mExpressionContextCallbackContext );
329
+ QStringList variables = context.variableNames ();
330
+ Q_FOREACH ( QString variable, variables )
331
+ {
332
+ if ( context.isReadOnly ( variable ) ) // only want to show user-set variables
333
+ continue ;
334
+ if ( variable.startsWith ( " _" ) ) // no hidden variables
335
+ continue ;
336
+
337
+ QAction* act = mVariablesMenu ->addAction ( variable );
338
+ act->setData ( QVariant ( variable ) );
339
+
340
+ if ( useExpression () && hasExp && getExpression () == " @" + variable )
341
+ {
342
+ act->setCheckable ( true );
343
+ act->setChecked ( true );
344
+ variableActive = true ;
345
+ }
346
+ }
347
+
348
+ if ( !variables.isEmpty () )
349
+ {
350
+ mDefineMenu ->addAction ( mActionVariables );
351
+ }
352
+ }
353
+
354
+ mVariablesMenu ->menuAction ()->setCheckable ( true );
355
+ mVariablesMenu ->menuAction ()->setChecked ( variableActive );
356
+
321
357
if ( hasExp )
322
358
{
323
359
QString expString = getExpression ();
@@ -339,7 +375,7 @@ void QgsDataDefinedButton::aboutToShowMenu()
339
375
mActionExpression ->setText ( expString );
340
376
}
341
377
mDefineMenu ->addAction ( mActionExpression );
342
- mActionExpression ->setChecked ( useExpression () );
378
+ mActionExpression ->setChecked ( useExpression () && !variableActive );
343
379
344
380
mDefineMenu ->addAction ( mActionExpDialog );
345
381
mDefineMenu ->addAction ( mActionCopyExpr );
@@ -423,6 +459,16 @@ void QgsDataDefinedButton::menuActionTriggered( QAction* action )
423
459
updateGui ();
424
460
}
425
461
}
462
+ else if ( mVariablesMenu ->actions ().contains ( action ) ) // a variable name clicked
463
+ {
464
+ if ( getExpression () != action->text ().prepend ( " @" ) )
465
+ {
466
+ setExpression ( action->data ().toString ().prepend ( " @" ) );
467
+ }
468
+ setUseExpression ( true );
469
+ setActive ( true );
470
+ updateGui ();
471
+ }
426
472
}
427
473
428
474
void QgsDataDefinedButton::showDescriptionDialog ()
0 commit comments