--- a/eric6/Graphics/ClassItem.py Thu May 06 19:46:00 2021 +0200 +++ b/eric6/Graphics/ClassItem.py Sat May 08 18:34:08 2021 +0200 @@ -139,7 +139,8 @@ self.external = external self.noAttrs = noAttrs - scene.addItem(self) + if scene: + scene.addItem(self) if self.model: self.__createTexts() @@ -398,10 +399,44 @@ @rtype dict """ return { + "id": self.getId(), + "x": self.x(), + "y": self.y(), + "type": self.getItemType(), "is_external": self.external, "no_attributes": self.noAttrs, - "name": self.model.getName(), + "model_name": self.model.getName(), "attributes": self.model.getInstanceAttributes(), "methods": self.model.getMethods(), "class_attributes": self.model.getClassAttributes(), } + + @classmethod + def fromDict(cls, data, colors=None): + """ + Class method to create a class 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 class item + @rtype ClassItem + """ + try: + model = ClassModel(data["model_name"], + data["methods"], + data["attributes"], + data["class_attributes"]) + itm = cls(model=model, + external=data["is_external"], + x=0, + y=0, + noAttrs=data["no_attributes"], + colors=colors) + itm.setPos(data["x"], data["y"]) + itm.setId(data["id"]) + return itm + except KeyError: + return None