def test_group_layer_updates_from_node(self): """ Test that group layer child layers are synced correctly from the group node """ group_node = QgsLayerTreeGroup('my group') options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext()) group_layer = group_node.convertToGroupLayer(options) self.assertFalse(group_layer.childLayers()) layer = QgsVectorLayer("Point?field=fldtxt:string", "layer1", "memory") group_node.addLayer(layer) self.assertEqual(group_layer.childLayers(), [layer]) layer2 = QgsVectorLayer("Point?field=fldtxt:string", "layer2", "memory") group_node.insertLayer(0, layer2) self.assertEqual(group_layer.childLayers(), [layer, layer2]) layer3 = QgsVectorLayer("Point?field=fldtxt:string", "layer3", "memory") layer4 = QgsVectorLayer("Point?field=fldtxt:string", "layer4", "memory") layer3_node = QgsLayerTreeLayer(layer3) layer4_node = QgsLayerTreeLayer(layer4) group_node.insertChildNodes(1, [layer3_node, layer4_node]) self.assertEqual(group_layer.childLayers(), [layer, layer4, layer3, layer2]) layer5 = QgsVectorLayer("Point?field=fldtxt:string", "layer5", "memory") layer5_node = QgsLayerTreeLayer(layer5) group_node.addChildNode(layer5_node) self.assertEqual(group_layer.childLayers(), [layer5, layer, layer4, layer3, layer2]) group_node.removeChildNode(layer3_node) self.assertEqual(group_layer.childLayers(), [layer5, layer, layer4, layer2]) group_node.removeLayer(layer) self.assertEqual(group_layer.childLayers(), [layer5, layer4, layer2]) group_node.removeChildren(0, 2) self.assertEqual(group_layer.childLayers(), [layer5]) group_node.removeAllChildren() self.assertEqual(group_layer.childLayers(), [])
def groupSelected(self): nodes = self.mView.selectedNodes(True) if len(nodes) < 2 or not QgsLayerTree.isGroup(nodes[0].parent()): return parentGroup = nodes[0].parent() insertIdx = parentGroup.children().index(nodes[0]) newGroup = QgsLayerTreeGroup(self.uniqueGroupName(parentGroup)) for node in nodes: newGroup.addChildNode(node.clone()) parentGroup.insertChildNode(insertIdx, newGroup) for node in nodes: group = node.parent() if group != None: group.removeChildNode(node) self.mView.setCurrentIndex( self.mView.layerTreeModel().node2index(newGroup))