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

[WIP] Upgrade to Qt 6 #164

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Install Xcode from the App Store.

Install [Docker Desktop](https://www.docker.com/products/docker-desktop). Make sure to choose your correct CPU, either Intel Chip or Apple Chip.

Install Python 3.9.9 [from python.org](https://www.python.org/downloads/release/python-399/).
Install Python 3.10.4 [from python.org](https://www.python.org/downloads/release/python-3104/).

Install Python dependencies:

Expand Down Expand Up @@ -110,7 +110,7 @@ The output is in the `dist` folder.

Install [Docker Desktop](https://www.docker.com/products/docker-desktop).

Install Python 3.9.9 (x86) [from python.org](https://www.python.org/downloads/release/python-399/). When installing it, make sure to check the "Add Python 3.9 to PATH" checkbox on the first page of the installer.
Install Python 3.10.4 (x86) [from python.org](https://www.python.org/downloads/release/python-3104/). When installing it, make sure to check the "Add Python 3.10 to PATH" checkbox on the first page of the installer.

Install [poetry](https://python-poetry.org/). Open PowerShell, and run:

Expand Down Expand Up @@ -155,7 +155,7 @@ Open a command prompt, cd into the dangerzone directory, and run:
poetry run python .\setup-windows.py build
```

In `build\exe.win32-3.9\` you will find `dangerzone.exe`, `dangerzone-cli.exe`, and all supporting files.
In `build\exe.win32-3.10\` you will find `dangerzone.exe`, `dangerzone-cli.exe`, and all supporting files.

### To build the installer

Expand Down
52 changes: 24 additions & 28 deletions dangerzone/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
import sys
import signal
import platform
from typing import Optional

import click
import uuid
from PySide2 import QtCore, QtWidgets
from PySide6 import QtCore
from PySide6.QtCore import QEvent
from PySide6.QtWidgets import QApplication

from .common import GuiCommon
from .main_window import MainWindow
from .systray import SysTray
from ..global_common import GlobalCommon


# 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):
class Application(QApplication):
document_selected = QtCore.Signal(str)
new_window = QtCore.Signal()
application_activated = QtCore.Signal()

def __init__(self):
super(ApplicationWrapper, self).__init__()
self.app = QtWidgets.QApplication()
self.app.setQuitOnLastWindowClosed(False)

self.original_event = self.app.event
super(Application, self).__init__()
self.setQuitOnLastWindowClosed(False)
self.original_event = self.event

def monkeypatch_event(event):
def monkeypatch_event(event: QEvent):
# In macOS, handle the file open event
if event.type() == QtCore.QEvent.FileOpen:
# Skip file open events in dev mode
Expand All @@ -36,19 +36,15 @@ def monkeypatch_event(event):
elif event.type() == QtCore.QEvent.ApplicationActivate:
self.application_activated.emit()
return True

return self.original_event(event)

self.app.event = monkeypatch_event
self.event = monkeypatch_event


@click.command()
@click.argument("filename", required=False)
def gui_main(filename):
if platform.system() == "Darwin":
# Required for macOS Big Sur: https://stackoverflow.com/a/64878899
os.environ["QT_MAC_WANTS_LAYER"] = "1"

# Make sure /usr/local/bin is in the path
os.environ["PATH"] = "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"

Expand All @@ -73,8 +69,7 @@ def flush(self):
sys.stderr = StdoutFilter(sys.stderr)

# Create the Qt app
app_wrapper = ApplicationWrapper()
app = app_wrapper.app
app = Application()

# Common objects
global_common = GlobalCommon()
Expand All @@ -84,7 +79,8 @@ def flush(self):
signal.signal(signal.SIGINT, signal.SIG_DFL)

# Create the system tray
systray = SysTray(global_common, gui_common, app, app_wrapper)
# noinspection PyUnusedLocal
systray = SysTray(global_common, gui_common, app)

closed_windows = {}
windows = {}
Expand All @@ -94,10 +90,10 @@ def delete_window(window_id):
del windows[window_id]

# Open a document in a window
def select_document(filename=None):
def select_document(path: Optional[str] = None):
if (
len(windows) == 1
and windows[list(windows.keys())[0]].common.input_filename == None
and windows[list(windows.keys())[0]].common.input_filename is None
):
window = windows[list(windows.keys())[0]]
else:
Expand All @@ -106,18 +102,18 @@ def select_document(filename=None):
window.delete_window.connect(delete_window)
windows[window_id] = window

if filename:
# Validate filename
filename = os.path.abspath(os.path.expanduser(filename))
if path is not None:
# Validate path
path = os.path.abspath(os.path.expanduser(path))
try:
open(filename, "rb")
open(path, "rb")
except FileNotFoundError:
click.echo("File not found")
return False
except PermissionError:
click.echo("Permission denied")
return False
window.common.input_filename = filename
window.common.input_filename = path
window.content_widget.doc_selection_widget.document_selected.emit()

return True
Expand All @@ -136,11 +132,11 @@ def application_activated():
select_document()

# If we get a file open event, open it
app_wrapper.document_selected.connect(select_document)
app_wrapper.new_window.connect(select_document)
app.document_selected.connect(select_document)
app.new_window.connect(select_document)

# If the application is activated and all windows are closed, open a new one
app_wrapper.application_activated.connect(application_activated)
app.application_activated.connect(application_activated)

# Launch the GUI
ret = app.exec_()
Expand Down
2 changes: 1 addition & 1 deletion dangerzone/gui/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess
import shlex
import pipes
from PySide2 import QtCore, QtGui, QtWidgets
from PySide6 import QtCore, QtGui, QtWidgets
from colorama import Fore

if platform.system() == "Darwin":
Expand Down
6 changes: 3 additions & 3 deletions dangerzone/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess
import json
import shutil
from PySide2 import QtCore, QtGui, QtWidgets
from PySide6 import QtCore, QtGui, QtWidgets
from colorama import Style, Fore

from ..common import Common
Expand Down Expand Up @@ -486,7 +486,7 @@ def start_button_clicked(self):


class ConvertThread(QtCore.QThread):
finished = QtCore.Signal(bool)
is_finished = QtCore.Signal(bool)
gmarmstrong marked this conversation as resolved.
Show resolved Hide resolved
update = QtCore.Signal(bool, str, int)

def __init__(self, global_common, common):
Expand All @@ -509,7 +509,7 @@ def run(self):
ocr_lang,
self.stdout_callback,
):
self.finished.emit(self.error)
self.is_finished.emit(self.error)
gmarmstrong marked this conversation as resolved.
Show resolved Hide resolved

def stdout_callback(self, line):
try:
Expand Down
13 changes: 8 additions & 5 deletions dangerzone/gui/systray.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import platform
from PySide2 import QtWidgets
from PySide6 import QtWidgets

from dangerzone.global_common import GlobalCommon
from dangerzone.gui import GuiCommon


class SysTray(QtWidgets.QSystemTrayIcon):
def __init__(self, global_common, gui_common, app, app_wrapper):
def __init__(
self, global_common: GlobalCommon, gui_common: GuiCommon, app
):
super(SysTray, self).__init__()
self.global_common = global_common
self.gui_common = gui_common
self.app = app
self.app_wrapper = app_wrapper

self.setIcon(self.gui_common.get_window_icon())

Expand All @@ -24,7 +27,7 @@ def __init__(self, global_common, gui_common, app, app_wrapper):
self.show()

def new_window(self):
self.app_wrapper.new_window.emit()
self.app.new_window.emit()

def quit_clicked(self):
self.app.quit()
Loading