Skip to content
Permalink
Browse files
Partly revert previous approach: and rely on layout clone
Disable follow preset and manually add layers
  • Loading branch information
elpaso committed May 26, 2021
1 parent cf45b6b commit 6657f8bea52b639c5d5a131b5e320f7afe847457
@@ -2128,17 +2128,17 @@ QList<QgsMapLayer *> QgsLayoutItemMap::layersToRender( const QgsExpressionContex
QList<QgsMapLayer *> renderLayers;

QString presetName = themeToRender( *evalContext );
if ( !layers().isEmpty() )
{
renderLayers = layers();
}
else if ( !presetName.isEmpty() )
if ( !presetName.isEmpty() )
{
if ( mLayout->project()->mapThemeCollection()->hasMapTheme( presetName ) )
renderLayers = mLayout->project()->mapThemeCollection()->mapThemeVisibleLayers( presetName );
else // fallback to using map canvas layers
renderLayers = mLayout->project()->mapThemeCollection()->masterVisibleLayers();
}
else if ( !layers().isEmpty() )
{
renderLayers = layers();
}
else
{
renderLayers = mLayout->project()->mapThemeCollection()->masterVisibleLayers();
@@ -653,7 +653,7 @@ namespace QgsWms
}
else
{
for ( const auto &layer : std::as_const( cMapParams.mLayers ) )
for ( auto layer : cMapParams.mLayers )
{
if ( mContext.isValidGroup( layer.mNickname ) )
{
@@ -689,9 +689,30 @@ namespace QgsWms
layerSet << mlayer;
}
}
std::reverse( layerSet.begin(), layerSet.end() );
}
layerSet << highlightLayers( cMapParams.mHighlightLayers );
std::reverse( layerSet.begin(), layerSet.end() );

// If the map is set to follow preset we need to disable follow preset and manually
// configure the layers here or the map item internal logic will override and get
// the layers from the map theme.
if ( map->followVisibilityPreset() )
{
if ( layerSet.isEmpty() )
{
// Get the layers from the theme
const QgsExpressionContext ex { map->createExpressionContext() };
layerSet = map->layersToRender( &ex );
}
map->setFollowVisibilityPreset( false );
}

// Handle highlight layers
const QList< QgsMapLayer *> highlights = highlightLayers( cMapParams.mHighlightLayers );
for ( const auto &hl : std::as_const( highlights ) )
{
layerSet.prepend( hl );
}

map->setLayers( layerSet );
map->setKeepLayerSet( true );
}
@@ -252,10 +252,6 @@ void TestQgsLayoutGeoPdfExport::testCollectingFeatures()
map2->setFollowVisibilityPreset( true );
map2->setFollowVisibilityPresetName( QStringLiteral( "test preset3" ) );

// Clear layers
map->setLayers( {} );
map2->setLayers( {} );

QgsLayoutGeoPdfExporter geoPdfExporter2( &l );
settings = QgsLayoutExporter::PdfExportSettings();
settings.writeGeoPdf = true;
@@ -1397,7 +1397,7 @@ void TestQgsLayoutMap::testLayeredExport()

l.renderContext().setExportThemes( QStringList() << QStringLiteral( "test preset2" ) << QStringLiteral( "test preset" ) << QStringLiteral( "test preset3" ) );

map->setLayers( {} );

map->startLayeredExport();
QVERIFY( map->nextExportPart() );
map->createStagedRenderJob( map->extent(), QSize( 512, 512 ), 72 );
@@ -37,9 +37,9 @@ class PyQgsServerWMSGetPrintMapTheme(QgsServerTestBase):

def test_wms_getprint_maptheme(self):
"""Test project has 2 layer: red and green and three templates:
red: map theme red
green: map theme green
blank: non map theme
red: follow map theme red
green: follow map theme green
blank: no map theme
"""

tmp_dir = QTemporaryDir()
@@ -67,6 +67,7 @@ def test_wms_getprint_maptheme(self):
######################################################
# Template map theme tests, no HIGHLIGHT

# blank template, specified layer is red
response = QgsBufferServerResponse()
request = QgsBufferServerRequest('?' + '&'.join(["%s=%s" % i for i in params.items()]))
self.server.handleRequest(request, response, project)
@@ -77,6 +78,7 @@ def test_wms_getprint_maptheme(self):
self.assertEqual(color.green(), 0)
self.assertEqual(color.blue(), 0)

# blank template, specified layer is green
params["map0:LAYERS"] = "green"
response = QgsBufferServerResponse()
request = QgsBufferServerRequest('?' + '&'.join(["%s=%s" % i for i in params.items()]))
@@ -88,6 +90,7 @@ def test_wms_getprint_maptheme(self):
self.assertEqual(color.green(), 255)
self.assertEqual(color.blue(), 0)

# red template, no specified layers
params["map0:LAYERS"] = ""
params["TEMPLATE"] = "red"
response = QgsBufferServerResponse()
@@ -100,6 +103,7 @@ def test_wms_getprint_maptheme(self):
self.assertEqual(color.green(), 0)
self.assertEqual(color.blue(), 0)

# green template, no specified layers
params["map0:LAYERS"] = ""
params["TEMPLATE"] = "green"
response = QgsBufferServerResponse()
@@ -112,6 +116,21 @@ def test_wms_getprint_maptheme(self):
self.assertEqual(color.green(), 255)
self.assertEqual(color.blue(), 0)

# green template, specified layer is red
# This is a conflict situation: the green template map is set to follow green theme
# but we tell the server to render the red layer, red is what we get.
params["map0:LAYERS"] = "red"
params["TEMPLATE"] = "green"
response = QgsBufferServerResponse()
request = QgsBufferServerRequest('?' + '&'.join(["%s=%s" % i for i in params.items()]))
self.server.handleRequest(request, response, project)

image = QImage.fromData(response.body(), "PNG")
color = image.pixelColor(100, 100)
self.assertEqual(color.red(), 255)
self.assertEqual(color.green(), 0)
self.assertEqual(color.blue(), 0)

######################################################
# Start HIGHLIGHT tests

0 comments on commit 6657f8b

Please sign in to comment.