From b8e8c741619e2b4cf9ee50ebea1ef0fa8d13708d Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 9 Jun 2021 13:25:22 -0700 Subject: [PATCH] Make an ApplicationWrapper to avoid inheriting from QApplication --- dangerzone/__init__.py | 3 +-- dangerzone/global_common.py | 3 ++- dangerzone/gui.py | 38 ++++++++++++++++++++++--------------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/dangerzone/__init__.py b/dangerzone/__init__.py index e890eb5..8edcba8 100644 --- a/dangerzone/__init__.py +++ b/dangerzone/__init__.py @@ -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 diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py index 0f74a6e..63c902b 100644 --- a/dangerzone/global_common.py +++ b/dangerzone/global_common.py @@ -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 = { diff --git a/dangerzone/gui.py b/dangerzone/gui.py index 04cedd9..4fe1107 100644 --- a/dangerzone/gui.py +++ b/dangerzone/gui.py @@ -20,23 +20,31 @@ 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): - # In macOS, handle the file open event - if event.type() == QtCore.QEvent.FileOpen: - self.document_selected.emit(event.file()) - return True - elif event.type() == QtCore.QEvent.ApplicationActivate: - self.application_activated.emit() - return True + self.original_event = self.app.event - return QtWidgets.QApplication.event(self, event) + def monkeypatch_event(event): + # In macOS, handle the file open event + if event.type() == QtCore.QEvent.FileOpen: + self.document_selected.emit(event.file()) + return True + elif event.type() == QtCore.QEvent.ApplicationActivate: + self.application_activated.emit() + return True + + 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_())