Skip to content

Commit 7c78f12

Browse files
committed
improve and moved CurveStatsTool test and rename test_point_tools into test_curve_tools
1 parent cfccfed commit 7c78f12

2 files changed

Lines changed: 71 additions & 52 deletions

File tree

plotpy/tests/unit/test_cursor_tools.py

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
import numpy as np
22
import pytest
3-
import qtpy.QtCore as QC
43
from guidata.qthelpers import exec_dialog, qt_app_context
5-
from qwt import QwtPlotItem
64

7-
from plotpy.builder import make
8-
from plotpy.interfaces.items import (
9-
ICurveItemType,
10-
IDecoratorItemType,
11-
IShapeItemType,
12-
ITrackableItemType,
13-
)
14-
from plotpy.items import CurveItem, DataInfoLabel, Marker, TrImageItem, XRangeSelection
15-
from plotpy.plot.base import BasePlot
16-
from plotpy.plot.plotwidget import PlotWindow
17-
from plotpy.tests.data import gen_image4
185
from plotpy.tests.unit.utils import (
196
create_window,
207
drag_mouse,
21-
keyboard_event,
228
)
239
from plotpy.tools import (
24-
CurveStatsTool,
2510
HCursorTool,
2611
HRangeTool,
2712
VCursorTool,
@@ -32,41 +17,13 @@
3217
# guitest: show
3318

3419

35-
def test_curve_stat_tool():
36-
with qt_app_context(exec_loop=False) as qapp:
37-
win, tool = create_window(CurveStatsTool)
38-
plot = win.manager.get_plot()
39-
40-
active_tool = win.manager.get_active_tool()
41-
assert isinstance(active_tool, CurveStatsTool)
42-
43-
og_stat_items = [
44-
item
45-
for item in plot.get_items()
46-
if isinstance(item, (DataInfoLabel, XRangeSelection))
47-
]
48-
49-
drag_mouse(win, qapp, np.array([0.5, 0.6, 0.7]), np.array([0.5, 0.6, 0.7]))
50-
51-
new_stat_items = [
52-
item
53-
for item in plot.get_items()
54-
if isinstance(item, (DataInfoLabel, XRangeSelection))
55-
]
56-
57-
# There should be one more new DataInfoLabel and one new XRangeSelection
58-
# compared to the original plot items (before mouse drag)
59-
assert len(new_stat_items) == len(og_stat_items) + 2
60-
61-
exec_dialog(win)
62-
63-
6420
@pytest.mark.parametrize(
6521
"cursor_tool", [HCursorTool, VCursorTool, XCursorTool, HRangeTool]
6622
)
6723
def test_cursor_tool(cursor_tool: type[BaseCursorTool]):
68-
with qt_app_context(exec_loop=False) as qapp:
24+
with qt_app_context(exec_loop=True) as qapp:
6925
win, tool = create_window(cursor_tool)
26+
win.show()
7027
plot = win.manager.get_plot()
7128

7229
active_tool = win.manager.get_active_tool()
@@ -75,14 +32,12 @@ def test_cursor_tool(cursor_tool: type[BaseCursorTool]):
7532
assert tool_shape_type not in (type(item) for item in plot.get_items())
7633

7734
drag_mouse(win, qapp, np.array([0.5, 0.6, 0.7]), np.array([0.5, 0.6, 0.7]))
78-
7935
assert tool_shape_type in (type(item) for item in plot.get_items())
8036

8137
exec_dialog(win)
8238

8339

8440
if __name__ == "__main__":
85-
test_curve_stat_tool()
8641
test_cursor_tool(HCursorTool)
8742
test_cursor_tool(VCursorTool)
8843
test_cursor_tool(XCursorTool)
Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import qtpy.QtCore as QC
77
from guidata.qthelpers import exec_dialog, qt_app_context
88

9+
from plotpy.coords import canvas_to_axes
10+
from plotpy.interfaces.items import ICurveItemType
911
from plotpy.tests.unit.utils import (
1012
CLICK,
1113
create_window,
@@ -20,6 +22,67 @@
2022
from plotpy.items.curve.base import CurveItem
2123

2224

25+
from plotpy.items import DataInfoLabel, XRangeSelection
26+
from plotpy.tools import (
27+
CurveStatsTool,
28+
)
29+
from plotpy.tools.selection import SelectTool
30+
31+
# guitest: show
32+
33+
34+
def test_curve_stat_tool():
35+
with qt_app_context(exec_loop=False) as qapp:
36+
win, tool = create_window(CurveStatsTool)
37+
win.show()
38+
plot = win.manager.get_plot()
39+
40+
original_stat_items = [
41+
item
42+
for item in plot.get_items()
43+
if isinstance(item, (DataInfoLabel, XRangeSelection))
44+
]
45+
46+
drag_mouse(win, qapp, np.array([0.4, 0.6]), np.array([0.5, 0.5]))
47+
48+
new_stat_items = [
49+
item
50+
for item in plot.get_items()
51+
if isinstance(item, (DataInfoLabel, XRangeSelection))
52+
]
53+
54+
assert len(new_stat_items) == len(original_stat_items) + 2
55+
56+
# There should be one more new DataInfoLabel and one new XRangeSelection
57+
# compared to the original plot items (before mouse drag)
58+
59+
win.manager.add_tool(SelectTool).activate()
60+
plot.unselect_all()
61+
62+
mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK)
63+
selected_items = plot.get_selected_items()
64+
assert len(selected_items) == 1 and isinstance(
65+
selected_items[0], XRangeSelection
66+
)
67+
range_item: XRangeSelection = selected_items[0]
68+
69+
x00, x01, y0 = range_item.get_handles_pos()
70+
drag_mouse(win, qapp, np.linspace(0.5, 0.9, 100), np.full(100, 0.5))
71+
x10, x11, y1 = range_item.get_handles_pos()
72+
73+
assert x00 < x10 and x01 < x11 and y0 == y1
74+
75+
curve_item: CurveItem = plot.get_items(item_type=ICurveItemType)[0] # type: ignore
76+
current_pos = canvas_to_axes(curve_item, QC.QPointF(x10, y1))
77+
x_min, _ = plot.get_axis_limits(curve_item.xAxis())
78+
79+
assert current_pos is not None
80+
range_item.move_shape(current_pos, (x_min, 0))
81+
assert np.isclose(range_item.get_range()[0], x_min)
82+
83+
exec_dialog(win)
84+
85+
2386
def test_free_select_point_tool():
2487
"""Test the free select point tool."""
2588
with qt_app_context(exec_loop=False) as qapp:
@@ -62,14 +125,14 @@ def test_select_points_tool():
62125
"""Test the select points tool constrained to a CurveItem."""
63126
with qt_app_context(exec_loop=False) as qapp:
64127
win, tool = create_window(tool_class=SelectPointsTool)
65-
win.show()
66128
mod = QC.Qt.KeyboardModifier.ControlModifier
67129

68130
mouse_event_at_relative_plot_pos(win, qapp, (0.4, 0.5), CLICK, mod)
69131
assert len(tool.get_coordinates() or ()) == 1
70132

71133
mouse_event_at_relative_plot_pos(win, qapp, (0.5, 0.5), CLICK, mod)
72134
mouse_event_at_relative_plot_pos(win, qapp, (0.8, 0.8), CLICK, mod)
135+
print(tool.get_coordinates())
73136
assert len(tool.get_coordinates() or ()) == 3
74137

75138
mouse_event_at_relative_plot_pos(win, qapp, (0.8, 0.8), CLICK, mod)
@@ -160,7 +223,8 @@ def test_edit_point_tool():
160223

161224

162225
if __name__ == "__main__":
163-
test_free_select_point_tool()
164-
test_contrained_select_point_tool()
165-
test_select_points_tool()
166-
test_edit_point_tool()
226+
test_curve_stat_tool()
227+
# test_free_select_point_tool()
228+
# test_contrained_select_point_tool()
229+
# test_select_points_tool()
230+
# test_edit_point_tool()

0 commit comments

Comments
 (0)