Skip to content

Commit 4dea16a

Browse files
committed
Show user-set variables in data defined buttons
1 parent 8f6669f commit 4dea16a

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/gui/qgsdatadefinedbutton.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ QgsDataDefinedButton::QgsDataDefinedButton( QWidget* parent,
6969
setMenu( mDefineMenu );
7070

7171
mFieldsMenu = new QMenu( this );
72-
7372
mActionDataTypes = new QAction( this );
7473
// list fields and types in submenu, since there may be many
7574
mActionDataTypes->setMenu( mFieldsMenu );
7675

76+
mActionVariables = new QAction( tr( "Variable" ), this );
77+
mVariablesMenu = new QMenu( this );
78+
mActionVariables->setMenu( mVariablesMenu );
79+
7780
mActionActive = new QAction( this );
7881
QFont f = mActionActive->font();
7982
f.setBold( true );
@@ -318,6 +321,39 @@ void QgsDataDefinedButton::aboutToShowMenu()
318321
exprTitleAct->setFont( titlefont );
319322
exprTitleAct->setEnabled( false );
320323

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+
321357
if ( hasExp )
322358
{
323359
QString expString = getExpression();
@@ -339,7 +375,7 @@ void QgsDataDefinedButton::aboutToShowMenu()
339375
mActionExpression->setText( expString );
340376
}
341377
mDefineMenu->addAction( mActionExpression );
342-
mActionExpression->setChecked( useExpression() );
378+
mActionExpression->setChecked( useExpression() && !variableActive );
343379

344380
mDefineMenu->addAction( mActionExpDialog );
345381
mDefineMenu->addAction( mActionCopyExpr );
@@ -423,6 +459,16 @@ void QgsDataDefinedButton::menuActionTriggered( QAction* action )
423459
updateGui();
424460
}
425461
}
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+
}
426472
}
427473

428474
void QgsDataDefinedButton::showDescriptionDialog()

src/gui/qgsdatadefinedbutton.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
319319
QMenu* mDefineMenu;
320320
QAction* mActionDataTypes;
321321
QMenu* mFieldsMenu;
322+
QMenu* mVariablesMenu;
323+
QAction* mActionVariables;
322324

323325
QAction* mActionActive;
324326
QAction* mActionDescription;

0 commit comments

Comments
 (0)