Skip to content

Commit

Permalink
Ensure that non-spatial layers are never set as canvas layers
Browse files Browse the repository at this point in the history
(cherry-picked from 8e4b8a)
  • Loading branch information
nyalldawson committed Mar 6, 2018
1 parent 7b7f915 commit 48410c4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/gui/layertree/qgslayertreemapcanvasbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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() )
Expand Down
36 changes: 36 additions & 0 deletions tests/src/python/test_qgslayertreemapcanvasbridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def testLayerOrderUpdatedThroughBridge(self):
"layer2", "memory")
layer3 = QgsVectorLayer("Point?field=fldtxt:string",
"layer3", "memory")

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

canvas = QgsMapCanvas()
Expand Down Expand Up @@ -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.