Skip to content

Commit

Permalink
Add method for highlighting variables in the expression builder
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 334ea50 commit 85bda6c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
15 changes: 15 additions & 0 deletions python/core/qgsexpressioncontext.sip
Expand Up @@ -221,6 +221,21 @@ class QgsExpressionContext
*/
QVariant variable( const QString& name ) const;

/** Returns true if the specified variable name is intended to be highlighted to the
* user. This is used by the expression builder to more prominently display the
* variable.
* @param name variable name
* @see setHighlightedVariables()
*/
bool isHighlightedVariable( const QString& name ) const;

/** Sets the list of variable names within the context intended to be highlighted to the user. This
* is used by the expression builder to more prominently display these variables.
* @param variableNames variable names to highlight
* @see isHighlightedVariable()
*/
void setHighlightedVariables( const QStringList& variableNames );

/** Returns the currently active scope from the context for a specified variable name.
* As scopes later in the stack override earlier contexts, this will be the last matching
* scope which contains a matching variable.
Expand Down
4 changes: 3 additions & 1 deletion python/gui/qgsexpressionbuilderwidget.sip
Expand Up @@ -121,10 +121,12 @@ class QgsExpressionBuilderWidget : QWidget
* @param expressionText The text that is inserted into the expression area when the user double clicks on the item.
* @param helpText The help text that the user will see when item is selected.
* @param type The type of the expression item.
* @param highlightedItem set to true to make the item highlighted, which inserts a bold copy of the item at the top level
*/
void registerItem( QString group, QString label, QString expressionText,
QString helpText = "",
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode );
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false );

bool isExpressionValid();

Expand Down
12 changes: 12 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -158,6 +158,7 @@ QgsExpressionContext::QgsExpressionContext( const QgsExpressionContext& other )
{
mStack << new QgsExpressionContextScope( *scope );
}
mHighlightedVariables = other.mHighlightedVariables;
}

QgsExpressionContext& QgsExpressionContext::operator=( const QgsExpressionContext & other )
Expand All @@ -168,6 +169,7 @@ QgsExpressionContext& QgsExpressionContext::operator=( const QgsExpressionContex
{
mStack << new QgsExpressionContextScope( *scope );
}
mHighlightedVariables = other.mHighlightedVariables;
return *this;
}

Expand All @@ -193,6 +195,16 @@ QVariant QgsExpressionContext::variable( const QString& name ) const
return scope ? scope->variable( name ) : QVariant();
}

bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const
{
return mHighlightedVariables.contains( name );
}

void QgsExpressionContext::setHighlightedVariables( const QStringList& variableNames )
{
mHighlightedVariables = variableNames;
}

const QgsExpressionContextScope* QgsExpressionContext::activeScopeForVariable( const QString& name ) const
{
//iterate through stack backwards, so that higher priority variables take precedence
Expand Down
16 changes: 16 additions & 0 deletions src/core/qgsexpressioncontext.h
Expand Up @@ -252,6 +252,21 @@ class CORE_EXPORT QgsExpressionContext
*/
QVariant variable( const QString& name ) const;

/** Returns true if the specified variable name is intended to be highlighted to the
* user. This is used by the expression builder to more prominently display the
* variable.
* @param name variable name
* @see setHighlightedVariables()
*/
bool isHighlightedVariable( const QString& name ) const;

/** Sets the list of variable names within the context intended to be highlighted to the user. This
* is used by the expression builder to more prominently display these variables.
* @param variableNames variable names to highlight
* @see isHighlightedVariable()
*/
void setHighlightedVariables( const QStringList& variableNames );

/** Returns the currently active scope from the context for a specified variable name.
* As scopes later in the stack override earlier contexts, this will be the last matching
* scope which contains a matching variable.
Expand Down Expand Up @@ -373,6 +388,7 @@ class CORE_EXPORT QgsExpressionContext
private:

QList< QgsExpressionContextScope* > mStack;
QStringList mHighlightedVariables;

};

Expand Down
20 changes: 18 additions & 2 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -336,10 +336,11 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
QString label,
QString expressionText,
QString helpText,
QgsExpressionItem::ItemType type )
QgsExpressionItem::ItemType type, bool highlightedItem )
{
QgsExpressionItem* item = new QgsExpressionItem( label, expressionText, helpText, type );
item->setData( label, Qt::UserRole );

// Look up the group and insert the new function.
if ( mExpressionGroups.contains( group ) )
{
Expand All @@ -355,6 +356,18 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
mModel->appendRow( newgroupNode );
mExpressionGroups.insert( group, newgroupNode );
}

if ( highlightedItem )
{
//insert a copy as a top level item
QgsExpressionItem* topLevelItem = new QgsExpressionItem( label, expressionText, helpText, type );
topLevelItem->setData( label, Qt::UserRole );
QFont font = topLevelItem->font();
font.setBold( true );
topLevelItem->setFont( font );
mModel->appendRow( topLevelItem );
}

}

bool QgsExpressionBuilderWidget::isExpressionValid()
Expand Down Expand Up @@ -581,7 +594,10 @@ void QgsExpressionBuilderWidget::loadExpressionContext()
QStringList variableNames = mExpressionContext.filteredVariableNames();
Q_FOREACH ( QString variable, variableNames )
{
registerItem( "Variables", variable, " @" + variable + " ", QgsExpression::variableHelpText( variable, true, mExpressionContext.variable( variable ) ) );
registerItem( "Variables", variable, " @" + variable + " ",
QgsExpression::variableHelpText( variable, true, mExpressionContext.variable( variable ) ),
QgsExpressionItem::ExpressionNode,
mExpressionContext.isHighlightedVariable( variable ) );
}

// Load the functions from the expression context
Expand Down
4 changes: 3 additions & 1 deletion src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -163,10 +163,12 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
* @param expressionText The text that is inserted into the expression area when the user double clicks on the item.
* @param helpText The help text that the user will see when item is selected.
* @param type The type of the expression item.
* @param highlightedItem set to true to make the item highlighted, which inserts a bold copy of the item at the top level
*/
void registerItem( QString group, QString label, QString expressionText,
QString helpText = "",
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode );
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false );

bool isExpressionValid();

Expand Down

0 comments on commit 85bda6c

Please sign in to comment.