Skip to content

Commit

Permalink
Fix identify menu when an expression is used as display name
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti authored and nyalldawson committed Feb 11, 2020
1 parent ef108f3 commit 879294d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 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 @@ -282,7 +283,12 @@ void QgsIdentifyMenu::addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapT
if ( !createMenu )
{
// case 1
QString featureTitle = results[0].mFeature.attribute( layer->displayField() ).toString();

QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
QgsExpression exp( layer->displayExpression() );
exp.prepare( &context );
context.setFeature( results[0].mFeature );
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 +310,11 @@ void QgsIdentifyMenu::addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapT
// case 2b
else
{
QString featureTitle = results[0].mFeature.attribute( layer->displayField() ).toString();
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
QgsExpression exp( layer->displayExpression() );
exp.prepare( &context );
context.setFeature( results[0].mFeature );
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 +374,11 @@ void QgsIdentifyMenu::addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapT
}

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

Expand Down

0 comments on commit 879294d

Please sign in to comment.