mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-10-30 00:21:27 +01:00
fix(pdf_viewer): connect document status once instead of disconnecting unconnected slots
This commit is contained in:
1 parent
4d12680d57
commit
af2c748127
2 files changed
+27
-11
No files matched your search
@@ -56,6 +56,7 @@ class PdfViewerWidget(BECWidget, QWidget):
|
||||
|
||||
# Create the PDF document and view first
|
||||
self._pdf_document = QPdfDocument(self)
|
||||
self._pdf_document.statusChanged.connect(self._on_document_status_changed)
|
||||
self.pdf_view = QPdfView()
|
||||
self.pdf_view.setZoomMode(QPdfView.ZoomMode.FitToWidth)
|
||||
|
||||
@@ -328,15 +329,6 @@ class PdfViewerWidget(BECWidget, QWidget):
|
||||
|
||||
self._current_file_path = file_path
|
||||
|
||||
# Disconnect any existing signal connections
|
||||
try:
|
||||
self._pdf_document.statusChanged.disconnect(self._on_document_status_changed)
|
||||
except (TypeError, RuntimeError):
|
||||
pass
|
||||
|
||||
# Connect to statusChanged signal to handle when document is ready
|
||||
self._pdf_document.statusChanged.connect(self._on_document_status_changed)
|
||||
|
||||
# Load the document
|
||||
self._pdf_document.load(file_path)
|
||||
|
||||
@@ -544,10 +536,12 @@ class PdfViewerWidget(BECWidget, QWidget):
|
||||
|
||||
def cleanup(self):
|
||||
"""Handle widget close event to prevent segfaults."""
|
||||
if hasattr(self, "_pdf_document") and self._pdf_document:
|
||||
self._pdf_document.statusChanged.disconnect()
|
||||
# Cleanup can run twice (explicit call, then close); disconnect only once.
|
||||
if getattr(self, "_pdf_document", None) is not None:
|
||||
self._pdf_document.statusChanged.disconnect(self._on_document_status_changed)
|
||||
empty_doc = QPdfDocument(self)
|
||||
self.pdf_view.setDocument(empty_doc)
|
||||
self._pdf_document = None
|
||||
|
||||
if hasattr(self, "toolbar"):
|
||||
self.toolbar.cleanup()
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
from qtpy.QtPdf import QPdfDocument
|
||||
from qtpy.QtPdfWidgets import QPdfView
|
||||
@@ -227,6 +229,26 @@ def test_load_pdf_file(qtbot, pdf_viewer_widget: PdfViewerWidget, temp_pdf_file,
|
||||
assert widget.current_file_path == temp_pdf_file
|
||||
|
||||
|
||||
def test_load_and_cleanup_do_not_warn_about_disconnect(
|
||||
qtbot, mocked_client, temp_pdf_file, temp_pdf_file_2
|
||||
):
|
||||
"""Loading, reloading and (repeated) cleanup never disconnect a slot that is not connected."""
|
||||
with warnings.catch_warnings(record=True) as caught:
|
||||
warnings.simplefilter("always")
|
||||
widget = PdfViewerWidget(client=mocked_client)
|
||||
qtbot.addWidget(widget)
|
||||
widget.load_pdf(temp_pdf_file)
|
||||
widget.load_pdf(temp_pdf_file_2)
|
||||
qtbot.wait(100)
|
||||
widget.cleanup()
|
||||
widget.cleanup() # a repeated cleanup must not disconnect again
|
||||
unloaded = PdfViewerWidget(client=mocked_client)
|
||||
qtbot.addWidget(unloaded)
|
||||
unloaded.cleanup()
|
||||
|
||||
assert [str(w.message) for w in caught if "Failed to disconnect" in str(w.message)] == []
|
||||
|
||||
|
||||
def test_load_invalid_pdf_file(qtbot, pdf_viewer_widget: PdfViewerWidget, tmpdir):
|
||||
"""Test loading an invalid PDF file into the viewer."""
|
||||
widget = pdf_viewer_widget
|
||||
|
||||
Reference in new issue
Block a user