|
14 | 14 | from plotpy.tools import MultiLineTool, PolygonTool |
15 | 15 |
|
16 | 16 |
|
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.""" |
20 | 21 | with qt_app_context(exec_loop=False): |
21 | | - win, tool = create_window(PolygonTool) |
| 22 | + win, tool = create_window(toolklass) |
22 | 23 |
|
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 |
26 | 33 |
|
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 |
29 | 36 |
|
30 | | - # Delete last point |
31 | 37 | keyboard_event(win, QC.Qt.Key.Key_Backspace) |
32 | 38 |
|
33 | | - points_count, _ = tool.shape.get_points().shape |
| 39 | + points_count, _ = tool.handler.shape.get_points().shape |
34 | 40 |
|
35 | | - assert points_count == (len(corners) - 1) |
| 41 | + assert points_count == (len(points) - 1) |
36 | 42 |
|
37 | 43 | # 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]) |
42 | 45 |
|
43 | | - keyboard_event(win, QC.Qt.Key.Key_Enter) |
| 46 | + points_count, _ = tool.handler.shape.get_points().shape |
| 47 | + assert points_count == len(points) |
44 | 48 |
|
45 | | - assert tool.shape is None |
| 49 | + keyboard_event(win, QC.Qt.Key.Key_Space) |
46 | 50 |
|
47 | 51 | exec_dialog(win) |
48 | 52 |
|
49 | 53 |
|
| 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 | + |
50 | 60 | def test_multiline_tool(): |
51 | 61 | """Test the multi line tool.""" |
52 | 62 | n = 100 |
53 | 63 | t = np.linspace(0, np.pi * 10, n) |
54 | | - |
55 | | - # Create x and y arrays |
56 | 64 | x_arr = t * np.cos(t) / n + 0.5 |
57 | 65 | 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) |
87 | 67 |
|
88 | 68 |
|
89 | 69 | if __name__ == "__main__": |
90 | | - test_free_form_tool() |
| 70 | + test_polygon_tool() |
91 | 71 | test_multiline_tool() |
0 commit comments