|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING, TypeVar, Union |
| 3 | +from typing import TYPE_CHECKING |
4 | 4 |
|
5 | 5 | import numpy as np |
6 | 6 | import qtpy.QtCore as QC |
7 | | -import qtpy.QtGui as QG |
8 | | -import qtpy.QtWidgets as QW |
9 | 7 | from guidata.qthelpers import exec_dialog, qt_app_context |
10 | 8 |
|
11 | | -from plotpy.interfaces.items import ICurveItemType, IItemType |
12 | | -from plotpy.panels.base import PanelWidget |
13 | | -from plotpy.plot.plotwidget import PlotDialog, PlotWindow |
14 | | -from plotpy.tests import vistools as ptv |
15 | | -from plotpy.tests.features.test_auto_curve_image import make_curve_image_legend |
16 | | -from plotpy.tools import ( |
17 | | - CommandTool, |
18 | | - EditPointTool, |
19 | | - InteractiveTool, |
20 | | - SelectPointsTool, |
21 | | - SelectPointTool, |
| 9 | +from plotpy.tests.unit.utils import ( |
| 10 | + CLICK, |
| 11 | + create_window, |
| 12 | + drag_mouse, |
| 13 | + keyboard_event, |
| 14 | + mouse_event_at_relative_plot_pos, |
22 | 15 | ) |
| 16 | +from plotpy.tools import EditPointTool, SelectPointsTool, SelectPointTool |
23 | 17 | from plotpy.tools.curve import DownSamplingTool |
24 | 18 |
|
25 | 19 | if TYPE_CHECKING: |
26 | 20 |
|
27 | 21 | from plotpy.items.curve.base import CurveItem |
28 | | - from plotpy.items.image.base import BaseImageItem |
29 | | - |
30 | | -CLICK = (QC.QEvent.Type.MouseButtonPress, QC.QEvent.Type.MouseButtonRelease) |
31 | | -ToolT = TypeVar("ToolT", bound=Union[InteractiveTool, CommandTool]) |
32 | | - |
33 | | - |
34 | | -def keyboard_event( |
35 | | - win: PlotDialog, |
36 | | - qapp: QW.QApplication, |
37 | | - key: QC.Qt.Key, |
38 | | - mod=QC.Qt.KeyboardModifier.NoModifier, |
39 | | -): |
40 | | - """Simulates a keyboard event on the plot. |
41 | | -
|
42 | | - Args: |
43 | | - win: window containing the plot |
44 | | - qapp: Main QApplication instance |
45 | | - key: Key to simulate |
46 | | - mod: Keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier. |
47 | | -
|
48 | | - Returns: |
49 | | - None |
50 | | - """ |
51 | | - plot = win.manager.get_plot() |
52 | | - canva: QW.QWidget = plot.canvas() # type: ignore |
53 | | - key_event = QG.QKeyEvent(QC.QEvent.Type.KeyPress, key, mod) |
54 | | - qapp.sendEvent(canva, key_event) |
55 | | - |
56 | | - |
57 | | -def mouse_event_at_relative_plot_pos( |
58 | | - win: PlotDialog, |
59 | | - qapp: QW.QApplication, |
60 | | - relative_xy: tuple[float, float], |
61 | | - click_types: tuple[QC.QEvent.Type, ...] = (QC.QEvent.Type.MouseButtonPress,), |
62 | | - mod=QC.Qt.KeyboardModifier.NoModifier, |
63 | | -) -> None: |
64 | | - """Simulates a click on the plot at the given relative position. |
65 | | -
|
66 | | - Args: |
67 | | - win: window containing the plot |
68 | | - qapp: Main QApplication instance |
69 | | - relative_xy: Relative position of the click on the plot |
70 | | - click_types: Different mouse button event types to send. |
71 | | - Defaults to (QC.QEvent.Type.MouseButtonPress,). |
72 | | - mod: Keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier. |
73 | | -
|
74 | | - Returns: |
75 | | - None |
76 | | - """ |
77 | | - plot = win.manager.get_plot() |
78 | | - |
79 | | - plot = win.manager.get_plot() |
80 | | - canva: QW.QWidget = plot.canvas() # type: ignore |
81 | | - size = canva.size() |
82 | | - pos_x, pos_y = ( |
83 | | - relative_xy[0] * size.width(), |
84 | | - relative_xy[1] * size.height(), |
85 | | - ) |
86 | | - # pos_x, pos_y = axes_to_canvas(plot.get_active_item(), x, y) |
87 | | - canva_pos = QC.QPointF(pos_x, pos_y).toPoint() |
88 | | - glob_pos = canva.mapToGlobal(canva_pos) |
89 | | - # QTest.mouseClick(canva, QC.Qt.MouseButton.LeftButton, mod, canva_pos) |
90 | | - |
91 | | - for type_ in click_types: |
92 | | - mouse_event_press = QG.QMouseEvent( |
93 | | - type_, |
94 | | - canva_pos, |
95 | | - glob_pos, |
96 | | - QC.Qt.MouseButton.LeftButton, |
97 | | - QC.Qt.MouseButton.LeftButton, |
98 | | - mod, |
99 | | - ) |
100 | | - qapp.sendEvent(canva, mouse_event_press) |
101 | | - |
102 | | - |
103 | | -def drag_mouse( |
104 | | - win: PlotWindow, |
105 | | - qapp: QW.QApplication, |
106 | | - x_path: np.ndarray, |
107 | | - y_path: np.ndarray, |
108 | | - mod=QC.Qt.KeyboardModifier.NoModifier, |
109 | | - click=True, |
110 | | -) -> None: |
111 | | - x0, y0 = x_path[0], y_path[0] |
112 | | - xn, yn = x_path[-1], y_path[-1] |
113 | | - press = (QC.QEvent.Type.MouseButtonPress,) |
114 | | - move = (QC.QEvent.Type.MouseMove,) |
115 | | - release = (QC.QEvent.Type.MouseButtonRelease,) |
116 | | - |
117 | | - if click: |
118 | | - mouse_event_at_relative_plot_pos(win, qapp, (x0, y0), press, mod) |
119 | | - for x, y in zip(x_path, y_path): |
120 | | - mouse_event_at_relative_plot_pos(win, qapp, (x, y), move, mod) |
121 | | - if click: |
122 | | - mouse_event_at_relative_plot_pos(win, qapp, (xn, yn), release, mod) |
123 | | - |
124 | | - |
125 | | -def create_window( |
126 | | - tool_class: type[ToolT], |
127 | | - active_item_type: type[IItemType] = ICurveItemType, |
128 | | - panels: list[type[PanelWidget]] | None = None, |
129 | | -) -> tuple[PlotWindow, ToolT]: |
130 | | - |
131 | | - items: list[CurveItem | BaseImageItem] = make_curve_image_legend() |
132 | | - win = ptv.show_items(items, wintitle="Unit test plot", auto_tools=False) |
133 | | - plot = win.manager.get_plot() |
134 | | - for item in win.manager.get_plot().get_items()[::-1]: |
135 | | - plot.set_active_item(item) |
136 | | - last_active_item = plot.get_last_active_item(active_item_type) |
137 | | - plot.set_active_item(last_active_item) |
138 | | - |
139 | | - if panels is not None: |
140 | | - for panel in panels: |
141 | | - win.manager.add_panel(panel()) |
142 | | - |
143 | | - tool = win.manager.add_tool(tool_class) |
144 | | - tool.activate() |
145 | | - return win, tool |
146 | 22 |
|
147 | 23 |
|
148 | 24 | def test_free_select_point_tool(): |
|
0 commit comments