Skip to content
Permalink
Browse files

Fix duplicate layers in custom layer order panel

Fixes #21955
  • Loading branch information
nyalldawson committed Apr 30, 2019
1 parent 24510cb commit 546b041214525999e27b885c5e7d4fc7f2d1da1f
Showing with 35 additions and 1 deletion.
  1. +11 −0 src/core/layertree/qgslayertree.cpp
  2. +24 −1 tests/src/python/test_qgslayertree.py
@@ -209,7 +209,18 @@ void QgsLayerTree::nodeRemovedChildren()
++layer;
}

// we need to ensure that the customLayerOrderChanged signal is ALWAYS raised
// here, since that order HAS changed due to removal of the child!
// setCustomLayerOrder will only emit this signal when the layers list
// at this stage is different to the stored customer layer order. If this
// isn't the case (i.e. the lists ARE the same) then manually emit the
// signal
const bool emitSignal = _qgis_listRawToQPointer( layers ) == mCustomLayerOrder;

setCustomLayerOrder( layers );
if ( emitSignal )
emit customLayerOrderChanged();

emit layerOrderChanged();
}

@@ -19,7 +19,8 @@
from qgis.core import (
QgsLayerTree,
QgsProject,
QgsVectorLayer
QgsVectorLayer,
QgsLayerTreeLayer
)
from qgis.testing import start_app, unittest
from utilities import (unitTestDataPath)
@@ -78,6 +79,28 @@ def testCustomLayerOrder(self):
prj.clear()
self.assertEqual(prj.layerTreeRoot().customLayerOrder(), [])

def testCustomLayerOrderChanged(self):
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")

layer_tree = QgsLayerTree()
layer_order_changed_spy = QSignalSpy(layer_tree.customLayerOrderChanged)
layer1_node = QgsLayerTreeLayer(layer)
layer_tree.addChildNode(layer1_node)
self.assertEqual(len(layer_order_changed_spy), 1)
layer2_node = QgsLayerTreeLayer(layer2)
layer_tree.addChildNode(layer2_node)
self.assertEqual(len(layer_order_changed_spy), 2)

# simulate a layer move in the tree
layer3_node = QgsLayerTreeLayer(layer)
layer_tree.addChildNode(layer3_node)
self.assertEqual(len(layer_order_changed_spy), 3)
layer_tree.removeChildNode(layer1_node)
self.assertEqual(len(layer_order_changed_spy), 4)


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

0 comments on commit 546b041

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