Graphics/UMLItem.py

changeset 2039
fa41ccd7f7bc
parent 2038
72557ef75de1
child 2302
f29e9405c851
equal deleted inserted replaced
2038:72557ef75de1 2039:fa41ccd7f7bc
11 from PyQt4.QtGui import QGraphicsItem, QGraphicsRectItem, QStyle 11 from PyQt4.QtGui import QGraphicsItem, QGraphicsRectItem, QStyle
12 12
13 import Preferences 13 import Preferences
14 14
15 15
16 class UMLModel(object):
17 """
18 Class implementing the UMLModel base class.
19 """
20 def __init__(self, name):
21 """
22 Constructor
23
24 @param name package name (string)
25 """
26 self.name = name
27
28 def getName(self):
29 """
30 Method to retrieve the model name.
31
32 @return model name (string)
33 """
34 return self.name
35
36
16 class UMLItem(QGraphicsRectItem): 37 class UMLItem(QGraphicsRectItem):
17 """ 38 """
18 Class implementing the UMLItem base class. 39 Class implementing the UMLItem base class.
19 """ 40 """
20 ItemType = "UMLItem" 41 ItemType = "UMLItem"
21 42
22 def __init__(self, x=0, y=0, rounded=False, parent=None): 43 def __init__(self, model=None, x=0, y=0, rounded=False, parent=None):
23 """ 44 """
24 Constructor 45 Constructor
25 46
47 @param model UML model containing the item data (UMLModel)
26 @param x x-coordinate (integer) 48 @param x x-coordinate (integer)
27 @param y y-coordinate (integer) 49 @param y y-coordinate (integer)
28 @param rounded flag indicating a rounded corner (boolean) 50 @param rounded flag indicating a rounded corner (boolean)
29 @keyparam parent reference to the parent object (QGraphicsItem) 51 @keyparam parent reference to the parent object (QGraphicsItem)
30 """ 52 """
31 super().__init__(parent) 53 super().__init__(parent)
54 self.model = model
55
32 self.font = Preferences.getGraphics("Font") 56 self.font = Preferences.getGraphics("Font")
33 self.margin = 5 57 self.margin = 5
34 self.associations = [] 58 self.associations = []
35 self.shouldAdjustAssociations = False 59 self.shouldAdjustAssociations = False
36 self.__id = -1 60 self.__id = -1
47 try: 71 try:
48 self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) 72 self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
49 except AttributeError: 73 except AttributeError:
50 # only available for Qt 4.6.0 and newer 74 # only available for Qt 4.6.0 and newer
51 pass 75 pass
52 76
77 def getName(self):
78 """
79 Method to retrieve the item name.
80
81 @return item name (string)
82 """
83 if self.model:
84 return self.model.name
85 else:
86 return ""
87
53 def setSize(self, width, height): 88 def setSize(self, width, height):
54 """ 89 """
55 Public method to set the rectangles size. 90 Public method to set the rectangles size.
56 91
57 @param width width of the rectangle (float) 92 @param width width of the rectangle (float)
58 @param height height of the rectangle (float) 93 @param height height of the rectangle (float)
59 """ 94 """
60 rect = self.rect() 95 rect = self.rect()
61 rect.setSize(QSizeF(width, height)) 96 rect.setSize(QSizeF(width, height))
62 self.setRect(rect) 97 self.setRect(rect)
63 98
64 def addAssociation(self, assoc): 99 def addAssociation(self, assoc):
65 """ 100 """
66 Method to add an association to this widget. 101 Method to add an association to this widget.
67 102
68 @param assoc association to be added (AssociationWidget) 103 @param assoc association to be added (AssociationWidget)
69 """ 104 """
70 if assoc and not assoc in self.associations: 105 if assoc and not assoc in self.associations:
71 self.associations.append(assoc) 106 self.associations.append(assoc)
72 107
73 def removeAssociation(self, assoc): 108 def removeAssociation(self, assoc):
74 """ 109 """
75 Method to remove an association to this widget. 110 Method to remove an association to this widget.
76 111
77 @param assoc association to be removed (AssociationWidget) 112 @param assoc association to be removed (AssociationWidget)
78 """ 113 """
79 if assoc and assoc in self.associations: 114 if assoc and assoc in self.associations:
80 self.associations.remove(assoc) 115 self.associations.remove(assoc)
81 116
82 def removeAssociations(self): 117 def removeAssociations(self):
83 """ 118 """
84 Method to remove all associations of this widget. 119 Method to remove all associations of this widget.
85 """ 120 """
86 for assoc in self.associations[:]: 121 for assoc in self.associations[:]:
87 assoc.unassociate() 122 assoc.unassociate()
88 assoc.hide() 123 assoc.hide()
89 del assoc 124 del assoc
90 125
91 def adjustAssociations(self): 126 def adjustAssociations(self):
92 """ 127 """
93 Method to adjust the associations to widget movements. 128 Method to adjust the associations to widget movements.
94 """ 129 """
95 if self.shouldAdjustAssociations: 130 if self.shouldAdjustAssociations:
96 for assoc in self.associations: 131 for assoc in self.associations:
97 assoc.widgetMoved() 132 assoc.widgetMoved()
98 self.shouldAdjustAssociations = False 133 self.shouldAdjustAssociations = False
99 134
100 def moveBy(self, dx, dy): 135 def moveBy(self, dx, dy):
101 """ 136 """
102 Overriden method to move the widget relative. 137 Overriden method to move the widget relative.
103 138
104 @param dx relative movement in x-direction (float) 139 @param dx relative movement in x-direction (float)
105 @param dy relative movement in y-direction (float) 140 @param dy relative movement in y-direction (float)
106 """ 141 """
107 super().moveBy(dx, dy) 142 super().moveBy(dx, dy)
108 self.adjustAssociations() 143 self.adjustAssociations()
109 144
110 def setPos(self, x, y): 145 def setPos(self, x, y):
111 """ 146 """
112 Overriden method to set the items position. 147 Overriden method to set the items position.
113 148
114 @param x absolute x-position (float) 149 @param x absolute x-position (float)
115 @param y absolute y-position (float) 150 @param y absolute y-position (float)
116 """ 151 """
117 super().setPos(x, y) 152 super().setPos(x, y)
118 self.adjustAssociations() 153 self.adjustAssociations()
119 154
120 def itemChange(self, change, value): 155 def itemChange(self, change, value):
121 """ 156 """
122 Protected method called when an items state changes. 157 Protected method called when an items state changes.
123 158
124 @param change the item's change (QGraphicsItem.GraphicsItemChange) 159 @param change the item's change (QGraphicsItem.GraphicsItemChange)
136 value.setX(min(rect.right(), max(value.x(), rect.left()))) 171 value.setX(min(rect.right(), max(value.x(), rect.left())))
137 value.setY(min(rect.bottom(), max(value.y(), rect.top()))) 172 value.setY(min(rect.bottom(), max(value.y(), rect.top())))
138 return value 173 return value
139 174
140 return QGraphicsItem.itemChange(self, change, value) 175 return QGraphicsItem.itemChange(self, change, value)
141 176
142 def paint(self, painter, option, widget=None): 177 def paint(self, painter, option, widget=None):
143 """ 178 """
144 Public method to paint the item in local coordinates. 179 Public method to paint the item in local coordinates.
145 180
146 @param painter reference to the painter object (QPainter) 181 @param painter reference to the painter object (QPainter)
155 190
156 painter.setPen(pen) 191 painter.setPen(pen)
157 painter.setBrush(self.brush()) 192 painter.setBrush(self.brush())
158 painter.drawRect(self.rect()) 193 painter.drawRect(self.rect())
159 self.adjustAssociations() 194 self.adjustAssociations()
160 195
161 def setId(self, id): 196 def setId(self, id):
162 """ 197 """
163 Public method to assign an ID to the item. 198 Public method to assign an ID to the item.
164 199
165 @param id assigned ID (integer) 200 @param id assigned ID (integer)

eric ide

mercurial