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 14, 2024
1 parent 1b923ba commit 05d172a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 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,9 @@ def _convert(self, project: QgsProject) -> None:
elif layer_action == SyncAction.REMOVE:
project.removeMapLayer(layer)

project_layers_groups = project.layerTreeRoot()
self.remove_empty_groups_from_project(project_layers_groups)

export_project_filename = self._export_filename

# save the original project path
Expand Down Expand Up @@ -346,6 +350,18 @@ 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_project(self, group) -> None:
"""
Recursively removes any empty groups from the given layer tree group.
"""
for child in group.children():
if isinstance(child, QgsLayerTreeGroup):
self.remove_empty_groups_from_project(child)

if not child.children():
print(f"Removing empty group: {child.name()}")
group.removeChildNode(child)

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

0 comments on commit 05d172a

Please sign in to comment.