Skip to content

Commit

Permalink
Disable share action when no project is loaded (#46)
Browse files Browse the repository at this point in the history
* Disable share action when no project is loaded

Fixes felt/qgis-plugin-discussion#35

* Correctly set all export action enabled states

* Lint
  • Loading branch information
nyalldawson authored Aug 30, 2023
1 parent 80e64f0 commit 7b853ab
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions felt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

from qgis.core import (
QgsLayerTreeLayer,
QgsMapLayer
QgsMapLayer,
QgsProject
)
from qgis.gui import (
QgisInterface
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)

0 comments on commit 7b853ab

Please sign in to comment.