src/eric7/Graphics/ModuleItem.py

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

eric ide

mercurial