|
6 | 6 | import qtpy.QtCore as QC |
7 | 7 | from guidata.qthelpers import exec_dialog, qt_app_context |
8 | 8 |
|
| 9 | +from plotpy.coords import canvas_to_axes |
| 10 | +from plotpy.interfaces.items import ICurveItemType |
9 | 11 | from plotpy.tests.unit.utils import ( |
10 | 12 | CLICK, |
11 | 13 | create_window, |
|
20 | 22 | from plotpy.items.curve.base import CurveItem |
21 | 23 |
|
22 | 24 |
|
| 25 | +from plotpy.items import DataInfoLabel, XRangeSelection |
| 26 | +from plotpy.tools import ( |
| 27 | + CurveStatsTool, |
| 28 | +) |
| 29 | +from plotpy.tools.selection import SelectTool |
| 30 | + |
| 31 | +# guitest: show |
| 32 | + |
| 33 | + |
| 34 | +def test_curve_stat_tool(): |
| 35 | + with qt_app_context(exec_loop=False) as qapp: |
| 36 | + win, tool = create_window(CurveStatsTool) |
| 37 | + win.show() |
| 38 | + plot = win.manager.get_plot() |
| 39 | + |
| 40 | + original_stat_items = [ |
| 41 | + item |
| 42 | + for item in plot.get_items() |
| 43 | + if isinstance(item, (DataInfoLabel, XRangeSelection)) |
| 44 | + ] |
| 45 | + |
| 46 | + drag_mouse(win, qapp, np.array([0.4, 0.6]), np.array([0.5, 0.5])) |
| 47 | + |
| 48 | + new_stat_items = [ |
| 49 | + item |
| 50 | + for item in plot.get_items() |
| 51 | + if isinstance(item, (DataInfoLabel, XRangeSelection)) |
| 52 | + ] |
| 53 | + |
| 54 | + assert len(new_stat_items) == len(original_stat_items) + 2 |
| 55 | + |
| 56 | + # There should be one more new DataInfoLabel and one new XRangeSelection |
| 57 | + # compared to the original plot items (before mouse drag) |
| 58 | + |
| 59 | + win.manager.add_tool(SelectTool).activate() |
| 60 | + plot.unselect_all() |
| 61 | + |
| 62 | + mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK) |
| 63 | + selected_items = plot.get_selected_items() |
| 64 | + assert len(selected_items) == 1 and isinstance( |
| 65 | + selected_items[0], XRangeSelection |
| 66 | + ) |
| 67 | + range_item: XRangeSelection = selected_items[0] |
| 68 | + |
| 69 | + x00, x01, y0 = range_item.get_handles_pos() |
| 70 | + drag_mouse(win, qapp, np.linspace(0.5, 0.9, 100), np.full(100, 0.5)) |
| 71 | + x10, x11, y1 = range_item.get_handles_pos() |
| 72 | + |
| 73 | + assert x00 < x10 and x01 < x11 and y0 == y1 |
| 74 | + |
| 75 | + curve_item: CurveItem = plot.get_items(item_type=ICurveItemType)[0] # type: ignore |
| 76 | + current_pos = canvas_to_axes(curve_item, QC.QPointF(x10, y1)) |
| 77 | + x_min, _ = plot.get_axis_limits(curve_item.xAxis()) |
| 78 | + |
| 79 | + assert current_pos is not None |
| 80 | + range_item.move_shape(current_pos, (x_min, 0)) |
| 81 | + assert np.isclose(range_item.get_range()[0], x_min) |
| 82 | + |
| 83 | + exec_dialog(win) |
| 84 | + |
| 85 | + |
23 | 86 | def test_free_select_point_tool(): |
24 | 87 | """Test the free select point tool.""" |
25 | 88 | with qt_app_context(exec_loop=False) as qapp: |
@@ -62,14 +125,14 @@ def test_select_points_tool(): |
62 | 125 | """Test the select points tool constrained to a CurveItem.""" |
63 | 126 | with qt_app_context(exec_loop=False) as qapp: |
64 | 127 | win, tool = create_window(tool_class=SelectPointsTool) |
65 | | - win.show() |
66 | 128 | mod = QC.Qt.KeyboardModifier.ControlModifier |
67 | 129 |
|
68 | 130 | mouse_event_at_relative_plot_pos(win, qapp, (0.4, 0.5), CLICK, mod) |
69 | 131 | assert len(tool.get_coordinates() or ()) == 1 |
70 | 132 |
|
71 | 133 | mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK, mod) |
72 | 134 | mouse_event_at_relative_plot_pos(win, qapp, (0.8, 0.8), CLICK, mod) |
| 135 | + print(tool.get_coordinates()) |
73 | 136 | assert len(tool.get_coordinates() or ()) == 3 |
74 | 137 |
|
75 | 138 | mouse_event_at_relative_plot_pos(win, qapp, (0.8, 0.8), CLICK, mod) |
@@ -160,7 +223,8 @@ def test_edit_point_tool(): |
160 | 223 |
|
161 | 224 |
|
162 | 225 | if __name__ == "__main__": |
163 | | - test_free_select_point_tool() |
164 | | - test_contrained_select_point_tool() |
165 | | - test_select_points_tool() |
166 | | - test_edit_point_tool() |
| 226 | + test_curve_stat_tool() |
| 227 | + # test_free_select_point_tool() |
| 228 | + # test_contrained_select_point_tool() |
| 229 | + # test_select_points_tool() |
| 230 | + # test_edit_point_tool() |
0 commit comments