Skip to content

Commit

Permalink
Make visible layer ids accessible via a custom QgsRenderContext property
Browse files Browse the repository at this point in the history
Provides a means for retrieving the visible layer ids from a render
context, via:

    layer_ids = rc.customRenderingFlags().get('visible_layer_ids')
  • Loading branch information
nyalldawson committed Jul 15, 2024
1 parent 9605cc3 commit fa0444d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/qgsrendercontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings &mapSet
ctx.mFrameRate = mapSettings.frameRate();
ctx.mCurrentFrame = mapSettings.currentFrame();

const QStringList layerIds = mapSettings.layerIds( true );
if ( !layerIds.empty() )
ctx.setCustomRenderingFlag( QStringLiteral( "visible_layer_ids" ), layerIds );

return ctx;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/src/python/test_qgsrendercontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
QgsRenderedFeatureHandlerInterface,
QgsUnitTypes,
QgsVectorSimplifyMethod,
QgsVectorLayer
)
import unittest
from qgis.testing import start_app, QgisTestCase
Expand Down Expand Up @@ -186,6 +187,11 @@ def testFromMapSettings(self):
ms.setFrameRate(30)
ms.setCurrentFrame(6)

layer1 = QgsVectorLayer('Point?crs=EPSG:3857', '', 'memory')
layer2 = QgsVectorLayer('Point?crs=EPSG:3857', '', 'memory')
layer3 = QgsVectorLayer('Point?crs=EPSG:3857', '', 'memory')
ms.setLayers([layer1, layer2, layer3])

ms.setTextRenderFormat(QgsRenderContext.TextRenderFormat.TextFormatAlwaysText)
rc = QgsRenderContext.fromMapSettings(ms)
self.assertEqual(rc.textRenderFormat(), QgsRenderContext.TextRenderFormat.TextFormatAlwaysText)
Expand All @@ -200,6 +206,7 @@ def testFromMapSettings(self):
self.assertEqual(rc.imageFormat(), QImage.Format.Format_Alpha8)
self.assertEqual(rc.frameRate(), 30)
self.assertEqual(rc.currentFrame(), 6)
self.assertEqual(rc.customRenderingFlags()['visible_layer_ids'], [layer1.id(), layer2.id(), layer3.id()])

# should have an valid mapToPixel
self.assertTrue(rc.mapToPixel().isValid())
Expand Down

0 comments on commit fa0444d

Please sign in to comment.