Graphics/PackageItem.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 a package item.
8 """
9
10 from PyQt4.QtCore import *
11 from PyQt4.QtGui import *
12
13 from UMLItem import UMLItem
14
15 class PackageModel(object):
16 """
17 Class implementing the package model.
18 """
19 def __init__(self, name, moduleslist = []):
20 """
21 Constructor
22
23 @param name package name (string)
24 @param moduleslist list of module names (list of strings)
25 """
26 self.name = name
27 self.moduleslist = moduleslist
28
29 def addModule(self, modulename):
30 """
31 Method to add a module to the package model.
32
33 @param modulename module name to be added (string)
34 """
35 self.moduleslist.append(classname)
36
37 def getModules(self):
38 """
39 Method to retrieve the modules of the package.
40
41 @return list of module names (list of strings)
42 """
43 return self.moduleslist[:]
44
45 def getName(self):
46 """
47 Method to retrieve the package name.
48
49 @return package name (string)
50 """
51 return self.name
52
53 class PackageItem(UMLItem):
54 """
55 Class implementing a package item.
56 """
57 def __init__(self, model = None, x = 0, y = 0, rounded = False,
58 noModules = False, parent = None, scene = None):
59 """
60 Constructor
61
62 @param model module model containing the module data (ModuleModel)
63 @param x x-coordinate (integer)
64 @param y y-coordinate (integer)
65 @param rounded flag indicating a rounded corner (boolean)
66 @keyparam noModules flag indicating, that no module names should be
67 shown (boolean)
68 @keyparam parent reference to the parent object (QGraphicsItem)
69 @keyparam scene reference to the scene object (QGraphicsScene)
70 """
71 UMLItem.__init__(self, x, y, rounded, parent)
72 self.model = model
73 self.noModules = noModules
74
75 scene.addItem(self)
76
77 if self.model:
78 self.__createTexts()
79 self.__calculateSize()
80
81 def __createTexts(self):
82 """
83 Private method to create the text items of the class item.
84 """
85 if self.model is None:
86 return
87
88 boldFont = QFont(self.font)
89 boldFont.setBold(True)
90
91 modules = self.model.getModules()
92
93 x = self.margin + self.rect().x()
94 y = self.margin + self.rect().y()
95 self.header = QGraphicsSimpleTextItem(self)
96 self.header.setFont(boldFont)
97 self.header.setText(self.model.getName())
98 self.header.setPos(x, y)
99 y += self.header.boundingRect().height() + self.margin
100
101 if not self.noModules:
102 if modules:
103 txt = "\n".join(modules)
104 else:
105 txt = " "
106 self.modules = QGraphicsSimpleTextItem(self)
107 self.modules.setFont(self.font)
108 self.modules.setText(txt)
109 self.modules.setPos(x, y)
110 else:
111 self.modules = None
112
113 def __calculateSize(self):
114 """
115 Method to calculate the size of the package widget.
116 """
117 if self.model is None:
118 return
119
120 width = self.header.boundingRect().width()
121 height = self.header.boundingRect().height()
122 if self.modules:
123 width = max(width, self.modules.boundingRect().width())
124 height = height + self.modules.boundingRect().height()
125 latchW = width / 3.0
126 latchH = min(15.0, latchW)
127 self.setSize(width + 2 * self.margin, height + latchH + 2 * self.margin)
128
129 x = self.margin + self.rect().x()
130 y = self.margin + self.rect().y() + latchH
131 self.header.setPos(x, y)
132 y += self.header.boundingRect().height() + self.margin
133 if self.modules:
134 self.modules.setPos(x, y)
135
136 def setModel(self, model):
137 """
138 Method to set the package model.
139
140 @param model package model containing the package data (PackageModel)
141 """
142 self.scene().removeItem(self.header)
143 self.header = None
144 if self.modules:
145 self.scene().removeItem(self.modules)
146 self.modules = None
147 self.model = model
148 self.__createTexts()
149 self.__calculateSize()
150
151 def paint(self, painter, option, widget = None):
152 """
153 Public method to paint the item in local coordinates.
154
155 @param painter reference to the painter object (QPainter)
156 @param option style options (QStyleOptionGraphicsItem)
157 @param widget optional reference to the widget painted on (QWidget)
158 """
159 pen = self.pen()
160 if (option.state & QStyle.State_Selected) == QStyle.State(QStyle.State_Selected):
161 pen.setWidth(2)
162 else:
163 pen.setWidth(1)
164
165 offsetX = self.rect().x()
166 offsetY = self.rect().y()
167 w = self.rect().width()
168 latchW = w / 3.0
169 latchH = min(15.0, latchW)
170 h = self.rect().height() - latchH + 1
171
172 painter.setPen(pen)
173 painter.setBrush(self.brush())
174 painter.setFont(self.font)
175
176 painter.drawRect(offsetX, offsetY, latchW, latchH)
177 painter.drawRect(offsetX, offsetY + latchH, w, h)
178 y = self.margin + self.header.boundingRect().height() + latchH
179 painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
180
181 self.adjustAssociations()

eric ide

mercurial