Skip to content

Commit 86b10a9

Browse files
committed
fix(plot_grid): honor minor pen arguments in QwtPlotGrid.make
QwtPlotGrid.make raised a TypeError when only some of the minor pen arguments (mincolor, minwidth, minstyle) were given, e.g. QwtPlotGrid.make(mincolor="red"): the default minor width and style were chosen by testing the major width and style arguments, which are never None at that point, instead of the minor ones, so None was passed to QPen. - Test minwidth and minstyle against None, as documented, and convert the minor width to float like the major one. - Regression tests: every combination of minor pen arguments, and major pen unchanged; calls which already worked keep the same pens. - Changelog entry. Refs #115 Assisted-by: Claude Opus 5.5
1 parent 95457ae commit 86b10a9

3 files changed

Lines changed: 61 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Fixed [Issue #113](https://github.com/PlotPyStack/PythonQwt/issues/113): after replacing the scale draw of an axis (`QwtPlot.setAxisScaleDraw`), the axis ticks could be misaligned with the grid lines and plot items, until the next scale division change (e.g. after panning or zooming): a replot did not fix it. The new scale draw was laid out before getting the position and length of the previous one, so that the axis reserved space for its end labels that the canvas did not take into account. The new scale draw now takes the geometry of the previous one, and the axis stays aligned with the canvas
1212
- Fixed `QwtGraphic.toPixmap(size)`, `QwtGraphic.toImage()` and `QwtGraphic.toImage(size)`, which raised a `TypeError` (possibly aborting the process, as the painter was still active on the destroyed pixmap or image) instead of rendering the graphic: they now render it like `QwtGraphic.toPixmap()` ([Issue #115](https://github.com/PlotPyStack/PythonQwt/issues/115))
1313
- Fixed `QwtPlotMarker.legendIcon` for markers with a symbol, which raised an `AssertionError` or a `TypeError`: such markers can now be shown in a legend ([Issue #115](https://github.com/PlotPyStack/PythonQwt/issues/115))
14+
- Fixed `QwtPlotGrid.make`, which raised a `TypeError` when only some of the minor pen arguments (`mincolor`, `minwidth`, `minstyle`) were given: each of them is now honored, the missing ones taking their documented default value ([Issue #115](https://github.com/PlotPyStack/PythonQwt/issues/115))
1415

1516
## Version 0.16.3
1617

‎qwt/plot_grid.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def make(
106106
item.setPen(QPen(color, width, style))
107107
if mincolor is not None or minwidth is not None or minstyle is not None:
108108
mincolor = qcolor_from_str(mincolor, Qt.gray)
109-
minwidth = 1.0 if width is None else minwidth
110-
minstyle = Qt.DotLine if style is None else minstyle
109+
minwidth = 1.0 if minwidth is None else float(minwidth)
110+
minstyle = Qt.DotLine if minstyle is None else minstyle
111111
item.setMinorPen(QPen(mincolor, minwidth, minstyle))
112112
if enablemajor is not None:
113113
if isinstance(enablemajor, tuple) and len(enablemajor) == 2:

‎qwt/tests/test_issue115_porting_bugs.py‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
from qtpy import QtGui as QG
1616
from qtpy import QtWidgets as QW
1717

18-
from qwt import QwtLegend, QwtPlot, QwtPlotItem, QwtPlotMarker, QwtSymbol
18+
from qwt import (
19+
QwtLegend,
20+
QwtPlot,
21+
QwtPlotGrid,
22+
QwtPlotItem,
23+
QwtPlotMarker,
24+
QwtSymbol,
25+
)
1926
from qwt.graphic import QwtGraphic
2027

2128
RED = QG.QColor(QC.Qt.red)
@@ -140,6 +147,55 @@ def test_marker_with_symbol_in_legend():
140147
del app
141148

142149

150+
# ---------------------------------------------------------------------------
151+
# QwtPlotGrid.make
152+
# ---------------------------------------------------------------------------
153+
154+
155+
def _pen_attrs(pen):
156+
return pen.color().name(), pen.widthF(), pen.style()
157+
158+
159+
@pytest.mark.parametrize(
160+
"kwargs, expected",
161+
(
162+
({"mincolor": "red"}, ("#ff0000", 1.0, QC.Qt.DotLine)),
163+
({"minwidth": 2}, ("#a0a0a4", 2.0, QC.Qt.DotLine)),
164+
({"minstyle": QC.Qt.DashLine}, ("#a0a0a4", 1.0, QC.Qt.DashLine)),
165+
({"mincolor": "red", "minwidth": 2}, ("#ff0000", 2.0, QC.Qt.DotLine)),
166+
(
167+
{"mincolor": "red", "minstyle": QC.Qt.DashLine},
168+
("#ff0000", 1.0, QC.Qt.DashLine),
169+
),
170+
(
171+
{"minwidth": 3.0, "minstyle": QC.Qt.SolidLine},
172+
("#a0a0a4", 3.0, QC.Qt.SolidLine),
173+
),
174+
(
175+
{"mincolor": "blue", "minwidth": 0, "minstyle": QC.Qt.DashDotLine},
176+
("#0000ff", 0.0, QC.Qt.DashDotLine),
177+
),
178+
),
179+
)
180+
def test_grid_make_minor_pen(kwargs, expected):
181+
"""QwtPlotGrid.make honors each minor pen argument, with documented defaults"""
182+
app = _ensure_app() # keep a reference alive for the duration of the test
183+
grid = QwtPlotGrid.make(color="green", width=4, style=QC.Qt.DashLine, **kwargs)
184+
assert _pen_attrs(grid.minorPen()) == expected
185+
assert _pen_attrs(grid.majorPen()) == ("#00ff00", 4.0, QC.Qt.DashLine)
186+
del app
187+
188+
189+
def test_grid_make_without_minor_pen():
190+
"""Without minor pen arguments, both pens are built from the major ones"""
191+
app = _ensure_app() # keep a reference alive for the duration of the test
192+
grid = QwtPlotGrid.make(color="green", width=0, style=QC.Qt.SolidLine)
193+
expected = ("#00ff00", 0.0, QC.Qt.SolidLine)
194+
assert _pen_attrs(grid.majorPen()) == expected
195+
assert _pen_attrs(grid.minorPen()) == expected
196+
del app
197+
198+
143199
if __name__ == "__main__":
144200
for name in ("toPixmap", "toImage"):
145201
test_graphic_conversion_default_size(name)
@@ -148,4 +204,5 @@ def test_marker_with_symbol_in_legend():
148204
for size in (QC.QSize(8, 8), QC.QSizeF(8.0, 8.0)):
149205
test_marker_legend_icon_with_symbol(size)
150206
test_marker_with_symbol_in_legend()
207+
test_grid_make_without_minor_pen()
151208
print("OK")

0 commit comments

Comments
 (0)