eric6/Graphics/UMLItem.py

changeset 8295
3f5e8b0a338e
parent 8291
3d79b1e5bf3c
diff -r cb4e5bbf3a2c -r 3f5e8b0a338e eric6/Graphics/UMLItem.py
--- a/eric6/Graphics/UMLItem.py	Thu May 06 19:46:00 2021 +0200
+++ b/eric6/Graphics/UMLItem.py	Sat May 08 18:34:08 2021 +0200
@@ -190,12 +190,14 @@
             self.shouldAdjustAssociations = True
             
             # 2. ensure the new position is inside the scene
-            rect = self.scene().sceneRect()
-            if not rect.contains(value):
-                # keep the item inside the scene
-                value.setX(min(rect.right(), max(value.x(), rect.left())))
-                value.setY(min(rect.bottom(), max(value.y(), rect.top())))
-                return value
+            scene = self.scene()
+            if scene:
+                rect = scene.sceneRect()
+                if not rect.contains(value):
+                    # keep the item inside the scene
+                    value.setX(min(rect.right(), max(value.x(), rect.left())))
+                    value.setY(min(rect.bottom(), max(value.y(), rect.top())))
+                    return value
             
         return QGraphicsItem.itemChange(self, change, value)
     
@@ -284,4 +286,35 @@
         @return dictionary containing data to be persisted
         @rtype dict
         """
-        return {}
+        return {
+            "id": self.getId(),
+            "x": self.x(),
+            "y": self.y(),
+            "type": self.getItemType(),
+            "model_name": self.model.getName(),
+        }
+    
+    @classmethod
+    def fromDict(cls, data, colors=None):
+        """
+        Class method to create a generic UML item from persisted data.
+        
+        @param data dictionary containing the persisted data as generated
+            by toDict()
+        @type dict
+        @param colors tuple containing the foreground and background colors
+        @type tuple of (QColor, QColor)
+        @return created UML item
+        @rtype UMLItem
+        """
+        try:
+            model = UMLModel(data["model_name"])
+            itm = cls(model=model,
+                      x=0,
+                      y=0,
+                      colors=colors)
+            itm.setPos(data["x"], data["y"])
+            itm.setId(data["id"])
+            return itm
+        except KeyError:
+            return None

eric ide

mercurial