Skip to content
Permalink
Browse files

Ensure that non-spatial layers are never set as canvas layers

(cherry-picked from 8e4b8a)
  • Loading branch information
nyalldawson committed Mar 1, 2018
1 parent 7b7f915 commit 48410c40794598cac9e29451c8de75228dce312e
@@ -49,6 +49,9 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()
QgsLayerTreeLayer *nodeLayer = mRoot->findLayer( layer->id() );
if ( nodeLayer )
{
if ( !nodeLayer->layer()->isSpatial() )
continue;

allLayerOrder << nodeLayer->layer();
if ( nodeLayer->isVisible() )
canvasLayers << nodeLayer->layer();
@@ -106,11 +109,14 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers( QgsLayerTreeNode *node, QList
if ( QgsLayerTree::isLayer( node ) )
{
QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
allLayers << nodeLayer->layer();
if ( nodeLayer->isVisible() )
canvasLayers << nodeLayer->layer();
if ( nodeLayer->customProperty( QStringLiteral( "overview" ), 0 ).toInt() )
overviewLayers << nodeLayer->layer();
if ( nodeLayer->layer()->isSpatial() )
{
allLayers << nodeLayer->layer();
if ( nodeLayer->isVisible() )
canvasLayers << nodeLayer->layer();
if ( nodeLayer->customProperty( QStringLiteral( "overview" ), 0 ).toInt() )
overviewLayers << nodeLayer->layer();
}
}

Q_FOREACH ( QgsLayerTreeNode *child, node->children() )
@@ -48,6 +48,7 @@ def testLayerOrderUpdatedThroughBridge(self):
"layer2", "memory")
layer3 = QgsVectorLayer("Point?field=fldtxt:string",
"layer3", "memory")

prj.addMapLayers([layer, layer2, layer3])

canvas = QgsMapCanvas()
@@ -131,6 +132,41 @@ def testCustomLayerOrderUpdatedFromProject(self):
self.assertEqual([l for l in prj.layerTreeRoot().layerOrder()], [layer2, layer, layer3])
self.assertFalse(prj.layerTreeRoot().hasCustomLayerOrder())

def testNonSpatialLayer(self):
""" test that non spatial layers are not passed to canvas """

prj = QgsProject.instance()
prj.clear()
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
layer3 = QgsVectorLayer("Point?field=fldtxt:string",
"layer3", "memory")
non_spatial = QgsVectorLayer("None?field=fldtxt:string",
"non_spatial", "memory")

prj.addMapLayers([layer, layer2, layer3, non_spatial])

canvas = QgsMapCanvas()
bridge = QgsLayerTreeMapCanvasBridge(prj.layerTreeRoot(), canvas)

#custom layer order
prj.layerTreeRoot().setHasCustomLayerOrder(True)
prj.layerTreeRoot().setCustomLayerOrder([layer3, layer, layer2])
app.processEvents()
self.assertEqual(canvas.mapSettings().layers(), [layer3, layer, layer2])

# with non-spatial (should not be possible through ui, but is through api)
prj.layerTreeRoot().setCustomLayerOrder([layer3, layer, layer2, non_spatial])
app.processEvents()
#self.assertEqual(canvas.mapSettings().layers(),[layer3,layer,layer2])

# no custom layer order
prj.layerTreeRoot().setHasCustomLayerOrder(False)
app.processEvents()
self.assertEqual(canvas.mapSettings().layers(), [layer, layer2, layer3])


if __name__ == '__main__':
unittest.main()

0 comments on commit 48410c4

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