-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_events.py
More file actions
167 lines (126 loc) · 4.82 KB
/
test_events.py
File metadata and controls
167 lines (126 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
This files tests the vent handlers not covered by other tests.
"""
from __future__ import annotations
import operator
from enum import Enum
from typing import TYPE_CHECKING, Callable
import numpy as np
import pytest
import qtpy.QtCore as QC
import qtpy.QtWidgets as QW
from guidata.qthelpers import exec_dialog, qt_app_context
from plotpy.tests.unit.utils import (
create_window,
drag_mouse,
scroll_wheel,
)
from plotpy.tools.selection import SelectTool
if TYPE_CHECKING:
from plotpy.plot.plotwidget import PlotDialog, PlotWindow
class ZoomType(Enum):
NO_ZOOM = (operator.eq, operator.eq, operator.eq, operator.eq)
ZOOM_IN = (operator.gt, operator.lt, operator.lt, operator.gt)
ZOOM_OUT = (operator.lt, operator.gt, operator.gt, operator.lt)
class ZoomEvent(Enum):
ZOOM_WITH_MOUSE = 0
ZOOM_WITH_WHEEL = 1
def result_zoom(zoom_type: ZoomType, event_type: ZoomEvent):
"""Wrapper used to parametrize the test_zoom function.
Args:
zoom_type: enum to specify the expected zoom.
event_type: enum to specify the event type (drag or scroll).
Returns:
A function used to zoom in the plot and the expected zoom type.
"""
if event_type is ZoomEvent.ZOOM_WITH_MOUSE:
if zoom_type is ZoomType.NO_ZOOM:
btn = QC.Qt.MouseButton.NoButton
x_path = np.array([0.5, 0.8])
elif zoom_type is ZoomType.ZOOM_IN:
btn = QC.Qt.MouseButton.RightButton
x_path = np.array([0.5, 0.8])
else:
btn = QC.Qt.MouseButton.RightButton
x_path = np.array([0.5, 0.2])
def _zoom_with_mouse(win: PlotWindow | PlotDialog):
"""Zoom in the plot by dragging the mouse while holding right click.
Args:
win: PlotWindow or PlotDialog instance.
"""
drag_mouse(win, x_path, np.array([0.5, 0.5]), btn=btn)
return _zoom_with_mouse, zoom_type
if zoom_type is ZoomType.NO_ZOOM:
angle_delta = 360
mod = QC.Qt.KeyboardModifier.NoModifier
elif zoom_type is ZoomType.ZOOM_IN:
angle_delta = 360
mod = QC.Qt.KeyboardModifier.ControlModifier
else:
angle_delta = -360
mod = QC.Qt.KeyboardModifier.ControlModifier
def _zoom_with_wheel(win: PlotWindow | PlotDialog):
"""Zoom in the plot by scrolling the mouse wheel while holding control.
Args:
win: PlotWindow or PlotDialog instance.
"""
scroll_wheel(win, (0.5, 0.5), angle_delta, 0, mods=mod)
return _zoom_with_wheel, zoom_type
@pytest.mark.parametrize(
"zoom_func, zoom_type",
[
result_zoom(ZoomType.NO_ZOOM, ZoomEvent.ZOOM_WITH_MOUSE),
result_zoom(ZoomType.NO_ZOOM, ZoomEvent.ZOOM_WITH_WHEEL),
result_zoom(ZoomType.ZOOM_IN, ZoomEvent.ZOOM_WITH_MOUSE),
result_zoom(ZoomType.ZOOM_IN, ZoomEvent.ZOOM_WITH_WHEEL),
result_zoom(ZoomType.ZOOM_OUT, ZoomEvent.ZOOM_WITH_MOUSE),
result_zoom(ZoomType.ZOOM_OUT, ZoomEvent.ZOOM_WITH_WHEEL),
],
)
def test_zoom(
zoom_func: Callable[[QW.QApplication, PlotWindow | PlotDialog], None],
zoom_type: ZoomType,
):
"""Test zooming on the plot by triggering several event types.
Args:
zoom_func: _description_
"""
with qt_app_context(exec_loop=False):
win, _ = create_window(SelectTool)
win.show()
plot = win.manager.get_plot()
plot.unselect_all()
axis_x, axis_y = plot.get_active_axes()
x_min0, x_max0 = plot.get_axis_limits(axis_x)
y_min0, y_max0 = plot.get_axis_limits(axis_y)
zoom_func(win)
x_min1, x_max1 = plot.get_axis_limits(axis_x)
y_min1, y_max1 = plot.get_axis_limits(axis_y)
c1, c2, c3, c4 = zoom_type.value
assert c1(x_min1, x_min0)
assert c2(x_max1, x_max0)
assert c3(y_min1, y_min0)
assert c4(y_max1, y_max0)
exec_dialog(win)
def test_pan():
"""Test panning the plot by dragging the mouse while holding middle click."""
with qt_app_context(exec_loop=False):
win, _ = create_window(SelectTool)
win.show()
plot = win.manager.get_plot()
plot.unselect_all()
axis_x, axis_y = plot.get_active_axes()
x_min0, x_max0 = plot.get_axis_limits(axis_x)
y_min0, y_max0 = plot.get_axis_limits(axis_y)
x_path, y_path = np.linspace(0.5, 0, 100), np.linspace(0.5, 0, 100)
drag_mouse(win, x_path, y_path, btn=QC.Qt.MouseButton.MiddleButton)
x_min1, x_max1 = plot.get_axis_limits(axis_x)
y_min1, y_max1 = plot.get_axis_limits(axis_y)
assert x_min1 > x_min0
assert x_max1 > x_max0
assert y_min1 > y_min0
assert y_max1 > y_max0
exec_dialog(win)
if __name__ == "__main__":
test_zoom(*result_zoom(ZoomType.ZOOM_OUT, ZoomEvent.ZOOM_WITH_WHEEL))
test_pan()