diff --git a/src/qgis_geonode/conf.py b/src/qgis_geonode/conf.py index 320488d..366148c 100644 --- a/src/qgis_geonode/conf.py +++ b/src/qgis_geonode/conf.py @@ -4,6 +4,8 @@ import json import typing import uuid +from configparser import ConfigParser +from pathlib import Path from qgis.PyQt import ( QtCore, @@ -94,6 +96,18 @@ def to_json(self): ) +class PluginMetadata: + def prepare(self, plugin_dir): + self.plugin_dir = plugin_dir + _plugin_metadata = ConfigParser() + _plugin_metadata.read(Path(self.plugin_dir) / "metadata.txt") + + self.plugin_metadata = _plugin_metadata["general"] + + def get(self, attr): + return self.plugin_metadata.get(attr) + + class SettingsManager(QtCore.QObject): """Manage saving/loading settings for the plugin in QgsSettings""" @@ -303,3 +317,4 @@ def clear_current_search_filters(self): settings_manager = SettingsManager() +plugin_metadata = PluginMetadata() diff --git a/src/qgis_geonode/gui/connection_dialog.py b/src/qgis_geonode/gui/connection_dialog.py index 10df889..1ae8034 100644 --- a/src/qgis_geonode/gui/connection_dialog.py +++ b/src/qgis_geonode/gui/connection_dialog.py @@ -3,7 +3,6 @@ import typing import uuid - import qgis.core from qgis.gui import QgsMessageBar from qgis.PyQt import ( @@ -16,11 +15,7 @@ from .. import apiclient, network, utils from ..apiclient.base import BaseGeonodeClient -from ..conf import ( - ConnectionSettings, - WfsVersion, - settings_manager, -) +from ..conf import ConnectionSettings, WfsVersion, settings_manager, plugin_metadata from ..utils import tr from packaging import version as packaging_version @@ -38,7 +33,7 @@ class ConnectionDialog(QtWidgets.QDialog, DialogUi): wfs_version_cb: QtWidgets.QComboBox detect_wfs_version_pb: QtWidgets.QPushButton network_timeout_sb: QtWidgets.QSpinBox - test_connection_pb: QtWidgets.QPushButton + connection_pb: QtWidgets.QPushButton buttonBox: QtWidgets.QDialogButtonBox options_gb: QtWidgets.QGroupBox bar: qgis.gui.QgsMessageBar @@ -57,7 +52,7 @@ def __init__(self, connection_settings: typing.Optional[ConnectionSettings] = No super().__init__() self.setupUi(self) self._widgets_to_toggle_during_connection_test = [ - self.test_connection_pb, + self.connection_pb, self.buttonBox, self.authcfg_acs, self.options_gb, @@ -92,7 +87,7 @@ def __init__(self, connection_settings: typing.Optional[ConnectionSettings] = No self.connection_id = uuid.uuid4() self.remote_geonode_version = None self.update_connection_details() - self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False) + # self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False) ok_signals = [ self.name_le.textChanged, self.url_le.textChanged, @@ -100,14 +95,32 @@ def __init__(self, connection_settings: typing.Optional[ConnectionSettings] = No for signal in ok_signals: signal.connect(self.update_ok_buttons) self.detect_wfs_version_pb.clicked.connect(self.detect_wfs_version) - self.test_connection_pb.clicked.connect(self.test_connection) + self.connection_pb.clicked.connect(self.test_connection) # disallow names that have a slash since that is not compatible with how we # are storing plugin state in QgsSettings self.name_le.setValidator( QtGui.QRegExpValidator(QtCore.QRegExp("[^\\/]+"), self.name_le) ) + + # Plugin's docs open through the help button + self.buttonBox.button(QtWidgets.QDialogButtonBox.Help).clicked.connect( + lambda: QtGui.QDesktopServices.openUrl( + QtCore.QUrl(plugin_metadata.get("homepage")) + ) + ) self.update_ok_buttons() + def validate_geonode_url(self): + + inserted_url = QtCore.QUrl(self.url_le.text().strip().rstrip("#/")) + + if inserted_url.isValid() == False: + return False + elif inserted_url.path() != "": + return False + else: + return True + def _populate_wfs_version_combobox(self): self.wfs_version_cb.clear() for name, member in WfsVersion.__members__.items(): @@ -150,6 +163,7 @@ def get_connection_settings(self) -> ConnectionSettings: def test_connection(self): for widget in self._widgets_to_toggle_during_connection_test: widget.setEnabled(False) + current_settings = self.get_connection_settings() self.discovery_task = network.NetworkRequestTask( [ @@ -159,12 +173,10 @@ def test_connection(self): ], network_task_timeout=current_settings.network_requests_timeout, authcfg=current_settings.auth_config, - description="Test GeoNode connection", + description="Connect to a GeoNode client", ) self.discovery_task.task_done.connect(self.handle_discovery_test) - utils.show_message( - self.bar, tr("Testing connection..."), add_loading_widget=True - ) + utils.show_message(self.bar, tr("Connecting..."), add_loading_widget=True) qgis.core.QgsApplication.taskManager().addTask(self.discovery_task) def handle_discovery_test(self, task_result: bool): @@ -230,7 +242,10 @@ def update_connection_details(self): self.detected_capabilities_lw.clear() self.detected_version_le.clear() if not invalid_version: + # Enable the detected_version_db and OK button self.detected_version_gb.setEnabled(True) + self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(True) + current_settings = self.get_connection_settings() client: BaseGeonodeClient = apiclient.get_geonode_client(current_settings) self.detected_version_le.setText(str(current_settings.geonode_version)) @@ -239,6 +254,7 @@ def update_connection_details(self): ) else: self.detected_version_gb.setEnabled(False) + self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False) def enable_post_test_connection_buttons(self): for widget in self._widgets_to_toggle_during_connection_test: @@ -268,9 +284,17 @@ def accept(self): super().accept() def update_ok_buttons(self): + + url_status = self.validate_geonode_url() + enabled_state = self.name_le.text() != "" and self.url_le.text() != "" - self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(enabled_state) - self.test_connection_pb.setEnabled(enabled_state) + self.connection_pb.setEnabled(enabled_state) + if url_status != True: + self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False) + self.connection_pb.setEnabled(False) + message = "Please insert only the domain of a valid GeoNode URL" + level = qgis.core.Qgis.Info + utils.show_message(self.bar, message, level) def _get_wfs_declared_versions(raw_response: QtCore.QByteArray) -> typing.List[str]: diff --git a/src/qgis_geonode/gui/geonode_data_source_widget.py b/src/qgis_geonode/gui/geonode_data_source_widget.py index 2d8bf54..db833b0 100644 --- a/src/qgis_geonode/gui/geonode_data_source_widget.py +++ b/src/qgis_geonode/gui/geonode_data_source_widget.py @@ -28,6 +28,7 @@ log, tr, ) +from ..conf import plugin_metadata WidgetUi, _ = loadUiType(Path(__file__).parents[1] / "ui/geonode_datasource_widget.ui") @@ -199,6 +200,13 @@ def __init__(self, parent, fl, widgetMode): self.title_le.returnPressed.connect(self.search_geonode) self._hide_core_geonode_provider() + # Plugin's docs open through the help button + self.buttonBox.button(QtWidgets.QDialogButtonBox.Help).clicked.connect( + lambda: QtGui.QDesktopServices.openUrl( + QtCore.QUrl(plugin_metadata.get("homepage")) + ) + ) + def _initialize_spatial_extent_box(self): # ATTENTION: the order of initialization of the self.spatial_extent_box widget # is crucial here. Only call self.spatial_extent_box.setMapCanvas() after @@ -286,6 +294,8 @@ def update_connections_combobox(self): def activate_connection_configuration(self, index: int): self.toggle_connection_management_buttons() + # Clear error messages from other connections + self.message_bar.clearWidgets() self.clear_search_results() self.current_page = 1 self.total_pages = 1 diff --git a/src/qgis_geonode/main.py b/src/qgis_geonode/main.py index af51bb8..84994a5 100644 --- a/src/qgis_geonode/main.py +++ b/src/qgis_geonode/main.py @@ -23,12 +23,14 @@ from .gui.geonode_maplayer_config_widget_factory import ( GeonodeMapLayerConfigWidgetFactory, ) +from .conf import plugin_metadata class QgisGeoNode: def __init__(self, iface): self.iface = iface self.plugin_dir = os.path.dirname(__file__) + plugin_metadata.prepare(self.plugin_dir) locale = QSettings().value("locale/userLocale")[0:2] locale_path = os.path.join( self.plugin_dir, "i18n", "QgisGeoNode_{}.qm".format(locale) diff --git a/src/qgis_geonode/ui/connection_dialog.ui b/src/qgis_geonode/ui/connection_dialog.ui index 31426db..283f2f7 100644 --- a/src/qgis_geonode/ui/connection_dialog.ui +++ b/src/qgis_geonode/ui/connection_dialog.ui @@ -138,7 +138,7 @@ - + 0 @@ -146,7 +146,7 @@ - Test Connection + Connect