Skip to content
Permalink
Browse files

Layer and layer node items have access to map layer expression contex…

…t scope
  • Loading branch information
nyalldawson committed Jan 18, 2019
1 parent 7ba5998 commit 349946c669366de8281be3dbaa2edf622881274e
@@ -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;
@@ -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(),
@@ -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;
}

@@ -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)
Binary file not shown.

0 comments on commit 349946c

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