Skip to content

Commit 36285dc

Browse files
committed
fix(plot_renderer): list image formats properly in the export dialog
The image filter of the file dialog shown by QwtPlotRenderer.exportTo read "Images (*.b'bmp' *.b'png' ...)": the image format names returned by QImageWriter.supportedImageFormats() are QByteArray objects, which were converted with str() instead of being decoded. - Decode the format names (bytes(fmt).decode(), supported by PyQt5, PyQt6 and PySide6). - Regression test: file dialog replaced by a stub capturing the filters, image filter equal to the decoded format names. - Changelog entry. The export itself (QwtPlotRenderer.renderDocument) is not affected: PNG, SVG and PDF exports already work with the three Qt bindings. Refs #115 Assisted-by: Claude Opus 5.5
1 parent aa1180d commit 36285dc

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Fixed the `QwtPainterCommand` copy constructor (`QwtPainterCommand(other)`), which raised an `AttributeError` for state commands and a `TypeError` (Qt objects cannot be pickled) for pixmap and image commands: commands of every type can now be copied, the copy being independent of the original ([Issue #115](https://github.com/PlotPyStack/PythonQwt/issues/115))
1919
- Fixed `QwtDynGridLayout` (used by `QwtLegend`), whose `numRows()` returned a float (e.g. `2.5` for 3 items on 2 columns) and which raised a `TypeError` when laying out its items with vertical expansion (`setExpandingDirections(Qt.Vertical)`) ([Issue #115](https://github.com/PlotPyStack/PythonQwt/issues/115))
2020
- Fixed canvas style sheets with rounded borders (e.g. `plot.canvas().setStyleSheet("border: 2px solid black; border-radius: 8px;")`), which raised a `TypeError` on each style change, resize and paint event (aborting the process with PyQt's default exception hook): the rounded border of the style sheet is now taken into account to clip the plot items and to fill the corners with the background of the parent widget, as in Qwt ([Issue #115](https://github.com/PlotPyStack/PythonQwt/issues/115))
21+
- Fixed the image formats listed by the file dialog of `QwtPlotRenderer.exportTo`, which read `*.b'bmp' *.b'png' ...` instead of `*.bmp *.png ...` ([Issue #115](https://github.com/PlotPyStack/PythonQwt/issues/115))
2122

2223
## Version 0.16.3
2324

‎qwt/plot_renderer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ def exportTo(self, plot, documentname, sizeMM=None, resolution=85):
722722
for idx, fmt in enumerate(imageFormats):
723723
if idx > 0:
724724
imageFilter += " "
725-
imageFilter += "*." + str(fmt)
725+
imageFilter += "*." + bytes(fmt).decode()
726726
imageFilter += ")"
727727
filter_ += [imageFilter]
728728
filename, _s = getsavefilename(

‎qwt/tests/test_issue115_porting_bugs.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from qtpy import QtGui as QG
2222
from qtpy import QtWidgets as QW
2323

24+
import qwt.plot_renderer
2425
from qwt import (
2526
QwtLegend,
2627
QwtLinearScaleEngine,
@@ -635,6 +636,33 @@ def test_canvas_rounded_style_sheet(style_sheet, backing_store):
635636
del app
636637

637638

639+
# ---------------------------------------------------------------------------
640+
# QwtPlotRenderer.exportTo
641+
# ---------------------------------------------------------------------------
642+
643+
644+
def test_renderer_export_to_image_filter(monkeypatch):
645+
"""The file dialog of exportTo lists the image formats properly"""
646+
app = _ensure_app() # keep a reference alive for the duration of the test
647+
captured = []
648+
649+
def fake_getsavefilename(*args, **kwargs):
650+
captured.append(args[3]) # filters
651+
return "", "" # dialog canceled
652+
653+
monkeypatch.setattr(qwt.plot_renderer, "getsavefilename", fake_getsavefilename)
654+
plot = QwtPlot()
655+
assert QwtPlotRenderer().exportTo(plot, "plot") is False
656+
filters = captured[0].split(";;")
657+
formats = QG.QImageWriter.supportedImageFormats()
658+
expected = " ".join("*." + bytes(fmt).decode() for fmt in formats)
659+
assert filters[-1] == f"Images ({expected})"
660+
assert "*.png" in filters[-1].split()
661+
assert "b'" not in captured[0]
662+
plot.close()
663+
del app
664+
665+
638666
if __name__ == "__main__":
639667
for name in ("toPixmap", "toImage"):
640668
test_graphic_conversion_default_size(name)

0 commit comments

Comments
 (0)