Skip to content

Commit

Permalink
Merge pull request #281 from janezd/report-to-pdf
Browse files Browse the repository at this point in the history
Report: Fix saving to PDF
  • Loading branch information
janezd authored Dec 6, 2024
2 parents 05bbef6 + 39b8e9a commit c28fde3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
5 changes: 3 additions & 2 deletions orangewidget/report/owreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(QPageSize.A4))
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(filename)
self._print_to_printer(printer)
Expand Down
20 changes: 15 additions & 5 deletions orangewidget/report/tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<format>\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)'))
Expand Down

0 comments on commit c28fde3

Please sign in to comment.