Skip to content

Commit 2fbdf78

Browse files
committed
new unit test utils functions and options
1 parent 5047fd2 commit 2fbdf78

1 file changed

Lines changed: 85 additions & 5 deletions

File tree

plotpy/tests/unit/utils.py

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,82 @@ def keyboard_event(
7373
qapp.sendEvent(canva, key_event)
7474

7575

76+
def undo(qapp: QW.QApplication, win: PlotWindow):
77+
keyboard_event(
78+
win, qapp, QC.Qt.Key.Key_Z, mod=QC.Qt.KeyboardModifier.ControlModifier
79+
)
80+
81+
82+
def redo(qapp: QW.QApplication, win: PlotWindow):
83+
keyboard_event(
84+
win, qapp, QC.Qt.Key.Key_Y, mod=QC.Qt.KeyboardModifier.ControlModifier
85+
)
86+
87+
88+
def undo_redo(qapp: QW.QApplication, win: PlotWindow):
89+
undo(qapp, win)
90+
time.sleep(0.1)
91+
redo(qapp, win)
92+
93+
94+
def new_wheel_event(
95+
canvas: BasePlot,
96+
pos_xy: tuple[float, float],
97+
angle_delta: int,
98+
pix_delta: int,
99+
btns: QC.Qt.MouseButton = QC.Qt.MouseButton.NoButton,
100+
mods=QC.Qt.KeyboardModifier.NoModifier,
101+
) -> QG.QWheelEvent:
102+
canva_pos, glob_pos = rel_pos_to_canvas_pos(canvas, pos_xy)
103+
angle_delta = QC.QPoint(0, angle_delta) # Scroll distance in eighths of a degree
104+
pix_delta = QC.QPoint(0, pix_delta) # Scroll distance in pixels
105+
qt4_delta = 0 # You may need to adjust this value
106+
orientation = QC.Qt.Orientation.Vertical # You may need to adjust this value
107+
108+
# Create QWheelEvent
109+
return QG.QWheelEvent(
110+
canva_pos, glob_pos, pix_delta, angle_delta, qt4_delta, orientation, btns, mods
111+
)
112+
113+
114+
def scroll_wheel(
115+
win: PlotDialog | PlotWindow,
116+
qapp: QW.QApplication,
117+
relative_xy: tuple[float, float],
118+
angle_delta: int,
119+
pix_delta: int = 0,
120+
btns: QC.Qt.MouseButton = QC.Qt.MouseButton.NoButton,
121+
mods=QC.Qt.KeyboardModifier.NoModifier,
122+
) -> None:
123+
"""Simulates a scroll wheel event on the plot at the given relative position.
124+
125+
Args:
126+
win: window containing the plot
127+
qapp: Main QApplication instance
128+
relative_xy: Relative position of the click on the plot
129+
angle_delta: Scroll distance in eighths of a degree
130+
pix_delta: Scroll distance in pixels. Defaults to 0.
131+
btns: Mouse buttons. Defaults to QC.Qt.MouseButton.NoButton.
132+
mod: Keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier.
133+
134+
Returns:
135+
None
136+
"""
137+
plot = win.manager.get_plot()
138+
canvas: BasePlot = plot.canvas() # type: ignore
139+
wheel_event = new_wheel_event(
140+
canvas, relative_xy, angle_delta, pix_delta, btns, mods
141+
)
142+
qapp.sendEvent(canvas, wheel_event)
143+
144+
76145
def mouse_event_at_relative_plot_pos(
77146
win: PlotDialog | PlotWindow,
78147
qapp: QW.QApplication,
79148
relative_xy: tuple[float, float],
80149
click_types: tuple[QC.QEvent.Type, ...] = (QC.QEvent.Type.MouseButtonPress,),
81150
mod=QC.Qt.KeyboardModifier.NoModifier,
151+
btn=QC.Qt.MouseButton.LeftButton,
82152
) -> None:
83153
"""Simulates a click on the plot at the given relative position.
84154
@@ -101,19 +171,29 @@ def mouse_event_at_relative_plot_pos(
101171
mouse_event = QG.QMouseEvent(
102172
type_,
103173
canvas_pos,
104-
QC.Qt.MouseButton.LeftButton,
105-
QC.Qt.MouseButton.LeftButton,
174+
btn,
175+
btn,
106176
mod,
107177
)
108178
qapp.sendEvent(canvas, mouse_event)
109179

110180

181+
# def mouse_wheel_event_at_relative_pos(
182+
# win: PlotDialog | PlotWindow,
183+
# qapp: QW.QApplication,
184+
# relative_xy: tuple[float, float],
185+
# click_types: tuple[QC.QEvent.Type, ...] = (QC.QEvent.Type.MouseButtonPress,),
186+
# mod=QC.Qt.KeyboardModifier.NoModifier,
187+
# ) -> None:
188+
189+
111190
def drag_mouse(
112191
win: PlotWindow,
113192
qapp: QW.QApplication,
114193
x_path: np.ndarray,
115194
y_path: np.ndarray,
116195
mod=QC.Qt.KeyboardModifier.NoModifier,
196+
btn=QC.Qt.MouseButton.LeftButton,
117197
click=True,
118198
) -> None:
119199
"""Simulates a mouse drag on the plot.
@@ -134,11 +214,11 @@ def drag_mouse(
134214
release = (QC.QEvent.Type.MouseButtonRelease,)
135215

136216
if click:
137-
mouse_event_at_relative_plot_pos(win, qapp, (x0, y0), press, mod)
217+
mouse_event_at_relative_plot_pos(win, qapp, (x0, y0), press, mod, btn)
138218
for x, y in zip(x_path, y_path):
139-
mouse_event_at_relative_plot_pos(win, qapp, (x, y), move, mod)
219+
mouse_event_at_relative_plot_pos(win, qapp, (x, y), move, mod, btn)
140220
if click:
141-
mouse_event_at_relative_plot_pos(win, qapp, (xn, yn), release, mod)
221+
mouse_event_at_relative_plot_pos(win, qapp, (xn, yn), release, mod, btn)
142222

143223

144224
def create_window(

0 commit comments

Comments
 (0)