|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | +from guidata.qthelpers import exec_dialog, execenv, qt_app_context |
| 8 | + |
| 9 | +from plotpy.interfaces.items import IBasePlotItem, IShapeItemType |
| 10 | +from plotpy.tests import vistools as ptv |
| 11 | +from plotpy.tests.features.test_auto_curve_image import make_curve_image_legend |
| 12 | +from plotpy.tools import ( |
| 13 | + AnnotatedCircleTool, |
| 14 | + AnnotatedEllipseTool, |
| 15 | + AnnotatedObliqueRectangleTool, |
| 16 | + AnnotatedPointTool, |
| 17 | + AnnotatedRectangleTool, |
| 18 | + AnnotatedSegmentTool, |
| 19 | + AverageCrossSectionTool, |
| 20 | + CircleTool, |
| 21 | + CrossSectionTool, |
| 22 | + EllipseTool, |
| 23 | + ImageStatsTool, |
| 24 | + InteractiveTool, |
| 25 | + LabelTool, |
| 26 | + ObliqueRectangleTool, |
| 27 | + PointTool, |
| 28 | + RectangleTool, |
| 29 | + SegmentTool, |
| 30 | + SelectTool, |
| 31 | + SnapshotTool, |
| 32 | +) |
| 33 | + |
| 34 | +if TYPE_CHECKING: |
| 35 | + from plotpy.items.image.base import BaseImageItem |
| 36 | + from plotpy.plot.plotwidget import PlotWindow |
| 37 | + |
| 38 | +from plotpy.tests.unit.utils import drag_mouse, undo_redo |
| 39 | + |
| 40 | +TOOLS: tuple[type[InteractiveTool], ...] = ( |
| 41 | + AnnotatedCircleTool, |
| 42 | + AnnotatedEllipseTool, |
| 43 | + AnnotatedObliqueRectangleTool, |
| 44 | + AnnotatedPointTool, |
| 45 | + AnnotatedRectangleTool, |
| 46 | + AnnotatedSegmentTool, |
| 47 | + AverageCrossSectionTool, |
| 48 | + CrossSectionTool, |
| 49 | + SnapshotTool, |
| 50 | + ImageStatsTool, |
| 51 | + CircleTool, |
| 52 | + EllipseTool, |
| 53 | + ObliqueRectangleTool, |
| 54 | + PointTool, |
| 55 | + RectangleTool, |
| 56 | + SegmentTool, |
| 57 | + LabelTool, |
| 58 | +) |
| 59 | + |
| 60 | + |
| 61 | +def create_window(tool_classes: tuple[type[InteractiveTool], ...]) -> PlotWindow: |
| 62 | + """Create a window with the given tools. The plot contains a curve, an image and a |
| 63 | + legend. |
| 64 | +
|
| 65 | + Args: |
| 66 | + tool_classes: tools to add to the window. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + PlotWindow: The window containing the tools. |
| 70 | + """ |
| 71 | + items = make_curve_image_legend() |
| 72 | + img: BaseImageItem = items[0] |
| 73 | + img.set_rotatable(True) |
| 74 | + img.set_movable(True) |
| 75 | + win = ptv.show_items( |
| 76 | + items, wintitle="Unit tests for RectangularActionTools", auto_tools=True |
| 77 | + ) |
| 78 | + |
| 79 | + for toolklass in tool_classes: |
| 80 | + _ = win.manager.add_tool(toolklass) |
| 81 | + |
| 82 | + assert len(tool_classes) > 0 |
| 83 | + |
| 84 | + return win |
| 85 | + |
| 86 | + |
| 87 | +def _test_annotation_tools(tool_classes: tuple[type[InteractiveTool], ...]): |
| 88 | + """Generic test for annotation tool. Simulates a mouse drag on the plot and checks |
| 89 | + that the tool is activated and deactivated correctly.""" |
| 90 | + with qt_app_context(exec_loop=False) as qapp: |
| 91 | + win = create_window(tool_classes) |
| 92 | + win.show() |
| 93 | + plot = win.manager.get_plot() |
| 94 | + default_tool = win.manager.get_default_tool() |
| 95 | + for tool_class in tool_classes: |
| 96 | + tool = win.manager.get_tool(tool_class) |
| 97 | + assert tool is not None |
| 98 | + tool.activate() |
| 99 | + x_path = np.linspace(0, 0.5, 100) |
| 100 | + y_path = np.linspace(0, 0.5, 100) |
| 101 | + with execenv.context(accept_dialogs=True): |
| 102 | + drag_mouse(win, qapp, x_path, y_path) |
| 103 | + if hasattr(tool_class, "SWITCH_TO_DEFAULT_TOOL"): |
| 104 | + assert win.manager.get_default_tool() == default_tool |
| 105 | + plot.unselect_all() |
| 106 | + shape_items: list[IBasePlotItem] = plot.get_items(item_type=IShapeItemType) |
| 107 | + plot.select_some_items(shape_items) |
| 108 | + select_tool = win.manager.get_tool(SelectTool) |
| 109 | + assert select_tool is not None |
| 110 | + select_tool.activate() |
| 111 | + |
| 112 | + drag_mouse(win, qapp, np.linspace(0.2, 0.5, 10), np.linspace(0.2, 0.5, 10)) |
| 113 | + |
| 114 | + undo_redo(qapp, win) |
| 115 | + |
| 116 | + exec_dialog(win) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize("tool", TOOLS) |
| 120 | +def test_tool(tool: type[InteractiveTool]) -> None: |
| 121 | + """Test a single tool.""" |
| 122 | + _test_annotation_tools((tool,)) |
| 123 | + |
| 124 | + |
| 125 | +def _test_all_tools(): |
| 126 | + """Test all tools.""" |
| 127 | + _test_annotation_tools(TOOLS) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + _test_all_tools() |
0 commit comments