|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +import qtpy.QtCore as QC |
| 4 | +from guidata.qthelpers import exec_dialog, qt_app_context |
| 5 | +from qwt import QwtPlotItem |
| 6 | + |
| 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 |
| 18 | +from plotpy.tests.unit.utils import ( |
| 19 | + create_window, |
| 20 | + drag_mouse, |
| 21 | + keyboard_event, |
| 22 | +) |
| 23 | +from plotpy.tools import ( |
| 24 | + CurveStatsTool, |
| 25 | + HCursorTool, |
| 26 | + HRangeTool, |
| 27 | + VCursorTool, |
| 28 | + XCursorTool, |
| 29 | +) |
| 30 | +from plotpy.tools.cursor import BaseCursorTool |
| 31 | + |
| 32 | +# guitest: show |
| 33 | + |
| 34 | + |
| 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 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "cursor_tool", [HCursorTool, VCursorTool, XCursorTool, HRangeTool] |
| 66 | +) |
| 67 | +def test_cursor_tool(cursor_tool: type[BaseCursorTool]): |
| 68 | + with qt_app_context(exec_loop=False) as qapp: |
| 69 | + win, tool = create_window(cursor_tool) |
| 70 | + plot = win.manager.get_plot() |
| 71 | + |
| 72 | + active_tool = win.manager.get_active_tool() |
| 73 | + assert isinstance(active_tool, cursor_tool) |
| 74 | + tool_shape_type = type(active_tool.create_shape()) |
| 75 | + assert tool_shape_type not in (type(item) for item in plot.get_items()) |
| 76 | + |
| 77 | + drag_mouse(win, qapp, np.array([0.5, 0.6, 0.7]), np.array([0.5, 0.6, 0.7])) |
| 78 | + |
| 79 | + assert tool_shape_type in (type(item) for item in plot.get_items()) |
| 80 | + |
| 81 | + exec_dialog(win) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + test_curve_stat_tool() |
| 86 | + test_cursor_tool(HCursorTool) |
| 87 | + test_cursor_tool(VCursorTool) |
| 88 | + test_cursor_tool(XCursorTool) |
| 89 | + test_cursor_tool(HRangeTool) |
0 commit comments