Skip to content

Commit

Permalink
Layer and layer node items have access to map layer expression contex…
Browse files Browse the repository at this point in the history
…t scope
  • Loading branch information
nyalldawson committed Jan 18, 2019
1 parent 7ba5998 commit 349946c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/core/qgslegendrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,25 @@ QgsLegendRenderer::Nucleon QgsLegendRenderer::drawSymbolItemInternal( QgsLayerTr
{
QgsLayerTreeModelLegendNode::ItemContext ctx;
ctx.context = context;

// add a layer expression context scope
QgsExpressionContextScope *layerScope = nullptr;
if ( context && symbolItem->layerNode()->layer() )
{
layerScope = QgsExpressionContextUtils::layerScope( symbolItem->layerNode()->layer() );
context->expressionContext().appendScope( layerScope );
}

ctx.painter = context ? context->painter() : painter;
ctx.point = point;
ctx.labelXOffset = labelXOffset;

QgsLayerTreeModelLegendNode::ItemMetrics im = symbolItem->draw( mSettings, context ? &ctx
: ( painter ? &ctx : nullptr ) );

if ( layerScope )
delete context->expressionContext().popScope();

Nucleon nucleon;
nucleon.item = symbolItem;
nucleon.symbolSize = im.symbolSize;
Expand Down Expand Up @@ -578,6 +590,13 @@ QSizeF QgsLegendRenderer::drawLayerTitleInternal( QgsLayerTreeLayer *nodeLayer,

QFont layerFont = mSettings.style( nodeLegendStyle( nodeLayer ) ).font();

QgsExpressionContextScope *layerScope = nullptr;
if ( context && nodeLayer->layer() )
{
layerScope = QgsExpressionContextUtils::layerScope( nodeLayer->layer() );
context->expressionContext().appendScope( layerScope );
}

QgsExpressionContext tempContext;

const QStringList lines = mSettings.evaluateItemText( mLegendModel->data( idx, Qt::DisplayRole ).toString(),
Expand All @@ -599,6 +618,9 @@ QSizeF QgsLegendRenderer::drawLayerTitleInternal( QgsLayerTreeLayer *nodeLayer,
size.rheight() = y - point.y();
size.rheight() += mSettings.style( nodeLegendStyle( nodeLayer ) ).margin( QgsLegendStyle::Side::Bottom );

if ( layerScope )
delete context->expressionContext().popScope();

return size;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/src/python/test_qgslayoutlegend.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ def testExpressionInText(self):

group = legend.model().rootGroup().addGroup("Group [% 1 + 5 %] [% @layout_name %]")
layer_tree_layer = group.addLayer(point_layer)
layer_tree_layer.setCustomProperty("legend/title-label", 'bbbb [% 1+2 %] xx [% @layout_name %]')
layer_tree_layer.setCustomProperty("legend/title-label", 'bbbb [% 1+2 %] xx [% @layout_name %] [% @layer_name %]')
QgsMapLayerLegendUtils.setLegendNodeUserLabel(layer_tree_layer, 0, 'xxxx')
legend.model().refreshLayerLegend(layer_tree_layer)
legend.model().layerLegendNodes(layer_tree_layer)[0].setUserLabel('bbbb [% 1+2 %] xx [% @layout_name %]')
legend.model().layerLegendNodes(layer_tree_layer)[0].setUserLabel('bbbb [% 1+2 %] xx [% @layout_name %] [% @layer_name %]')

layout.addLayoutItem(legend)
legend.setLinkedMap(map)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 349946c

Please sign in to comment.