Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the error after the reload a default style or metadata from GeoNode #294

Merged
merged 10 commits into from
Nov 27, 2024
10 changes: 9 additions & 1 deletion src/qgis_geonode/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import typing
import uuid
import urllib.parse
from configparser import ConfigParser
from pathlib import Path

Expand Down Expand Up @@ -104,8 +105,15 @@ def prepare(self, plugin_dir):

self.plugin_metadata = _plugin_metadata["general"]

homepage = urllib.parse.urlparse(self.plugin_metadata.get("homepage"))
self.homepage_root = f"{homepage.scheme}://{homepage.hostname}/"
self.help_page = homepage.path.strip("/")

def get(self, attr):
return self.plugin_metadata.get(attr)
try:
return getattr(self, attr)
except ValueError:
return self.plugin_metadata.get(attr)


class SettingsManager(QtCore.QObject):
Expand Down
39 changes: 31 additions & 8 deletions src/qgis_geonode/gui/geonode_map_layer_config_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def connection_settings(self) -> typing.Optional[conf.ConnectionSettings]:
def __init__(self, layer, canvas, parent):
super().__init__(layer, canvas, parent)
self.setupUi(self)
self.setProperty("helpPage", conf.plugin_metadata.get("help_page"))
self.open_detail_url_pb.setIcon(
QtGui.QIcon(":/plugins/qgis_geonode/mIconGeonode.svg")
)
Expand Down Expand Up @@ -314,8 +315,8 @@ def _apply_metadata(self) -> None:
dataset = self.get_dataset()
updated_metadata = populate_metadata(self.layer.metadata(), dataset)
self.layer.setMetadata(updated_metadata)
layer_properties_dialog = self._get_layer_properties_dialog()
layer_properties_dialog.syncToLayer()
# sync layer properties with the reloaded SLD and/or Metadata from GeoNode
self.sync_layer_properties()

# FIXME: rather use the api_client to perform the metadata upload
def upload_metadata(self) -> None:
Expand Down Expand Up @@ -433,8 +434,7 @@ def _apply_sld(self) -> None:
dataset.default_style.sld, sld_load_error_msg
)
if sld_load_result:
layer_properties_dialog = self._get_layer_properties_dialog()
layer_properties_dialog.syncToLayer()
self.sync_layer_properties()
else:
self._show_message(
message=f"Could not load GeoNode style: {sld_load_error_msg}",
Expand All @@ -449,10 +449,33 @@ def _show_message(
) -> None:
utils.show_message(self.message_bar, message, level, add_loading_widget)

def _get_layer_properties_dialog(self):
# FIXME: This is a very hacky way to get the layer properties dialog
# but I've not been able to find a more elegant way to retrieve it yet
return self.parent().parent().parent().parent()
def find_parent_by_type(self, obj, target_type):
# Find the desired object by type
# from a structure: self.parent().parent()...
current_obj = obj
while current_obj is not None:
if isinstance(current_obj, target_type):
return current_obj
if hasattr(current_obj, "parent"):
current_obj = current_obj.parent()
else:
break
return None

def sync_layer_properties(self):
# get layer properties dialog
# We need to find QDialog object from a structure like:
# self.parent().parent()...
properties_dialog = self.find_parent_by_type(self, QtWidgets.QDialog)

if properties_dialog is not None:
# Sync GeoNode's SLD or / and metadata with the layer properties dialog
properties_dialog.syncToLayer()
else:
self._show_message(
"The corresponding layer properties from GeoNode cannot be loaded correctly...",
level=qgis.core.Qgis.Critical,
)

def _toggle_link_controls(self, enabled: bool) -> None:
self.links_gb.setEnabled(enabled)
Expand Down
12 changes: 12 additions & 0 deletions src/qgis_geonode/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import os.path

from qgis.core import QgsSettings
from qgis.gui import QgsGui
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIcon
Expand All @@ -36,6 +37,17 @@ def __init__(self, iface):
self.plugin_dir, "i18n", "QgisGeoNode_{}.qm".format(locale)
)

settings = QgsSettings()
help_paths = settings.value("help/helpSearchPath")
homepage_root = plugin_metadata.get("homepage_root")

if isinstance(help_paths, str):
help_paths = [homepage_root, help_paths]
elif isinstance(help_paths, list) and not homepage_root in help_paths:
help_paths = [homepage_root] + help_paths

settings.setValue("help/helpSearchPath", help_paths)

if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
Expand Down
Loading