Graphics/ClassItem.py

changeset 0
de9c2efb9d02
child 12
1d8dd9706f46
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2007 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing an UML like class item.
8 """
9
10 from PyQt4.QtCore import *
11 from PyQt4.QtGui import *
12
13 from UMLItem import UMLItem
14
15 class ClassModel(object):
16 """
17 Class implementing the class model.
18 """
19 def __init__(self, name, methods = [], attributes = []):
20 """
21 Constructor
22
23 @param name the class name (string)
24 @param methods list of method names of the class
25 (list of strings)
26 @param attributes list of attribute names of the class
27 (list of strings)
28 """
29 self.name = name
30 self.methods = methods
31 self.attributes = attributes
32
33 def addMethod(self, method):
34 """
35 Method to add a method to the class model.
36
37 @param method method name to be added (string)
38 """
39 self.methods.append(method)
40
41 def addAttribute(self, attribute):
42 """
43 Method to add an attribute to the class model.
44
45 @param attribute attribute name to be added (string)
46 """
47 self.attributes.append(attribute)
48
49 def getMethods(self):
50 """
51 Method to retrieve the methods of the class.
52
53 @return list of class methods (list of strings)
54 """
55 return self.methods[:]
56
57 def getAttributes(self):
58 """
59 Method to retrieve the attributes of the class.
60
61 @return list of class attributes (list of strings)
62 """
63 return self.attributes[:]
64
65 def getName(self):
66 """
67 Method to retrieve the class name.
68
69 @return class name (string)
70 """
71 return self.name
72
73 class ClassItem(UMLItem):
74 """
75 Class implementing an UML like class item.
76 """
77 def __init__(self, model = None, external = False, x = 0, y = 0,
78 rounded = False, noAttrs = False, parent = None, scene = None):
79 """
80 Constructor
81
82 @param model class model containing the class data (ClassModel)
83 @param external flag indicating a class defined outside our scope (boolean)
84 @param x x-coordinate (integer)
85 @param y y-coordinate (integer)
86 @keyparam rounded flag indicating a rounded corner (boolean)
87 @keyparam noAttrs flag indicating, that no attributes should be shown (boolean)
88 @keyparam parent reference to the parent object (QGraphicsItem)
89 @keyparam scene reference to the scene object (QGraphicsScene)
90 """
91 UMLItem.__init__(self, x, y, rounded, parent)
92 self.model = model
93 self.external = external
94 self.noAttrs = noAttrs
95
96 scene.addItem(self)
97
98 if self.model:
99 self.__createTexts()
100 self.__calculateSize()
101
102 def __createTexts(self):
103 """
104 Private method to create the text items of the class item.
105 """
106 if self.model is None:
107 return
108
109 boldFont = QFont(self.font)
110 boldFont.setBold(True)
111
112 attrs = self.model.getAttributes()
113 meths = self.model.getMethods()
114
115 x = self.margin + self.rect().x()
116 y = self.margin + self.rect().y()
117 self.header = QGraphicsSimpleTextItem(self)
118 self.header.setFont(boldFont)
119 self.header.setText(self.model.getName())
120 self.header.setPos(x, y)
121 y += self.header.boundingRect().height() + self.margin
122 if not self.noAttrs and not self.external:
123 if attrs:
124 txt = "\n".join(attrs)
125 else:
126 txt = " "
127 self.attrs = QGraphicsSimpleTextItem(self)
128 self.attrs.setFont(self.font)
129 self.attrs.setText(txt)
130 self.attrs.setPos(x, y)
131 y += self.attrs.boundingRect().height() + self.margin
132 else:
133 self.attrs = None
134 if meths:
135 txt = "\n".join(meths)
136 else:
137 txt = " "
138 self.meths = QGraphicsSimpleTextItem(self)
139 self.meths.setFont(self.font)
140 self.meths.setText(txt)
141 self.meths.setPos(x, y)
142
143 def __calculateSize(self):
144 """
145 Private method to calculate the size of the class item.
146 """
147 if self.model is None:
148 return
149
150 width = self.header.boundingRect().width()
151 height = self.header.boundingRect().height()
152 if self.attrs:
153 width = max(width, self.attrs.boundingRect().width())
154 height = height + self.attrs.boundingRect().height() + self.margin
155 if self.meths:
156 width = max(width, self.meths.boundingRect().width())
157 height = height + self.meths.boundingRect().height()
158 self.setSize(width + 2 * self.margin, height + 2 * self.margin)
159
160 def setModel(self, model):
161 """
162 Method to set the class model.
163
164 @param model class model containing the class data (ClassModel)
165 """
166 self.scene().removeItem(self.header)
167 self.header = None
168 if self.attrs:
169 self.scene().removeItem(self.attrs)
170 self.attrs = None
171 if self.meths:
172 self.scene().removeItem(self.meths)
173 self.meths = None
174 self.model = model
175 self.__createTexts()
176 self.__calculateSize()
177
178 def paint(self, painter, option, widget = None):
179 """
180 Public method to paint the item in local coordinates.
181
182 @param painter reference to the painter object (QPainter)
183 @param option style options (QStyleOptionGraphicsItem)
184 @param widget optional reference to the widget painted on (QWidget)
185 """
186 pen = self.pen()
187 if (option.state & QStyle.State_Selected) == QStyle.State(QStyle.State_Selected):
188 pen.setWidth(2)
189 else:
190 pen.setWidth(1)
191
192 painter.setPen(pen)
193 painter.setBrush(self.brush())
194 painter.setFont(self.font)
195
196 offsetX = self.rect().x()
197 offsetY = self.rect().y()
198 w = self.rect().width()
199 h = self.rect().height()
200
201 painter.drawRect(offsetX, offsetY, w, h)
202 y = self.margin + self.header.boundingRect().height()
203 painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
204 if self.attrs:
205 y += self.margin + self.attrs.boundingRect().height()
206 painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
207
208 self.adjustAssociations()
209
210 def isExternal(self):
211 """
212 Method returning the external state.
213
214 @return external state (boolean)
215 """
216 return self.external

eric ide

mercurial