Graphics/AssociationItem.py

changeset 2024
717b72b32420
parent 1509
c0b5e693b0eb
child 2030
db11a2fe9bbc
equal deleted inserted replaced
2023:6636ca1adc33 2024:717b72b32420
33 Class implementing a graphics item for an association between two items. 33 Class implementing a graphics item for an association between two items.
34 34
35 The association is drawn as an arrow starting at the first items and 35 The association is drawn as an arrow starting at the first items and
36 ending at the second. 36 ending at the second.
37 """ 37 """
38 def __init__(self, itemA, itemB, type=Normal, parent=None): 38 def __init__(self, itemA, itemB, type=Normal, topToBottom=False,
39 parent=None):
39 """ 40 """
40 Constructor 41 Constructor
41 42
42 @param itemA first widget of the association 43 @param itemA first widget of the association
43 @param itemB second widget of the association 44 @param itemB second widget of the association
45 <ul> 46 <ul>
46 <li>Normal (default)</li> 47 <li>Normal (default)</li>
47 <li>Generalisation</li> 48 <li>Generalisation</li>
48 <li>Imports</li> 49 <li>Imports</li>
49 </ul> 50 </ul>
51 @keyparam topToBottom flag indicating to draw the association
52 from item A top to item B bottom (boolean)
50 @keyparam parent reference to the parent object (QGraphicsItem) 53 @keyparam parent reference to the parent object (QGraphicsItem)
51 """ 54 """
52 if type == Normal: 55 if type == Normal:
53 arrowType = NormalArrow 56 arrowType = NormalArrow
54 arrowFilled = True 57 arrowFilled = True
63 arrowFilled, arrowType, parent) 66 arrowFilled, arrowType, parent)
64 67
65 self.setFlag(QGraphicsItem.ItemIsMovable, False) 68 self.setFlag(QGraphicsItem.ItemIsMovable, False)
66 self.setFlag(QGraphicsItem.ItemIsSelectable, False) 69 self.setFlag(QGraphicsItem.ItemIsSelectable, False)
67 70
68 ## self.calculateEndingPoints = self.__calculateEndingPoints_center 71 if topToBottom:
69 self.calculateEndingPoints = self.__calculateEndingPoints_rectangle 72 self.calculateEndingPoints = self.__calculateEndingPoints_topToBottom
73 else:
74 ## self.calculateEndingPoints = self.__calculateEndingPoints_center
75 self.calculateEndingPoints = self.__calculateEndingPoints_rectangle
70 76
71 self.itemA = itemA 77 self.itemA = itemA
72 self.itemB = itemB 78 self.itemB = itemB
73 self.assocType = type 79 self.assocType = type
74 80
89 """ 95 """
90 rect = item.rect() 96 rect = item.rect()
91 tl = self.mapFromItem(item, rect.topLeft()) 97 tl = self.mapFromItem(item, rect.topLeft())
92 return QRectF(tl.x(), tl.y(), rect.width(), rect.height()) 98 return QRectF(tl.x(), tl.y(), rect.width(), rect.height())
93 99
100 def __calculateEndingPoints_topToBottom(self):
101 """
102 Private method to calculate the ending points of the association item.
103
104 The ending points are calculated from the top center of the lower item
105 to the bottom center of the upper item.
106 """
107 if self.itemA is None or self.itemB is None:
108 return
109
110 self.prepareGeometryChange()
111
112 rectA = self.__mapRectFromItem(self.itemA)
113 rectB = self.__mapRectFromItem(self.itemB)
114 midA = QPointF(rectA.x() + rectA.width() / 2.0,
115 rectA.y() + rectA.height() / 2.0)
116 midB = QPointF(rectB.x() + rectB.width() / 2.0,
117 rectB.y() + rectB.height() / 2.0)
118 if midA.y() > midB.y():
119 startP = QPointF(rectA.x() + rectA.width() / 2.0, rectA.y())
120 endP = QPointF(rectB.x() + rectB.width() / 2.0,
121 rectB.y() + rectB.height())
122 else:
123 startP = QPointF(rectA.x() + rectA.width() / 2.0,
124 rectA.y() + rectA.height())
125 endP = QPointF(rectB.x() + rectB.width() / 2.0, rectB.y())
126 self.setPoints(startP.x(), startP.y(), endP.x(), endP.y())
127
94 def __calculateEndingPoints_center(self): 128 def __calculateEndingPoints_center(self):
95 """ 129 """
96 Private method to calculate the ending points of the association item. 130 Private method to calculate the ending points of the association item.
97 131
98 The ending points are calculated from the centers of the 132 The ending points are calculated from the centers of the

eric ide

mercurial