source: orange/Orange/OrangeCanvas/canvas/items/tests/test_graphicspathobject.py @ 11160:6bfea7812243

Revision 11160:6bfea7812243, 2.0 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Refactored GraphicsPathObject and ControlPoint/Rect/Line into two new modules.

Line 
1from PyQt4.QtGui import QPainterPath, QBrush, QPen, QColor
2from PyQt4.QtCore import QPointF
3
4from . import TestItems
5
6from ..graphicspathobject import GraphicsPathObject, shapeFromPath
7
8
9def area(rect):
10    return rect.width() * rect.height()
11
12
13class TestGraphicsPathObject(TestItems):
14
15    def test_graphicspathobject(self):
16        obj = GraphicsPathObject()
17        path = QPainterPath()
18        obj.setFlag(GraphicsPathObject.ItemIsMovable)
19
20        path.addEllipse(20, 20, 50, 50)
21
22        obj.setPath(path)
23        self.assertEqual(obj.path(), path)
24
25        self.assertTrue(obj.path() is not path,
26                        msg="setPath stores the path not a copy")
27
28        brect = obj.boundingRect()
29        self.assertTrue(area(brect) == 50 ** 2)
30
31        with self.assertRaises(TypeError):
32            obj.setPath("This is not a path")
33
34        brush = QBrush(QColor("#ffbb11"))
35        obj.setBrush(brush)
36
37        self.assertEqual(obj.brush(), brush)
38
39        self.assertTrue(obj.brush() is not brush,
40                        "setBrush stores the brush not a copy")
41
42        pen = QPen(QColor("#FFFFFF"), 1.4)
43        obj.setPen(pen)
44
45        self.assertEqual(obj.pen(), pen)
46
47        self.assertTrue(obj.pen() is not pen,
48                        "setPen stores the pen not a copy")
49
50        brect = obj.boundingRect()
51        self.assertGreaterEqual(area(brect), (50 + 1.4 * 2) ** 2)
52
53        self.assertIsInstance(obj.shape(), QPainterPath)
54
55        positions = []
56        obj.positionChanged[QPointF].connect(positions.append)
57
58        pos = QPointF(10, 10)
59        obj.setPos(pos)
60
61        self.assertEqual(positions, [pos])
62
63        self.scene.addItem(obj)
64        self.view.show()
65
66        self.app.exec_()
67
68    def test_shapeFromPath(self):
69        path = QPainterPath()
70        path.addRect(10, 10, 20, 20)
71
72        pen = QPen(QColor("#FFF"), 2.0)
73        path = shapeFromPath(path, pen)
74
75        self.assertGreaterEqual(area(path.controlPointRect()),
76                                (20 + 2.0) ** 2)
Note: See TracBrowser for help on using the repository browser.