Skip to content

Commit

Permalink
Accept a group parameter to be taken as root when adding layers
Browse files Browse the repository at this point in the history
  • Loading branch information
gacarrillor committed Jan 27, 2022
1 parent 2784c43 commit 31cca21
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
6 changes: 4 additions & 2 deletions QgisModelBaker/libqgsprojectgen/dataobjects/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

from qgis.core import QgsProject

from QgisModelBaker.utils.qgis_utils import get_suggested_index_for_layer
from QgisModelBaker.utils.qgis_utils import (get_suggested_index_for_layer,
get_group_non_recursive)



Expand Down Expand Up @@ -64,8 +65,9 @@ def create(self, qgis_project: QgsProject, group=None):

for item in self.items:
if isinstance(item, LegendGroup):
subgroup = group.findGroup(item.name)
subgroup = get_group_non_recursive(group, item.name)
if subgroup is None:
# If the subgroup does not exist or it's not child of root
subgroup = group.addGroup(item.name)
subgroup.setExpanded(item.expanded)
item.create(qgis_project, subgroup)
Expand Down
7 changes: 4 additions & 3 deletions QgisModelBaker/libqgsprojectgen/dataobjects/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from qgis.core import (QgsCoordinateReferenceSystem,
QgsProject,
QgsEditorWidgetSetup,
QgsFieldConstraints)
QgsFieldConstraints,
QgsLayerTreeGroup)
from qgis.PyQt.QtCore import QObject, pyqtSignal

from QgisModelBaker.libqgsprojectgen.dataobjects.layers import Layer
Expand Down Expand Up @@ -82,7 +83,7 @@ def load(self, definition):
layer.load(layer_definition)
self.layers.append(layer)

def create(self, path: str, qgis_project: QgsProject):
def create(self, path: str, qgis_project: QgsProject, group: QgsLayerTreeGroup):
qgis_project.setAutoTransaction(self.auto_transaction)
qgis_project.setEvaluateDefaultValues(self.evaluate_default_values)
qgis_layers = list()
Expand Down Expand Up @@ -186,7 +187,7 @@ def create(self, path: str, qgis_project: QgsProject):
layer.create_form(self)

if self.legend:
self.legend.create(qgis_project)
self.legend.create(qgis_project, group)

if path:
qgis_project.write(path)
Expand Down
5 changes: 3 additions & 2 deletions QgisModelBaker/qgismodelbaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def show_about_dialog(self):
def get_generator(self):
return Generator

def create_project(self, layers, relations, bags_of_enum, legend, auto_transaction=True, evaluate_default_values=True):
def create_project(self, layers, relations, bags_of_enum, legend, auto_transaction=True, evaluate_default_values=True, group=None):
"""
Expose the main functionality from Model Baker to other plugins,
namely, create a QGIS project from objects obtained from the Generator
Expand All @@ -254,6 +254,7 @@ def create_project(self, layers, relations, bags_of_enum, legend, auto_transacti
:param evaluate_default_values: should default values be evaluated on
provider side when requested and not
when committed. (from QGIS docs)
:param group: QgsLayerTreeGroup that should be taken as parent for all loaded layers and subgroups
"""
project = Project(auto_transaction, evaluate_default_values)
project.layers = layers
Expand All @@ -262,4 +263,4 @@ def create_project(self, layers, relations, bags_of_enum, legend, auto_transacti
project.legend = legend
project.post_generate()
qgis_project = QgsProject.instance()
project.create(None, qgis_project)
project.create(None, qgis_project, group)
9 changes: 9 additions & 0 deletions QgisModelBaker/utils/qgis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ def get_layer_type(layer):
return 'raster'
else:
return 'unknown'

def get_group_non_recursive(group, group_name):
groups = group.findGroups(False)
for group in groups:
if group.name() == group_name:
return group

return None

0 comments on commit 31cca21

Please sign in to comment.