Skip to content

Commit

Permalink
Added test_get_rectangle_with_svg
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRaybaut committed Oct 10, 2023
1 parent 364ccfb commit f746b4d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 17 deletions.
43 changes: 27 additions & 16 deletions plotpy/tests/gui/svg_sample.svg → plotpy/tests/gui/svg_tool.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions plotpy/tests/gui/test_get_rectangle_with_svg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
#
# For licensing and distribution details, please read carefully xgrid/__init__.py

"""
Get rectangular selection from image with SVG shape
"""

# guitest: show

import os.path as osp

from guidata.env import execenv
from guidata.qthelpers import qt_app_context

from plotpy.builder import make
from plotpy.tests.data import gen_image4
from plotpy.tests.gui.test_get_segment import SEG_AXES_COORDS, PatchedSelectDialog
from plotpy.tools import RectangularShapeTool
from plotpy.widgets.selectdialog import select_with_shape_tool


class SVGToolExample(RectangularShapeTool):
"""Tool to select a rectangular area and create a pattern from it"""

TITLE = "Pattern selection tool"
ICON = "pattern.svg"
AVOID_NULL_SHAPE = True
SVG_FNAME = osp.join(osp.dirname(__file__), "svg_tool.svg")

def create_shape(self):
"""Create shape to be drawn"""
svg_data = open(self.SVG_FNAME, "rb").read()
shape = make.svg("rectangle", svg_data, 0, 0, 1, 1, "SVG")
self.set_shape_style(shape)
return shape, 0, 2


def test_get_rectangle_with_svg():
"""Test get_rectangle_with_svg"""
with qt_app_context():
image = make.image(data=gen_image4(200, 200), colormap="gray")
shape = select_with_shape_tool(
None, SVGToolExample, image, "Test", tooldialogclass=PatchedSelectDialog
)
rect = shape.get_rect()
if execenv.unattended:
assert [round(i) for i in list(rect)] == SEG_AXES_COORDS
elif rect is not None:
print("Area:", rect)


if __name__ == "__main__":
test_get_rectangle_with_svg()
2 changes: 1 addition & 1 deletion plotpy/tests/gui/test_svgshapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_path(fname):
def test_image():
"""Testing ImageItem object"""
with qt_app_context(exec_loop=True):
path1, path2 = get_path("svg_sample.svg"), get_path("svg_target.svg")
path1, path2 = get_path("svg_tool.svg"), get_path("svg_target.svg")
csvg = make.svg("circle", path2, 0, 0, 100, 100, "Circle")
rsvg = make.svg("rectangle", path1, 150, 0, 200, 100, "Rect")
ssvg = make.svg("square", path1, 0, 150, 100, 250, "Square")
Expand Down
5 changes: 5 additions & 0 deletions plotpy/widgets/selectdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
.. literalinclude:: ../../plotpy/tests/gui/test_get_rectangle.py
Example: get rectangle with SVG
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. literalinclude:: ../../plotpy/tests/gui/test_get_rectangle_with_svg.py
Reference
~~~~~~~~~
Expand Down

0 comments on commit f746b4d

Please sign in to comment.