mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-05-16 02:42:28 +12:00
Merge pull request #835 from lingsamuel/sigint-handler
setup signal handler to avoid accident data loss
This commit is contained in:
commit
e52818043d
|
@ -4,6 +4,7 @@ import faulthandler
|
|||
import os
|
||||
import platform
|
||||
import sys
|
||||
import signal
|
||||
|
||||
import manuskript.ui.views.webView
|
||||
from PyQt5.QtCore import QLocale, QTranslator, QSettings, Qt
|
||||
|
@ -15,6 +16,7 @@ from manuskript.version import getVersion
|
|||
|
||||
faulthandler.enable()
|
||||
|
||||
|
||||
def prepare(tests=False):
|
||||
app = QApplication(sys.argv)
|
||||
app.setOrganizationName("manuskript" + ("_tests" if tests else ""))
|
||||
|
@ -38,6 +40,7 @@ def prepare(tests=False):
|
|||
|
||||
# Translation process
|
||||
appTranslator = QTranslator(app)
|
||||
|
||||
# By default: locale
|
||||
|
||||
def tryLoadTranslation(translation, source):
|
||||
|
@ -106,7 +109,9 @@ def prepare(tests=False):
|
|||
|
||||
# Basic Windows 10 Dark Theme support.
|
||||
# Source: https://forum.qt.io/topic/101391/windows-10-dark-theme/4
|
||||
themeSettings = QSettings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", QSettings.NativeFormat)
|
||||
themeSettings = QSettings(
|
||||
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
|
||||
QSettings.NativeFormat)
|
||||
if themeSettings.value("AppsUseLightTheme") == 0:
|
||||
darkPalette = QPalette()
|
||||
darkColor = QColor(45, 45, 45)
|
||||
|
@ -166,8 +171,8 @@ def prepare(tests=False):
|
|||
|
||||
return app, MW
|
||||
|
||||
def launch(app, MW = None):
|
||||
|
||||
def launch(app, MW=None):
|
||||
if MW == None:
|
||||
from manuskript.functions import mainWindow
|
||||
MW = mainWindow()
|
||||
|
@ -207,6 +212,7 @@ def launch(app, MW = None):
|
|||
app.quit()
|
||||
console.kill()
|
||||
kernel.io_loop.stop()
|
||||
|
||||
app.lastWindowClosed.connect(console_cleanup)
|
||||
|
||||
# Very important, IPython-specific step: this gets GUI event loop
|
||||
|
@ -221,6 +227,20 @@ def launch(app, MW = None):
|
|||
qApp.exec_()
|
||||
qApp.deleteLater()
|
||||
|
||||
|
||||
def sigint_handler(sig, MW):
|
||||
def handler(*args):
|
||||
MW.close()
|
||||
print(f'{sig} received, quit.')
|
||||
|
||||
return handler
|
||||
|
||||
|
||||
def setup_signal_handlers(MW):
|
||||
signal.signal(signal.SIGINT, sigint_handler("SIGINT", MW))
|
||||
signal.signal(signal.SIGTERM, sigint_handler("SIGTERM", MW))
|
||||
|
||||
|
||||
def run():
|
||||
"""
|
||||
Run separates prepare and launch for two reasons:
|
||||
|
@ -229,9 +249,11 @@ def run():
|
|||
"""
|
||||
# Need to return and keep `app` otherwise it gets deleted.
|
||||
app, MW = prepare()
|
||||
setup_signal_handlers(MW)
|
||||
# Separating launch to avoid segfault, so it seem.
|
||||
# Cf. http://stackoverflow.com/questions/12433491/is-this-pyqt-4-python-bug-or-wrongly-behaving-code
|
||||
launch(app, MW)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
|
|
Loading…
Reference in a new issue