44
55from __future__ import annotations
66
7- from typing import Callable
7+ import operator
8+ from enum import Enum
9+ from typing import TYPE_CHECKING , Callable
810
911import numpy as np
1012import pytest
1113import qtpy .QtCore as QC
1214import qtpy .QtWidgets as QW
1315from guidata .qthelpers import exec_dialog , qt_app_context
1416
15- from plotpy .plot .plotwidget import PlotDialog , PlotWindow
1617from plotpy .tests .unit .utils import (
1718 create_window ,
1819 drag_mouse ,
1920 scroll_wheel ,
2021)
2122from plotpy .tools .selection import SelectTool
2223
23- # guitest: show
24-
25-
26- def _zoom_with_mouse (qapp : QW .QApplication , win : PlotWindow | PlotDialog ):
27- drag_mouse (
28- win ,
29- qapp ,
30- np .array ([0.5 , 0.8 ]),
31- np .array ([0.5 , 0.5 ]),
32- btn = QC .Qt .MouseButton .RightButton ,
33- )
34-
24+ if TYPE_CHECKING :
25+ from plotpy .plot .plotwidget import PlotDialog , PlotWindow
26+
27+
28+ class ZoomType (Enum ):
29+ NO_ZOOM = (operator .eq , operator .eq , operator .eq , operator .eq )
30+ ZOOM_IN = (operator .gt , operator .lt , operator .lt , operator .gt )
31+ ZOOM_OUT = (operator .lt , operator .gt , operator .gt , operator .lt )
32+
33+
34+ class ZoomEvent (Enum ):
35+ ZOOM_WITH_MOUSE = 0
36+ ZOOM_WITH_WHEEL = 1
37+
38+
39+ def result_zoom (zoom_type : ZoomType , event_type : ZoomEvent ):
40+ """Wrapper used to parametrize the test_zoom function.
41+
42+ Args:
43+ zoom_type: enum to specify the expected zoom.
44+ event_type: enum to specify the event type (drag or scroll).
45+
46+ Returns:
47+ A function used to zoom in the plot and the expected zoom type.
48+ """
49+ if event_type is ZoomEvent .ZOOM_WITH_MOUSE :
50+ if zoom_type is ZoomType .NO_ZOOM :
51+ btn = QC .Qt .MouseButton .NoButton
52+ x_path = np .array ([0.5 , 0.8 ])
53+ elif zoom_type is ZoomType .ZOOM_IN :
54+ btn = QC .Qt .MouseButton .RightButton
55+ x_path = np .array ([0.5 , 0.8 ])
56+ else :
57+ btn = QC .Qt .MouseButton .RightButton
58+ x_path = np .array ([0.5 , 0.2 ])
59+
60+ def _zoom_with_mouse (qapp : QW .QApplication , win : PlotWindow | PlotDialog ):
61+ """Zoom in the plot by dragging the mouse while holding right click.
62+
63+ Args:
64+ qapp: QApplication instance.
65+ win: PlotWindow or PlotDialog instance.
66+ """
67+ drag_mouse (
68+ win ,
69+ qapp ,
70+ x_path ,
71+ np .array ([0.5 , 0.5 ]),
72+ btn = btn ,
73+ )
74+
75+ return _zoom_with_mouse , zoom_type
76+
77+ if zoom_type is ZoomType .NO_ZOOM :
78+ angle_delta = 360
79+ mod = QC .Qt .KeyboardModifier .NoModifier
80+ elif zoom_type is ZoomType .ZOOM_IN :
81+ angle_delta = 360
82+ mod = QC .Qt .KeyboardModifier .ControlModifier
83+ else :
84+ angle_delta = - 360
85+ mod = QC .Qt .KeyboardModifier .ControlModifier
86+
87+ def _zoom_with_wheel (qapp : QW .QApplication , win : PlotWindow | PlotDialog ):
88+ """Zoom in the plot by scrolling the mouse wheel while holding control.
89+
90+ Args:
91+ qapp: QApplication instance.
92+ win: PlotWindow or PlotDialog instance.
93+ """
94+ scroll_wheel (
95+ win ,
96+ qapp ,
97+ (0.5 , 0.5 ),
98+ angle_delta ,
99+ 0 ,
100+ mods = mod ,
101+ )
35102
36- def _zoom_with_wheel (qapp : QW .QApplication , win : PlotWindow | PlotDialog ):
37- scroll_wheel (
38- win ,
39- qapp ,
40- (0.5 , 0.5 ),
41- 360 ,
42- 0 ,
43- mods = QC .Qt .KeyboardModifier .ControlModifier ,
44- )
103+ return _zoom_with_wheel , zoom_type
45104
46105
47106@pytest .mark .parametrize (
48- "zoom_func" ,
49- [_zoom_with_mouse , _zoom_with_wheel ],
107+ "zoom_func, zoom_type" ,
108+ [
109+ result_zoom (ZoomType .NO_ZOOM , ZoomEvent .ZOOM_WITH_MOUSE ),
110+ result_zoom (ZoomType .NO_ZOOM , ZoomEvent .ZOOM_WITH_WHEEL ),
111+ result_zoom (ZoomType .ZOOM_IN , ZoomEvent .ZOOM_WITH_MOUSE ),
112+ result_zoom (ZoomType .ZOOM_IN , ZoomEvent .ZOOM_WITH_WHEEL ),
113+ result_zoom (ZoomType .ZOOM_OUT , ZoomEvent .ZOOM_WITH_MOUSE ),
114+ result_zoom (ZoomType .ZOOM_OUT , ZoomEvent .ZOOM_WITH_WHEEL ),
115+ ],
50116)
51117def test_zoom (
52118 zoom_func : Callable [[QW .QApplication , PlotWindow | PlotDialog ], None ],
119+ zoom_type : ZoomType ,
53120):
121+ """Test zooming on the plot by triggering several event types.
122+
123+ Args:
124+ zoom_func: _description_
125+ """
54126 with qt_app_context (exec_loop = False ) as qapp :
55127 win , _ = create_window (SelectTool )
56128 win .show ()
@@ -68,17 +140,17 @@ def test_zoom(
68140 x_min1 , x_max1 = plot .get_axis_limits (axis_x )
69141 y_min1 , y_max1 = plot .get_axis_limits (axis_y )
70142
71- print (x_min0 , x_max0 , y_min0 , y_max0 )
72- print (x_min1 , x_max1 , y_min1 , y_max1 )
73- assert x_min1 > x_min0
74- assert x_max1 < x_max0
75- assert y_min1 < y_min0
76- assert y_max1 > y_max0
143+ c1 , c2 , c3 , c4 = zoom_type .value
144+ assert c1 (x_min1 , x_min0 )
145+ assert c2 (x_max1 , x_max0 )
146+ assert c3 (y_min1 , y_min0 )
147+ assert c4 (y_max1 , y_max0 )
77148
78149 exec_dialog (win )
79150
80151
81152def test_pan ():
153+ """Test panning the plot by dragging the mouse while holding middle click."""
82154 with qt_app_context (exec_loop = False ) as qapp :
83155 win , _ = create_window (SelectTool )
84156 win .show ()
@@ -102,17 +174,14 @@ def test_pan():
102174 x_min1 , x_max1 = plot .get_axis_limits (axis_x )
103175 y_min1 , y_max1 = plot .get_axis_limits (axis_y )
104176
105- print (x_min0 , x_max0 , y_min0 , y_max0 )
106- print (x_min1 , x_max1 , y_min1 , y_max1 )
107- # assert x_min1 > x_min0
108- # assert x_max1 > x_max0
109- # assert y_min1 > y_min0
110- # assert y_max1 > y_max0
177+ assert x_min1 > x_min0
178+ assert x_max1 > x_max0
179+ assert y_min1 > y_min0
180+ assert y_max1 > y_max0
111181
112182 exec_dialog (win )
113183
114184
115185if __name__ == "__main__" :
116- test_zoom (_zoom_with_mouse )
117- test_zoom (_zoom_with_wheel )
186+ test_zoom (* result_zoom (ZoomType .ZOOM_OUT , ZoomEvent .ZOOM_WITH_WHEEL ))
118187 test_pan ()
0 commit comments