eric7/Graphics/ModuleItem.py

branch
eric7
changeset 8312
800c432b34c8
parent 8295
3f5e8b0a338e
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2007 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a module item.
8 """
9
10 from PyQt5.QtGui import QFont
11 from PyQt5.QtWidgets import QGraphicsSimpleTextItem, QStyle
12
13 from .UMLItem import UMLModel, UMLItem
14
15
16 class ModuleModel(UMLModel):
17 """
18 Class implementing the module model.
19 """
20 def __init__(self, name, classlist=None):
21 """
22 Constructor
23
24 @param name the module name
25 @type str
26 @param classlist list of class names
27 @type list of str
28 """
29 super().__init__(name)
30
31 self.classlist = [] if classlist is None else classlist[:]
32
33 def addClass(self, classname):
34 """
35 Public method to add a class to the module model.
36
37 @param classname class name to be added
38 @type str
39 """
40 self.classlist.append(classname)
41
42 def getClasses(self):
43 """
44 Public method to retrieve the classes of the module.
45
46 @return list of class names
47 @rtype list of str
48 """
49 return self.classlist[:]
50
51
52 class ModuleItem(UMLItem):
53 """
54 Class implementing a module item.
55 """
56 ItemType = "module"
57
58 def __init__(self, model=None, x=0, y=0, rounded=False, colors=None,
59 parent=None, scene=None):
60 """
61 Constructor
62
63 @param model module model containing the module data
64 @type ModuleModel
65 @param x x-coordinate
66 @type int
67 @param y y-coordinate
68 @type int
69 @param rounded flag indicating a rounded corner
70 @type bool
71 @param colors tuple containing the foreground and background colors
72 @type tuple of (QColor, QColor)
73 @param parent reference to the parent object
74 @type QGraphicsItem
75 @param scene reference to the scene object
76 @type QGraphicsScene
77 """
78 UMLItem.__init__(self, model, x, y, rounded, colors, parent)
79
80 if scene:
81 scene.addItem(self)
82
83 if self.model:
84 self.__createTexts()
85 self.__calculateSize()
86
87 def __createTexts(self):
88 """
89 Private method to create the text items of the module item.
90 """
91 if self.model is None:
92 return
93
94 boldFont = QFont(self.font)
95 boldFont.setBold(True)
96
97 classes = self.model.getClasses()
98
99 x = self.margin + self.rect().x()
100 y = self.margin + self.rect().y()
101 self.header = QGraphicsSimpleTextItem(self)
102 self.header.setBrush(self._colors[0])
103 self.header.setFont(boldFont)
104 self.header.setText(self.model.getName())
105 self.header.setPos(x, y)
106 y += self.header.boundingRect().height() + self.margin
107 txt = "\n".join(classes) if classes else " "
108 self.classes = QGraphicsSimpleTextItem(self)
109 self.classes.setBrush(self._colors[0])
110 self.classes.setFont(self.font)
111 self.classes.setText(txt)
112 self.classes.setPos(x, y)
113
114 def __calculateSize(self):
115 """
116 Private method to calculate the size of the module item.
117 """
118 if self.model is None:
119 return
120
121 width = self.header.boundingRect().width()
122 height = self.header.boundingRect().height()
123 if self.classes:
124 width = max(width, self.classes.boundingRect().width())
125 height += self.classes.boundingRect().height()
126 self.setSize(width + 2 * self.margin, height + 2 * self.margin)
127
128 def setModel(self, model):
129 """
130 Public method to set the module model.
131
132 @param model module model containing the module data
133 @type ModuleModel
134 """
135 self.scene().removeItem(self.header)
136 self.header = None
137 if self.classes:
138 self.scene().removeItem(self.classes)
139 self.meths = None
140 self.model = model
141 self.__createTexts()
142 self.__calculateSize()
143
144 def paint(self, painter, option, widget=None):
145 """
146 Public method to paint the item in local coordinates.
147
148 @param painter reference to the painter object
149 @type QPainter
150 @param option style options
151 @type QStyleOptionGraphicsItem
152 @param widget optional reference to the widget painted on
153 @type QWidget
154 """
155 pen = self.pen()
156 if (
157 (option.state & QStyle.StateFlag.State_Selected) ==
158 QStyle.State(QStyle.StateFlag.State_Selected)
159 ):
160 pen.setWidth(2)
161 else:
162 pen.setWidth(1)
163
164 painter.setPen(pen)
165 painter.setBrush(self.brush())
166 painter.setFont(self.font)
167
168 offsetX = self.rect().x()
169 offsetY = self.rect().y()
170 w = self.rect().width()
171 h = self.rect().height()
172
173 painter.drawRect(offsetX, offsetY, w, h)
174 y = self.margin + self.header.boundingRect().height()
175 painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
176
177 self.adjustAssociations()
178
179 def buildItemDataString(self):
180 """
181 Public method to build a string to persist the specific item data.
182
183 This string must start with ", " and should be built like
184 "attribute=value" with pairs separated by ", ". value must not
185 contain ", " or newlines.
186
187 @return persistence data
188 @rtype str
189 """
190 entries = [
191 "name={0}".format(self.model.getName()),
192 ]
193 classes = self.model.getClasses()
194 if classes:
195 entries.append("classes={0}".format("||".join(classes)))
196
197 return ", " + ", ".join(entries)
198
199 def parseItemDataString(self, version, data):
200 """
201 Public method to parse the given persistence data.
202
203 @param version version of the data
204 @type str
205 @param data persisted data to be parsed
206 @type str
207 @return flag indicating success
208 @rtype bool
209 """
210 parts = data.split(", ")
211 if len(parts) < 1:
212 return False
213
214 name = ""
215 classes = []
216
217 for part in parts:
218 key, value = part.split("=", 1)
219 if key == "name":
220 name = value.strip()
221 elif key == "classes":
222 classes = value.strip().split("||")
223 else:
224 return False
225
226 self.model = ModuleModel(name, classes)
227 self.__createTexts()
228 self.__calculateSize()
229
230 return True
231
232 def toDict(self):
233 """
234 Public method to collect data to be persisted.
235
236 @return dictionary containing data to be persisted
237 @rtype dict
238 """
239 return {
240 "id": self.getId(),
241 "x": self.x(),
242 "y": self.y(),
243 "type": self.getItemType(),
244 "model_name": self.model.getName(),
245 "classes": self.model.getClasses(),
246 }
247
248 @classmethod
249 def fromDict(cls, data, colors=None):
250 """
251 Class method to create a class item from persisted data.
252
253 @param data dictionary containing the persisted data as generated
254 by toDict()
255 @type dict
256 @param colors tuple containing the foreground and background colors
257 @type tuple of (QColor, QColor)
258 @return created class item
259 @rtype ClassItem
260 """
261 try:
262 model = ModuleModel(data["model_name"],
263 data["classes"])
264 itm = cls(model,
265 x=0,
266 y=0,
267 colors=colors)
268 itm.setPos(data["x"], data["y"])
269 itm.setId(data["id"])
270 return itm
271 except KeyError:
272 return None

eric ide

mercurial