Skip to content
Permalink
Browse files

Fix identify menu when an expression is used as display name

  • Loading branch information
lbartoletti authored and nyalldawson committed Feb 7, 2020
1 parent ef108f3 commit 879294df72c8c4fb973653c1b35a421bf71e6d97
Showing with 17 additions and 3 deletions.
  1. +17 −3 src/gui/qgsidentifymenu.cpp
@@ -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 )
@@ -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 );
@@ -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 );
@@ -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() );

0 comments on commit 879294d

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