Skip to content

Commit

Permalink
Removing Layer groups removed when no layers left in them
Browse files Browse the repository at this point in the history
  • Loading branch information
SeqLaz committed Nov 15, 2024
1 parent 1b923ba commit eb0ebd8
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions libqfieldsync/offline_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
QgsRasterLayer,
QgsValueRelationFieldFormatter,
QgsVectorLayer,
QgsLayerTreeGroup,
)
from qgis.PyQt.QtCore import QCoreApplication, QObject, pyqtSignal

Expand Down Expand Up @@ -279,6 +280,8 @@ def _convert(self, project: QgsProject) -> None:
elif layer_action == SyncAction.REMOVE:
project.removeMapLayer(layer)

self.remove_empty_groups_from_layer_tree_group(project.layerTreeRoot())

export_project_filename = self._export_filename

# save the original project path
Expand Down Expand Up @@ -346,6 +349,22 @@ def _convert(self, project: QgsProject) -> None:
QgsProject.instance().write(str(export_project_filename))
project.writeProject.disconnect(on_original_project_write)

def remove_empty_groups_from_layer_tree_group(
self, group: QgsLayerTreeGroup
) -> None:
"""
Recursively removes any empty groups from the given layer tree group.
"""
for child in group.children():
if not isinstance(child, QgsLayerTreeGroup):
continue

# remove recursively
self.remove_empty_groups_from_layer_tree_group(child)

if not child.children():
group.removeChildNode(child)

def post_process_offline_layers(self):
project = QgsProject.instance()
project.setEvaluateDefaultValues(False)
Expand Down

0 comments on commit eb0ebd8

Please sign in to comment.