Skip to content

Commit

Permalink
[cuegui] Add exit handler to save state (AcademySoftwareFoundation#1489)
Browse files Browse the repository at this point in the history
When the application is closed or killed, an exit_handler is triggered
to save the state on a qt settings file.
  • Loading branch information
DiegoTavares authored Aug 21, 2024
1 parent 470fb94 commit 45e2175
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
25 changes: 25 additions & 0 deletions cuegui/cuegui/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import signal

from qtpy import QtGui
from qtpy import QtCore

import cuegui
import cuegui.Layout
Expand Down Expand Up @@ -81,6 +82,14 @@ def startup(app_name, app_version, argv):
mainWindow.displayStartupNotice()
mainWindow.show()

# Allow ctrl-c to kill the application
signal.signal(signal.SIGINT, mainWindow.handleExit)
signal.signal(signal.SIGTERM, mainWindow.handleExit)

# Custom qt message handler to ignore known warnings
QtCore.qInstallMessageHandler(warning_handler)


# Open all windows that were open when the app was last closed
for name in mainWindow.windows_names[1:]:
if settings.value("%s/Open" % name, "false").lower() == 'true':
Expand Down Expand Up @@ -112,6 +121,22 @@ def __setup_sentry():
except ImportError:
logger.warning('Failed to import Sentry')


def warning_handler(msg_type, msg_log_context, msg_string):
"""
Handler qt warnings. Ignore known warning messages that happens when
multi-threaded/multiple updates happen in a short span
"""
if ('QTextCursor::setPosition:' in msg_string or
'SelectionRequest too old' in msg_string):
return

logger.warning('%s: %s, Message: %s',
str(msg_type),
str(msg_log_context),
str(msg_string))


def closingTime():
"""Window close callback."""
logger.info("Closing all threads...")
Expand Down
12 changes: 9 additions & 3 deletions cuegui/cuegui/MainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ def displayAbout(self):
msg += "Python:\n%s\n\n" % sys.version
QtWidgets.QMessageBox.about(self, "About", msg)

def handleExit(self, sig, flag):
"""Save current state and close the application"""
del sig
del flag
self.__saveSettings()
self.__windowCloseApplication()

@staticmethod
def openSuggestionPage():
"""Opens the suggestion page URL."""
Expand Down Expand Up @@ -364,9 +371,6 @@ def __windowOpened(self):
def __windowClosed(self):
"""Called from closeEvent on window close"""

# Disconnect to avoid multiple attempts to close a window
self.app.quit.connect(self.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)

Expand All @@ -386,6 +390,8 @@ def __windowCloseApplication(self):
to exit."""
self.app.closingApp = True
self.app.quit.emit()
# Give the application some time to save the state
time.sleep(4)

################################################################################

Expand Down
1 change: 1 addition & 0 deletions cuegui/cuegui/plugins/LogViewPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ def __init__(self, parent=None):

self.SIG_CONTENT_UPDATED.connect(self._update_log_content)
self.log_thread_pool = QtCore.QThreadPool()
self.log_thread_pool.waitForDone()

def _on_mouse_pressed(self, pos):
"""
Expand Down

0 comments on commit 45e2175

Please sign in to comment.