Modernized some more code.

Sat, 01 May 2021 16:01:43 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 01 May 2021 16:01:43 +0200
changeset 8276
1436fd09d1e1
parent 8275
2f4b8212a151
child 8277
ea734702ae94

Modernized some more code.

eric6/Graphics/UMLDialog.py file | annotate | diff | comparison | revisions
eric6/Project/Project.py file | annotate | diff | comparison | revisions
eric6/Project/ProjectSourcesBrowser.py file | annotate | diff | comparison | revisions
eric6/QScintilla/Editor.py file | annotate | diff | comparison | revisions
--- a/eric6/Graphics/UMLDialog.py	Sat May 01 15:08:38 2021 +0200
+++ b/eric6/Graphics/UMLDialog.py	Sat May 01 16:01:43 2021 +0200
@@ -7,6 +7,8 @@
 Module implementing a dialog showing UML like diagrams.
 """
 
+import enum
+
 from PyQt5.QtCore import pyqtSlot, Qt, QFileInfo
 from PyQt5.QtWidgets import QAction, QToolBar, QGraphicsScene
 
@@ -17,32 +19,41 @@
 import UI.PixmapCache
 
 
+class UMLDialogType(enum.Enum):
+    """
+    Class defining the UML dialog types.
+    """
+    CLASS_DIAGRAM = 0
+    PACKAGE_DIAGRAM = 1
+    IMPORTS_DIAGRAM = 2
+    APPLICATION_DIAGRAM = 3
+    NO_DIAGRAM = 255
+
+
 class UMLDialog(E5MainWindow):
     """
     Class implementing a dialog showing UML like diagrams.
     """
-    # convert to Enum
-    NoDiagram = 255
-    ClassDiagram = 0
-    PackageDiagram = 1
-    ImportsDiagram = 2
-    ApplicationDiagram = 3
-    
-    FileVersions = ["1.0"]
+    FileVersions = ("1.0")
     
     def __init__(self, diagramType, project, path="", parent=None,
                  initBuilder=True, **kwargs):
         """
         Constructor
         
-        @param diagramType type of the diagram (one of ApplicationDiagram,
-            ClassDiagram, ImportsDiagram, NoDiagram, PackageDiagram)
-        @param project reference to the project object (Project)
-        @param path file or directory path to build the diagram from (string)
-        @param parent parent widget of the dialog (QWidget)
+        @param diagramType type of the diagram
+        @type UMLDialogType
+        @param project reference to the project object
+        @type Project
+        @param path file or directory path to build the diagram from
+        @type str
+        @param parent parent widget of the dialog
+        @type QWidget
         @param initBuilder flag indicating to initialize the diagram
-            builder (boolean)
+            builder
+        @type bool
         @keyparam kwargs diagram specific data
+        @type dict
         """
         super().__init__(parent)
         self.setObjectName("UMLDialog")
@@ -50,10 +61,10 @@
         self.__project = project
         self.__diagramType = diagramType
         self.__diagramTypeString = {
-            UMLDialog.ClassDiagram: "Class Diagram",
-            UMLDialog.PackageDiagram: "Package Diagram",
-            UMLDialog.ImportsDiagram: "Imports Diagram",
-            UMLDialog.ApplicationDiagram: "Application Diagram",
+            UMLDialogType.CLASS_DIAGRAM: "Class Diagram",
+            UMLDialogType.PACKAGE_DIAGRAM: "Package Diagram",
+            UMLDialogType.IMPORTS_DIAGRAM: "Imports Diagram",
+            UMLDialogType.APPLICATION_DIAGRAM: "Application Diagram",
         }.get(diagramType, "Illegal Diagram Type")
         
         from .UMLGraphicsView import UMLGraphicsView
@@ -163,34 +174,27 @@
         Private method to instantiate a diagram builder object.
         
         @param diagramType type of the diagram
-            (one of ApplicationDiagram, ClassDiagram, ImportsDiagram,
-            PackageDiagram)
-        @param path file or directory path to build the diagram from (string)
+        @type UMLDialogType
+        @param path file or directory path to build the diagram from
+        @type str
         @keyparam kwargs diagram specific data
+        @type dict
         @return reference to the instantiated diagram builder
-        @exception ValueError raised to indicate an illegal diagram type
+        @rtype UMLDiagramBuilder
         """
-        if diagramType not in (
-            UMLDialog.ClassDiagram, UMLDialog.PackageDiagram,
-            UMLDialog.ImportsDiagram, UMLDialog.ApplicationDiagram,
-            UMLDialog.NoDiagram
-        ):
-            raise ValueError(self.tr(
-                "Illegal diagram type '{0}' given.").format(diagramType))
-        
-        if diagramType == UMLDialog.ClassDiagram:
+        if diagramType == UMLDialogType.CLASS_DIAGRAM:
             from .UMLClassDiagramBuilder import UMLClassDiagramBuilder
             return UMLClassDiagramBuilder(
                 self, self.umlView, self.__project, path, **kwargs)
-        elif diagramType == UMLDialog.PackageDiagram:
+        elif diagramType == UMLDialogType.PACKAGE_DIAGRAM:
             from .PackageDiagramBuilder import PackageDiagramBuilder
             return PackageDiagramBuilder(
                 self, self.umlView, self.__project, path, **kwargs)
-        elif diagramType == UMLDialog.ImportsDiagram:
+        elif diagramType == UMLDialogType.IMPORTS_DIAGRAM:
             from .ImportsDiagramBuilder import ImportsDiagramBuilder
             return ImportsDiagramBuilder(
                 self, self.umlView, self.__project, path, **kwargs)
-        elif diagramType == UMLDialog.ApplicationDiagram:
+        elif diagramType == UMLDialogType.APPLICATION_DIAGRAM:
             from .ApplicationDiagramBuilder import ApplicationDiagramBuilder
             return ApplicationDiagramBuilder(
                 self, self.umlView, self.__project, **kwargs)
@@ -240,7 +244,7 @@
         lines = [
             "version: 1.0",
             "diagram_type: {0} ({1})".format(
-                self.__diagramType, self.__diagramTypeString),
+                self.__diagramType.value, self.__diagramTypeString),
             "scene_size: {0};{1}".format(self.scene.width(),
                                          self.scene.height()),
         ]
@@ -318,7 +322,7 @@
                 return False
             try:
                 diagramType, diagramTypeString = value.strip().split(None, 1)
-                self.__diagramType = int(self.__diagramType)
+                self.__diagramType = UMLDialogType(int(self.__diagramType))
                 self.__diagramTypeString = diagramTypeString[1:-1]
                 # remove opening an closing bracket
             except ValueError:
--- a/eric6/Project/Project.py	Sat May 01 15:08:38 2021 +0200
+++ b/eric6/Project/Project.py	Sat May 01 16:01:43 2021 +0200
@@ -5123,18 +5123,19 @@
             self.tr("""Include module names?"""),
             yesDefault=True)
         
-        from Graphics.UMLDialog import UMLDialog
-        self.applicationDiagram = UMLDialog(UMLDialog.ApplicationDiagram, self,
-                                            self.parent(), noModules=not res)
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
+        self.applicationDiagram = UMLDialog(
+            UMLDialogType.APPLICATION_DIAGRAM, self, self.parent(),
+            noModules=not res)
         self.applicationDiagram.show()
     
     def __loadDiagram(self):
         """
         Private slot to load a diagram from file.
         """
-        from Graphics.UMLDialog import UMLDialog
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         self.loadedDiagram = None
-        loadedDiagram = UMLDialog(UMLDialog.NoDiagram,
+        loadedDiagram = UMLDialog(UMLDialogType.NO_DIAGRAM,
                                   self, parent=self.parent())
         if loadedDiagram.load():
             self.loadedDiagram = loadedDiagram
--- a/eric6/Project/ProjectSourcesBrowser.py	Sat May 01 15:08:38 2021 +0200
+++ b/eric6/Project/ProjectSourcesBrowser.py	Sat May 01 16:01:43 2021 +0200
@@ -123,6 +123,7 @@
             self.tr('Profile data...'), self.__showProfileData)
         self.menuShow.aboutToShow.connect(self.__showContextMenuShow)
         
+        # TODO: differentiate between file and directory/package
         self.graphicsMenu = QMenu(self.tr('Diagrams'))
         self.classDiagramAction = self.graphicsMenu.addAction(
             self.tr("Class Diagram..."), self.__showClassDiagram)
@@ -1077,9 +1078,11 @@
             self.tr("Class Diagram"),
             self.tr("""Include class attributes?"""),
             yesDefault=True)
-        from Graphics.UMLDialog import UMLDialog
-        self.classDiagram = UMLDialog(UMLDialog.ClassDiagram, self.project, fn,
-                                      self, noAttrs=not res)
+        
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
+        self.classDiagram = UMLDialog(
+            UMLDialogType.CLASS_DIAGRAM, self.project, fn,
+            self, noAttrs=not res)
         self.classDiagram.show()
         
     def __showImportsDiagram(self):
@@ -1096,9 +1099,10 @@
             self,
             self.tr("Imports Diagram"),
             self.tr("""Include imports from external modules?"""))
-        from Graphics.UMLDialog import UMLDialog
+        
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         self.importsDiagram = UMLDialog(
-            UMLDialog.ImportsDiagram, self.project, package,
+            UMLDialogType.IMPORTS_DIAGRAM, self.project, package,
             self, showExternalImports=res)
         self.importsDiagram.show()
         
@@ -1117,9 +1121,10 @@
             self.tr("Package Diagram"),
             self.tr("""Include class attributes?"""),
             yesDefault=True)
-        from Graphics.UMLDialog import UMLDialog
+        
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         self.packageDiagram = UMLDialog(
-            UMLDialog.PackageDiagram, self.project, package,
+            UMLDialogType.PACKAGE_DIAGRAM, self.project, package,
             self, noAttrs=not res)
         self.packageDiagram.show()
         
@@ -1132,9 +1137,10 @@
             self.tr("Application Diagram"),
             self.tr("""Include module names?"""),
             yesDefault=True)
-        from Graphics.UMLDialog import UMLDialog
+        
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         self.applicationDiagram = UMLDialog(
-            UMLDialog.ApplicationDiagram, self.project,
+            UMLDialogType.APPLICATION_DIAGRAM, self.project,
             self, noModules=not res)
         self.applicationDiagram.show()
     
@@ -1142,10 +1148,10 @@
         """
         Private slot to load a diagram from file.
         """
-        from Graphics.UMLDialog import UMLDialog
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         self.loadedDiagram = None
         loadedDiagram = UMLDialog(
-            UMLDialog.NoDiagram, self.project, parent=self)
+            UMLDialogType.NO_DIAGRAM, self.project, parent=self)
         if loadedDiagram.load():
             self.loadedDiagram = loadedDiagram
             self.loadedDiagram.show(fromFile=True)
--- a/eric6/QScintilla/Editor.py	Sat May 01 15:08:38 2021 +0200
+++ b/eric6/QScintilla/Editor.py	Sat May 01 16:01:43 2021 +0200
@@ -7556,12 +7556,12 @@
         """
         Private method to handle the Class Diagram context menu action.
         """
-        from Graphics.UMLDialog import UMLDialog
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         if not self.checkDirty():
             return
         
         self.classDiagram = UMLDialog(
-            UMLDialog.ClassDiagram, self.project, self.fileName,
+            UMLDialogType.CLASS_DIAGRAM, self.project, self.fileName,
             self, noAttrs=False)
         self.classDiagram.show()
         
@@ -7569,7 +7569,7 @@
         """
         Private method to handle the Package Diagram context menu action.
         """
-        from Graphics.UMLDialog import UMLDialog
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         if not self.checkDirty():
             return
         
@@ -7583,7 +7583,7 @@
             self.tr("""Include class attributes?"""),
             yesDefault=True)
         self.packageDiagram = UMLDialog(
-            UMLDialog.PackageDiagram, self.project, package,
+            UMLDialogType.PACKAGE_DIAGRAM, self.project, package,
             self, noAttrs=not res)
         self.packageDiagram.show()
         
@@ -7591,7 +7591,7 @@
         """
         Private method to handle the Imports Diagram context menu action.
         """
-        from Graphics.UMLDialog import UMLDialog
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         if not self.checkDirty():
             return
         
@@ -7604,7 +7604,7 @@
             self.tr("Imports Diagram"),
             self.tr("""Include imports from external modules?"""))
         self.importsDiagram = UMLDialog(
-            UMLDialog.ImportsDiagram, self.project, package,
+            UMLDialogType.IMPORTS_DIAGRAM, self.project, package,
             self, showExternalImports=res)
         self.importsDiagram.show()
         
@@ -7612,14 +7612,14 @@
         """
         Private method to handle the Imports Diagram context menu action.
         """
-        from Graphics.UMLDialog import UMLDialog
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         res = E5MessageBox.yesNo(
             self,
             self.tr("Application Diagram"),
             self.tr("""Include module names?"""),
             yesDefault=True)
         self.applicationDiagram = UMLDialog(
-            UMLDialog.ApplicationDiagram, self.project,
+            UMLDialogType.APPLICATION_DIAGRAM, self.project,
             self, noModules=not res)
         self.applicationDiagram.show()
     
@@ -7627,9 +7627,9 @@
         """
         Private slot to load a diagram from file.
         """
-        from Graphics.UMLDialog import UMLDialog
+        from Graphics.UMLDialog import UMLDialog, UMLDialogType
         self.loadedDiagram = UMLDialog(
-            UMLDialog.NoDiagram, self.project, parent=self)
+            UMLDialogType.NO_DIAGRAM, self.project, parent=self)
         if self.loadedDiagram.load():
             self.loadedDiagram.show(fromFile=True)
         else:

eric ide

mercurial