Skip to content

Commit

Permalink
Save/Restore window state to/from config
Browse files Browse the repository at this point in the history
See #30
  • Loading branch information
PierreRaybaut committed Feb 6, 2024
1 parent b9eba4d commit 6d41afd
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 345 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ for future and past milestones.
* Added `peak-to-peak` to the signal and image "Statistics" result table
* Curve fitting feature: fit results are now stored in a dictionary in the signal
metadata (instead of being stored individually in the signal metadata)
* Window state:
* The toolbars and dock widgets state (visibility, position, etc.) are now stored
in the configuration file and restored at startup (size and position were already
stored and restored)
* This implements part of [Issue #30](https://github.com/Codra-Ingenierie-Informatique/DataLab/issues/30) - Save/restore main window layout

🛠️ Bug fixes:

Expand Down
1 change: 1 addition & 0 deletions cdl/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class MainSection(conf.Section, metaclass=conf.SectionMeta):
window_maximized = conf.Option()
window_position = conf.Option()
window_size = conf.Option()
window_state = conf.Option()
base_dir = conf.WorkingDirOption()
available_memory_threshold = conf.Option()
current_tab = conf.Option()
Expand Down
22 changes: 19 additions & 3 deletions cdl/core/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,15 +616,25 @@ def __restore_pos_and_size(self) -> None:
posy = min(max(posy, 0), sgeo.height() - height)
self.move(QC.QPoint(posx, posy))

def __save_pos_and_size(self) -> None:
"""Save main window position and size to configuration"""
def __restore_state(self) -> None:
"""Restore main window state from configuration"""
state = Conf.main.window_state.get(None)
if state is not None:
self.restoreState(QC.QByteArray(state))
for widget in self.children():
if isinstance(widget, QW.QDockWidget):
self.restoreDockWidget(widget)

def __save_pos_size_and_state(self) -> None:
"""Save main window position, size and state to configuration"""
is_maximized = self.windowState() == QC.Qt.WindowMaximized
Conf.main.window_maximized.set(is_maximized)
if not is_maximized:
size = self.size()
Conf.main.window_size.set((size.width(), size.height()))
pos = self.pos()
Conf.main.window_position.set((pos.x(), pos.y()))
Conf.main.window_state.set(self.saveState().data())

def setup(self, console: bool = False) -> None:
"""Setup main window
Expand All @@ -644,6 +654,8 @@ def setup(self, console: bool = False) -> None:
self.__update_actions()
self.__add_macro_panel()
self.__configure_panels()
# Now that everything is set up, we can restore the window state:
self.__restore_state()

def __register_plugins(self) -> None:
"""Register plugins"""
Expand Down Expand Up @@ -723,6 +735,7 @@ def __setup_global_actions(self) -> None:
triggered=self.__edit_settings,
)
self.main_toolbar = self.addToolBar(_("Main Toolbar"))
self.main_toolbar.setObjectName("main_toolbar")
add_actions(
self.main_toolbar,
[
Expand Down Expand Up @@ -769,6 +782,7 @@ def __setup_global_actions(self) -> None:
def __add_signal_panel(self) -> None:
"""Setup signal toolbar, widgets and panel"""
self.signal_toolbar = self.addToolBar(_("Signal Toolbar"))
self.signal_toolbar.setObjectName("signal_toolbar")
curvewidget = DockablePlotWidget(self, PlotType.CURVE)
curveplot = curvewidget.get_plot()
curveplot.add_item(make.legend("TR"))
Expand All @@ -781,6 +795,7 @@ def __add_signal_panel(self) -> None:
def __add_image_panel(self) -> None:
"""Setup image toolbar, widgets and panel"""
self.image_toolbar = self.addToolBar(_("Image Toolbar"))
self.image_toolbar.setObjectName("image_toolbar")
imagewidget = DockablePlotWidget(self, PlotType.IMAGE)
self.imagepanel = image.ImagePanel(
self, imagewidget.plotwidget, self.image_toolbar
Expand Down Expand Up @@ -1090,6 +1105,7 @@ def set_modified(self, state: bool = True) -> None:
def __add_dockwidget(self, child, title: str) -> QW.QDockWidget:
"""Add QDockWidget and toggleViewAction"""
dockwidget, location = child.create_dockwidget(title)
dockwidget.setObjectName(title)
self.addDockWidget(location, dockwidget)
return dockwidget

Expand Down Expand Up @@ -1539,7 +1555,7 @@ def close_properly(self) -> bool:
# configurations only.
pass
self.reset_all()
self.__save_pos_and_size()
self.__save_pos_size_and_state()
self.__unregister_plugins()

# Saving current tab for next session
Expand Down
Loading

0 comments on commit 6d41afd

Please sign in to comment.