|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# For licensing and distribution details, please read carefully xgrid/__init__.py |
| 4 | + |
| 5 | +""" |
| 6 | +Get rectangular selection from image with SVG shape |
| 7 | +""" |
| 8 | + |
| 9 | +# guitest: show |
| 10 | + |
| 11 | +import os.path as osp |
| 12 | + |
| 13 | +from guidata.env import execenv |
| 14 | +from guidata.qthelpers import qt_app_context |
| 15 | + |
| 16 | +from plotpy.builder import make |
| 17 | +from plotpy.tests.data import gen_image4 |
| 18 | +from plotpy.tests.gui.test_get_segment import SEG_AXES_COORDS, PatchedSelectDialog |
| 19 | +from plotpy.tools import RectangularShapeTool |
| 20 | +from plotpy.widgets.selectdialog import select_with_shape_tool |
| 21 | + |
| 22 | + |
| 23 | +class SVGToolExample(RectangularShapeTool): |
| 24 | + """Tool to select a rectangular area and create a pattern from it""" |
| 25 | + |
| 26 | + TITLE = "Pattern selection tool" |
| 27 | + ICON = "pattern.svg" |
| 28 | + AVOID_NULL_SHAPE = True |
| 29 | + SVG_FNAME = osp.join(osp.dirname(__file__), "svg_tool.svg") |
| 30 | + |
| 31 | + def create_shape(self): |
| 32 | + """Create shape to be drawn""" |
| 33 | + svg_data = open(self.SVG_FNAME, "rb").read() |
| 34 | + shape = make.svg("rectangle", svg_data, 0, 0, 1, 1, "SVG") |
| 35 | + self.set_shape_style(shape) |
| 36 | + return shape, 0, 2 |
| 37 | + |
| 38 | + |
| 39 | +def test_get_rectangle_with_svg(): |
| 40 | + """Test get_rectangle_with_svg""" |
| 41 | + with qt_app_context(): |
| 42 | + image = make.image(data=gen_image4(200, 200), colormap="gray") |
| 43 | + shape = select_with_shape_tool( |
| 44 | + None, SVGToolExample, image, "Test", tooldialogclass=PatchedSelectDialog |
| 45 | + ) |
| 46 | + rect = shape.get_rect() |
| 47 | + if execenv.unattended: |
| 48 | + assert [round(i) for i in list(rect)] == SEG_AXES_COORDS |
| 49 | + elif rect is not None: |
| 50 | + print("Area:", rect) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + test_get_rectangle_with_svg() |
0 commit comments