From 7b853ab5f18996529885359fe5ba68b663c6a294 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 31 Aug 2023 08:50:00 +1000 Subject: [PATCH] Disable share action when no project is loaded (#46) * Disable share action when no project is loaded Fixes https://github.com/felt/qgis-plugin-discussion/issues/35 * Correctly set all export action enabled states * Lint --- felt/plugin.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/felt/plugin.py b/felt/plugin.py index 26ff11e..38fd177 100644 --- a/felt/plugin.py +++ b/felt/plugin.py @@ -31,7 +31,8 @@ from qgis.core import ( QgsLayerTreeLayer, - QgsMapLayer + QgsMapLayer, + QgsProject ) from qgis.gui import ( QgisInterface @@ -127,6 +128,12 @@ def initGui(self): AUTHORIZATION_MANAGER.status_changed.connect(self._auth_state_changed) + QgsProject.instance().layersRemoved.connect( + self._update_action_enabled_states) + QgsProject.instance().layersAdded.connect( + self._update_action_enabled_states) + self._update_action_enabled_states() + def unload(self): if self.felt_web_menu and not sip.isdeleted(self.felt_web_menu): self.felt_web_menu.deleteLater() @@ -166,16 +173,13 @@ def tr(message): # noinspection PyTypeChecker,PyArgumentList,PyCallByClass return QCoreApplication.translate('Felt', message) + # pylint:disable=unused-argument def _auth_state_changed(self, state: AuthState): """ Called when the plugin authorization state changes """ - if state == AuthState.Authorizing: - self.share_map_to_felt_action.setEnabled(False) - self.create_map_action.setEnabled(False) - else: - self.share_map_to_felt_action.setEnabled(True) - self.create_map_action.setEnabled(True) + self._update_action_enabled_states() + # pylint:enable=unused-argument def _share_layer_to_felt(self, layer: QgsMapLayer): """ @@ -257,3 +261,13 @@ def _layer_tree_view_context_menu_about_to_show(self, menu: QMenu): share_to_felt_action.triggered.connect( partial(self._share_layer_to_felt, layer) ) + + def _update_action_enabled_states(self): + """ + Updates the enabled state of export actions + """ + has_layers = bool(QgsProject.instance().mapLayers()) + is_authorizing = AUTHORIZATION_MANAGER.status == AuthState.Authorizing + allowed_to_export = has_layers and not is_authorizing + self.share_map_to_felt_action.setEnabled(allowed_to_export) + self.create_map_action.setEnabled(allowed_to_export)