Skip to content

Commit 04537c8

Browse files
committed
Fix unit tests related to polygon/multiline tools
1 parent 5629287 commit 04537c8

File tree

3 files changed

+36
-53
lines changed

3 files changed

+36
-53
lines changed

plotpy/tests/unit/test_annotation_tools.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
AnnotatedEllipseTool,
1515
AnnotatedObliqueRectangleTool,
1616
AnnotatedPointTool,
17+
AnnotatedPolygonTool,
1718
AnnotatedRectangleTool,
1819
AnnotatedSegmentTool,
1920
AverageCrossSectionTool,
@@ -44,6 +45,7 @@
4445
AnnotatedPointTool,
4546
AnnotatedRectangleTool,
4647
AnnotatedSegmentTool,
48+
AnnotatedPolygonTool,
4749
AverageCrossSectionTool,
4850
CrossSectionTool,
4951
SnapshotTool,

plotpy/tests/unit/test_multiline_tools.py

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,78 +14,58 @@
1414
from plotpy.tools import MultiLineTool, PolygonTool
1515

1616

17-
def test_free_form_tool():
18-
"""Test the free form tool."""
19-
corners = np.array(((0.1, 0.1), (0.1, 0.8), (0.8, 0.8), (0.8, 0.1)))
17+
def __generic_polyline_tool_test(
18+
toolklass: type[MultiLineTool] | type[PolygonTool], points: np.ndarray
19+
) -> None:
20+
"""Generic polyline tool test."""
2021
with qt_app_context(exec_loop=False):
21-
win, tool = create_window(PolygonTool)
22+
win, tool = create_window(toolklass)
2223

23-
# drag_mouse(win, x_path, y_path)
24-
for x, y in corners:
25-
mouse_event_at_relative_plot_pos(win, (x, y), CLICK)
24+
x0, y0 = None, None
25+
for x, y in points:
26+
if x0 is not None:
27+
x_path = np.linspace(x0, x, 10)
28+
y_path = np.linspace(y0, y, 10)
29+
drag_mouse(win, x_path, y_path)
30+
else:
31+
mouse_event_at_relative_plot_pos(win, (x, y), CLICK)
32+
x0, y0 = x, y
2633

27-
assert tool.shape is not None
28-
assert tool.shape.get_points().shape == corners.shape
34+
assert tool.handler.shape is not None
35+
assert tool.handler.shape.get_points().shape == points.shape
2936

30-
# Delete last point
3137
keyboard_event(win, QC.Qt.Key.Key_Backspace)
3238

33-
points_count, _ = tool.shape.get_points().shape
39+
points_count, _ = tool.handler.shape.get_points().shape
3440

35-
assert points_count == (len(corners) - 1)
41+
assert points_count == (len(points) - 1)
3642

3743
# add last point by dragging mouse
38-
drag_mouse(win, corners[-2:, 0], corners[-2:, 1])
39-
40-
points_count, _ = tool.shape.get_points().shape
41-
assert points_count == len(corners)
44+
drag_mouse(win, points[-2:, 0], points[-2:, 1])
4245

43-
keyboard_event(win, QC.Qt.Key.Key_Enter)
46+
points_count, _ = tool.handler.shape.get_points().shape
47+
assert points_count == len(points)
4448

45-
assert tool.shape is None
49+
keyboard_event(win, QC.Qt.Key.Key_Space)
4650

4751
exec_dialog(win)
4852

4953

54+
def test_polygon_tool():
55+
"""Test the polygon tool."""
56+
corners = np.array(((0.1, 0.1), (0.1, 0.8), (0.8, 0.8), (0.8, 0.1)))
57+
__generic_polyline_tool_test(PolygonTool, corners)
58+
59+
5060
def test_multiline_tool():
5161
"""Test the multi line tool."""
5262
n = 100
5363
t = np.linspace(0, np.pi * 10, n)
54-
55-
# Create x and y arrays
5664
x_arr = t * np.cos(t) / n + 0.5
5765
y_arr = t * np.sin(t) / n + 0.5
58-
59-
with qt_app_context(exec_loop=False):
60-
win, tool = create_window(MultiLineTool)
61-
62-
# drag_mouse(win, x_path, y_path)
63-
for x, y in zip(x_arr, y_arr):
64-
mouse_event_at_relative_plot_pos(win, (x, y), CLICK)
65-
66-
assert tool.shape is not None
67-
assert tool.shape.get_points().shape == np.array([x_arr, y_arr]).T.shape
68-
69-
# Delete last point
70-
keyboard_event(win, QC.Qt.Key.Key_Backspace)
71-
72-
points_count, _ = tool.shape.get_points().shape
73-
74-
assert points_count == (n - 1)
75-
76-
# add last point by dragging mouse
77-
drag_mouse(win, x_arr[-2:], y_arr[-2:])
78-
79-
points_count, _ = tool.shape.get_points().shape
80-
assert points_count == n
81-
82-
keyboard_event(win, QC.Qt.Key.Key_Enter)
83-
84-
assert tool.shape is None
85-
86-
exec_dialog(win)
66+
__generic_polyline_tool_test(MultiLineTool, np.array([x_arr, y_arr]).T)
8767

8868

8969
if __name__ == "__main__":
90-
test_free_form_tool()
70+
test_polygon_tool()
9171
test_multiline_tool()

plotpy/tools/shape.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(
7575
tip=tip,
7676
switch_to_default_tool=switch_to_default_tool,
7777
)
78+
self.handler: MultilineSelectionHandler | None = None
7879
self.switch_to_default_tool = switch_to_default_tool
7980
self.setup_shape_cb = setup_shape_cb
8081
self.handle_final_shape_cb = handle_final_shape_cb
@@ -127,12 +128,12 @@ def setup_filter(self, baseplot: BasePlot) -> StatefulEventFilter:
127128
"""
128129
filter = baseplot.filter
129130
start_state = filter.new_state()
130-
handler = MultilineSelectionHandler(
131+
self.handler = MultilineSelectionHandler(
131132
filter, QC.Qt.LeftButton, start_state=start_state, closed=self.CLOSED
132133
)
133-
handler.SIG_END_POLYLINE.connect(self.end_polyline)
134+
self.handler.SIG_END_POLYLINE.connect(self.end_polyline)
134135
shape = self.get_shape()
135-
handler.set_shape(shape, self.setup_shape)
136+
self.handler.set_shape(shape, self.setup_shape)
136137
return setup_standard_tool_filter(filter, start_state)
137138

138139
def end_polyline(self, filter: StatefulEventFilter, points: np.ndarray) -> None:

0 commit comments

Comments
 (0)