Skip to content

Commit

Permalink
Merge pull request qgis#54692 from elpaso/bugfix-gh8155-atlas-legend-…
Browse files Browse the repository at this point in the history
…support-clipping

ATLAS: LEGEND support clipping
  • Loading branch information
elpaso authored Sep 26, 2023
2 parents 5d497bc + bbfefcf commit 096d741
Show file tree
Hide file tree
Showing 4 changed files with 1,477 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/core/layout/qgslayoutitemlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,14 +1133,29 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()

mapSettings.setExpressionContext( createExpressionContext() );

const QgsGeometry atlasGeometry = mInAtlas ? mLayout->reportContext().currentGeometry( mapSettings.destinationCrs() ) : QgsGeometry();
const QgsGeometry atlasGeometry { mLayout->reportContext().currentGeometry( mapSettings.destinationCrs() ) };

QgsLayerTreeFilterSettings filterSettings( mapSettings );

QList<QgsMapLayer *> layersToClip;
if ( !atlasGeometry.isNull() && mMap->atlasClippingSettings()->enabled() )
{
layersToClip = mMap->atlasClippingSettings()->layersToClip();
for ( QgsMapLayer *layer : std::as_const( layersToClip ) )
{
QList<QgsMapLayer *> mapLayers { filterSettings.mapSettings().layers( true ) };
mapLayers.removeAll( layer );
filterSettings.mapSettings().setLayers( mapLayers );
filterSettings.addVisibleExtentForLayer( layer, QgsReferencedGeometry( atlasGeometry, mapSettings.destinationCrs() ) );
}
}


if ( !linkedFilterMaps.empty() )
{
for ( QgsLayoutItemMap *map : std::as_const( linkedFilterMaps ) )
{

if ( map == mMap )
continue;

Expand All @@ -1160,7 +1175,7 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()
const QList< QgsMapLayer * > layersForMap = map->layersToRender();
for ( QgsMapLayer *layer : layersForMap )
{
if ( !atlasGeometry.isNull() )
if ( mInAtlas && !atlasGeometry.isNull() )
{
mapExtent = mapExtent.intersection( atlasGeometry );
}
Expand Down
33 changes: 32 additions & 1 deletion tests/src/python/test_qgslayoutlegend.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
QgsSymbol,
QgsVectorLayer,
QgsReadWriteContext,
QgsTextFormat
QgsTextFormat,
QgsFeatureRequest,
)
import unittest
from qgis.testing import start_app, QgisTestCase
Expand Down Expand Up @@ -1131,6 +1132,36 @@ def test_filter_by_map_content_rendering_different_layers(self):
)
)

def test_atlas_legend_clipping(self):
"""Test issue GH #54654"""

p = QgsProject()
self.assertTrue(p.read(os.path.join(TEST_DATA_DIR, 'layouts', 'atlas_legend_clipping.qgs')))

layout = p.layoutManager().layoutByName('layout1')

layer = list(p.mapLayers().values())[0]
feature = layer.getFeature(3)
req = QgsFeatureRequest()
req.setFilterExpression('value = 11')
feature = next(layer.getFeatures(req))

layout.reportContext().setFeature(feature)
legend = layout.items()[0]
legend.setStyleFont(QgsLegendStyle.Title, QgsFontUtils.getStandardTestFont('Bold', 20))
legend.setStyleFont(QgsLegendStyle.Group, QgsFontUtils.getStandardTestFont('Bold', 20))
legend.setStyleFont(QgsLegendStyle.Subgroup, QgsFontUtils.getStandardTestFont('Bold', 20))
legend.setStyleFont(QgsLegendStyle.Symbol, QgsFontUtils.getStandardTestFont('Bold', 20))
legend.setStyleFont(QgsLegendStyle.SymbolLabel,
QgsFontUtils.getStandardTestFont('Bold', 20))
legend.refresh()

self.assertTrue(
self.render_layout_check(
'atlas_legend_clipping', layout
)
)

def test_filter_by_map_content_rendering_different_layers_in_atlas(self):
point_path = os.path.join(TEST_DATA_DIR, 'points.shp')
point_layer = QgsVectorLayer(point_path, 'points', 'ogr')
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 096d741

Please sign in to comment.