Skip to content

Commit

Permalink
Show user-set variables in data defined buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 8f6669f commit 4dea16a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/gui/qgsdatadefinedbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ QgsDataDefinedButton::QgsDataDefinedButton( QWidget* parent,
setMenu( mDefineMenu );

mFieldsMenu = new QMenu( this );

mActionDataTypes = new QAction( this );
// list fields and types in submenu, since there may be many
mActionDataTypes->setMenu( mFieldsMenu );

mActionVariables = new QAction( tr( "Variable" ), this );
mVariablesMenu = new QMenu( this );
mActionVariables->setMenu( mVariablesMenu );

mActionActive = new QAction( this );
QFont f = mActionActive->font();
f.setBold( true );
Expand Down Expand Up @@ -318,6 +321,39 @@ void QgsDataDefinedButton::aboutToShowMenu()
exprTitleAct->setFont( titlefont );
exprTitleAct->setEnabled( false );

mVariablesMenu->clear();
bool variableActive = false;
if ( mExpressionContextCallback )
{
QgsExpressionContext context = mExpressionContextCallback( mExpressionContextCallbackContext );
QStringList variables = context.variableNames();
Q_FOREACH ( QString variable, variables )
{
if ( context.isReadOnly( variable ) ) //only want to show user-set variables
continue;
if ( variable.startsWith( "_" ) ) //no hidden variables
continue;

QAction* act = mVariablesMenu->addAction( variable );
act->setData( QVariant( variable ) );

if ( useExpression() && hasExp && getExpression() == "@" + variable )
{
act->setCheckable( true );
act->setChecked( true );
variableActive = true;
}
}

if ( !variables.isEmpty() )
{
mDefineMenu->addAction( mActionVariables );
}
}

mVariablesMenu->menuAction()->setCheckable( true );
mVariablesMenu->menuAction()->setChecked( variableActive );

if ( hasExp )
{
QString expString = getExpression();
Expand All @@ -339,7 +375,7 @@ void QgsDataDefinedButton::aboutToShowMenu()
mActionExpression->setText( expString );
}
mDefineMenu->addAction( mActionExpression );
mActionExpression->setChecked( useExpression() );
mActionExpression->setChecked( useExpression() && !variableActive );

mDefineMenu->addAction( mActionExpDialog );
mDefineMenu->addAction( mActionCopyExpr );
Expand Down Expand Up @@ -423,6 +459,16 @@ void QgsDataDefinedButton::menuActionTriggered( QAction* action )
updateGui();
}
}
else if ( mVariablesMenu->actions().contains( action ) ) // a variable name clicked
{
if ( getExpression() != action->text().prepend( "@" ) )
{
setExpression( action->data().toString().prepend( "@" ) );
}
setUseExpression( true );
setActive( true );
updateGui();
}
}

void QgsDataDefinedButton::showDescriptionDialog()
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsdatadefinedbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
QMenu* mDefineMenu;
QAction* mActionDataTypes;
QMenu* mFieldsMenu;
QMenu* mVariablesMenu;
QAction* mActionVariables;

QAction* mActionActive;
QAction* mActionDescription;
Expand Down

0 comments on commit 4dea16a

Please sign in to comment.