Graphics/UMLItem.py

changeset 0
de9c2efb9d02
child 7
c679fb30c8f3
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2004 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the UMLWidget base class.
8 """
9
10 from PyQt4.QtCore import *
11 from PyQt4.QtGui import *
12
13 import Preferences
14
15 class UMLItem(QGraphicsRectItem):
16 """
17 Class implementing the UMLItem base class.
18 """
19 def __init__(self, x = 0, y = 0, rounded = False, parent = None):
20 """
21 Constructor
22
23 @param x x-coordinate (integer)
24 @param y y-coordinate (integer)
25 @param rounded flag indicating a rounded corner (boolean)
26 @keyparam parent reference to the parent object (QGraphicsItem)
27 """
28 QGraphicsRectItem.__init__(self, parent)
29 self.font = Preferences.getGraphics("Font")
30 self.margin = 5
31 self.associations = []
32 self.shouldAdjustAssociations = False
33
34 self.setRect(x, y, 60, 30)
35
36 if rounded:
37 p = self.pen()
38 p.setCapStyle(Qt.RoundCap)
39 p.setJoinStyle(Qt.RoundJoin)
40
41 self.setFlag(QGraphicsItem.ItemIsMovable, True)
42 self.setFlag(QGraphicsItem.ItemIsSelectable, True)
43
44 def setSize(self, width, height):
45 """
46 Public method to set the rectangles size.
47
48 @param width width of the rectangle (float)
49 @param height height of the rectangle (float)
50 """
51 rect = self.rect()
52 rect.setSize(QSizeF(width, height))
53 self.setRect(rect)
54
55 def addAssociation(self, assoc):
56 """
57 Method to add an association to this widget.
58
59 @param assoc association to be added (AssociationWidget)
60 """
61 if assoc and not assoc in self.associations:
62 self.associations.append(assoc)
63
64 def removeAssociation(self, assoc):
65 """
66 Method to remove an association to this widget.
67
68 @param assoc association to be removed (AssociationWidget)
69 """
70 if assoc and assoc in self.associations:
71 self.associations.remove(assoc)
72
73 def removeAssociations(self):
74 """
75 Method to remove all associations of this widget.
76 """
77 for assoc in self.associations[:]:
78 assoc.unassociate()
79 assoc.hide()
80 del assoc
81
82 def adjustAssociations(self):
83 """
84 Method to adjust the associations to widget movements.
85 """
86 if self.shouldAdjustAssociations:
87 for assoc in self.associations:
88 assoc.widgetMoved()
89 self.shouldAdjustAssociations = False
90
91 def moveBy(self, dx, dy):
92 """
93 Overriden method to move the widget relative.
94
95 @param dx relative movement in x-direction (float)
96 @param dy relative movement in y-direction (float)
97 """
98 QGraphicsRectItem.moveBy(self, dx, dy)
99 self.adjustAssociations()
100
101 def setPos(self, x, y):
102 """
103 Overriden method to set the items position.
104
105 @param x absolute x-position (float)
106 @param y absolute y-position (float)
107 """
108 QGraphicsRectItem.setPos(self, x, y)
109 self.adjustAssociations()
110
111 def itemChange(self, change, value):
112 """
113 Protected method called when an items state changes.
114
115 @param change the item's change (QGraphicsItem.GraphicsItemChange)
116 @param value the value of the change (QVariant)
117 @return adjusted values (QVariant)
118 """
119 if change == QGraphicsItem.ItemPositionChange:
120 self.shouldAdjustAssociations = True
121 return QGraphicsItem.itemChange(self, change, value)
122
123 def paint(self, painter, option, widget = None):
124 """
125 Public method to paint the item in local coordinates.
126
127 @param painter reference to the painter object (QPainter)
128 @param option style options (QStyleOptionGraphicsItem)
129 @param widget optional reference to the widget painted on (QWidget)
130 """
131 pen = self.pen()
132 if (option.state & QStyle.State_Selected) == QStyle.State(QStyle.State_Selected):
133 pen.setWidth(2)
134 else:
135 pen.setWidth(1)
136
137 painter.setPen(pen)
138 painter.setBrush(self.brush())
139 painter.drawRect(self.rect())
140 self.adjustAssociations()

eric ide

mercurial