Skip to content

Commit

Permalink
[cuegui] Add toggleable option to save settings on exit (#1612)
Browse files Browse the repository at this point in the history
**Link the Issue(s) this Pull Request is related to.**
#1601 

**Summarize your change.**
This adds an option (which is enabled by default) to save window
settings on exit.

This enables one to save a setup which is the same no matter if you
close the windows 1-by-1 or all at once.
  • Loading branch information
lithorus authored Dec 13, 2024
1 parent 8a02963 commit 5e00de8
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions cuegui/cuegui/MainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def __init__(self, app_name, app_version, window_name, parent = None):
self.setAnimated(False)
self.setDockNestingEnabled(True)

# Create checkable menuitem
self.saveWindowSettingsCheck = QtWidgets.QAction("Save Window Settings on Exit", self)
self.saveWindowSettingsCheck.setCheckable(True)

# Register this window
self.__windowOpened()

Expand Down Expand Up @@ -138,7 +142,9 @@ def handleExit(self, sig, flag):
"""Save current state and close the application"""
del sig
del flag
self.__saveSettings()
# Only save settings on exit if toggled
if self.saveWindowSettingsCheck.isChecked():
self.__saveSettings()
self.__windowCloseApplication()

@staticmethod
Expand Down Expand Up @@ -293,6 +299,10 @@ def __windowMenuSetup(self, menu):
changeTitle.triggered.connect(self.__windowMenuHandleChangeTitle) # pylint: disable=no-member
menu.addAction(changeTitle)

# Menu Bar: Window -> Save Window Settings on Exit
self.saveWindowSettingsCheck.triggered.connect(self.__saveSettingsToggle) # pylint: disable=no-member
menu.addAction(self.saveWindowSettingsCheck)

# Menu Bar: Window -> Save Window Settings
saveWindowSettings = QtWidgets.QAction("Save Window Settings", self)
saveWindowSettings.triggered.connect(self.__saveSettings) # pylint: disable=no-member
Expand Down Expand Up @@ -402,9 +412,6 @@ def __windowOpened(self):
def __windowClosed(self):
"""Called from closeEvent on window close"""

# Save the fact that this window is open or not when the app closed
self.settings.setValue("%s/Open" % self.name, self.app.closingApp)

# pylint: disable=bare-except
try:
self.windows.remove(self)
Expand Down Expand Up @@ -456,7 +463,9 @@ def closeEvent(self, event):
@type event: QEvent
@param event: The close event"""
del event
self.__saveSettings()
# Only save settings on exit if toggled
if self.saveWindowSettingsCheck.isChecked():
self.__saveSettings()
self.__windowClosed()

def __restoreSettings(self):
Expand All @@ -472,6 +481,15 @@ def __restoreSettings(self):
self.move(self.settings.value("%s/Position" % self.name,
QtCore.QPoint(0, 0)))

self.saveWindowSettingsCheck.setChecked(self.settings.value("SaveOnExit", "true") == "true")

def __saveSettingsToggle(self, checked):
"""Toggles saving window settings on exit"""

# Make sure that it has the same state in all windows
for window in self.windows:
window.saveWindowSettingsCheck.setChecked(checked)

def __saveSettings(self):
"""Saves the windows settings"""
logger.info('Saving: %s', self.settings.fileName())
Expand All @@ -480,6 +498,15 @@ def __saveSettings(self):

# For populating the default state: print self.saveState().toBase64()

# Save the fact that this window is open or not
for windowName in self.windows_names:
for window in self.windows:
if window.name == windowName:
self.settings.setValue("%s/Open" % windowName, True)
break
else:
self.settings.setValue("%s/Open" % windowName, False)

self.settings.setValue("Version", self.app_version)

self.settings.setValue("%s/Title" % self.name,
Expand All @@ -491,6 +518,8 @@ def __saveSettings(self):
self.settings.setValue("%s/Position" % self.name,
self.pos())

self.settings.setValue("SaveOnExit", self.saveWindowSettingsCheck.isChecked())

def __revertLayout(self):
"""Revert back to default window layout"""
result = QtWidgets.QMessageBox.question(
Expand Down

0 comments on commit 5e00de8

Please sign in to comment.