Make an ApplicationWrapper to avoid inheriting from QApplication

This commit is contained in:
Micah Lee 2021-06-09 13:25:22 -07:00
parent 791723db20
commit b8e8c74161
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
3 changed files with 26 additions and 18 deletions

View file

@ -1,6 +1,5 @@
import os
import sys
from .container import container_main
dangerzone_version = "0.1.5"
@ -8,7 +7,7 @@ dangerzone_version = "0.1.5"
# PyInstaller builds a single binary
basename = os.path.basename(sys.argv[0])
if basename == "dangerzone-container" or basename == "dangerzone-container.exe":
main = container_main
from .container import container_main as main
else:
# If the binary isn't "dangerzone-contatiner", then launch the GUI
from .gui import gui_main as main

View file

@ -51,7 +51,8 @@ class GlobalCommon(object):
self.dz_container_path = self.get_dangerzone_container_path()
# Preload list of PDF viewers on computer
self.pdf_viewers = self._find_pdf_viewers()
# self.pdf_viewers = self._find_pdf_viewers()
self.pdf_viewers = {"Preview": "com.apple.Preview"}
# Languages supported by tesseract
self.ocr_languages = {

View file

@ -20,14 +20,20 @@ from .docker_installer import (
from .container import container_runtime
class Application(QtWidgets.QApplication):
# For some reason, Dangerzone segfaults if I inherit from QApplication directly, so instead
# this is a class whose job is to hold a QApplication object and customize it
class ApplicationWrapper(QtCore.QObject):
document_selected = QtCore.Signal(str)
application_activated = QtCore.Signal()
def __init__(self):
QtWidgets.QApplication.__init__(self, sys.argv)
super(ApplicationWrapper, self).__init__()
self.app = QtWidgets.QApplication()
self.app.setQuitOnLastWindowClosed(False)
def event(self, event):
self.original_event = self.app.event
def monkeypatch_event(event):
# In macOS, handle the file open event
if event.type() == QtCore.QEvent.FileOpen:
self.document_selected.emit(event.file())
@ -36,7 +42,9 @@ class Application(QtWidgets.QApplication):
self.application_activated.emit()
return True
return QtWidgets.QApplication.event(self, event)
return self.original_event(event)
self.app.event = monkeypatch_event
@click.command()
@ -48,8 +56,8 @@ def gui_main(custom_container, filename):
os.environ["QT_MAC_WANTS_LAYER"] = "1"
# Create the Qt app
app = Application()
app.setQuitOnLastWindowClosed(False)
app_wrapper = ApplicationWrapper()
app = app_wrapper.app
# GlobalCommon object
global_common = GlobalCommon(app)
@ -150,9 +158,9 @@ def gui_main(custom_container, filename):
select_document()
# If we get a file open event, open it
app.document_selected.connect(select_document)
app_wrapper.document_selected.connect(select_document)
# If the application is activated and all windows are closed, open a new one
app.application_activated.connect(application_activated)
app_wrapper.application_activated.connect(application_activated)
sys.exit(app.exec_())