Skip to content

Commit

Permalink
Use a recursive function to browse all group names when looking for d…
Browse files Browse the repository at this point in the history
…uplicated items
  • Loading branch information
Gustry committed Dec 9, 2024
1 parent 3bdf97c commit c7ba30e
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lizmap/project_checker_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,24 @@ def invalid_type_primary_key(layer: QgsVectorLayer, check_field: str) -> bool:
return field.typeName().lower() == check_field


def _duplicated_layer_name_or_group(layer_tree: QgsLayerTreeNode, result: Dict) -> Dict[str, int]:
""" Recursive function to check all group names. """
for child in layer_tree.children():
if QgsLayerTree.isGroup(child):
child = cast_to_group(child)
name = child.name()
if name not in result.keys():
result[name] = 1
else:
result[name] += 1
result = _duplicated_layer_name_or_group(child, result)
return result


def duplicated_layer_name_or_group(project: QgsProject) -> dict:
""" The CFG can only store layer/group names which are unique. """
result = {}
# Vector and raster layers
# For all layer within the project
for layer in project.mapLayers().values():
layer: QgsMapLayer
name = layer.name()
Expand All @@ -252,16 +266,8 @@ def duplicated_layer_name_or_group(project: QgsProject) -> dict:
else:
result[name] += 1

# Groups
for child in project.layerTreeRoot().children():
if QgsLayerTree.isGroup(child):
child = cast_to_group(child)
name = child.name()
if name not in result.keys():
result[name] = 1
else:
result[name] += 1

# For all groups with a recursive function
result = _duplicated_layer_name_or_group(project.layerTreeRoot(), result)
return result


Expand Down

0 comments on commit c7ba30e

Please sign in to comment.