188 if change == QGraphicsItem.GraphicsItemChange.ItemPositionChange: |
188 if change == QGraphicsItem.GraphicsItemChange.ItemPositionChange: |
189 # 1. remember to adjust associations |
189 # 1. remember to adjust associations |
190 self.shouldAdjustAssociations = True |
190 self.shouldAdjustAssociations = True |
191 |
191 |
192 # 2. ensure the new position is inside the scene |
192 # 2. ensure the new position is inside the scene |
193 rect = self.scene().sceneRect() |
193 scene = self.scene() |
194 if not rect.contains(value): |
194 if scene: |
195 # keep the item inside the scene |
195 rect = scene.sceneRect() |
196 value.setX(min(rect.right(), max(value.x(), rect.left()))) |
196 if not rect.contains(value): |
197 value.setY(min(rect.bottom(), max(value.y(), rect.top()))) |
197 # keep the item inside the scene |
198 return value |
198 value.setX(min(rect.right(), max(value.x(), rect.left()))) |
|
199 value.setY(min(rect.bottom(), max(value.y(), rect.top()))) |
|
200 return value |
199 |
201 |
200 return QGraphicsItem.itemChange(self, change, value) |
202 return QGraphicsItem.itemChange(self, change, value) |
201 |
203 |
202 def paint(self, painter, option, widget=None): |
204 def paint(self, painter, option, widget=None): |
203 """ |
205 """ |
282 Public method to collect data to be persisted. |
284 Public method to collect data to be persisted. |
283 |
285 |
284 @return dictionary containing data to be persisted |
286 @return dictionary containing data to be persisted |
285 @rtype dict |
287 @rtype dict |
286 """ |
288 """ |
287 return {} |
289 return { |
|
290 "id": self.getId(), |
|
291 "x": self.x(), |
|
292 "y": self.y(), |
|
293 "type": self.getItemType(), |
|
294 "model_name": self.model.getName(), |
|
295 } |
|
296 |
|
297 @classmethod |
|
298 def fromDict(cls, data, colors=None): |
|
299 """ |
|
300 Class method to create a generic UML item from persisted data. |
|
301 |
|
302 @param data dictionary containing the persisted data as generated |
|
303 by toDict() |
|
304 @type dict |
|
305 @param colors tuple containing the foreground and background colors |
|
306 @type tuple of (QColor, QColor) |
|
307 @return created UML item |
|
308 @rtype UMLItem |
|
309 """ |
|
310 try: |
|
311 model = UMLModel(data["model_name"]) |
|
312 itm = cls(model=model, |
|
313 x=0, |
|
314 y=0, |
|
315 colors=colors) |
|
316 itm.setPos(data["x"], data["y"]) |
|
317 itm.setId(data["id"]) |
|
318 return itm |
|
319 except KeyError: |
|
320 return None |