|
| 1 | +import contextlib |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +from guidata.env import execenv |
| 6 | +from guidata.qthelpers import exec_dialog, qt_app_context |
| 7 | + |
| 8 | +from plotpy.builder import make |
| 9 | +from plotpy.interfaces.items import IImageItemType |
| 10 | +from plotpy.plot import BasePlot |
| 11 | +from plotpy.tests.data import gen_image4 |
| 12 | +from plotpy.tests.unit.utils import ( |
| 13 | + drag_mouse, |
| 14 | + mouse_event_at_relative_plot_pos, |
| 15 | + rel_pos_to_canvas_pos, |
| 16 | +) |
| 17 | +from plotpy.tools import ColormapTool, ReverseColormapTool |
| 18 | +from plotpy.tools.image import ImageMaskTool, LockTrImageTool |
| 19 | +from plotpy.tools.shape import CircleTool, RectangleTool, RectangularShapeTool |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("shape_tool_cls", [RectangleTool, CircleTool]) |
| 23 | +@pytest.mark.parametrize("inside", [True, False]) |
| 24 | +def test_image_mask_tool(shape_tool_cls: type[RectangularShapeTool], inside: bool): |
| 25 | + """Test ImageMaskTool""" |
| 26 | + with qt_app_context(exec_loop=False): |
| 27 | + win = make.dialog(type="image", toolbar=True) |
| 28 | + item = make.maskedimage(gen_image4(100, 100)) |
| 29 | + plot = win.manager.get_plot() |
| 30 | + plot.add_item(item) # type: ignore |
| 31 | + plot.select_item(item) # type: ignore |
| 32 | + win.show() |
| 33 | + mask_tool = win.manager.add_tool( |
| 34 | + ImageMaskTool, |
| 35 | + ) |
| 36 | + shape_tool = win.manager.add_tool( |
| 37 | + shape_tool_cls, |
| 38 | + toolbar_id=None, |
| 39 | + handle_final_shape_cb=lambda shape: mask_tool.handle_shape( |
| 40 | + shape, inside=inside |
| 41 | + ), |
| 42 | + ) |
| 43 | + shape_tool.activate() |
| 44 | + xy0, xy1 = (0.1, 0.1), (0.9, 0.9) |
| 45 | + canvas = plot.canvas() |
| 46 | + assert canvas is not None |
| 47 | + pos0, _ = rel_pos_to_canvas_pos(canvas, xy0) |
| 48 | + pos1, _ = rel_pos_to_canvas_pos(canvas, xy1) |
| 49 | + |
| 50 | + with execenv.context(accept_dialogs=True): |
| 51 | + shape_tool.add_shape_to_plot(plot, pos0, pos1) |
| 52 | + mask_tool.apply_mask() |
| 53 | + mask_tool.clear_mask() |
| 54 | + |
| 55 | + shape_tool.add_shape_to_plot(plot, pos0, pos1) |
| 56 | + mask_tool.remove_all_shapes() |
| 57 | + |
| 58 | + exec_dialog(win) |
0 commit comments