Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix identify menu when an expression is used as display name #34361

Merged
merged 2 commits into from
Feb 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/gui/qgsidentifymenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgsgui.h"
#include "qgsexpressioncontextutils.h"

//TODO 4.0 add explicitly qobject parent to constructor
QgsIdentifyMenu::QgsIdentifyMenu( QgsMapCanvas *canvas )
Expand Down Expand Up @@ -278,11 +279,15 @@ void QgsIdentifyMenu::addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapT
}
}

QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
QgsExpression exp( layer->displayExpression() );
exp.prepare( &context );
context.setFeature( results[0].mFeature );
// use a menu only if actions will be listed
if ( !createMenu )
{
// case 1
QString featureTitle = results[0].mFeature.attribute( layer->displayField() ).toString();
QString featureTitle = exp.evaluate( &context ).toString();
if ( featureTitle.isEmpty() )
featureTitle = QStringLiteral( "%1" ).arg( results[0].mFeature.id() );
layerAction = new QAction( QStringLiteral( "%1 (%2)" ).arg( layer->name(), featureTitle ), this );
Expand All @@ -304,7 +309,7 @@ void QgsIdentifyMenu::addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapT
// case 2b
else
{
QString featureTitle = results[0].mFeature.attribute( layer->displayField() ).toString();
QString featureTitle = exp.evaluate( &context ).toString();
if ( featureTitle.isEmpty() )
featureTitle = QStringLiteral( "%1" ).arg( results[0].mFeature.id() );
layerMenu = new QMenu( QStringLiteral( "%1 (%2)" ).arg( layer->name(), featureTitle ), this );
Expand Down Expand Up @@ -364,7 +369,8 @@ void QgsIdentifyMenu::addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapT
}

// feature title
QString featureTitle = result.mFeature.attribute( layer->displayField() ).toString();
context.setFeature( result.mFeature );
QString featureTitle = exp.evaluate( &context ).toString();
if ( featureTitle.isEmpty() )
featureTitle = QStringLiteral( "%1" ).arg( result.mFeature.id() );

Expand Down