Skip to content

Commit 4caab38

Browse files
fix(items): prevent crashes when clicking empty legends
1 parent fed5e3c commit 4caab38

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

plotpy/items/label.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ def click_inside(self, locx: float, locy: float) -> tuple[float, float, bool, ty
828828
for item in self.plot().get_items()
829829
if self.include_item(item) and isinstance(item, CurveItem)
830830
]
831-
if line < len(items):
831+
if 0 <= line < len(items):
832832
return 1000.0, None, False, items[line]
833833
return 2.0, 1, True, None
834834

plotpy/tests/items/test_label.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Label and legend item tests."""
4+
5+
import numpy as np
6+
from guidata.qthelpers import qt_app_context
7+
8+
from plotpy.builder import make
9+
10+
11+
def test_legend_click_inside_out_of_range_row() -> None:
12+
"""Clicking outside a legend row must not index the curve list negatively."""
13+
with qt_app_context(exec_loop=False):
14+
window = make.dialog(edit=False, type="curve", size=(640, 480))
15+
plot = window.manager.get_plot()
16+
plot.add_item(make.curve(np.arange(3), np.arange(3), title="Signal"))
17+
legend = make.legend("TR")
18+
plot.add_item(legend)
19+
legend.get_text_rect()
20+
21+
result = legend.click_inside(6.0, -100.0)
22+
23+
assert result == (2.0, 1, True, None)
24+
window.close()
25+
26+
27+
def test_empty_legend_click_inside_does_not_crash() -> None:
28+
"""Clicking an empty legend must not index an empty curve list."""
29+
with qt_app_context(exec_loop=False):
30+
window = make.dialog(edit=False, type="curve", size=(640, 480))
31+
plot = window.manager.get_plot()
32+
legend = make.legend("TR")
33+
plot.add_item(legend)
34+
legend.get_text_rect()
35+
36+
result = legend.click_inside(6.0, -100.0)
37+
38+
assert result == (2.0, 1, True, None)
39+
window.close()

0 commit comments

Comments
 (0)