1212 List ,
1313 Optional ,
1414 Protocol ,
15- Union ,
15+ Tuple ,
1616)
1717
1818from qgis .core import (
@@ -887,7 +887,7 @@ def _add_group_legend(
887887 root_group = project .layerTreeRoot ()
888888
889889 qgis_group = self .existing_group (root_group , label )
890- if qgis_group :
890+ if qgis_group is not None :
891891 return qgis_group
892892
893893 new_group = root_group .addGroup (label )
@@ -896,16 +896,15 @@ def _add_group_legend(
896896 return new_group
897897
898898 @staticmethod
899- def existing_group (
900- root_group : QgsLayerTree ,
899+ def _existing_group (
900+ root_group : Optional [ QgsLayerTree ] ,
901901 label : str ,
902- index : bool = False ,
903- ) -> Optional [Union [QgsLayerTreeGroup , int ]]:
902+ ) -> Optional [Tuple [QgsLayerTreeGroup , int ]]:
904903 """Return the existing group in the legend if existing.
905904
906905 It will either return the group itself if found, or its index.
907906 """
908- if not root_group :
907+ if root_group is None :
909908 return None
910909
911910 # Iterate over all child (layers and groups)
@@ -916,19 +915,34 @@ def existing_group(
916915 i += 1
917916 continue
918917
919- qgis_group = cast_to_group (child )
920- qgis_group : QgsLayerTreeGroup
918+ qgis_group : QgsLayerTreeGroup = cast_to_group (child )
921919 count_children = len (qgis_group .children ())
922920 if count_children >= 1 or qgis_group .name () == label :
923921 # We do not want to count empty groups
924922 # Except for the one we are looking for
925923 i += 1
926924
927925 if qgis_group .name () == label :
928- return i if index else qgis_group
926+ return ( qgis_group , i )
929927
930928 return None
931929
930+ @staticmethod
931+ def existing_group (
932+ root_group : Optional [QgsLayerTree ],
933+ label : str ,
934+ ) -> Optional [QgsLayerTreeGroup ]:
935+ g = LayerTreeManager ._existing_group (root_group , label )
936+ return g [0 ] if g is not None else None
937+
938+ @staticmethod
939+ def existing_group_index (
940+ root_group : Optional [QgsLayerTree ],
941+ label : str ,
942+ ) -> Optional [QgsLayerTreeGroup ]:
943+ g = LayerTreeManager ._existing_group (root_group , label )
944+ return g [1 ] if g is not None else None
945+
932946 def _add_base_layer (
933947 self ,
934948 source : str ,
0 commit comments