|
| 1 | +# guitest: show |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, TypeVar |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import qtpy.QtCore as QC |
| 7 | +import qtpy.QtGui as QG |
| 8 | +import qtpy.QtWidgets as QW |
| 9 | +from guidata.qthelpers import exec_dialog, qt_app_context |
| 10 | +from numpy import linspace, sin |
| 11 | + |
| 12 | +from plotpy.builder import make |
| 13 | +from plotpy.plot.plotwidget import PlotDialog, PlotWindow |
| 14 | +from plotpy.tests import vistools as ptv |
| 15 | +from plotpy.tools.base import InteractiveTool |
| 16 | +from plotpy.tools.curve import EditPointTool, SelectPointsTool, SelectPointTool |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from plotpy.items.curve.base import CurveItem |
| 20 | + |
| 21 | +CLICK = (QC.QEvent.Type.MouseButtonPress, QC.QEvent.Type.MouseButtonRelease) |
| 22 | +ToolT = TypeVar("ToolT", bound=InteractiveTool) |
| 23 | + |
| 24 | + |
| 25 | +def mouse_event_at_relative_plot_pos( |
| 26 | + win: PlotDialog, |
| 27 | + qapp: QW.QApplication, |
| 28 | + relative_xy: tuple[float, float], |
| 29 | + click_types: tuple[QC.QEvent.Type, ...] = (QC.QEvent.Type.MouseButtonPress,), |
| 30 | + mod=QC.Qt.KeyboardModifier.NoModifier, |
| 31 | +) -> None: |
| 32 | + """Simulates a click on the plot at the given relative position. |
| 33 | +
|
| 34 | + Args: |
| 35 | + win: window containing the plot |
| 36 | + qapp: Main QApplication instance |
| 37 | + relative_xy: Relative position of the click on the plot |
| 38 | + click_types: Different mouse button event types to send. |
| 39 | + Defaults to (QC.QEvent.Type.MouseButtonPress,). |
| 40 | + mod: Keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + None |
| 44 | + """ |
| 45 | + plot = win.manager.get_plot() |
| 46 | + |
| 47 | + plot = win.manager.get_plot() |
| 48 | + canva: QW.QWidget = plot.canvas() # type: ignore |
| 49 | + size = canva.size() |
| 50 | + pos_x, pos_y = ( |
| 51 | + relative_xy[0] * size.width(), |
| 52 | + size.height() - relative_xy[1] * size.height(), |
| 53 | + ) |
| 54 | + # pos_x, pos_y = axes_to_canvas(plot.get_active_item(), x, y) |
| 55 | + canva_pos = QC.QPointF(pos_x, pos_y).toPoint() |
| 56 | + glob_pos = canva.mapToGlobal(canva_pos) |
| 57 | + # QTest.mouseClick(canva, QC.Qt.MouseButton.LeftButton, mod, canva_pos) |
| 58 | + |
| 59 | + for type_ in click_types: |
| 60 | + mouse_event_press = QG.QMouseEvent( |
| 61 | + type_, |
| 62 | + canva_pos, |
| 63 | + glob_pos, |
| 64 | + QC.Qt.MouseButton.LeftButton, |
| 65 | + QC.Qt.MouseButton.LeftButton, |
| 66 | + mod, |
| 67 | + ) |
| 68 | + qapp.sendEvent(canva, mouse_event_press) |
| 69 | + |
| 70 | + |
| 71 | +def drag_mouse( |
| 72 | + win: PlotDialog, |
| 73 | + qapp: QW.QApplication, |
| 74 | + x_path: np.ndarray, |
| 75 | + y_path: np.ndarray, |
| 76 | + mod=QC.Qt.KeyboardModifier.NoModifier, |
| 77 | +) -> None: |
| 78 | + x0, y0 = x_path[0], y_path[0] |
| 79 | + xn, yn = x_path[-1], y_path[-1] |
| 80 | + press = (QC.QEvent.Type.MouseButtonPress,) |
| 81 | + move = (QC.QEvent.Type.MouseMove,) |
| 82 | + release = (QC.QEvent.Type.MouseButtonRelease,) |
| 83 | + |
| 84 | + mouse_event_at_relative_plot_pos(win, qapp, (x0, y0), press, mod) |
| 85 | + for x, y in zip(x_path, y_path): |
| 86 | + mouse_event_at_relative_plot_pos(win, qapp, (x, y), move, mod) |
| 87 | + mouse_event_at_relative_plot_pos(win, qapp, (xn, yn), release, mod) |
| 88 | + |
| 89 | + |
| 90 | +def create_window(tool_class: type[ToolT]) -> tuple[PlotWindow, ToolT]: |
| 91 | + n = 100 |
| 92 | + x = linspace( |
| 93 | + 0, |
| 94 | + n, |
| 95 | + ) |
| 96 | + y = (sin(sin(sin(x / (n / 10)))) - 1) * -n |
| 97 | + curve = make.curve(x, y, color="b") |
| 98 | + win = ptv.show_items( |
| 99 | + [curve], wintitle="Unit tests for Point tools", auto_tools=False |
| 100 | + ) |
| 101 | + plot = win.manager.get_plot() |
| 102 | + plot.set_active_item(curve) # type: ignore |
| 103 | + |
| 104 | + tool = win.manager.add_tool(tool_class) |
| 105 | + tool.activate() |
| 106 | + return win, tool |
| 107 | + |
| 108 | + |
| 109 | +def test_select_point_tool_1(): |
| 110 | + with qt_app_context(exec_loop=False) as qapp: |
| 111 | + win, tool = create_window(SelectPointTool) |
| 112 | + |
| 113 | + mouse_event_at_relative_plot_pos( |
| 114 | + win, |
| 115 | + qapp, |
| 116 | + (0.5, 0.5), |
| 117 | + CLICK, |
| 118 | + ) |
| 119 | + assert tool.get_coordinates() is not None |
| 120 | + exec_dialog(win) |
| 121 | + |
| 122 | + |
| 123 | +def test_select_point_tool_2(): |
| 124 | + with qt_app_context(exec_loop=False) as qapp: |
| 125 | + win, tool = create_window(SelectPointTool) |
| 126 | + tool.on_active_item = True |
| 127 | + mouse_event_at_relative_plot_pos( |
| 128 | + win, |
| 129 | + qapp, |
| 130 | + (0.5, 0.5), |
| 131 | + CLICK, |
| 132 | + ) |
| 133 | + assert tool.get_coordinates() is not None |
| 134 | + exec_dialog(win) |
| 135 | + |
| 136 | + |
| 137 | +def test_select_points_tool(): |
| 138 | + with qt_app_context(exec_loop=False) as qapp: |
| 139 | + win, tool = create_window(tool_class=SelectPointsTool) |
| 140 | + mod = QC.Qt.KeyboardModifier.ControlModifier |
| 141 | + |
| 142 | + mouse_event_at_relative_plot_pos(win, qapp, (0.4, 0.5), CLICK, mod) |
| 143 | + assert len(tool.get_coordinates() or ()) == 1 |
| 144 | + |
| 145 | + mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK, mod) |
| 146 | + mouse_event_at_relative_plot_pos(win, qapp, (0.6, 0.5), CLICK, mod) |
| 147 | + assert len(tool.get_coordinates() or ()) == 3 |
| 148 | + |
| 149 | + mouse_event_at_relative_plot_pos(win, qapp, (0.6, 0.5), CLICK, mod) |
| 150 | + assert len(tool.get_coordinates() or ()) == 2 |
| 151 | + |
| 152 | + mouse_event_at_relative_plot_pos(win, qapp, (0.7, 0.5), CLICK, mod) |
| 153 | + assert len(tool.get_coordinates() or ()) == 3 |
| 154 | + |
| 155 | + mouse_event_at_relative_plot_pos(win, qapp, (0.1, 0.1), CLICK) |
| 156 | + assert len(tool.get_coordinates() or ()) == 1 |
| 157 | + |
| 158 | + exec_dialog(win) |
| 159 | + |
| 160 | + |
| 161 | +def test_edit_point_tool(): |
| 162 | + with qt_app_context(exec_loop=False) as qapp: |
| 163 | + win, tool = create_window(EditPointTool) |
| 164 | + assert tool is not None |
| 165 | + |
| 166 | + n = 100 |
| 167 | + min_v, max_v = 0, 1 |
| 168 | + x_path = np.full(n, min_v) |
| 169 | + y_path = np.linspace(max_v, min_v, n) |
| 170 | + drag_mouse(win, qapp, x_path, y_path) |
| 171 | + |
| 172 | + x_path = np.full(n, max_v) |
| 173 | + |
| 174 | + drag_mouse(win, qapp, x_path, y_path) |
| 175 | + |
| 176 | + curve_item: CurveItem = win.manager.get_plot().get_active_item() # type: ignore |
| 177 | + curve_changes = tool.get_changes()[curve_item] |
| 178 | + for i, (x, y) in curve_changes.items(): |
| 179 | + x_arr, y_arr = curve_item.get_data() |
| 180 | + assert x == x_arr[i] |
| 181 | + assert y == y_arr[i] |
| 182 | + exec_dialog(win) |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + test_select_point_tool_1() |
| 187 | + test_select_point_tool_2() |
| 188 | + test_select_points_tool() |
| 189 | + test_edit_point_tool() |
0 commit comments