Skip to content

Commit

Permalink
Merge pull request #3 from opengisch/layertreenode_fallback
Browse files Browse the repository at this point in the history
Get layer over `mapLayersByName` when node is no `QgsLayerTreeLayer`
  • Loading branch information
signedav authored Sep 8, 2022
2 parents 7f60af2 + b93e7df commit 6bc659c
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions toppingmaker/projecttopping.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
QgsLayerDefinition,
QgsLayerTreeGroup,
QgsLayerTreeLayer,
QgsLayerTreeNode,
QgsMapLayer,
QgsProject,
)
Expand Down Expand Up @@ -94,6 +95,7 @@ def __init__(self):

def make_item(
self,
project: QgsProject,
node: Union[QgsLayerTreeLayer, QgsLayerTreeGroup],
export_settings: ExportSettings,
):
Expand All @@ -102,65 +104,72 @@ def make_item(
self.properties.checked = node.itemVisibilityChecked()
self.properties.expanded = node.isExpanded()

if isinstance(node, QgsLayerTreeLayer):
if isinstance(node, QgsLayerTreeGroup):
# it's a group
self.properties.group = True
self.properties.mutually_exclusive = node.isMutuallyExclusive()

index = 0
for child in node.children():
item = ProjectTopping.LayerTreeItem()
item.make_item(project, child, export_settings)
# set the first checked item as mutually exclusive child
if (
self.properties.mutually_exclusive
and self.properties.mutually_exclusive_child == -1
):
if item.properties.checked:
self.properties.mutually_exclusive_child = index
self.items.append(item)
index += 1
else:
if isinstance(node, QgsLayerTreeLayer):
layer = node.layer()
else:
# must be not recognized as QgsLayerTreeLayer (but QgsLayerTreeNode instead)
layer = self._layer_of_node(project, node)
self.properties.featurecount = node.customProperty("showFeatureCount")
source_setting = export_settings.get_setting(
ExportSettings.ToppingType.SOURCE, node, node.name()
)
if source_setting.get("export", False):
if node.layer().dataProvider():
if node.layer().dataProvider():
self.properties.provider = (
node.layer().dataProvider().name()
)
if layer.dataProvider():
if layer.dataProvider():
self.properties.provider = layer.dataProvider().name()
self.properties.uri = (
QgsProject.instance()
.pathResolver()
.writePath(node.layer().publicSource())
.writePath(layer.publicSource())
)
qml_setting = export_settings.get_setting(
ExportSettings.ToppingType.QMLSTYLE, node, node.name()
)
if qml_setting.get("export", False):
self.properties.qmlstylefile = self._temporary_qmlstylefile(
node,
layer,
QgsMapLayer.StyleCategory(
qml_setting.get(
"categories",
QgsMapLayer.StyleCategory.AllStyleCategories,
)
),
)
elif isinstance(node, QgsLayerTreeGroup):
# it's a group
self.properties.group = True
self.properties.mutually_exclusive = node.isMutuallyExclusive()

index = 0
for child in node.children():
item = ProjectTopping.LayerTreeItem()
item.make_item(child, export_settings)
# set the first checked item as mutually exclusive child
if (
self.properties.mutually_exclusive
and self.properties.mutually_exclusive_child == -1
):
if item.properties.checked:
self.properties.mutually_exclusive_child = index
self.items.append(item)
index += 1
else:
print(
f"Here with {node.name()} we have the problem with the LayerTreeNode (it recognizes on QgsLayerTreeLayer QgsLayerTreeNode instead. Similar to https://github.com/opengisch/QgisModelBaker/pull/514 - this needs a fix - maybe in QGIS"
)
return

definition_setting = export_settings.get_setting(
ExportSettings.ToppingType.DEFINITION, node, node.name()
)
if definition_setting.get("export", False):
self.properties.definitionfile = self._temporary_definitionfile(node)

def _layer_of_node(
self,
project: QgsProject,
node: QgsLayerTreeNode,
) -> QgsLayerTreeLayer:
# workaround when layer has not been detected as QgsLayerTreeLayer.
# See https://github.com/opengisch/QgisModelBaker/pull/514
return project.mapLayersByName(node.name())[0]

def _temporary_definitionfile(
self, node: Union[QgsLayerTreeLayer, QgsLayerTreeGroup]
):
Expand All @@ -174,15 +183,15 @@ def _temporary_definitionfile(

def _temporary_qmlstylefile(
self,
node: QgsLayerTreeLayer,
layer: QgsMapLayer,
categories: QgsMapLayer.StyleCategories = QgsMapLayer.StyleCategory.AllStyleCategories,
):
filename_slug = f"{slugify(self.name)}.qml"
os.makedirs(self.temporary_toppingfile_dir, exist_ok=True)
temporary_toppingfile_path = os.path.join(
self.temporary_toppingfile_dir, filename_slug
)
node.layer().saveNamedStyle(temporary_toppingfile_path, categories)
layer.saveNamedStyle(temporary_toppingfile_path, categories)
return temporary_toppingfile_path

def __init__(self):
Expand All @@ -201,7 +210,7 @@ def parse_project(
"""
root = project.layerTreeRoot()
if root:
self.layertree.make_item(project.layerTreeRoot(), export_settings)
self.layertree.make_item(project, project.layerTreeRoot(), export_settings)
self.layerorder = (
root.customLayerOrder() if root.hasCustomLayerOrder() else []
)
Expand Down

0 comments on commit 6bc659c

Please sign in to comment.