mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Clicking start starts running the tasks
This commit is contained in:
parent
dbff6b4b79
commit
bdcd61b964
4 changed files with 75 additions and 27 deletions
|
@ -44,10 +44,12 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||
self.doc_selection_widget.document_selected.connect(
|
||||
self.settings_widget.document_selected
|
||||
)
|
||||
self.settings_widget.start_clicked.connect(self.start_clicked)
|
||||
self.settings_widget.hide()
|
||||
|
||||
# Tasks
|
||||
self.tasks_widget = TasksWidget(self.common)
|
||||
self.settings_widget.start_clicked.connect(self.tasks_widget.start)
|
||||
self.tasks_widget.hide()
|
||||
|
||||
# Layout
|
||||
|
@ -67,6 +69,10 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||
self.doc_selection_widget.hide()
|
||||
self.settings_widget.show()
|
||||
|
||||
def start_clicked(self):
|
||||
self.settings_widget.hide()
|
||||
self.tasks_widget.show()
|
||||
|
||||
def closeEvent(self, e):
|
||||
e.accept()
|
||||
self.app.quit()
|
||||
|
|
|
@ -24,7 +24,6 @@ class Settings:
|
|||
|
||||
def set(self, key, val):
|
||||
self.settings[key] = val
|
||||
self.settings.save()
|
||||
|
||||
def load(self):
|
||||
if os.path.isfile(self.settings_filename):
|
||||
|
|
|
@ -3,6 +3,8 @@ from PyQt5 import QtCore, QtGui, QtWidgets
|
|||
|
||||
|
||||
class SettingsWidget(QtWidgets.QWidget):
|
||||
start_clicked = QtCore.pyqtSignal()
|
||||
|
||||
def __init__(self, common):
|
||||
super(SettingsWidget, self).__init__()
|
||||
self.common = common
|
||||
|
@ -53,13 +55,14 @@ class SettingsWidget(QtWidgets.QWidget):
|
|||
update_layout.addStretch()
|
||||
|
||||
# Button
|
||||
self.button_start = QtWidgets.QPushButton("Convert to Save Document")
|
||||
self.button_start.setStyleSheet(
|
||||
self.start_button = QtWidgets.QPushButton("Convert to Save Document")
|
||||
self.start_button.clicked.connect(self.start_button_clicked)
|
||||
self.start_button.setStyleSheet(
|
||||
"QPushButton { font-size: 16px; font-weight: bold; padding: 10px; }"
|
||||
)
|
||||
button_layout = QtWidgets.QHBoxLayout()
|
||||
button_layout.addStretch()
|
||||
button_layout.addWidget(self.button_start)
|
||||
button_layout.addWidget(self.start_button)
|
||||
button_layout.addStretch()
|
||||
|
||||
# Layout
|
||||
|
@ -124,3 +127,24 @@ class SettingsWidget(QtWidgets.QWidget):
|
|||
if filename[0] != "":
|
||||
self.common.save_filename = filename[0]
|
||||
self.save_lineedit.setText(os.path.basename(self.common.save_filename))
|
||||
|
||||
def start_button_clicked(self):
|
||||
# Update settings
|
||||
self.common.settings.set(
|
||||
"save", self.save_checkbox.checkState() == QtCore.Qt.Checked
|
||||
)
|
||||
self.common.settings.set(
|
||||
"ocr", self.ocr_checkbox.checkState() == QtCore.Qt.Checked
|
||||
)
|
||||
self.common.settings.set("ocr_language", self.ocr_combobox.currentText())
|
||||
self.common.settings.set(
|
||||
"open", self.open_checkbox.checkState() == QtCore.Qt.Checked
|
||||
)
|
||||
self.common.settings.set("open_app", self.open_combobox.currentText())
|
||||
self.common.settings.set(
|
||||
"update_container", self.update_checkbox.checkState() == QtCore.Qt.Checked
|
||||
)
|
||||
self.common.settings.save()
|
||||
|
||||
# Start!
|
||||
self.start_clicked.emit()
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import shlex
|
||||
import subprocess
|
||||
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||
|
||||
from .tasks import PullImageTask, BuildContainerTask, ConvertToPixels, ConvertToPDF
|
||||
|
@ -26,18 +28,23 @@ class TasksWidget(QtWidgets.QWidget):
|
|||
self.scroll_to_bottom
|
||||
)
|
||||
|
||||
self.tasks = [PullImageTask, BuildContainerTask, ConvertToPixels, ConvertToPDF]
|
||||
# Layout
|
||||
layout = QtWidgets.QVBoxLayout()
|
||||
layout.addWidget(self.task_label)
|
||||
layout.addWidget(self.details_scrollarea)
|
||||
self.setLayout(layout)
|
||||
|
||||
def start(self, filename):
|
||||
print(f"Input document: {filename}")
|
||||
self.common.set_document_filename(filename)
|
||||
self.show()
|
||||
self.tasks = []
|
||||
|
||||
def start(self):
|
||||
if self.common.settings.get("update_container"):
|
||||
self.tasks += [PullImageTask, BuildContainerTask]
|
||||
self.tasks += [ConvertToPixels, ConvertToPDF]
|
||||
self.next_task()
|
||||
|
||||
def next_task(self):
|
||||
if len(self.tasks) == 0:
|
||||
self.save_safe_pdf()
|
||||
self.all_done()
|
||||
return
|
||||
|
||||
self.task_details.setText("")
|
||||
|
@ -62,21 +69,33 @@ class TasksWidget(QtWidgets.QWidget):
|
|||
f"Directory with pixel data: {self.common.pixel_dir.name}\n\n{err}"
|
||||
)
|
||||
|
||||
def save_safe_pdf(self):
|
||||
suggested_filename = (
|
||||
f"{os.path.splitext(self.common.document_filename)[0]}-safe.pdf"
|
||||
)
|
||||
|
||||
filename = QtWidgets.QFileDialog.getSaveFileName(
|
||||
self, "Save safe PDF", suggested_filename, filter="Documents (*.pdf)"
|
||||
)
|
||||
if filename[0] == "":
|
||||
print("Save file dialog canceled")
|
||||
else:
|
||||
def all_done(self):
|
||||
# Save safe PDF
|
||||
if self.common.settings.get("save"):
|
||||
source_filename = f"{self.common.safe_dir.name}/safe-output-compressed.pdf"
|
||||
dest_filename = filename[0]
|
||||
dest_filename = self.common.save_filename
|
||||
shutil.move(source_filename, dest_filename)
|
||||
|
||||
# Open
|
||||
if self.common.settings.get("open"):
|
||||
if self.common.settings.get("open_app") in self.common.pdf_viewers:
|
||||
# Get the PDF reader command
|
||||
args = shlex.split(
|
||||
self.common.pdf_viewers[self.common.settings.get("open_app")]
|
||||
)
|
||||
# %f, %F, %u, and %U are filenames or URLS -- so replace with the file to open
|
||||
for i in range(len(args)):
|
||||
if (
|
||||
args[i] == "%f"
|
||||
or args[i] == "%F"
|
||||
or args[i] == "%u"
|
||||
or args[i] == "%U"
|
||||
):
|
||||
args[i] = self.save_filename
|
||||
|
||||
# Open as a background process
|
||||
subprocess.Popen(args)
|
||||
|
||||
# Clean up
|
||||
self.common.pixel_dir.cleanup()
|
||||
self.common.safe_dir.cleanup()
|
||||
|
|
Loading…
Reference in a new issue