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

File Preview PoC #758

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
35 changes: 30 additions & 5 deletions dangerzone/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ def start_button_clicked(self) -> None:

class ConvertTask(QtCore.QObject):
finished = QtCore.Signal(bool)
update = QtCore.Signal(bool, str, int)
update_progress = QtCore.Signal(bool, str, int)
update_preview = QtCore.Signal(int, int, bytes)

def __init__(
self,
Expand All @@ -904,14 +905,18 @@ def convert_document(self) -> None:
self.document,
self.ocr_lang,
self.progress_callback,
self.preview_callback,
)
self.finished.emit(self.error)

def progress_callback(self, error: bool, text: str, percentage: int) -> None:
if error:
self.error = True

self.update.emit(error, text, percentage)
self.update_progress.emit(error, text, percentage)

def preview_callback(self, width: int, height: int, data: bytes) -> None:
self.update_preview.emit(width, height, data)


class DocumentsListWidget(QtWidgets.QListWidget):
Expand All @@ -933,7 +938,7 @@ def clear(self) -> None:
def documents_added(self, docs: List[Document]) -> None:
for document in docs:
item = QtWidgets.QListWidgetItem()
item.setSizeHint(QtCore.QSize(500, 50))
item.setSizeHint(QtCore.QSize(500, 800))
widget = DocumentWidget(self.dangerzone, document)
self.addItem(item)
self.setItemWidget(item, widget)
Expand All @@ -950,7 +955,8 @@ def start_conversion(self) -> None:
for doc in self.docs_list:
task = ConvertTask(self.dangerzone, doc, self.get_ocr_lang())
doc_widget = self.docs_list_widget_map[doc]
task.update.connect(doc_widget.update_progress)
task.update_progress.connect(doc_widget.update_progress)
task.update_preview.connect(doc_widget.update_preview)
task.finished.connect(doc_widget.all_done)
self.thread_pool.apply_async(task.convert_document)

Expand Down Expand Up @@ -1005,13 +1011,22 @@ def __init__(
self.progress.setRange(0, 100)
self.progress.setValue(0)

# Preview
self.doc_preview = QtWidgets.QLabel()
self.doc_preview.setAlignment(
QtCore.Qt.AlignVCenter | QtCore.Qt.AlignLeft
)

# Layout
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.status_image)
layout.addWidget(self.dangerous_doc_label)
layout.addWidget(self.progress)
layout.addWidget(self.error_label)
self.setLayout(layout)
layout2 = QtWidgets.QVBoxLayout()
layout2.addLayout(layout)
layout2.addWidget(self.doc_preview)
self.setLayout(layout2)

def update_progress(self, error: bool, text: str, percentage: int) -> None:
self.update_status_image()
Expand All @@ -1025,6 +1040,16 @@ def update_progress(self, error: bool, text: str, percentage: int) -> None:
self.progress.setToolTip(text)
self.progress.setValue(percentage)

def update_preview(self, width: int, height: int, data: bytes) -> None:
log.debug(f"UPDATTING PREVIEW {width}, {height}")
# with open("tests/test_docs/sample-ppm.ppm", 'rb') as ppm_file:
ppm_header = f"P6\n{width} {height}\n255\n".encode('ascii')
ppm_data = ppm_header + data
log.info(ppm_data[:20])
pix = QtGui.QPixmap()
pix.loadFromData(ppm_data)
self.doc_preview.setPixmap(pix)

def load_status_image(self, filename: str) -> QtGui.QPixmap:
path = get_resource_path(filename)
img = QtGui.QImage(path)
Expand Down
6 changes: 6 additions & 0 deletions dangerzone/isolation_provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ def convert(
document: Document,
ocr_lang: Optional[str],
progress_callback: Optional[Callable] = None,
preview_callback: Optional[Callable] = None,
) -> None:
self.progress_callback = progress_callback
self.preview_callback = preview_callback
document.mark_as_converting()
try:
with tempfile.TemporaryDirectory() as t:
Expand Down Expand Up @@ -125,6 +127,10 @@ def doc_to_pixels(self, document: Document, tempdir: str) -> None:
num_pixels,
)

# Preview
if self.preview_callback:
self.preview_callback(width, height, untrusted_pixels)

# Wrapper code
with open(f"{tempdir}/pixels/page-{page}.width", "w") as f_width:
f_width.write(str(width))
Expand Down
Loading