Skip to content

Commit f746b4d

Browse files
committed
Added test_get_rectangle_with_svg
1 parent 364ccfb commit f746b4d

File tree

4 files changed

+87
-17
lines changed

4 files changed

+87
-17
lines changed
Lines changed: 27 additions & 16 deletions
Loading
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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()

plotpy/tests/gui/test_svgshapes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def get_path(fname):
2323
def test_image():
2424
"""Testing ImageItem object"""
2525
with qt_app_context(exec_loop=True):
26-
path1, path2 = get_path("svg_sample.svg"), get_path("svg_target.svg")
26+
path1, path2 = get_path("svg_tool.svg"), get_path("svg_target.svg")
2727
csvg = make.svg("circle", path2, 0, 0, 100, 100, "Circle")
2828
rsvg = make.svg("rectangle", path1, 150, 0, 200, 100, "Rect")
2929
ssvg = make.svg("square", path1, 0, 150, 100, 250, "Square")

plotpy/widgets/selectdialog.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
2424
.. literalinclude:: ../../plotpy/tests/gui/test_get_rectangle.py
2525
26+
Example: get rectangle with SVG
27+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28+
29+
.. literalinclude:: ../../plotpy/tests/gui/test_get_rectangle_with_svg.py
30+
2631
Reference
2732
~~~~~~~~~
2833

0 commit comments

Comments
 (0)