Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup signal handler to avoid accident data loss #835

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 30 additions & 8 deletions manuskript/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import platform
import sys
import signal

import manuskript.ui.views.webView
from PyQt5.QtCore import QLocale, QTranslator, QSettings, Qt
Expand All @@ -15,11 +16,12 @@

faulthandler.enable()


def prepare(tests=False):
app = QApplication(sys.argv)
app.setOrganizationName("manuskript"+("_tests" if tests else ""))
app.setOrganizationName("manuskript" + ("_tests" if tests else ""))
app.setOrganizationDomain("www.theologeek.ch")
app.setApplicationName("manuskript"+("_tests" if tests else ""))
app.setApplicationName("manuskript" + ("_tests" if tests else ""))
app.setApplicationVersion(getVersion())

print("Running manuskript version {}.".format(getVersion()))
Expand All @@ -38,6 +40,7 @@ def prepare(tests=False):

# Translation process
appTranslator = QTranslator(app)

# By default: locale

def tryLoadTranslation(translation, source):
Expand Down Expand Up @@ -106,14 +109,16 @@ def respectSystemDarkThemeSetting():

# 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)
disabledColor = QColor(127,127,127)
darkColor = QColor(45, 45, 45)
disabledColor = QColor(127, 127, 127)
darkPalette.setColor(QPalette.Window, darkColor)
darkPalette.setColor(QPalette.WindowText, Qt.white)
darkPalette.setColor(QPalette.Base, QColor(18,18,18))
darkPalette.setColor(QPalette.Base, QColor(18, 18, 18))
darkPalette.setColor(QPalette.AlternateBase, darkColor)
darkPalette.setColor(QPalette.ToolTipBase, Qt.white)
darkPalette.setColor(QPalette.ToolTipText, Qt.white)
Expand All @@ -137,7 +142,7 @@ def respectSystemDarkThemeSetting():

# This broke the Settings Dialog at one point... and then it stopped breaking it.
# TODO: Why'd it break? Check if tooltips look OK... and if not, make them look OK.
#app.setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }")
# app.setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }")

respectSystemDarkThemeSetting()

Expand Down Expand Up @@ -166,8 +171,8 @@ def respectSystemDarkThemeSetting():

return app, MW

def launch(app, MW = None):

def launch(app, MW=None):
if MW == None:
from manuskript.functions import mainWindow
MW = mainWindow()
Expand Down Expand Up @@ -207,6 +212,7 @@ def console_cleanup():
app.quit()
console.kill()
kernel.io_loop.stop()

app.lastWindowClosed.connect(console_cleanup)

# Very important, IPython-specific step: this gets GUI event loop
Expand All @@ -221,6 +227,20 @@ def console_cleanup():
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:
Expand All @@ -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()