Skip to content
Permalink
Browse files

Show user-set variables in data defined buttons

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 8f6669f commit 4dea16ad0b3c89e79d1c76d42cc5e2614b7f10e5
Showing with 50 additions and 2 deletions.
  1. +48 −2 src/gui/qgsdatadefinedbutton.cpp
  2. +2 −0 src/gui/qgsdatadefinedbutton.h
@@ -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 );
@@ -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();
@@ -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 );
@@ -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()
@@ -319,6 +319,8 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
QMenu* mDefineMenu;
QAction* mActionDataTypes;
QMenu* mFieldsMenu;
QMenu* mVariablesMenu;
QAction* mActionVariables;

QAction* mActionActive;
QAction* mActionDescription;

0 comments on commit 4dea16a

Please sign in to comment.
You can’t perform that action at this time.