From 9a3397909750c78afdc2ed9b81dc76b91c337d3f Mon Sep 17 00:00:00 2001 From: Niko Aarnio Date: Fri, 21 Feb 2025 12:16:39 +0200 Subject: [PATCH] change toolbar dock icon behaviour --- arho_feature_template/core/plan_manager.py | 4 +- .../gui/docks/new_feature_dock.py | 4 +- .../gui/docks/regulation_groups_dock.py | 4 +- arho_feature_template/plugin.py | 59 ++++++------------- 4 files changed, 25 insertions(+), 46 deletions(-) diff --git a/arho_feature_template/core/plan_manager.py b/arho_feature_template/core/plan_manager.py index 10f8088..ee1bac9 100644 --- a/arho_feature_template/core/plan_manager.py +++ b/arho_feature_template/core/plan_manager.py @@ -95,12 +95,12 @@ def __init__(self): self.regulation_group_libraries = [] # Initialize new feature dock - self.new_feature_dock = NewFeatureDock() + self.new_feature_dock = NewFeatureDock(iface.mainWindow()) self.new_feature_dock.tool_activated.connect(self.add_new_plan_feature) self.new_feature_dock.hide() # Initialize regulation groups dock - self.regulation_groups_dock = RegulationGroupsDock() + self.regulation_groups_dock = RegulationGroupsDock(iface.mainWindow()) self.regulation_groups_dock.new_regulation_group_requested.connect(self.create_new_regulation_group) self.regulation_groups_dock.edit_regulation_group_requested.connect(self.edit_regulation_group) self.regulation_groups_dock.delete_regulation_group_requested.connect(self.delete_regulation_group) diff --git a/arho_feature_template/gui/docks/new_feature_dock.py b/arho_feature_template/gui/docks/new_feature_dock.py index 8fdc21d..c5b1cda 100644 --- a/arho_feature_template/gui/docks/new_feature_dock.py +++ b/arho_feature_template/gui/docks/new_feature_dock.py @@ -29,8 +29,8 @@ class NewFeatureDock(QgsDockWidget, DockClass): # type: ignore tool_activated = pyqtSignal() - def __init__(self) -> None: - super().__init__() + def __init__(self, parent=None) -> None: + super().__init__(parent) self.setupUi(self) # INIT diff --git a/arho_feature_template/gui/docks/regulation_groups_dock.py b/arho_feature_template/gui/docks/regulation_groups_dock.py index 5d57350..fe464a6 100644 --- a/arho_feature_template/gui/docks/regulation_groups_dock.py +++ b/arho_feature_template/gui/docks/regulation_groups_dock.py @@ -34,8 +34,8 @@ class RegulationGroupsDock(QgsDockWidget, DockClass): # type: ignore edit_regulation_group_requested = pyqtSignal(RegulationGroup) delete_regulation_group_requested = pyqtSignal(RegulationGroup) - def __init__(self): - super().__init__() + def __init__(self, parent=None): + super().__init__(parent) self.setupUi(self) self.new_btn.setIcon(QgsApplication.getThemeIcon("mActionAdd.svg")) diff --git a/arho_feature_template/plugin.py b/arho_feature_template/plugin.py index 5522a68..b7ab8a7 100644 --- a/arho_feature_template/plugin.py +++ b/arho_feature_template/plugin.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Callable +from typing import TYPE_CHECKING, Callable from qgis.core import QgsApplication from qgis.PyQt.QtCore import QCoreApplication, Qt, QTranslator @@ -15,6 +15,9 @@ from arho_feature_template.qgis_plugin_tools.tools.resources import plugin_name, resources_path from arho_feature_template.utils.misc_utils import disconnect_signal, iface +if TYPE_CHECKING: + from qgis.gui import QgsDockWidget + class Plugin: """QGIS Plugin Implementation.""" @@ -120,26 +123,15 @@ def add_action( return action def initGui(self) -> None: # noqa N802 + # Plan manager self.plan_manager = PlanManager() + iface.mapCanvas().mapToolSet.connect(self.plan_manager.plan_digitize_map_tool.deactivate) - iface.addDockWidget(Qt.RightDockWidgetArea, self.plan_manager.new_feature_dock) - self.plan_manager.new_feature_dock.setUserVisible(False) - - self.plan_manager.new_feature_dock.visibilityChanged.connect(self.new_feature_dock_visibility_changed) - + # Docks self.validation_dock = ValidationDock() iface.addDockWidget(Qt.RightDockWidgetArea, self.validation_dock) - self.validation_dock.setUserVisible(False) - self.validation_dock.visibilityChanged.connect(self.validation_dock_visibility_changed) - - iface.mapCanvas().mapToolSet.connect(self.plan_manager.plan_digitize_map_tool.deactivate) - - # Regulation groups dock + iface.addDockWidget(Qt.RightDockWidgetArea, self.plan_manager.new_feature_dock) iface.addDockWidget(Qt.RightDockWidgetArea, self.plan_manager.regulation_groups_dock) - self.plan_manager.regulation_groups_dock.setUserVisible(False) - self.plan_manager.regulation_groups_dock.visibilityChanged.connect( - self.regulation_groups_dock_visibility_changed - ) # Try initializing the plugin immediately in case the project is already open self.plan_manager.initialize_from_project() @@ -147,6 +139,7 @@ def initGui(self) -> None: # noqa N802 # (Re)initialize whenever a project is opened iface.projectRead.connect(self.plan_manager.initialize_from_project) + # Actions self.new_land_use_plan_action = self.add_action( text="Luo kaava", icon=QIcon(resources_path("icons", "toolbar", "luo_kaava2.svg")), @@ -183,8 +176,7 @@ def initGui(self) -> None: # noqa N802 text="Luo kaavakohde", # icon=QgsApplication.getThemeIcon("mIconFieldGeometry.svg"), icon=QIcon(resources_path("icons", "toolbar", "luo_kaavakohde.svg")), - toggled_callback=self.toggle_new_feature_dock, - checkable=True, + triggered_callback=lambda _: self.toggle_dock_visibility(self.plan_manager.new_feature_dock), add_to_menu=True, add_to_toolbar=True, ) @@ -201,8 +193,7 @@ def initGui(self) -> None: # noqa N802 self.regulation_groups_dock_action = self.add_action( text="Hallitse kaavamääräysryhmiä", icon=QgsApplication.getThemeIcon("mActionOpenTable.svg"), - toggled_callback=self.toggle_regulation_groups_dock, - checkable=True, + triggered_callback=lambda _: self.toggle_dock_visibility(self.plan_manager.regulation_groups_dock), add_to_menu=True, add_to_toolbar=True, ) @@ -211,8 +202,7 @@ def initGui(self) -> None: # noqa N802 text="Validointi", # icon=QgsApplication.getThemeIcon("mActionEditNodesItem.svg"), icon=QIcon(resources_path("icons", "toolbar", "kaavan_validointi2.svg")), - toggled_callback=self.toggle_validation_dock, - checkable=True, + triggered_callback=lambda _: self.toggle_dock_visibility(self.validation_dock), add_to_menu=True, add_to_toolbar=True, ) @@ -249,6 +239,13 @@ def initGui(self) -> None: # noqa N802 lambda: self.identify_plan_features_action.setChecked(False) ) + def toggle_dock_visibility(self, dock_widget: QgsDockWidget): + if dock_widget.isUserVisible(): + dock_widget.hide() + else: + dock_widget.show() + dock_widget.raise_() + def add_new_plan(self): self.plan_manager.add_new_plan() @@ -295,21 +292,3 @@ def unload(self) -> None: # Handle logger teardown_logger(Plugin.name) - - def new_feature_dock_visibility_changed(self, visible: bool) -> None: # noqa: FBT001 - self.new_feature_dock_action.setChecked(visible) - - def toggle_new_feature_dock(self, show: bool) -> None: # noqa: FBT001 - self.plan_manager.new_feature_dock.setUserVisible(show) - - def regulation_groups_dock_visibility_changed(self, visible: bool) -> None: # noqa: FBT001 - self.regulation_groups_dock_action.setChecked(visible) - - def toggle_regulation_groups_dock(self, show: bool) -> None: # noqa: FBT001 - self.plan_manager.regulation_groups_dock.setUserVisible(show) - - def validation_dock_visibility_changed(self, visible: bool) -> None: # noqa: FBT001 - self.validation_dock_action.setChecked(visible) - - def toggle_validation_dock(self, show: bool) -> None: # noqa: FBT001 - self.validation_dock.setUserVisible(show)