Skip to content

Commit

Permalink
ITEM_NAME_ROLE in QgsExpressionItem to store the field name to use it…
Browse files Browse the repository at this point in the history
… on getting the example values in the expression builder

and still be able to store generic labels on the item (like the field name and the field alias)
This fixes that there has been no example values in the expression builder on the fields when an alias has been used
  • Loading branch information
signedav authored and nyalldawson committed Jun 28, 2020
1 parent 7b2dc6e commit 9b8e983
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions python/gui/auto_generated/qgsexpressiontreeview.sip.in
Expand Up @@ -67,6 +67,7 @@ Gets the type of expression item, e.g., header, field, ExpressionNode.
static const int CUSTOM_SORT_ROLE;
static const int ITEM_TYPE_ROLE;
static const int SEARCH_TAGS_ROLE;
static const int ITEM_NAME_ROLE;

};

Expand Down
12 changes: 6 additions & 6 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -309,7 +309,7 @@ void QgsExpressionBuilderWidget::expressionTreeItemChanged( QgsExpressionItem *i
{
mValuesModel->clear();

cbxValuesInUse->setVisible( formatterCanProvideAvailableValues( mLayer, item->text() ) );
cbxValuesInUse->setVisible( formatterCanProvideAvailableValues( mLayer, item->data( QgsExpressionItem::ITEM_NAME_ROLE ).toString() ) );
cbxValuesInUse->setChecked( false );
}
mValueGroupBox->setVisible( isField );
Expand Down Expand Up @@ -827,7 +827,7 @@ void QgsExpressionBuilderWidget::loadSampleValues()
return;

mValueGroupBox->show();
fillFieldValues( item->text(), 10 );
fillFieldValues( item->data( QgsExpressionItem::ITEM_NAME_ROLE ).toString(), 10 );
}

void QgsExpressionBuilderWidget::loadAllValues()
Expand All @@ -839,7 +839,7 @@ void QgsExpressionBuilderWidget::loadAllValues()
return;

mValueGroupBox->show();
fillFieldValues( item->text(), -1 );
fillFieldValues( item->data( QgsExpressionItem::ITEM_NAME_ROLE ).toString(), -1 );
}

void QgsExpressionBuilderWidget::loadSampleUsedValues()
Expand All @@ -851,7 +851,7 @@ void QgsExpressionBuilderWidget::loadSampleUsedValues()
return;

mValueGroupBox->show();
fillFieldValues( item->text(), 10, true );
fillFieldValues( item->data( QgsExpressionItem::ITEM_NAME_ROLE ).toString(), 10, true );
}

void QgsExpressionBuilderWidget::loadAllUsedValues()
Expand All @@ -863,7 +863,7 @@ void QgsExpressionBuilderWidget::loadAllUsedValues()
return;

mValueGroupBox->show();
fillFieldValues( item->text(), -1, true );
fillFieldValues( item->data( QgsExpressionItem::ITEM_NAME_ROLE ).toString(), -1, true );
}

void QgsExpressionBuilderWidget::txtPython_textChanged()
Expand Down Expand Up @@ -1113,7 +1113,7 @@ QMenu *QgsExpressionBuilderWidget::ExpressionTreeMenuProvider::createContextMenu
menu->addAction( tr( "Load First 10 Unique Values" ), mExpressionBuilderWidget, &QgsExpressionBuilderWidget::loadSampleValues );
menu->addAction( tr( "Load All Unique Values" ), mExpressionBuilderWidget, &QgsExpressionBuilderWidget::loadAllValues );

if ( formatterCanProvideAvailableValues( layer, item->text() ) )
if ( formatterCanProvideAvailableValues( layer, item->data( QgsExpressionItem::ITEM_NAME_ROLE ).toString() ) )
{
menu->addAction( tr( "Load First 10 Unique Used Values" ), mExpressionBuilderWidget, SLOT( loadSampleUsedValues() ) );
menu->addAction( tr( "Load All Unique Used Values" ), mExpressionBuilderWidget, &QgsExpressionBuilderWidget::loadAllUsedValues );
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgsexpressiontreeview.cpp
Expand Up @@ -324,12 +324,13 @@ void QgsExpressionTreeView::registerItem( const QString &group,
const QString &label,
const QString &expressionText,
const QString &helpText,
QgsExpressionItem::ItemType type, bool highlightedItem, int sortOrder, QIcon icon, const QStringList &tags )
QgsExpressionItem::ItemType type, bool highlightedItem, int sortOrder, QIcon icon, const QStringList &tags, const QString &name )
{
QgsExpressionItem *item = new QgsExpressionItem( label, expressionText, helpText, type );
item->setData( label, Qt::UserRole );
item->setData( sortOrder, QgsExpressionItem::CUSTOM_SORT_ROLE );
item->setData( tags, QgsExpressionItem::SEARCH_TAGS_ROLE );
item->setData( name, QgsExpressionItem::ITEM_NAME_ROLE );
item->setIcon( icon );

// Look up the group and insert the new function.
Expand Down Expand Up @@ -420,7 +421,7 @@ void QgsExpressionTreeView::loadFieldNames( const QgsFields &fields )
const QgsField field = fields.at( i );
QIcon icon = fields.iconForField( i );
registerItem( QStringLiteral( "Fields and Values" ), field.displayNameWithAlias(),
" \"" + field.name() + "\" ", QString(), QgsExpressionItem::Field, false, i, icon );
" \"" + field.name() + "\" ", QString(), QgsExpressionItem::Field, false, i, icon, QStringList(), field.name() );
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/gui/qgsexpressiontreeview.h
Expand Up @@ -95,6 +95,8 @@ class GUI_EXPORT QgsExpressionItem : public QStandardItem
static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
//! Search tags role
static const int SEARCH_TAGS_ROLE = Qt::UserRole + 3;
//! Item name role
static const int ITEM_NAME_ROLE = Qt::UserRole + 4;

private:
QString mExpressionText;
Expand Down Expand Up @@ -308,7 +310,8 @@ class GUI_EXPORT QgsExpressionTreeView : public QTreeView
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false, int sortOrder = 1,
QIcon icon = QIcon(),
const QStringList &tags = QStringList() );
const QStringList &tags = QStringList(),
const QString &name = QString() );

/**
* Registers a node item for the expression builder, adding multiple items when the function exists in multiple groups
Expand Down

0 comments on commit 9b8e983

Please sign in to comment.