src/eric7/Graphics/PackageItem.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
17 17
18 class PackageModel(UMLModel): 18 class PackageModel(UMLModel):
19 """ 19 """
20 Class implementing the package model. 20 Class implementing the package model.
21 """ 21 """
22
22 def __init__(self, name, moduleslist=None): 23 def __init__(self, name, moduleslist=None):
23 """ 24 """
24 Constructor 25 Constructor
25 26
26 @param name package name 27 @param name package name
27 @type str 28 @type str
28 @param moduleslist list of module names 29 @param moduleslist list of module names
29 @type list of str 30 @type list of str
30 """ 31 """
31 super().__init__(name) 32 super().__init__(name)
32 33
33 self.moduleslist = [] if moduleslist is None else moduleslist[:] 34 self.moduleslist = [] if moduleslist is None else moduleslist[:]
34 35
35 def addModule(self, modulename): 36 def addModule(self, modulename):
36 """ 37 """
37 Public method to add a module to the package model. 38 Public method to add a module to the package model.
38 39
39 @param modulename module name to be added 40 @param modulename module name to be added
40 @type str 41 @type str
41 """ 42 """
42 self.moduleslist.append(modulename) 43 self.moduleslist.append(modulename)
43 44
44 def getModules(self): 45 def getModules(self):
45 """ 46 """
46 Public method to retrieve the modules of the package. 47 Public method to retrieve the modules of the package.
47 48
48 @return list of module names 49 @return list of module names
49 @rtype list of str 50 @rtype list of str
50 """ 51 """
51 return self.moduleslist[:] 52 return self.moduleslist[:]
52 53
53 54
54 class PackageItem(UMLItem): 55 class PackageItem(UMLItem):
55 """ 56 """
56 Class implementing a package item. 57 Class implementing a package item.
57 """ 58 """
59
58 ItemType = "package" 60 ItemType = "package"
59 61
60 def __init__(self, model=None, x=0, y=0, rounded=False, 62 def __init__(
61 noModules=False, colors=None, parent=None, scene=None): 63 self,
64 model=None,
65 x=0,
66 y=0,
67 rounded=False,
68 noModules=False,
69 colors=None,
70 parent=None,
71 scene=None,
72 ):
62 """ 73 """
63 Constructor 74 Constructor
64 75
65 @param model package model containing the package data 76 @param model package model containing the package data
66 @type PackageModel 77 @type PackageModel
67 @param x x-coordinate 78 @param x x-coordinate
68 @type int 79 @type int
69 @param y y-coordinate 80 @param y y-coordinate
80 @param scene reference to the scene object 91 @param scene reference to the scene object
81 @type QGraphicsScene 92 @type QGraphicsScene
82 """ 93 """
83 UMLItem.__init__(self, model, x, y, rounded, colors, parent) 94 UMLItem.__init__(self, model, x, y, rounded, colors, parent)
84 self.noModules = noModules 95 self.noModules = noModules
85 96
86 if scene: 97 if scene:
87 scene.addItem(self) 98 scene.addItem(self)
88 99
89 if self.model: 100 if self.model:
90 self.__createTexts() 101 self.__createTexts()
91 self.__calculateSize() 102 self.__calculateSize()
92 103
93 def __createTexts(self): 104 def __createTexts(self):
94 """ 105 """
95 Private method to create the text items of the class item. 106 Private method to create the text items of the class item.
96 """ 107 """
97 if self.model is None: 108 if self.model is None:
98 return 109 return
99 110
100 boldFont = QFont(self.font) 111 boldFont = QFont(self.font)
101 boldFont.setBold(True) 112 boldFont.setBold(True)
102 113
103 modules = self.model.getModules() 114 modules = self.model.getModules()
104 115
105 x = self.margin + int(self.rect().x()) 116 x = self.margin + int(self.rect().x())
106 y = self.margin + int(self.rect().y()) 117 y = self.margin + int(self.rect().y())
107 self.header = QGraphicsSimpleTextItem(self) 118 self.header = QGraphicsSimpleTextItem(self)
108 self.header.setBrush(self._colors[0]) 119 self.header.setBrush(self._colors[0])
109 self.header.setFont(boldFont) 120 self.header.setFont(boldFont)
110 self.header.setText(self.model.getName()) 121 self.header.setText(self.model.getName())
111 self.header.setPos(x, y) 122 self.header.setPos(x, y)
112 y += int(self.header.boundingRect().height()) + self.margin 123 y += int(self.header.boundingRect().height()) + self.margin
113 124
114 if not self.noModules: 125 if not self.noModules:
115 if modules: 126 if modules:
116 txt = "\n".join(modules) 127 txt = "\n".join(modules)
117 else: 128 else:
118 txt = " " 129 txt = " "
121 self.modules.setFont(self.font) 132 self.modules.setFont(self.font)
122 self.modules.setText(txt) 133 self.modules.setText(txt)
123 self.modules.setPos(x, y) 134 self.modules.setPos(x, y)
124 else: 135 else:
125 self.modules = None 136 self.modules = None
126 137
127 def __calculateSize(self): 138 def __calculateSize(self):
128 """ 139 """
129 Private method to calculate the size of the package widget. 140 Private method to calculate the size of the package widget.
130 """ 141 """
131 if self.model is None: 142 if self.model is None:
132 return 143 return
133 144
134 width = int(self.header.boundingRect().width()) 145 width = int(self.header.boundingRect().width())
135 height = int(self.header.boundingRect().height()) 146 height = int(self.header.boundingRect().height())
136 if self.modules: 147 if self.modules:
137 width = max(width, 148 width = max(width, int(self.modules.boundingRect().width()))
138 int(self.modules.boundingRect().width()))
139 height += int(self.modules.boundingRect().height()) 149 height += int(self.modules.boundingRect().height())
140 latchW = width / 3.0 150 latchW = width / 3.0
141 latchH = min(15.0, latchW) 151 latchH = min(15.0, latchW)
142 self.setSize(width + 2 * self.margin, 152 self.setSize(width + 2 * self.margin, height + latchH + 2 * self.margin)
143 height + latchH + 2 * self.margin) 153
144
145 x = self.margin + int(self.rect().x()) 154 x = self.margin + int(self.rect().x())
146 y = self.margin + int(self.rect().y()) + latchH 155 y = self.margin + int(self.rect().y()) + latchH
147 self.header.setPos(x, y) 156 self.header.setPos(x, y)
148 y += int(self.header.boundingRect().height()) + self.margin 157 y += int(self.header.boundingRect().height()) + self.margin
149 if self.modules: 158 if self.modules:
150 self.modules.setPos(x, y) 159 self.modules.setPos(x, y)
151 160
152 def setModel(self, model): 161 def setModel(self, model):
153 """ 162 """
154 Public method to set the package model. 163 Public method to set the package model.
155 164
156 @param model package model containing the package data 165 @param model package model containing the package data
157 @type PackageModel 166 @type PackageModel
158 """ 167 """
159 self.scene().removeItem(self.header) 168 self.scene().removeItem(self.header)
160 self.header = None 169 self.header = None
162 self.scene().removeItem(self.modules) 171 self.scene().removeItem(self.modules)
163 self.modules = None 172 self.modules = None
164 self.model = model 173 self.model = model
165 self.__createTexts() 174 self.__createTexts()
166 self.__calculateSize() 175 self.__calculateSize()
167 176
168 def paint(self, painter, option, widget=None): 177 def paint(self, painter, option, widget=None):
169 """ 178 """
170 Public method to paint the item in local coordinates. 179 Public method to paint the item in local coordinates.
171 180
172 @param painter reference to the painter object 181 @param painter reference to the painter object
173 @type QPainter 182 @type QPainter
174 @param option style options 183 @param option style options
175 @type QStyleOptionGraphicsItem 184 @type QStyleOptionGraphicsItem
176 @param widget optional reference to the widget painted on 185 @param widget optional reference to the widget painted on
177 @type QWidget 186 @type QWidget
178 """ 187 """
179 pen = self.pen() 188 pen = self.pen()
180 if ( 189 if (
181 (option.state & QStyle.StateFlag.State_Selected) == 190 option.state & QStyle.StateFlag.State_Selected
182 QStyle.StateFlag.State_Selected 191 ) == QStyle.StateFlag.State_Selected:
183 ):
184 pen.setWidth(2) 192 pen.setWidth(2)
185 else: 193 else:
186 pen.setWidth(1) 194 pen.setWidth(1)
187 195
188 offsetX = int(self.rect().x()) 196 offsetX = int(self.rect().x())
189 offsetY = int(self.rect().y()) 197 offsetY = int(self.rect().y())
190 w = int(self.rect().width()) 198 w = int(self.rect().width())
191 latchW = w / 3.0 199 latchW = w / 3.0
192 latchH = min(15.0, latchW) 200 latchH = min(15.0, latchW)
193 h = int(self.rect().height() - latchH + 1) 201 h = int(self.rect().height() - latchH + 1)
194 202
195 painter.setPen(pen) 203 painter.setPen(pen)
196 painter.setBrush(self.brush()) 204 painter.setBrush(self.brush())
197 painter.setFont(self.font) 205 painter.setFont(self.font)
198 206
199 painter.drawRect(offsetX, offsetY, int(latchW), int(latchH)) 207 painter.drawRect(offsetX, offsetY, int(latchW), int(latchH))
200 painter.drawRect(offsetX, offsetY + int(latchH), w, h) 208 painter.drawRect(offsetX, offsetY + int(latchH), w, h)
201 y = int(self.margin + self.header.boundingRect().height() + latchH) 209 y = int(self.margin + self.header.boundingRect().height() + latchH)
202 painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y) 210 painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
203 211
204 self.adjustAssociations() 212 self.adjustAssociations()
205 213
206 def parseItemDataString(self, version, data): 214 def parseItemDataString(self, version, data):
207 """ 215 """
208 Public method to parse the given persistence data. 216 Public method to parse the given persistence data.
209 217
210 @param version version of the data 218 @param version version of the data
211 @type str 219 @type str
212 @param data persisted data to be parsed 220 @param data persisted data to be parsed
213 @type str 221 @type str
214 @return flag indicating success 222 @return flag indicating success
215 @rtype bool 223 @rtype bool
216 """ 224 """
217 parts = data.split(", ") 225 parts = data.split(", ")
218 if len(parts) < 2: 226 if len(parts) < 2:
219 return False 227 return False
220 228
221 name = "" 229 name = ""
222 modules = [] 230 modules = []
223 231
224 for part in parts: 232 for part in parts:
225 key, value = part.split("=", 1) 233 key, value = part.split("=", 1)
226 if key == "no_modules": 234 if key == "no_modules":
227 self.external = Utilities.toBool(value.strip()) 235 self.external = Utilities.toBool(value.strip())
228 elif key == "name": 236 elif key == "name":
229 name = value.strip() 237 name = value.strip()
230 elif key == "modules": 238 elif key == "modules":
231 modules = value.strip().split("||") 239 modules = value.strip().split("||")
232 else: 240 else:
233 return False 241 return False
234 242
235 self.model = PackageModel(name, modules) 243 self.model = PackageModel(name, modules)
236 self.__createTexts() 244 self.__createTexts()
237 self.__calculateSize() 245 self.__calculateSize()
238 246
239 return True 247 return True
240 248
241 def toDict(self): 249 def toDict(self):
242 """ 250 """
243 Public method to collect data to be persisted. 251 Public method to collect data to be persisted.
244 252
245 @return dictionary containing data to be persisted 253 @return dictionary containing data to be persisted
246 @rtype dict 254 @rtype dict
247 """ 255 """
248 return { 256 return {
249 "id": self.getId(), 257 "id": self.getId(),
252 "type": self.getItemType(), 260 "type": self.getItemType(),
253 "model_name": self.model.getName(), 261 "model_name": self.model.getName(),
254 "no_nodules": self.noModules, 262 "no_nodules": self.noModules,
255 "modules": self.model.getModules(), 263 "modules": self.model.getModules(),
256 } 264 }
257 265
258 @classmethod 266 @classmethod
259 def fromDict(cls, data, colors=None): 267 def fromDict(cls, data, colors=None):
260 """ 268 """
261 Class method to create a class item from persisted data. 269 Class method to create a class item from persisted data.
262 270
263 @param data dictionary containing the persisted data as generated 271 @param data dictionary containing the persisted data as generated
264 by toDict() 272 by toDict()
265 @type dict 273 @type dict
266 @param colors tuple containing the foreground and background colors 274 @param colors tuple containing the foreground and background colors
267 @type tuple of (QColor, QColor) 275 @type tuple of (QColor, QColor)
268 @return created class item 276 @return created class item
269 @rtype ClassItem 277 @rtype ClassItem
270 """ 278 """
271 try: 279 try:
272 model = PackageModel(data["model_name"], 280 model = PackageModel(data["model_name"], data["modules"])
273 data["modules"]) 281 itm = cls(model, x=0, y=0, noModules=data["no_nodules"], colors=colors)
274 itm = cls(model,
275 x=0,
276 y=0,
277 noModules=data["no_nodules"],
278 colors=colors)
279 itm.setPos(data["x"], data["y"]) 282 itm.setPos(data["x"], data["y"])
280 itm.setId(data["id"]) 283 itm.setId(data["id"])
281 return itm 284 return itm
282 except KeyError: 285 except KeyError:
283 return None 286 return None

eric ide

mercurial