From 4f17d079701ff1c7754e6c366c7b322f68511faf Mon Sep 17 00:00:00 2001 From: mmouchous-ledger Date: Wed, 27 Nov 2024 13:35:02 +0100 Subject: [PATCH] The Viewer's mode is deselected when the button is clicked again. --- laserstudio/laserstudio.py | 22 ++++++---------------- laserstudio/widgets/viewer.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/laserstudio/laserstudio.py b/laserstudio/laserstudio.py index b1bed0a..04d6240 100644 --- a/laserstudio/laserstudio.py +++ b/laserstudio/laserstudio.py @@ -5,7 +5,7 @@ QMainWindow, QButtonGroup, ) -from typing import Optional, Any +from typing import Optional, Any, Union from .widgets.viewer import Viewer from .instruments.instruments import ( @@ -65,9 +65,7 @@ def __init__(self, config: Optional[dict]): # Create group of buttons for Viewer mode selection self.viewer_buttons_group = group = QButtonGroup(self) - group.idClicked.connect( - lambda _id: self.viewer.__setattr__("mode", Viewer.Mode(_id)) - ) + group.idClicked.connect(self.viewer.select_mode) self.viewer.mode_changed.connect(self.update_buttons_mode) # Toolbar: Main @@ -120,23 +118,15 @@ def __init__(self, config: Optional[dict]): # Create shortcuts shortcut = QShortcut(Qt.Key.Key_Escape, self) - shortcut.activated.connect( - lambda: self.viewer.__setattr__("mode", Viewer.Mode.NONE) - ) + shortcut.activated.connect(lambda: self.viewer.select_mode(Viewer.Mode.NONE)) shortcut = QShortcut(Qt.Key.Key_R, self) - shortcut.activated.connect( - lambda: self.viewer.__setattr__("mode", Viewer.Mode.ZONE) - ) + shortcut.activated.connect(lambda: self.viewer.select_mode(Viewer.Mode.ZONE)) # shortcut = QShortcut(Qt.Key_T, self) # shortcut.activated.connect(self.zone_rot_mode) shortcut = QShortcut(Qt.Key.Key_M, self) - shortcut.activated.connect( - lambda: self.viewer.__setattr__("mode", Viewer.Mode.STAGE) - ) + shortcut.activated.connect(lambda: self.viewer.select_mode(Viewer.Mode.STAGE)) shortcut = QShortcut(Qt.Key.Key_P, self) - shortcut.activated.connect( - lambda: self.viewer.__setattr__("mode", Viewer.Mode.PIN) - ) + shortcut.activated.connect(lambda: self.viewer.select_mode(Viewer.Mode.PIN)) if (stage := self.instruments.stage) is not None: shortcut = QShortcut(Qt.Key.Key_PageUp, self) shortcut.activated.connect( diff --git a/laserstudio/widgets/viewer.py b/laserstudio/widgets/viewer.py index 4c7e66e..dca0582 100644 --- a/laserstudio/widgets/viewer.py +++ b/laserstudio/widgets/viewer.py @@ -228,6 +228,19 @@ def mode(self, new_mode: Mode): logging.getLogger("laserstudio").debug(f"Viewer mode selection: {new_mode}") self.mode_changed.emit(int(new_mode)) + def select_mode(self, mode: Union[Mode, int]): + """Selects the Viewer's mode. In the case of a button click, + the mode is the button's id, in the case of a shortcut, the mode + is directly an instance of Mode. + """ + if isinstance(mode, int): + new_mode = Viewer.Mode(mode) + # If it has been requested to select the same mode, we deselect it. + if self.mode == new_mode: + new_mode = Viewer.Mode.NONE + + self.mode = Viewer.Mode.NONE + def go_next(self): """Actions to perform when Laser Studio receive a Go Next command. Retrieve the next point position from Scan Geometry