Have GuiCommon subclass Common

This commit is contained in:
Guthrie McAfee Armstrong 2022-06-06 22:28:01 -04:00
parent 52e5154ee7
commit c655d3a2ca
No known key found for this signature in database
GPG key ID: ED4DAE89F08242D2
5 changed files with 50 additions and 55 deletions

View file

@ -1,6 +1,6 @@
class Common(object): class Common(object):
""" """
The Common class is a singleton of shared functionality throughout an open dangerzone window The Common class is a singleton of shared functionality throughout a dangerzone process
""" """
def __init__(self): def __init__(self):

View file

@ -49,14 +49,14 @@ def gui_main(filename):
colorama.init(autoreset=True) colorama.init(autoreset=True)
# Common objects # Common objects
gui_common = GuiCommon(app) common = GuiCommon(app)
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception # Allow Ctrl-C to smoothly quit the program instead of throwing an exception
signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGINT, signal.SIG_DFL)
# Create the system tray # Create the system tray
# noinspection PyUnusedLocal # noinspection PyUnusedLocal
systray = SysTray(gui_common, app) systray = SysTray(common, app)
closed_windows = {} closed_windows = {}
windows = {} windows = {}
@ -74,7 +74,7 @@ def gui_main(filename):
window: MainWindow = windows[list(windows.keys())[0]] window: MainWindow = windows[list(windows.keys())[0]]
else: else:
window_id = uuid.uuid4().hex window_id = uuid.uuid4().hex
window = MainWindow(gui_common, window_id) window = MainWindow(common, window_id)
window.delete_window.connect(delete_window) window.delete_window.connect(delete_window)
windows[window_id] = window windows[window_id] = window

View file

@ -6,6 +6,7 @@ import pipes
from PySide6 import QtGui from PySide6 import QtGui
from colorama import Fore from colorama import Fore
from dangerzone.common import Common
from dangerzone.gui import Application from dangerzone.gui import Application
from dangerzone.gui.settings import Settings from dangerzone.gui.settings import Settings
@ -13,12 +14,14 @@ if platform.system() == "Linux":
from xdg.DesktopEntry import DesktopEntry # type: ignore from xdg.DesktopEntry import DesktopEntry # type: ignore
class GuiCommon(object): class GuiCommon(Common):
""" """
The GuiCommon class is a singleton of shared functionality for the GUI The GuiCommon class adds GUI-specific features to Common
""" """
def __init__(self, app: Application): def __init__(self, app: Application):
super().__init__()
# Qt app # Qt app
self.app = app self.app = app

View file

@ -10,7 +10,6 @@ from colorama import Style, Fore
import dangerzone.util as dzutil import dangerzone.util as dzutil
from dangerzone.gui import GuiCommon from dangerzone.gui import GuiCommon
from dangerzone.common import Common
from dangerzone.container import convert from dangerzone.container import convert
from dangerzone.util import install_container from dangerzone.util import install_container
@ -18,11 +17,10 @@ from dangerzone.util import install_container
class MainWindow(QtWidgets.QMainWindow): class MainWindow(QtWidgets.QMainWindow):
delete_window = QtCore.Signal(str) delete_window = QtCore.Signal(str)
def __init__(self, gui_common: GuiCommon, window_id: str): def __init__(self, common: GuiCommon, window_id: str):
super(MainWindow, self).__init__() super(MainWindow, self).__init__()
self.gui_common = gui_common self.common = common
self.window_id = window_id self.window_id = window_id
self.common = Common()
self.setWindowTitle("Dangerzone") self.setWindowTitle("Dangerzone")
self.setWindowIcon(QIcon(dzutil.WINDOW_ICON_PATH)) self.setWindowIcon(QIcon(dzutil.WINDOW_ICON_PATH))
@ -36,7 +34,7 @@ class MainWindow(QtWidgets.QMainWindow):
QtGui.QPixmap.fromImage(QtGui.QImage(dzutil.get_resource_path("icon.png"))) QtGui.QPixmap.fromImage(QtGui.QImage(dzutil.get_resource_path("icon.png")))
) )
header_label = QtWidgets.QLabel("dangerzone") header_label = QtWidgets.QLabel("dangerzone")
header_label.setFont(self.gui_common.fixed_font) header_label.setFont(self.common.fixed_font)
header_label.setStyleSheet("QLabel { font-weight: bold; font-size: 50px; }") header_label.setStyleSheet("QLabel { font-weight: bold; font-size: 50px; }")
header_layout = QtWidgets.QHBoxLayout() header_layout = QtWidgets.QHBoxLayout()
header_layout.addStretch() header_layout.addStretch()
@ -46,15 +44,15 @@ class MainWindow(QtWidgets.QMainWindow):
header_layout.addStretch() header_layout.addStretch()
# Waiting widget, replaces content widget while container runtime isn't available # Waiting widget, replaces content widget while container runtime isn't available
self.waiting_widget = WaitingWidget(self.gui_common) self.waiting_widget = WaitingWidget(self.common)
self.waiting_widget.finished.connect(self.waiting_finished) self.waiting_widget.finished.connect(self.waiting_finished)
# Content widget, contains all the window content except waiting widget # Content widget, contains all the window content except waiting widget
self.content_widget = ContentWidget(self.gui_common, self.common) self.content_widget = ContentWidget(self.common)
self.content_widget.close_window.connect(self.close) self.content_widget.close_window.connect(self.close)
# Only use the waiting widget if container runtime isn't available # Only use the waiting widget if container runtime isn't available
if self.gui_common.is_waiting_finished: if self.common.is_waiting_finished:
self.waiting_widget.hide() self.waiting_widget.hide()
self.content_widget.show() self.content_widget.show()
else: else:
@ -74,7 +72,7 @@ class MainWindow(QtWidgets.QMainWindow):
self.show() self.show()
def waiting_finished(self): def waiting_finished(self):
self.gui_common.is_waiting_finished = True self.common.is_waiting_finished = True
self.waiting_widget.hide() self.waiting_widget.hide()
self.content_widget.show() self.content_widget.show()
@ -83,7 +81,7 @@ class MainWindow(QtWidgets.QMainWindow):
self.delete_window.emit(self.window_id) self.delete_window.emit(self.window_id)
if platform.system() != "Darwin": if platform.system() != "Darwin":
self.gui_common.app.quit() self.common.app.quit()
class InstallContainerThread(QtCore.QThread): class InstallContainerThread(QtCore.QThread):
@ -109,9 +107,9 @@ class WaitingWidget(QtWidgets.QWidget):
# - "install_container" # - "install_container"
finished = QtCore.Signal() finished = QtCore.Signal()
def __init__(self, gui_common: GuiCommon): def __init__(self, common: GuiCommon):
super(WaitingWidget, self).__init__() super(WaitingWidget, self).__init__()
self.gui_common = gui_common self.common = common
self.label = QtWidgets.QLabel() self.label = QtWidgets.QLabel()
self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setAlignment(QtCore.Qt.AlignCenter)
@ -144,7 +142,7 @@ class WaitingWidget(QtWidgets.QWidget):
def check_state(self): def check_state(self):
state: str state: str
# Can we find the container runtime binary binary # Can we find the container runtime binary
if platform.system() == "Linux": if platform.system() == "Linux":
container_runtime = shutil.which("podman") container_runtime = shutil.which("podman")
else: else:
@ -197,9 +195,8 @@ class WaitingWidget(QtWidgets.QWidget):
class ContentWidget(QtWidgets.QWidget): class ContentWidget(QtWidgets.QWidget):
close_window = QtCore.Signal() close_window = QtCore.Signal()
def __init__(self, gui_common: GuiCommon, common: Common): def __init__(self, common: GuiCommon):
super(ContentWidget, self).__init__() super(ContentWidget, self).__init__()
self.gui_common = gui_common
self.common = common self.common = common
# Doc selection widget # Doc selection widget
@ -207,7 +204,7 @@ class ContentWidget(QtWidgets.QWidget):
self.doc_selection_widget.document_selected.connect(self.document_selected) self.doc_selection_widget.document_selected.connect(self.document_selected)
# Settings # Settings
self.settings_widget = SettingsWidget(self.gui_common, self.common) self.settings_widget = SettingsWidget(self.common)
self.doc_selection_widget.document_selected.connect( self.doc_selection_widget.document_selected.connect(
self.settings_widget.document_selected self.settings_widget.document_selected
) )
@ -216,7 +213,7 @@ class ContentWidget(QtWidgets.QWidget):
self.settings_widget.hide() self.settings_widget.hide()
# Convert # Convert
self.convert_widget = ConvertWidget(self.gui_common, self.common) self.convert_widget = ConvertWidget(self.common)
self.convert_widget.close_window.connect(self._close_window) self.convert_widget.close_window.connect(self._close_window)
self.doc_selection_widget.document_selected.connect( self.doc_selection_widget.document_selected.connect(
self.convert_widget.document_selected self.convert_widget.document_selected
@ -246,7 +243,7 @@ class ContentWidget(QtWidgets.QWidget):
class DocSelectionWidget(QtWidgets.QWidget): class DocSelectionWidget(QtWidgets.QWidget):
document_selected = QtCore.Signal() document_selected = QtCore.Signal()
def __init__(self, common: Common): def __init__(self, common: GuiCommon):
super(DocSelectionWidget, self).__init__() super(DocSelectionWidget, self).__init__()
self.common = common self.common = common
@ -289,9 +286,8 @@ class SettingsWidget(QtWidgets.QWidget):
start_clicked = QtCore.Signal() start_clicked = QtCore.Signal()
close_window = QtCore.Signal() close_window = QtCore.Signal()
def __init__(self, gui_common: GuiCommon, common: Common): def __init__(self, common: GuiCommon):
super(SettingsWidget, self).__init__() super(SettingsWidget, self).__init__()
self.gui_common = gui_common
self.common = common self.common = common
# Dangerous document label # Dangerous document label
@ -336,8 +332,8 @@ class SettingsWidget(QtWidgets.QWidget):
) )
self.open_checkbox.clicked.connect(self.update_ui) self.open_checkbox.clicked.connect(self.update_ui)
self.open_combobox = QtWidgets.QComboBox() self.open_combobox = QtWidgets.QComboBox()
for k in self.gui_common.pdf_viewers: for k in self.common.pdf_viewers:
self.open_combobox.addItem(k, self.gui_common.pdf_viewers[k]) self.open_combobox.addItem(k, self.common.pdf_viewers[k])
if platform.system() == "Darwin" or platform.system() == "Linux": if platform.system() == "Darwin" or platform.system() == "Linux":
open_layout = QtWidgets.QHBoxLayout() open_layout = QtWidgets.QHBoxLayout()
@ -381,29 +377,29 @@ class SettingsWidget(QtWidgets.QWidget):
self.setLayout(layout) self.setLayout(layout)
# Load values from settings # Load values from settings
if self.gui_common.settings.get("save"): if self.common.settings.get("save"):
self.save_checkbox.setCheckState(QtCore.Qt.Checked) self.save_checkbox.setCheckState(QtCore.Qt.Checked)
else: else:
self.save_checkbox.setCheckState(QtCore.Qt.Unchecked) self.save_checkbox.setCheckState(QtCore.Qt.Unchecked)
if self.gui_common.settings.get("ocr"): if self.common.settings.get("ocr"):
self.ocr_checkbox.setCheckState(QtCore.Qt.Checked) self.ocr_checkbox.setCheckState(QtCore.Qt.Checked)
else: else:
self.ocr_checkbox.setCheckState(QtCore.Qt.Unchecked) self.ocr_checkbox.setCheckState(QtCore.Qt.Unchecked)
index = self.ocr_combobox.findText(self.gui_common.settings.get("ocr_language")) index = self.ocr_combobox.findText(self.common.settings.get("ocr_language"))
if index != -1: if index != -1:
self.ocr_combobox.setCurrentIndex(index) self.ocr_combobox.setCurrentIndex(index)
if platform.system() == "Darwin" or platform.system() == "Linux": if platform.system() == "Darwin" or platform.system() == "Linux":
if self.gui_common.settings.get("open"): if self.common.settings.get("open"):
self.open_checkbox.setCheckState(QtCore.Qt.Checked) self.open_checkbox.setCheckState(QtCore.Qt.Checked)
else: else:
self.open_checkbox.setCheckState(QtCore.Qt.Unchecked) self.open_checkbox.setCheckState(QtCore.Qt.Unchecked)
if platform.system() == "Linux": if platform.system() == "Linux":
index = self.open_combobox.findText( index = self.open_combobox.findText(
self.gui_common.settings.get("open_app") self.common.settings.get("open_app")
) )
if index != -1: if index != -1:
self.open_combobox.setCurrentIndex(index) self.open_combobox.setCurrentIndex(index)
@ -452,22 +448,20 @@ class SettingsWidget(QtWidgets.QWidget):
self.common.output_filename = tmp[1] self.common.output_filename = tmp[1]
# Update settings # Update settings
self.gui_common.settings.set( self.common.settings.set(
"save", self.save_checkbox.checkState() == QtCore.Qt.Checked "save", self.save_checkbox.checkState() == QtCore.Qt.Checked
) )
self.gui_common.settings.set( self.common.settings.set(
"ocr", self.ocr_checkbox.checkState() == QtCore.Qt.Checked "ocr", self.ocr_checkbox.checkState() == QtCore.Qt.Checked
) )
self.gui_common.settings.set("ocr_language", self.ocr_combobox.currentText()) self.common.settings.set("ocr_language", self.ocr_combobox.currentText())
if platform.system() == "Darwin" or platform.system() == "Linux": if platform.system() == "Darwin" or platform.system() == "Linux":
self.gui_common.settings.set( self.common.settings.set(
"open", self.open_checkbox.checkState() == QtCore.Qt.Checked "open", self.open_checkbox.checkState() == QtCore.Qt.Checked
) )
if platform.system() == "Linux": if platform.system() == "Linux":
self.gui_common.settings.set( self.common.settings.set("open_app", self.open_combobox.currentText())
"open_app", self.open_combobox.currentText() self.common.settings.save()
)
self.gui_common.settings.save()
# Start! # Start!
self.start_clicked.emit() self.start_clicked.emit()
@ -477,17 +471,15 @@ class ConvertThread(QtCore.QThread):
is_finished = QtCore.Signal(bool) is_finished = QtCore.Signal(bool)
update = QtCore.Signal(bool, str, int) update = QtCore.Signal(bool, str, int)
def __init__(self, gui_common: GuiCommon, common: Common): def __init__(self, common: GuiCommon):
super(ConvertThread, self).__init__() super(ConvertThread, self).__init__()
self.gui_common = gui_common self.common = common
self.common = common self.common = common
self.error = False self.error = False
def run(self): def run(self):
if self.gui_common.settings.get("ocr"): if self.common.settings.get("ocr"):
ocr_lang = dzutil.OCR_LANGUAGES[ ocr_lang = dzutil.OCR_LANGUAGES[self.common.settings.get("ocr_language")]
self.gui_common.settings.get("ocr_language")
]
else: else:
ocr_lang = None ocr_lang = None
@ -524,9 +516,9 @@ class ConvertThread(QtCore.QThread):
class ConvertWidget(QtWidgets.QWidget): class ConvertWidget(QtWidgets.QWidget):
close_window = QtCore.Signal() close_window = QtCore.Signal()
def __init__(self, gui_common: GuiCommon, common: Common): def __init__(self, common: GuiCommon):
super(ConvertWidget, self).__init__() super(ConvertWidget, self).__init__()
self.gui_common = gui_common self.common = common
self.common = common self.common = common
self.error = False self.error = False
@ -575,7 +567,7 @@ class ConvertWidget(QtWidgets.QWidget):
) )
def start(self): def start(self):
self.convert_t = ConvertThread(self.gui_common, self.common) self.convert_t = ConvertThread(self.common)
self.convert_t.update.connect(self.update) self.convert_t.update.connect(self.update)
self.convert_t.finished.connect(self.all_done) self.convert_t.finished.connect(self.all_done)
self.convert_t.start() self.convert_t.start()
@ -603,12 +595,12 @@ class ConvertWidget(QtWidgets.QWidget):
) )
# Open # Open
if self.gui_common.settings.get("open"): if self.common.settings.get("open"):
self.gui_common.open_pdf_viewer(self.common.output_filename) self.common.open_pdf_viewer(self.common.output_filename)
# Quit # Quit
if platform.system() == "Darwin": if platform.system() == "Darwin":
# In macOS, just close the window # In macOS, just close the window
self.close_window.emit() self.close_window.emit()
else: else:
self.gui_common.app.quit() self.common.app.quit()

View file

@ -6,9 +6,9 @@ import dangerzone.util as dzutil
class SysTray(QtWidgets.QSystemTrayIcon): class SysTray(QtWidgets.QSystemTrayIcon):
def __init__(self, gui_common: GuiCommon, app: Application): def __init__(self, common: GuiCommon, app: Application):
super(SysTray, self).__init__() super(SysTray, self).__init__()
self.gui_common = gui_common self.common = common
self.app = app self.app = app
self.setIcon(QIcon(dzutil.WINDOW_ICON_PATH)) self.setIcon(QIcon(dzutil.WINDOW_ICON_PATH))