src/eric7/Graphics/ModuleItem.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2007 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a module item.
8 """
9
10 from PyQt6.QtGui import QFont
11 from PyQt6.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 + int(self.rect().x())
100 y = self.margin + int(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 += int(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 = int(self.header.boundingRect().width())
122 height = int(self.header.boundingRect().height())
123 if self.classes:
124 width = max(width, int(self.classes.boundingRect().width()))
125 height += int(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.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 = int(self.rect().x())
169 offsetY = int(self.rect().y())
170 w = int(self.rect().width())
171 h = int(self.rect().height())
172
173 painter.drawRect(offsetX, offsetY, w, h)
174 y = self.margin + int(self.header.boundingRect().height())
175 painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
176
177 self.adjustAssociations()
178
179 def parseItemDataString(self, version, data):
180 """
181 Public method to parse the given persistence data.
182
183 @param version version of the data
184 @type str
185 @param data persisted data to be parsed
186 @type str
187 @return flag indicating success
188 @rtype bool
189 """
190 parts = data.split(", ")
191 if len(parts) < 1:
192 return False
193
194 name = ""
195 classes = []
196
197 for part in parts:
198 key, value = part.split("=", 1)
199 if key == "name":
200 name = value.strip()
201 elif key == "classes":
202 classes = value.strip().split("||")
203 else:
204 return False
205
206 self.model = ModuleModel(name, classes)
207 self.__createTexts()
208 self.__calculateSize()
209
210 return True
211
212 def toDict(self):
213 """
214 Public method to collect data to be persisted.
215
216 @return dictionary containing data to be persisted
217 @rtype dict
218 """
219 return {
220 "id": self.getId(),
221 "x": self.x(),
222 "y": self.y(),
223 "type": self.getItemType(),
224 "model_name": self.model.getName(),
225 "classes": self.model.getClasses(),
226 }
227
228 @classmethod
229 def fromDict(cls, data, colors=None):
230 """
231 Class method to create a class item from persisted data.
232
233 @param data dictionary containing the persisted data as generated
234 by toDict()
235 @type dict
236 @param colors tuple containing the foreground and background colors
237 @type tuple of (QColor, QColor)
238 @return created class item
239 @rtype ClassItem
240 """
241 try:
242 model = ModuleModel(data["model_name"],
243 data["classes"])
244 itm = cls(model,
245 x=0,
246 y=0,
247 colors=colors)
248 itm.setPos(data["x"], data["y"])
249 itm.setId(data["id"])
250 return itm
251 except KeyError:
252 return None

eric ide

mercurial