Skip to content

Commit

Permalink
Fix some casting issue with SIP QgsLayerTreeNode fix #528
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Dec 4, 2023
1 parent 0025a41 commit 6415ebe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
8 changes: 5 additions & 3 deletions lizmap/ogc_project_validity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
QgsProject,
)

from lizmap.tools import random_string, unaccent
from lizmap.tools import cast_to_group, cast_to_layer, random_string, unaccent

LOGGER = logging.getLogger('Lizmap')

Expand All @@ -42,7 +42,7 @@ def _add_all_shortnames(self, layer_tree: QgsLayerTreeNode, existing_shortnames:
for child in layer_tree.children():
# noinspection PyArgumentList
if QgsLayerTree.isLayer(child):
child: QgsLayerTreeLayer
child = cast_to_layer(child)
layer = self.project.mapLayer(child.layerId())
short_name = layer.shortName()
if not short_name or short_name in duplicated:
Expand All @@ -53,7 +53,7 @@ def _add_all_shortnames(self, layer_tree: QgsLayerTreeNode, existing_shortnames:
LOGGER.info(f"New shortname added on layer '{layer.name()}' : {new_shortname}")
self.new_shortnames_added.append(new_shortname)
else:
child: QgsLayerTreeGroup
child = cast_to_group(child)
if not child.customProperty("wmsShortName"):
new_shortname = self.short_name(child.name(), existing_shortnames)
existing_shortnames.append(new_shortname)
Expand All @@ -77,11 +77,13 @@ def _read_all_shortnames(self, group: QgsLayerTreeNode, existing_shortnames: Lis
for child in group.children():
# noinspection PyArgumentList
if QgsLayerTree.isLayer(child):
child = cast_to_layer(child)
child: QgsLayerTreeLayer
layer = self.project.mapLayer(child.layerId())
if layer.shortName():
existing_shortnames.append(layer.shortName())
else:
child = cast_to_group(child)
child: QgsLayerTreeGroup
group_shortname = child.customProperty("wmsShortName")
if group_shortname:
Expand Down
15 changes: 3 additions & 12 deletions lizmap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
QgsExpression,
QgsLayerTree,
QgsLayerTreeGroup,
QgsLayerTreeLayer,
QgsMapLayer,
QgsMapLayerModel,
QgsMapLayerProxyModel,
Expand All @@ -31,7 +30,6 @@
QgsVectorLayer,
QgsWkbTypes,
)
from qgis.PyQt import sip
from qgis.PyQt.QtCore import (
QCoreApplication,
QRegExp,
Expand Down Expand Up @@ -135,6 +133,7 @@
from lizmap.table_manager.base import TableManager
from lizmap.table_manager.dataviz import TableManagerDataviz
from lizmap.table_manager.layouts import TableManagerLayouts
from lizmap.tools import cast_to_group, cast_to_layer
from lizmap.widgets.project_tools import is_layer_wms_excluded

try:
Expand Down Expand Up @@ -2146,21 +2145,13 @@ def process_node(self, node, parent_node, json_layers):
"""
for child in node.children():
if QgsLayerTree.isGroup(child):
if not isinstance(child, QgsLayerTreeGroup):
# Sip cast issue
# https://github.com/3liz/lizmap-plugin/issues/299
# noinspection PyTypeChecker
child = sip.cast(child, QgsLayerTreeGroup)
child = cast_to_group(child)
child_id = child.name()
child_type = 'group'
# noinspection PyCallByClass,PyArgumentList
child_icon = QIcon(QgsApplication.iconPath('mActionFolder.svg'))
elif QgsLayerTree.isLayer(child):
if not isinstance(child, QgsLayerTreeLayer):
# Sip cast issue
# https://github.com/3liz/lizmap-plugin/issues/299
# noinspection PyTypeChecker
child = sip.cast(child, QgsLayerTreeLayer)
child = cast_to_layer(child)
child_id = child.layerId()
child_type = 'layer'
# noinspection PyArgumentList
Expand Down
3 changes: 2 additions & 1 deletion lizmap/project_checker_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from lizmap.definitions.lizmap_cloud import CLOUD_DOMAIN
from lizmap.qgis_plugin_tools.tools.i18n import tr
from lizmap.tools import is_vector_pg, update_uri
from lizmap.tools import cast_to_group, is_vector_pg, update_uri
from lizmap.widgets.check_project import Checks, SourceLayer

""" Some checks which can be done on a layer. """
Expand Down Expand Up @@ -208,6 +208,7 @@ def duplicated_layer_name_or_group(project: QgsProject) -> dict:
# 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
Expand Down
28 changes: 28 additions & 0 deletions lizmap/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
Qgis,
QgsApplication,
QgsDataSourceUri,
QgsLayerTreeGroup,
QgsLayerTreeLayer,
QgsLayerTreeNode,
QgsMapLayer,
QgsProviderRegistry,
QgsVectorLayer,
)
from qgis.PyQt import sip
from qgis.PyQt.QtCore import QDateTime, QDir, Qt

from lizmap.definitions.definitions import LayerProperties
Expand Down Expand Up @@ -311,3 +315,27 @@ def convert_lizmap_popup(content: str, layer: QgsVectorLayer) -> Tuple[str, List
errors.append(variable[1])

return content, errors


# SIP cast issues
# Related to
# https://github.com/3liz/lizmap-plugin/issues/299
# https://github.com/3liz/lizmap-plugin/issues/528


def cast_to_layer(node: QgsLayerTreeNode) -> QgsLayerTreeLayer:
""" Cast a legend node to a layer. """
if isinstance(node, QgsLayerTreeLayer):
return node

# noinspection PyTypeChecker
return sip.cast(node, QgsLayerTreeLayer)


def cast_to_group(node: QgsLayerTreeNode) -> QgsLayerTreeGroup:
"""Cast a legend node to a group. """
if isinstance(node, QgsLayerTreeGroup):
return node

# noinspection PyTypeChecker
return sip.cast(node, QgsLayerTreeGroup)

0 comments on commit 6415ebe

Please sign in to comment.