src/eric7/Graphics/ClassItem.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
diff -r e9e7eca7efee -r bf71ee032bb4 src/eric7/Graphics/ClassItem.py
--- a/src/eric7/Graphics/ClassItem.py	Wed Jul 13 11:16:20 2022 +0200
+++ b/src/eric7/Graphics/ClassItem.py	Wed Jul 13 14:55:47 2022 +0200
@@ -20,11 +20,13 @@
     """
     Class implementing the class model.
     """
-    def __init__(self, name, methods=None, instanceAttributes=None,
-                 classAttributes=None):
+
+    def __init__(
+        self, name, methods=None, instanceAttributes=None, classAttributes=None
+    ):
         """
         Constructor
-        
+
         @param name the class name
         @type str
         @param methods list of method names of the class
@@ -35,68 +37,62 @@
         @type list of str
         """
         super().__init__(name)
-        
+
         self.methods = [] if methods is None else methods[:]
         self.instanceAttributes = (
-            []
-            if instanceAttributes is None else
-            instanceAttributes[:]
+            [] if instanceAttributes is None else instanceAttributes[:]
         )
-        self.classAttributes = (
-            []
-            if classAttributes is None else
-            classAttributes[:]
-        )
-    
+        self.classAttributes = [] if classAttributes is None else classAttributes[:]
+
     def addMethod(self, method):
         """
         Public method to add a method to the class model.
-        
+
         @param method method name to be added
         @type str
         """
         self.methods.append(method)
-    
+
     def addInstanceAttribute(self, attribute):
         """
         Public method to add an instance attribute to the class model.
-        
+
         @param attribute instance attribute name to be added
         @type str
         """
         self.instanceAttributes.append(attribute)
-    
+
     def addClassAttribute(self, attribute):
         """
         Public method to add a class attribute to the class model.
-        
+
         @param attribute class attribute name to be added
         @type str
         """
         self.classAttributes.append(attribute)
-    
+
     def getMethods(self):
         """
         Public method to retrieve the methods of the class.
-        
+
         @return list of class methods
         @rtype list of str
         """
         return self.methods[:]
-    
+
     def getInstanceAttributes(self):
         """
         Public method to retrieve the attributes of the class.
-        
+
         @return list of instance attributes
         @rtype list of str
         """
         return self.instanceAttributes[:]
-    
+
     def getClassAttributes(self):
         """
         Public method to retrieve the global attributes of the class.
-        
+
         @return list of class attributes
         @rtype list of str
         """
@@ -107,14 +103,24 @@
     """
     Class implementing an UML like class item.
     """
+
     ItemType = "class"
-    
-    def __init__(self, model=None, external=False, x=0, y=0,
-                 rounded=False, noAttrs=False, colors=None, parent=None,
-                 scene=None):
+
+    def __init__(
+        self,
+        model=None,
+        external=False,
+        x=0,
+        y=0,
+        rounded=False,
+        noAttrs=False,
+        colors=None,
+        parent=None,
+        scene=None,
+    ):
         """
         Constructor
-        
+
         @param model class model containing the class data
         @type ClassModel
         @param external flag indicating a class defined outside our scope
@@ -135,32 +141,32 @@
         @type QGraphicsScene
         """
         UMLItem.__init__(self, model, x, y, rounded, colors, parent)
-        
+
         self.external = external
         self.noAttrs = noAttrs
-        
+
         if scene:
             scene.addItem(self)
-        
+
         if self.model:
             self.__createTexts()
             self.__calculateSize()
-        
+
     def __createTexts(self):
         """
         Private method to create the text items of the class item.
         """
         if self.model is None:
             return
-        
+
         boldFont = QFont(self.font)
         boldFont.setBold(True)
         boldFont.setUnderline(True)
-        
+
         classAttributes = self.model.getClassAttributes()
         attrs = self.model.getInstanceAttributes()
         meths = self.model.getMethods()
-        
+
         x = self.margin + int(self.rect().x())
         y = self.margin + int(self.rect().y())
         self.header = QGraphicsSimpleTextItem(self)
@@ -169,32 +175,29 @@
         self.header.setText(self.model.getName())
         self.header.setPos(x, y)
         y += int(self.header.boundingRect().height()) + self.margin
-        
+
         if self.external:
             self.classAttributes = None
         else:
-            txt = QCoreApplication.translate(
-                "ClassItem", "Class Attributes:\n  ")
+            txt = QCoreApplication.translate("ClassItem", "Class Attributes:\n  ")
             txt += (
                 "\n  ".join(classAttributes)
-                if globals else
-                "  " + QCoreApplication.translate("ClassItem", "none")
+                if globals
+                else "  " + QCoreApplication.translate("ClassItem", "none")
             )
             self.classAttributes = QGraphicsSimpleTextItem(self)
             self.classAttributes.setBrush(self._colors[0])
             self.classAttributes.setFont(self.font)
             self.classAttributes.setText(txt)
             self.classAttributes.setPos(x, y)
-            y += (int(self.classAttributes.boundingRect().height()) +
-                  self.margin)
-        
+            y += int(self.classAttributes.boundingRect().height()) + self.margin
+
         if not self.noAttrs and not self.external:
-            txt = QCoreApplication.translate(
-                "ClassItem", "Instance Attributes:\n  ")
+            txt = QCoreApplication.translate("ClassItem", "Instance Attributes:\n  ")
             txt += (
                 "\n  ".join(attrs)
-                if attrs else
-                "  " + QCoreApplication.translate("ClassItem", "none")
+                if attrs
+                else "  " + QCoreApplication.translate("ClassItem", "none")
             )
             self.attrs = QGraphicsSimpleTextItem(self)
             self.attrs.setBrush(self._colors[0])
@@ -204,54 +207,47 @@
             y += int(self.attrs.boundingRect().height()) + self.margin
         else:
             self.attrs = None
-        
+
         if self.external:
             txt = " "
         else:
             txt = QCoreApplication.translate("ClassItem", "Methods:\n  ")
             txt += (
                 "\n  ".join(meths)
-                if meths else
-                "  " + QCoreApplication.translate("ClassItem", "none")
+                if meths
+                else "  " + QCoreApplication.translate("ClassItem", "none")
             )
         self.meths = QGraphicsSimpleTextItem(self)
         self.meths.setBrush(self._colors[0])
         self.meths.setFont(self.font)
         self.meths.setText(txt)
         self.meths.setPos(x, y)
-        
+
     def __calculateSize(self):
         """
         Private method to calculate the size of the class item.
         """
         if self.model is None:
             return
-        
+
         width = int(self.header.boundingRect().width())
         height = int(self.header.boundingRect().height())
         if self.classAttributes:
-            width = max(width,
-                        int(self.classAttributes.boundingRect().width()))
-            height += (
-                int(self.classAttributes.boundingRect().height()) + self.margin
-            )
+            width = max(width, int(self.classAttributes.boundingRect().width()))
+            height += int(self.classAttributes.boundingRect().height()) + self.margin
         if self.attrs:
-            width = max(width,
-                        int(self.attrs.boundingRect().width()))
-            height = (
-                height + int(self.attrs.boundingRect().height()) + self.margin
-            )
+            width = max(width, int(self.attrs.boundingRect().width()))
+            height = height + int(self.attrs.boundingRect().height()) + self.margin
         if self.meths:
-            width = max(width,
-                        int(self.meths.boundingRect().width()))
+            width = max(width, int(self.meths.boundingRect().width()))
             height += int(self.meths.boundingRect().height())
-        
+
         self.setSize(width + 2 * self.margin, height + 2 * self.margin)
-        
+
     def setModel(self, model):
         """
         Public method to set the class model.
-        
+
         @param model class model containing the class data
         @type ClassModel
         """
@@ -269,11 +265,11 @@
         self.model = model
         self.__createTexts()
         self.__calculateSize()
-        
+
     def paint(self, painter, option, widget=None):
         """
         Public method to paint the item in local coordinates.
-        
+
         @param painter reference to the painter object
         @type QPainter
         @param option style options
@@ -283,52 +279,46 @@
         """
         pen = self.pen()
         if (
-            (option.state & QStyle.StateFlag.State_Selected) ==
-            QStyle.StateFlag.State_Selected
-        ):
+            option.state & QStyle.StateFlag.State_Selected
+        ) == QStyle.StateFlag.State_Selected:
             pen.setWidth(2)
         else:
             pen.setWidth(1)
-        
+
         painter.setPen(pen)
         painter.setBrush(self.brush())
         painter.setFont(self.font)
-        
+
         offsetX = int(self.rect().x())
         offsetY = int(self.rect().y())
         w = int(self.rect().width())
         h = int(self.rect().height())
-        
+
         painter.drawRect(offsetX, offsetY, w, h)
         y = self.margin + int(self.header.boundingRect().height())
         painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
         if self.classAttributes:
-            y += (
-                self.margin +
-                int(self.classAttributes.boundingRect().height())
-            )
-            painter.drawLine(offsetX, offsetY + y,
-                             offsetX + w - 1, offsetY + y)
+            y += self.margin + int(self.classAttributes.boundingRect().height())
+            painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
         if self.attrs:
             y += self.margin + int(self.attrs.boundingRect().height())
-            painter.drawLine(offsetX, offsetY + y,
-                             offsetX + w - 1, offsetY + y)
-        
+            painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
+
         self.adjustAssociations()
-        
+
     def isExternal(self):
         """
         Public method returning the external state.
-        
+
         @return external state
         @rtype bool
         """
         return self.external
-    
+
     def parseItemDataString(self, version, data):
         """
         Public method to parse the given persistence data.
-        
+
         @param version version of the data
         @type str
         @param data persisted data to be parsed
@@ -339,12 +329,12 @@
         parts = data.split(", ")
         if len(parts) < 3:
             return False
-        
+
         name = ""
         instanceAttributes = []
         methods = []
         classAttributes = []
-        
+
         for part in parts:
             key, value = part.split("=", 1)
             if key == "is_external":
@@ -361,18 +351,17 @@
                 classAttributes = value.strip().split("||")
             else:
                 return False
-        
-        self.model = ClassModel(name, methods, instanceAttributes,
-                                classAttributes)
+
+        self.model = ClassModel(name, methods, instanceAttributes, classAttributes)
         self.__createTexts()
         self.__calculateSize()
-        
+
         return True
-    
+
     def toDict(self):
         """
         Public method to collect data to be persisted.
-        
+
         @return dictionary containing data to be persisted
         @rtype dict
         """
@@ -388,12 +377,12 @@
             "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
@@ -403,16 +392,20 @@
         @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)
+            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

eric ide

mercurial