From 1db6fdd4753963ee24c84795985617010ee6094b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20H=C3=A9riveaux?= Date: Tue, 23 Jul 2024 13:25:55 +0200 Subject: [PATCH] Window and toolbars settings save and restore --- laserstudio/laserstudio.py | 19 ++++++++++++++++++- .../widgets/toolbars/cameranittoolbar.py | 1 + laserstudio/widgets/toolbars/cameratoolbar.py | 1 + .../widgets/toolbars/laserdrivertoolbar.py | 2 +- laserstudio/widgets/toolbars/lasertoolbar.py | 8 +++++++- laserstudio/widgets/toolbars/maintoolbar.py | 1 + laserstudio/widgets/toolbars/pdmtoolbar.py | 3 +-- .../widgets/toolbars/picturetoolbar.py | 1 + laserstudio/widgets/toolbars/scantoolbar.py | 1 + laserstudio/widgets/toolbars/stagetoolbar.py | 1 + laserstudio/widgets/toolbars/zoomtoolbar.py | 1 + 11 files changed, 34 insertions(+), 5 deletions(-) diff --git a/laserstudio/laserstudio.py b/laserstudio/laserstudio.py index 2af9876..8e71937 100644 --- a/laserstudio/laserstudio.py +++ b/laserstudio/laserstudio.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -from PyQt6.QtCore import Qt, QKeyCombination +from PyQt6.QtCore import Qt, QKeyCombination, QSettings from PyQt6.QtGui import QColor, QShortcut, QKeySequence from PyQt6.QtWidgets import ( QMainWindow, @@ -43,6 +43,9 @@ def __init__(self, config: Optional[dict]): if config is None: config = {} + # User settings + self.settings = QSettings("ledger", "laserstudio") + # Instantiate all instruments self.instruments = Instruments(config) @@ -169,6 +172,20 @@ def __init__(self, config: Optional[dict]): ) shortcut.activated.connect(self.handle_go_next) + # Restore docks are previous session + geometry = self.settings.value("geometry") + if geometry is not None: + self.restoreGeometry(geometry) + window_state = self.settings.value("window-state") + if window_state is not None: + self.restoreState(window_state) + + def closeEvent(self, event): + """Saves user settings before closing the application.""" + self.settings.setValue("geometry", self.saveGeometry()) + self.settings.setValue("window-state", self.saveState()) + super().closeEvent(event) + def handle_go_next(self) -> dict: """Go Next operation. Triggers the instruments to perform changes to go to next step of scan. diff --git a/laserstudio/widgets/toolbars/cameranittoolbar.py b/laserstudio/widgets/toolbars/cameranittoolbar.py index daccbee..e263994 100644 --- a/laserstudio/widgets/toolbars/cameranittoolbar.py +++ b/laserstudio/widgets/toolbars/cameranittoolbar.py @@ -28,6 +28,7 @@ def __init__(self, laser_studio: "LaserStudio"): self.laser_studio = laser_studio super().__init__("NIT Camera parameters", laser_studio) + self.setObjectName("toolbar-camera-nit") # For settings save and restore self.setAllowedAreas( Qt.ToolBarArea.LeftToolBarArea | Qt.ToolBarArea.RightToolBarArea diff --git a/laserstudio/widgets/toolbars/cameratoolbar.py b/laserstudio/widgets/toolbars/cameratoolbar.py index 4a14679..abe99a5 100644 --- a/laserstudio/widgets/toolbars/cameratoolbar.py +++ b/laserstudio/widgets/toolbars/cameratoolbar.py @@ -27,6 +27,7 @@ def __init__(self, laser_studio: "LaserStudio"): assert laser_studio.instruments.camera is not None self.camera = laser_studio.instruments.camera super().__init__("Camera parameters", laser_studio) + self.setObjectName("toolbar-camera") # For settings save and restore self.setAllowedAreas( Qt.ToolBarArea.LeftToolBarArea | Qt.ToolBarArea.RightToolBarArea diff --git a/laserstudio/widgets/toolbars/laserdrivertoolbar.py b/laserstudio/widgets/toolbars/laserdrivertoolbar.py index 538d19e..26d36a2 100644 --- a/laserstudio/widgets/toolbars/laserdrivertoolbar.py +++ b/laserstudio/widgets/toolbars/laserdrivertoolbar.py @@ -17,7 +17,7 @@ def __init__(self, laser_studio: "LaserStudio", laser_num: int): assert laser_num < len(laser_studio.instruments.lasers) self.laser = laser_studio.instruments.lasers[laser_num] assert isinstance(self.laser, LaserDriverInstrument) - super().__init__(f"Laser {laser_num} (Donjon Driver)", laser_studio) + super().__init__(f"Laser {laser_num} (Donjon Driver)", f"toolbar-laser-donjon-{laser_num}") self.setAllowedAreas( Qt.ToolBarArea.LeftToolBarArea | Qt.ToolBarArea.RightToolBarArea ) diff --git a/laserstudio/widgets/toolbars/lasertoolbar.py b/laserstudio/widgets/toolbars/lasertoolbar.py index 4439862..f1851aa 100644 --- a/laserstudio/widgets/toolbars/lasertoolbar.py +++ b/laserstudio/widgets/toolbars/lasertoolbar.py @@ -2,4 +2,10 @@ class LaserToolbar(QToolBar): - pass + def __init__(self, title: str, object_name: str): + """ + :param title: Toolbar title, as displayed in the context menus. + :param object_name: Unique object name for Qt, used for settings save and restore. + """ + super().__init__(title) + super().setObjectName(object_name) # For settings save and restore diff --git a/laserstudio/widgets/toolbars/maintoolbar.py b/laserstudio/widgets/toolbars/maintoolbar.py index c8e1779..90ea0bb 100644 --- a/laserstudio/widgets/toolbars/maintoolbar.py +++ b/laserstudio/widgets/toolbars/maintoolbar.py @@ -12,6 +12,7 @@ class MainToolbar(QToolBar): def __init__(self, laser_studio: "LaserStudio"): super().__init__("Main", laser_studio) + self.setObjectName("toolbar-main") # For settings save and restore group = laser_studio.viewer_buttons_group self.setAllowedAreas( Qt.ToolBarArea.LeftToolBarArea | Qt.ToolBarArea.RightToolBarArea diff --git a/laserstudio/widgets/toolbars/pdmtoolbar.py b/laserstudio/widgets/toolbars/pdmtoolbar.py index 6abe590..031a482 100644 --- a/laserstudio/widgets/toolbars/pdmtoolbar.py +++ b/laserstudio/widgets/toolbars/pdmtoolbar.py @@ -24,12 +24,11 @@ def __init__(self, laser_studio: "LaserStudio", laser_num: int): assert laser_num < len(laser_studio.instruments.lasers) self.laser = laser_studio.instruments.lasers[laser_num] assert isinstance(self.laser, PDMInstrument) - super().__init__(f"Laser {laser_num} (PDM)", laser_studio) + super().__init__(f"Laser {laser_num} (PDM)", f"toolbar-laser-pdm-{laser_num}") self.setAllowedAreas( Qt.ToolBarArea.LeftToolBarArea | Qt.ToolBarArea.RightToolBarArea ) - self.setFloatable(True) w = QPushButton(self) w.setToolTip("On/Off Laser") diff --git a/laserstudio/widgets/toolbars/picturetoolbar.py b/laserstudio/widgets/toolbars/picturetoolbar.py index 064dbfb..f2e5351 100644 --- a/laserstudio/widgets/toolbars/picturetoolbar.py +++ b/laserstudio/widgets/toolbars/picturetoolbar.py @@ -15,6 +15,7 @@ class PictureToolbar(QToolBar): def __init__(self, laser_studio: "LaserStudio"): super().__init__("Background picture", laser_studio) + self.setObjectName("toolbar-background-picture") # For settings save and restore group = laser_studio.viewer_buttons_group self.setAllowedAreas(Qt.ToolBarArea.TopToolBarArea) self.setFloatable(True) diff --git a/laserstudio/widgets/toolbars/scantoolbar.py b/laserstudio/widgets/toolbars/scantoolbar.py index 4588c92..6d9c927 100644 --- a/laserstudio/widgets/toolbars/scantoolbar.py +++ b/laserstudio/widgets/toolbars/scantoolbar.py @@ -16,6 +16,7 @@ class ScanToolbar(QToolBar): def __init__(self, laser_studio: "LaserStudio"): super().__init__("Scanning Zones", laser_studio) + self.setObjectName("toolbar-scanning") # For settings save and restore group = laser_studio.viewer_buttons_group self.setAllowedAreas(Qt.ToolBarArea.TopToolBarArea) self.setFloatable(True) diff --git a/laserstudio/widgets/toolbars/stagetoolbar.py b/laserstudio/widgets/toolbars/stagetoolbar.py index 5c0252a..1b73c08 100644 --- a/laserstudio/widgets/toolbars/stagetoolbar.py +++ b/laserstudio/widgets/toolbars/stagetoolbar.py @@ -26,6 +26,7 @@ def __init__(self, laser_studio: "LaserStudio"): assert laser_studio.instruments.stage is not None self.stage = laser_studio.instruments.stage super().__init__("Stage control", laser_studio) + self.setObjectName("toolbar-stage") # For settings save and restore group = laser_studio.viewer_buttons_group self.setAllowedAreas( Qt.ToolBarArea.LeftToolBarArea diff --git a/laserstudio/widgets/toolbars/zoomtoolbar.py b/laserstudio/widgets/toolbars/zoomtoolbar.py index ea12d32..baa4116 100644 --- a/laserstudio/widgets/toolbars/zoomtoolbar.py +++ b/laserstudio/widgets/toolbars/zoomtoolbar.py @@ -13,6 +13,7 @@ class ZoomToolbar(QToolBar): def __init__(self, laser_studio: "LaserStudio"): super().__init__("Zoom control", laser_studio) + self.setObjectName("toolbar-zoom") # For settings save and restore self.setAllowedAreas(Qt.ToolBarArea.TopToolBarArea) self.setFloatable(True)