Example #1
0
    def test_layer_is_contained_in_group_layer(self):
        p = QgsProject()
        layer = QgsVectorLayer("Point?field=fldtxt:string", "layer1", "memory")
        p.addMapLayer(layer)
        layer2 = QgsVectorLayer("Point?field=fldtxt:string", "layer2",
                                "memory")
        p.addMapLayer(layer2)
        layer3 = QgsVectorLayer("Point?field=fldtxt:string", "layer3",
                                "memory")
        p.addMapLayer(layer3)
        layer4 = QgsVectorLayer("Point?field=fldtxt:string", "layer4",
                                "memory")
        p.addMapLayer(layer4)

        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('group', options)
        group_layer.setChildLayers([layer, layer4])
        p.addMapLayer(group_layer)

        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer2 = QgsGroupLayer('group2', options)
        group_layer2.setChildLayers([group_layer, layer3])
        p.addMapLayer(group_layer2)

        self.assertTrue(QgsProjectUtils.layerIsContainedInGroupLayer(p, layer))
        self.assertFalse(
            QgsProjectUtils.layerIsContainedInGroupLayer(p, layer2))
        self.assertTrue(QgsProjectUtils.layerIsContainedInGroupLayer(
            p, layer3))
        self.assertTrue(QgsProjectUtils.layerIsContainedInGroupLayer(
            p, layer4))
Example #2
0
    def test_render_group_opacity(self):
        """
        Test rendering layers as a group with opacity
        """
        vl1 = QgsVectorLayer(TEST_DATA_DIR + '/lines.shp')
        self.assertTrue(vl1.isValid())
        vl2 = QgsVectorLayer(TEST_DATA_DIR + '/points.shp')
        self.assertTrue(vl2.isValid())
        vl3 = QgsVectorLayer(TEST_DATA_DIR + '/polys.shp')
        self.assertTrue(vl3.isValid())

        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('group', options)
        group_layer.setChildLayers([vl1, vl2, vl3])
        # render group with 50% opacity
        group_layer.setOpacity(0.5)

        mapsettings = QgsMapSettings()
        mapsettings.setOutputSize(QSize(600, 400))
        mapsettings.setOutputDpi(96)
        mapsettings.setDestinationCrs(group_layer.crs())
        mapsettings.setExtent(group_layer.extent())
        mapsettings.setLayers([group_layer])

        renderchecker = QgsMultiRenderChecker()
        renderchecker.setMapSettings(mapsettings)
        renderchecker.setControlPathPrefix('group_layer')
        renderchecker.setControlName('expected_group_opacity')
        result = renderchecker.runTest('expected_group_opacity')
        TestQgsGroupLayer.report += renderchecker.report()
        self.assertTrue(result)
Example #3
0
    def test_layer_order_with_group_layer(self):
        """
        Test retrieving layer order with group layers present
        """
        p = QgsProject()
        layer = QgsVectorLayer("Point?field=fldtxt:string", "layer1", "memory")
        p.addMapLayer(layer, False)
        layer2 = QgsVectorLayer("Point?field=fldtxt:string", "layer2",
                                "memory")
        p.addMapLayer(layer2, False)
        layer3 = QgsVectorLayer("Point?field=fldtxt:string", "layer3",
                                "memory")
        p.addMapLayer(layer3, False)
        layer4 = QgsVectorLayer("Point?field=fldtxt:string", "layer4",
                                "memory")
        p.addMapLayer(layer4, False)

        p.layerTreeRoot().addLayer(layer)
        group_node = p.layerTreeRoot().addGroup('my group')
        group_node.addLayer(layer2)
        group_node.addLayer(layer3)
        p.layerTreeRoot().addLayer(layer4)

        self.assertEqual(p.layerTreeRoot().layerOrder(),
                         [layer, layer2, layer3, layer4])

        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = group_node.convertToGroupLayer(options)
        p.addMapLayer(group_layer, False)
        self.assertEqual(p.layerTreeRoot().layerOrder(),
                         [layer, group_layer, layer4])
        self.assertEqual(group_layer.childLayers(), [layer3, layer2])
Example #4
0
    def test_render_group_blend_mode(self):
        """
        Test rendering layers as a group limits child layer blend mode scope
        """
        vl1 = QgsVectorLayer(TEST_DATA_DIR + '/lines.shp')
        self.assertTrue(vl1.isValid())
        vl2 = QgsVectorLayer(TEST_DATA_DIR + '/points.shp')
        self.assertTrue(vl2.isValid())

        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('group', options)
        group_layer.setChildLayers([vl2, vl1])
        vl1.setBlendMode(QPainter.CompositionMode_DestinationIn)

        mapsettings = QgsMapSettings()
        mapsettings.setOutputSize(QSize(600, 400))
        mapsettings.setOutputDpi(96)
        mapsettings.setDestinationCrs(group_layer.crs())
        mapsettings.setExtent(group_layer.extent())
        mapsettings.setLayers([group_layer])

        renderchecker = QgsMultiRenderChecker()
        renderchecker.setMapSettings(mapsettings)
        renderchecker.setControlPathPrefix('group_layer')
        renderchecker.setControlName('expected_group_child_blend_mode')
        result = renderchecker.runTest('expected_group_child_blend_mode')
        TestQgsGroupLayer.report += renderchecker.report()
        self.assertTrue(result)
Example #5
0
    def test_extent(self):
        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('test', options)
        self.assertTrue(group_layer.isValid())
        layer1 = QgsVectorLayer('Point?crs=epsg:3111', 'Point', 'memory')
        f = QgsFeature()
        f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(2478778, 2487236)))
        layer1.startEditing()
        layer1.addFeature(f)
        layer1.commitChanges()

        group_layer.setChildLayers([layer1])
        extent = group_layer.extent()
        self.assertAlmostEqual(extent.xMinimum(), 2478778, -2)
        self.assertAlmostEqual(extent.xMaximum(), 2478778, -2)
        self.assertAlmostEqual(extent.yMinimum(), 2487236, -2)
        self.assertAlmostEqual(extent.yMaximum(), 2487236, -2)

        layer2 = QgsVectorLayer('Point?crs=epsg:4326', 'Point', 'memory')
        f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(142.178, -35.943)))
        layer2.startEditing()
        layer2.addFeature(f)
        layer2.commitChanges()

        group_layer.setChildLayers([layer1, layer2])
        extent = group_layer.extent()
        self.assertAlmostEqual(extent.xMinimum(), 2245407, -2)
        self.assertEqual(extent.xMaximum(), 2478778)
        self.assertEqual(extent.yMinimum(), 2487236)
        self.assertAlmostEqual(extent.yMaximum(), 2613508, -2)
Example #6
0
    def test_convert_group_to_group_layer(self):
        """
        Test converting a QgsLayerTreeGroup to a QgsGroupLayer
        """
        group_node = QgsLayerTreeGroup()

        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = group_node.convertToGroupLayer(options)
        self.assertFalse(group_layer.childLayers())
        self.assertEqual(group_node.groupLayer(), group_layer)

        # if a group layer is already assigned, convertToGroupLayer should do nothing
        self.assertIsNone(group_node.convertToGroupLayer(options))

        group_node.setGroupLayer(None)
        # add some child layers to node
        layer = QgsVectorLayer("Point?field=fldtxt:string", "layer1", "memory")
        group_node.addLayer(layer)
        layer2 = QgsVectorLayer("Point?field=fldtxt:string", "layer2",
                                "memory")
        group_node.addLayer(layer2)

        group_layer = group_node.convertToGroupLayer(options)
        self.assertEqual(group_layer.childLayers(), [layer2, layer])
        self.assertEqual(group_node.groupLayer(), group_layer)
Example #7
0
    def test_clone(self):
        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('test', options)
        self.assertTrue(group_layer.isValid())
        layer1 = QgsVectorLayer('Point?crs=epsg:3111', 'Point', 'memory')
        layer2 = QgsVectorLayer('Point?crs=epsg:4326', 'Point', 'memory')
        group_layer.setChildLayers([layer1, layer2])

        group_cloned = group_layer.clone()

        self.assertEqual(group_cloned.childLayers(), [layer1, layer2])
Example #8
0
    def test_copy_layer_tree_group(self):
        # copying layer tree group should also copy group layer setting
        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('group', options)

        group_node = QgsLayerTreeGroup()
        group_node.setGroupLayer(group_layer)
        self.assertEqual(group_node.groupLayer(), group_layer)

        group_node2 = group_node.clone()
        self.assertEqual(group_node2.groupLayer(), group_layer)
Example #9
0
    def test_sort_layers_by_type(self):
        vl1 = QgsVectorLayer("Point?field=x:string", 'vector 1', "memory")
        vl2 = QgsVectorLayer("Point?field=x:string", 'vector 2', "memory")
        options = QgsAnnotationLayer.LayerOptions(
            QgsProject.instance().transformContext())
        al1 = QgsAnnotationLayer('annotations 1', options)
        al2 = QgsAnnotationLayer('annotations 2', options)
        rl1 = QgsRasterLayer(
            f'GPKG:{unitTestDataPath()}/mixed_layers.gpkg:band1', 'raster 1')
        options = QgsGroupLayer.LayerOptions(
            QgsProject.instance().transformContext())
        gp1 = QgsGroupLayer('group 1', options)

        self.assertEqual(
            QgsMapLayerUtils.sortLayersByType([vl1, rl1, gp1, vl2, al2, al1],
                                              []),
            [vl1, rl1, gp1, vl2, al2, al1])
        self.assertEqual(
            QgsMapLayerUtils.sortLayersByType([vl1, rl1, gp1, vl2, al2, al1],
                                              [QgsMapLayerType.VectorLayer]),
            [vl1, vl2, rl1, gp1, al2, al1])
        self.assertEqual(
            QgsMapLayerUtils.sortLayersByType(
                [vl1, rl1, gp1, vl2, al2, al1],
                [QgsMapLayerType.RasterLayer, QgsMapLayerType.VectorLayer]),
            [rl1, vl1, vl2, gp1, al2, al1])
        self.assertEqual(
            QgsMapLayerUtils.sortLayersByType(
                [vl1, rl1, gp1, vl2, al2, al1],
                [QgsMapLayerType.GroupLayer, QgsMapLayerType.VectorLayer]),
            [gp1, vl1, vl2, rl1, al2, al1])
        self.assertEqual(
            QgsMapLayerUtils.sortLayersByType([vl1, rl1, gp1, vl2, al2, al1], [
                QgsMapLayerType.GroupLayer, QgsMapLayerType.VectorLayer,
                QgsMapLayerType.AnnotationLayer
            ]), [gp1, vl1, vl2, al2, al1, rl1])
        self.assertEqual(
            QgsMapLayerUtils.sortLayersByType([vl1, rl1, gp1, vl2, al2, al1], [
                QgsMapLayerType.GroupLayer, QgsMapLayerType.VectorLayer,
                QgsMapLayerType.RasterLayer, QgsMapLayerType.AnnotationLayer
            ]), [gp1, vl1, vl2, rl1, al2, al1])
        self.assertEqual(
            QgsMapLayerUtils.sortLayersByType([vl1, rl1, gp1, vl2], [
                QgsMapLayerType.GroupLayer, QgsMapLayerType.VectorLayer,
                QgsMapLayerType.RasterLayer
            ]), [gp1, vl1, vl2, rl1])
        self.assertEqual(
            QgsMapLayerUtils.sortLayersByType(
                [vl1, rl1, gp1, vl2], [QgsMapLayerType.AnnotationLayer]),
            [vl1, rl1, gp1, vl2])
Example #10
0
    def test_save_restore(self):
        """
        Test saving/restoring a project with a group layer
        """

        p = QgsProject()
        layer1 = QgsVectorLayer('Point?crs=epsg:3111', 'L1', 'memory')
        layer2 = QgsVectorLayer('Point?crs=epsg:4326', 'L2', 'memory')
        layer3 = QgsVectorLayer('Point?crs=epsg:3111', 'L3', 'memory')

        p.addMapLayer(layer1)
        p.addMapLayer(layer2)
        p.addMapLayer(layer3)

        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer1 = QgsGroupLayer('group 1', options)
        group_layer1.setChildLayers([layer1, layer2, layer3])
        group_layer2 = QgsGroupLayer('group 2', options)
        group_layer2.setChildLayers([layer3, layer1])

        drop_shadow = QgsDropShadowEffect()
        group_layer2.setPaintEffect(drop_shadow)

        p.addMapLayer(group_layer1, False)
        p.addMapLayer(group_layer2, False)

        with TemporaryDirectory() as d:
            path = os.path.join(d, 'group_layers.qgs')

            p.setFileName(path)
            p.write()

            p2 = QgsProject()
            p2.read(path)

            restored_layer1 = p2.mapLayersByName('L1')[0]
            restored_layer2 = p2.mapLayersByName('L2')[0]
            restored_layer3 = p2.mapLayersByName('L3')[0]
            restored_group_1 = p2.mapLayersByName('group 1')[0]
            restored_group_2 = p2.mapLayersByName('group 2')[0]

            self.assertEqual(
                restored_group_1.childLayers(),
                [restored_layer1, restored_layer2, restored_layer3])
            self.assertEqual(restored_group_2.childLayers(),
                             [restored_layer3, restored_layer1])

            self.assertIsInstance(restored_group_2.paintEffect(),
                                  QgsDropShadowEffect)
Example #11
0
    def test_crs(self):
        # group layer should inherit first child layer crs
        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('test', options)
        self.assertTrue(group_layer.isValid())
        layer1 = QgsVectorLayer('Point?crs=epsg:3111', 'Point', 'memory')
        layer2 = QgsVectorLayer('Point?crs=epsg:4326', 'Point', 'memory')

        self.assertFalse(group_layer.crs().isValid())

        group_layer.setChildLayers([layer1, layer2])
        self.assertEqual(group_layer.crs().authid(), 'EPSG:3111')

        group_layer.setChildLayers([layer2, layer1])
        self.assertEqual(group_layer.crs().authid(), 'EPSG:4326')
Example #12
0
    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(), [])
Example #13
0
    def test_layer_tree_group_layer(self):
        """
        Test setting a group layer on a QgsLayerTreeGroup
        """
        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('group', options)

        group_node = QgsLayerTreeGroup()
        self.assertFalse(group_node.groupLayer())
        group_node.setGroupLayer(group_layer)
        self.assertEqual(group_node.groupLayer(), group_layer)

        group_layer.deleteLater()
        group_layer = None
        QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
        # should be automatically cleaned
        self.assertFalse(group_node.groupLayer())
Example #14
0
    def test_group_layer_nested(self):
        """
        Test group node with child nodes converted to group layer
        """
        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)
        group2 = group_node.addGroup('child group 1')
        layer2 = QgsVectorLayer("Point?field=fldtxt:string", "layer2",
                                "memory")
        group_node.addLayer(layer2)

        layer3 = QgsVectorLayer("Point?field=fldtxt:string", "layer3",
                                "memory")
        layer3_node = group2.addLayer(layer3)

        group3 = group2.addGroup('grand child group 1')
        group4 = group2.addGroup('grand child group 2')

        layer4 = QgsVectorLayer("Point?field=fldtxt:string", "layer4",
                                "memory")
        layer4_node = group3.addLayer(layer4)
        layer5 = QgsVectorLayer("Point?field=fldtxt:string", "layer5",
                                "memory")
        layer5_node = group4.addLayer(layer5)

        self.assertEqual(group_layer.childLayers(),
                         [layer2, layer5, layer4, layer3, layer])

        layer5_node.setItemVisibilityChecked(False)
        self.assertEqual(group_layer.childLayers(),
                         [layer2, layer4, layer3, layer])

        group2.setItemVisibilityChecked(False)
        self.assertEqual(group_layer.childLayers(), [layer2, layer])
        group2.setItemVisibilityCheckedRecursive(True)
        self.assertEqual(group_layer.childLayers(),
                         [layer2, layer5, layer4, layer3, layer])
Example #15
0
    def test_children(self):
        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('test', options)
        self.assertTrue(group_layer.isValid())

        self.assertFalse(group_layer.childLayers())

        # add some child layers
        layer1 = QgsVectorLayer('Point?crs=epsg:3111', 'Point', 'memory')
        layer2 = QgsVectorLayer('Point?crs=epsg:4326', 'Point', 'memory')

        group_layer.setChildLayers([layer1, layer2])
        self.assertEqual(group_layer.childLayers(), [layer1, layer2])

        # force deletion of a layer
        layer1.deleteLater()
        layer1 = None
        QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
        # should be automatically cleaned
        self.assertEqual(group_layer.childLayers(), [layer2])
Example #16
0
    def test_render_paint_effect(self):
        """
        Test rendering layers as a group with paint effect
        """
        vl1 = QgsVectorLayer(TEST_DATA_DIR + '/lines.shp')
        self.assertTrue(vl1.isValid())
        vl2 = QgsVectorLayer(TEST_DATA_DIR + '/points.shp')
        self.assertTrue(vl2.isValid())

        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = QgsGroupLayer('group', options)
        group_layer.setChildLayers([vl2, vl1])

        drop_shadow = QgsDropShadowEffect()
        drop_shadow.setBlurLevel(0)
        drop_shadow.setOpacity(1)
        drop_shadow.setColor(QColor(255, 0, 255))
        drop_shadow.setOffsetDistance(3)

        effect_stack = QgsEffectStack()
        effect_stack.appendEffect(drop_shadow)
        effect_stack.appendEffect(QgsDrawSourceEffect())
        group_layer.setPaintEffect(effect_stack)

        mapsettings = QgsMapSettings()
        mapsettings.setOutputSize(QSize(600, 400))
        mapsettings.setOutputDpi(96)
        mapsettings.setDestinationCrs(group_layer.crs())
        mapsettings.setExtent(group_layer.extent())
        mapsettings.setLayers([group_layer])

        renderchecker = QgsMultiRenderChecker()
        renderchecker.setMapSettings(mapsettings)
        renderchecker.setControlPathPrefix('group_layer')
        renderchecker.setControlName('expected_group_paint_effect')
        result = renderchecker.runTest('expected_group_paint_effect')
        TestQgsGroupLayer.report += renderchecker.report()
        self.assertTrue(result)
Example #17
0
    def test_restore_group_node_group_layer(self):
        """
        Test that group node's QgsGroupLayers are restored with projects
        """
        p = QgsProject()
        layer = QgsVectorLayer("Point?field=fldtxt:string", "layer1", "memory")
        p.addMapLayer(layer, False)
        layer2 = QgsVectorLayer("Point?field=fldtxt:string", "layer2",
                                "memory")
        p.addMapLayer(layer2, False)

        group_node = p.layerTreeRoot().addGroup('my group')
        group_node.addLayer(layer)
        group_node.addLayer(layer2)
        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = group_node.convertToGroupLayer(options)
        p.addMapLayer(group_layer, False)

        with TemporaryDirectory() as d:
            path = os.path.join(d, 'group_layers.qgs')

            p.setFileName(path)
            p.write()

            p2 = QgsProject()
            p2.read(path)

            restored_group_node = p2.layerTreeRoot().children()[0]
            self.assertEqual(restored_group_node.name(), 'my group')

            restored_group_layer = restored_group_node.groupLayer()
            self.assertIsNotNone(restored_group_layer)

            self.assertEqual(restored_group_layer.childLayers()[0].name(),
                             'layer2')
            self.assertEqual(restored_group_layer.childLayers()[1].name(),
                             'layer1')
Example #18
0
    def test_group_layer_updates_from_node_visibility(self):
        """
        Test that group layer child layers are synced correctly from the group node when layer visibility is changed
        """
        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)
        layer2 = QgsVectorLayer("Point?field=fldtxt:string", "layer2",
                                "memory")
        layer2_node = group_node.addLayer(layer2)
        layer3 = QgsVectorLayer("Point?field=fldtxt:string", "layer3",
                                "memory")
        group_node.addLayer(layer3)
        self.assertEqual(group_layer.childLayers(), [layer3, layer2, layer])

        layer2_node.setItemVisibilityChecked(False)
        self.assertEqual(group_layer.childLayers(), [layer3, layer])

        layer2_node.setItemVisibilityChecked(True)
        self.assertEqual(group_layer.childLayers(), [layer3, layer2, layer])
Example #19
0
    def test_nested_groups(self):
        """
        Test logic relating to nested groups with group layers
        """
        p = QgsProject()
        layer = QgsVectorLayer("Point?field=fldtxt:string", "layer1", "memory")
        p.addMapLayer(layer, False)
        layer2 = QgsVectorLayer("Point?field=fldtxt:string", "layer2",
                                "memory")
        p.addMapLayer(layer2, False)
        layer3 = QgsVectorLayer("Point?field=fldtxt:string", "layer3",
                                "memory")
        p.addMapLayer(layer3, False)
        layer4 = QgsVectorLayer("Point?field=fldtxt:string", "layer4",
                                "memory")
        p.addMapLayer(layer4, False)

        group_node = p.layerTreeRoot().addGroup('my group')
        group_node.addLayer(layer)
        group_node.addLayer(layer2)

        child_group = group_node.addGroup('child')
        layer3_node = child_group.addLayer(layer3)

        grandchild_group = child_group.addGroup('grandchild')
        layer4_node = grandchild_group.addLayer(layer4)

        self.assertEqual(p.layerTreeRoot().layerOrder(),
                         [layer, layer2, layer3, layer4])
        self.assertEqual(p.layerTreeRoot().checkedLayers(),
                         [layer, layer2, layer3, layer4])

        spy = QSignalSpy(p.layerTreeRoot().layerOrderChanged)

        options = QgsGroupLayer.LayerOptions(QgsCoordinateTransformContext())
        group_layer = group_node.convertToGroupLayer(options)
        p.addMapLayer(group_layer, False)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [layer4, layer3, layer2, layer])
        spy_count = len(spy)
        self.assertEqual(spy_count, 1)

        grandchild_group_layer = grandchild_group.convertToGroupLayer(options)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [grandchild_group_layer, layer3, layer2, layer])
        self.assertEqual(grandchild_group_layer.childLayers(), [layer4])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        layer4_node.setItemVisibilityChecked(False)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [grandchild_group_layer, layer3, layer2, layer])
        self.assertEqual(grandchild_group_layer.childLayers(), [])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        layer4_node.setItemVisibilityChecked(True)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [grandchild_group_layer, layer3, layer2, layer])
        self.assertEqual(grandchild_group_layer.childLayers(), [layer4])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        grandchild_group.setItemVisibilityChecked(False)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(), [layer3, layer2, layer])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        grandchild_group.setItemVisibilityChecked(True)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [grandchild_group_layer, layer3, layer2, layer])
        self.assertEqual(grandchild_group_layer.childLayers(), [layer4])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        child_group_layer = child_group.convertToGroupLayer(options)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [child_group_layer, layer2, layer])
        self.assertEqual(child_group_layer.childLayers(),
                         [grandchild_group_layer, layer3])
        self.assertEqual(grandchild_group_layer.childLayers(), [layer4])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        layer4_node.setItemVisibilityChecked(False)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [child_group_layer, layer2, layer])
        self.assertEqual(child_group_layer.childLayers(),
                         [grandchild_group_layer, layer3])
        self.assertEqual(grandchild_group_layer.childLayers(), [])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        layer4_node.setItemVisibilityChecked(True)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [child_group_layer, layer2, layer])
        self.assertEqual(child_group_layer.childLayers(),
                         [grandchild_group_layer, layer3])
        self.assertEqual(grandchild_group_layer.childLayers(), [layer4])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        grandchild_group.setItemVisibilityChecked(False)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [child_group_layer, layer2, layer])
        self.assertEqual(child_group_layer.childLayers(), [layer3])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        grandchild_group.setItemVisibilityChecked(True)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [child_group_layer, layer2, layer])
        self.assertEqual(child_group_layer.childLayers(),
                         [grandchild_group_layer, layer3])
        self.assertEqual(grandchild_group_layer.childLayers(), [layer4])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        layer3_node.setItemVisibilityChecked(False)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [child_group_layer, layer2, layer])
        self.assertEqual(child_group_layer.childLayers(),
                         [grandchild_group_layer])
        self.assertEqual(grandchild_group_layer.childLayers(), [layer4])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        layer3_node.setItemVisibilityChecked(True)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [child_group_layer, layer2, layer])
        self.assertEqual(child_group_layer.childLayers(),
                         [grandchild_group_layer, layer3])
        self.assertEqual(grandchild_group_layer.childLayers(), [layer4])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        grandchild_group.setGroupLayer(None)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [child_group_layer, layer2, layer])
        self.assertEqual(child_group_layer.childLayers(), [layer4, layer3])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        child_group.setGroupLayer(None)
        self.assertEqual(p.layerTreeRoot().layerOrder(), [group_layer])
        self.assertEqual(p.layerTreeRoot().checkedLayers(), [group_layer])
        self.assertEqual(group_layer.childLayers(),
                         [layer4, layer3, layer2, layer])
        self.assertGreater(len(spy), spy_count)
        spy_count = len(spy)

        group_node.setGroupLayer(None)
        self.assertEqual(p.layerTreeRoot().layerOrder(),
                         [layer, layer2, layer3, layer4])
        self.assertEqual(p.layerTreeRoot().checkedLayers(),
                         [layer, layer2, layer3, layer4])
        self.assertGreater(len(spy), spy_count)