From 05a2d09483f44102d5462a2c2316c4fbbc814817 Mon Sep 17 00:00:00 2001 From: janezd Date: Thu, 5 Dec 2024 22:56:54 +0100 Subject: [PATCH 1/3] Report: Fix saving to PDF --- orangewidget/report/owreport.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/orangewidget/report/owreport.py b/orangewidget/report/owreport.py index 7da395f83..3db712106 100644 --- a/orangewidget/report/owreport.py +++ b/orangewidget/report/owreport.py @@ -11,7 +11,8 @@ from typing import Optional from AnyQt.QtCore import Qt, QObject, pyqtSlot, QSize -from AnyQt.QtGui import QIcon, QCursor, QStandardItemModel, QStandardItem +from AnyQt.QtGui import ( + QIcon, QCursor, QStandardItemModel, QStandardItem, QPageSize) from AnyQt.QtWidgets import ( QApplication, QDialog, QFileDialog, QTableView, QHeaderView, QMessageBox) @@ -354,7 +355,7 @@ def save_report(self): _, extension = os.path.splitext(filename) if extension == ".pdf": printer = QPrinter() - printer.setPageSize(QPrinter.A4) + printer.setPageSize(QPageSize.A4) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(filename) self._print_to_printer(printer) From d932d5fd0240b6d21e737e6ebb54d039796409e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Erjavec?= Date: Fri, 6 Dec 2024 10:29:08 +0100 Subject: [PATCH 2/3] owreport: Add tests for saving to html and pdf --- orangewidget/report/tests/test_report.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/orangewidget/report/tests/test_report.py b/orangewidget/report/tests/test_report.py index f7eab73d5..17cc0317d 100644 --- a/orangewidget/report/tests/test_report.py +++ b/orangewidget/report/tests/test_report.py @@ -79,22 +79,32 @@ def test_save_report_permission(self): rep.save_report() log.assert_called() - def test_save_report(self): + def _test_save_report(self, filter): + ext = re.search(r"\.(?P\w+)\)$", filter).group("format") rep = OWReport() widget = TstWidget() widget.create_report_html() rep.make_report(widget) - temp_dir = tempfile.mkdtemp() - temp_name = os.path.join(temp_dir, "f.report") + temp_f = tempfile.NamedTemporaryFile("w", suffix=f".{ext}", delete=False) + temp_name = temp_f.name + temp_f.close() try: with patch("AnyQt.QtWidgets.QFileDialog.getSaveFileName", - return_value=(temp_name, 'Report (*.report)')), \ + return_value=(temp_name, filter)), \ patch("AnyQt.QtWidgets.QMessageBox.exec", return_value=True): rep.save_report() finally: os.remove(temp_name) - os.rmdir(temp_dir) + + def test_save_report_pdf(self): + self._test_save_report("PDF (*.pdf)") + + def test_save_report_html(self): + self._test_save_report("HTML (*.html)") + + def test_save_report_(self): + self._test_save_report("Report (*.report)") @patch("AnyQt.QtWidgets.QFileDialog.getSaveFileName", return_value=(False, 'HTML (*.html)')) From 39b8e9a17eab5c14ec074ce6aae121f4e0fca804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Erjavec?= Date: Fri, 6 Dec 2024 10:32:59 +0100 Subject: [PATCH 3/3] owreport: Use setPageSize(QPageSize(QPageSize.A4)) --- orangewidget/report/owreport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orangewidget/report/owreport.py b/orangewidget/report/owreport.py index 3db712106..0dc4d4fad 100644 --- a/orangewidget/report/owreport.py +++ b/orangewidget/report/owreport.py @@ -355,7 +355,7 @@ def save_report(self): _, extension = os.path.splitext(filename) if extension == ".pdf": printer = QPrinter() - printer.setPageSize(QPageSize.A4) + printer.setPageSize(QPageSize(QPageSize.A4)) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(filename) self._print_to_printer(printer)