Graphics/UMLDialog.py

changeset 2033
4b99609f6a87
parent 2031
c36c2eb62a75
child 2034
8de0fc1f7fef
equal deleted inserted replaced
2032:704593d042fe 2033:4b99609f6a87
5 5
6 """ 6 """
7 Module implementing a dialog showing UML like diagrams. 7 Module implementing a dialog showing UML like diagrams.
8 """ 8 """
9 9
10 from PyQt4.QtCore import Qt 10 from PyQt4.QtCore import Qt, QFileInfo
11 from PyQt4.QtGui import QMainWindow, QAction, QToolBar, QGraphicsScene 11 from PyQt4.QtGui import QMainWindow, QAction, QToolBar, QGraphicsScene
12
13 from E5Gui import E5MessageBox, E5FileDialog
12 14
13 from .UMLGraphicsView import UMLGraphicsView 15 from .UMLGraphicsView import UMLGraphicsView
14 16
15 import UI.Config 17 import UI.Config
16 import UI.PixmapCache 18 import UI.PixmapCache
23 ClassDiagram = 0 25 ClassDiagram = 0
24 PackageDiagram = 1 26 PackageDiagram = 1
25 ImportsDiagram = 2 27 ImportsDiagram = 2
26 ApplicationDiagram = 3 28 ApplicationDiagram = 3
27 29
28 def __init__(self, diagramType, project, path, parent=None, **kwargs): 30 def __init__(self, diagramType, project, path="", parent=None, initBuilder=True,
31 **kwargs):
29 """ 32 """
30 Constructor 33 Constructor
31 34
32 @param diagramType type of the diagram 35 @param diagramType type of the diagram
33 (one of ApplicationDiagram, ClassDiagram, ImportsDiagram, PackageDiagram) 36 (one of ApplicationDiagram, ClassDiagram, ImportsDiagram, PackageDiagram)
34 @param project reference to the project object (Project) 37 @param project reference to the project object (Project)
35 @param path file or directory path to build the diagram from (string) 38 @param path file or directory path to build the diagram from (string)
36 @param parent parent widget of the view (QWidget) 39 @param parent parent widget of the dialog (QWidget)
40 @keyparam initBuilder flag indicating to initialize the diagram builder (boolean)
37 @param kwargs diagram specific data 41 @param kwargs diagram specific data
38 """ 42 """
39 super().__init__(parent) 43 super().__init__(parent)
40 self.setObjectName("UMLDialog") 44 self.setObjectName("UMLDialog")
41 45
46 self.__diagramType = diagramType
47
42 self.scene = QGraphicsScene(0.0, 0.0, 800.0, 600.0) 48 self.scene = QGraphicsScene(0.0, 0.0, 800.0, 600.0)
43 self.umlView = UMLGraphicsView(self.scene, diagramType, parent=self) 49 self.umlView = UMLGraphicsView(self.scene, parent=self)
44 self.builder = self.__diagramBuilder(diagramType, project, path, **kwargs) 50 self.builder = self.__diagramBuilder(self.__diagramType, project, path, **kwargs)
45 51 if initBuilder:
52 self.builder.initialize()
53
54 self.__fileName = ""
55
56 self.__initActions()
57 self.__initToolBars()
58
59 self.setCentralWidget(self.umlView)
60
61 self.umlView.relayout.connect(self.__relayout)
62
63 def __initActions(self):
64 """
65 Private slot to initialize the actions.
66 """
46 self.closeAct = \ 67 self.closeAct = \
47 QAction(UI.PixmapCache.getIcon("close.png"), 68 QAction(UI.PixmapCache.getIcon("close.png"),
48 self.trUtf8("Close"), self) 69 self.trUtf8("Close"), self)
49 self.closeAct.triggered[()].connect(self.close) 70 self.closeAct.triggered[()].connect(self.close)
50 71
72 self.saveAct = \
73 QAction(UI.PixmapCache.getIcon("fileSave.png"),
74 self.trUtf8("Save"), self)
75 self.saveAct.triggered[()].connect(self.__save)
76
77 self.saveAsAct = \
78 QAction(UI.PixmapCache.getIcon("fileSaveAs.png"),
79 self.trUtf8("Save As..."), self)
80 self.saveAsAct.triggered[()].connect(self.__saveAs)
81
82 self.saveImageAct = \
83 QAction(UI.PixmapCache.getIcon("fileSavePixmap.png"),
84 self.trUtf8("Save as PNG"), self)
85 self.saveImageAct.triggered[()].connect(self.umlView.saveImage)
86
87 self.printAct = \
88 QAction(UI.PixmapCache.getIcon("print.png"),
89 self.trUtf8("Print"), self)
90 self.printAct.triggered[()].connect(self.umlView.printDiagram)
91
92 self.printPreviewAct = \
93 QAction(UI.PixmapCache.getIcon("printPreview.png"),
94 self.trUtf8("Print Preview"), self)
95 self.printPreviewAct.triggered[()].connect(self.umlView.printPreviewDiagram)
96
97 def __initToolBars(self):
98 """
99 Private slot to initialize the toolbars.
100 """
51 self.windowToolBar = QToolBar(self.trUtf8("Window"), self) 101 self.windowToolBar = QToolBar(self.trUtf8("Window"), self)
52 self.windowToolBar.setIconSize(UI.Config.ToolBarIconSize) 102 self.windowToolBar.setIconSize(UI.Config.ToolBarIconSize)
53 self.windowToolBar.addAction(self.closeAct) 103 self.windowToolBar.addAction(self.closeAct)
54 104
105 self.fileToolBar = QToolBar(self.trUtf8("File"), self)
106 self.fileToolBar.setIconSize(UI.Config.ToolBarIconSize)
107 self.fileToolBar.addAction(self.saveAct)
108 self.fileToolBar.addAction(self.saveAsAct)
109 self.fileToolBar.addAction(self.saveImageAct)
110 self.fileToolBar.addSeparator()
111 self.fileToolBar.addAction(self.printPreviewAct)
112 self.fileToolBar.addAction(self.printAct)
113
55 self.umlToolBar = self.umlView.initToolBar() 114 self.umlToolBar = self.umlView.initToolBar()
56 115
116 self.addToolBar(Qt.TopToolBarArea, self.fileToolBar)
57 self.addToolBar(Qt.TopToolBarArea, self.windowToolBar) 117 self.addToolBar(Qt.TopToolBarArea, self.windowToolBar)
58 self.addToolBar(Qt.TopToolBarArea, self.umlToolBar) 118 self.addToolBar(Qt.TopToolBarArea, self.umlToolBar)
59
60 self.setCentralWidget(self.umlView)
61
62 self.umlView.relayout.connect(self.__relayout)
63 119
64 def show(self): 120 def show(self):
65 """ 121 """
66 Overriden method to show the dialog. 122 Overriden method to show the dialog.
67 """ 123 """
98 return ApplicationDiagramBuilder(self, self.umlView, project, **kwargs) 154 return ApplicationDiagramBuilder(self, self.umlView, project, **kwargs)
99 else: 155 else:
100 raise ValueError( 156 raise ValueError(
101 self.trUtf8("Illegal diagram type '{0}' given.").format(diagramType)) 157 self.trUtf8("Illegal diagram type '{0}' given.").format(diagramType))
102 158
103 def diagramTypeToString(self, diagramType): 159 def __diagramTypeString(self):
104 """ 160 """
105 Public method to convert the diagram type to a readable string. 161 Private method to generate a readable string for the diagram type.
106 162
107 @param diagramType type of the diagram
108 (one of ApplicationDiagram, ClassDiagram, ImportsDiagram, PackageDiagram)
109 @return readable type string (string) 163 @return readable type string (string)
110 """ 164 """
111 if diagramType == UMLDialog.ClassDiagram: 165 if self.__diagramType == UMLDialog.ClassDiagram:
112 return "Class Diagram" 166 return "Class Diagram"
113 elif diagramType == UMLDialog.PackageDiagram: 167 elif self.__diagramType == UMLDialog.PackageDiagram:
114 return "Package Diagram" 168 return "Package Diagram"
115 elif diagramType == UMLDialog.ImportsDiagram: 169 elif self.__diagramType == UMLDialog.ImportsDiagram:
116 return "Imports Diagram" 170 return "Imports Diagram"
117 elif diagramType == UMLDialog.ApplicationDiagram: 171 elif self.__diagramType == UMLDialog.ApplicationDiagram:
118 return "Application Diagram" 172 return "Application Diagram"
119 else: 173 else:
120 return "Illegal Diagram Type" 174 return "Illegal Diagram Type"
175
176 def __save(self):
177 """
178 Private slot to save the diagram with the current name.
179 """
180 self.__saveAs(self.__fileName)
181
182 def __saveAs(self, filename=""):
183 """
184 Private slot to save the diagram.
185
186 @param filename name of the file to write to (string)
187 """
188 if not filename:
189 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
190 self,
191 self.trUtf8("Save Diagram"),
192 "",
193 self.trUtf8("Eric5 Graphics File (*.e5g);;All Files (*)"),
194 "",
195 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
196 if not fname:
197 return
198 ext = QFileInfo(fname).suffix()
199 if not ext:
200 ex = selectedFilter.split("(*")[1].split(")")[0]
201 if ex:
202 fname += ex
203 if QFileInfo(fname).exists():
204 res = E5MessageBox.yesNo(self,
205 self.trUtf8("Save Diagram"),
206 self.trUtf8("<p>The file <b>{0}</b> already exists."
207 " Overwrite it?</p>").format(fname),
208 icon=E5MessageBox.Warning)
209 if not res:
210 return
211 filename = fname
212
213 lines = [
214 "version: 1.0",
215 "diagram_type: {0} ({1})".format(self.__diagramType,
216 self.__diagramTypeString()),
217 "scene_size: {0};{1}".format(self.scene.width(), self.scene.height()),
218 ]
219 persistenceData = self.builder.getPersistenceData()
220 if persistenceData:
221 lines.append("builder_data: {0}".format(persistenceData))
222 lines.extend(self.umlView.getPersistenceData())
223
224 try:
225 f = open(filename, "w", encoding="utf-8")
226 f.write("\n".join(lines))
227 f.close()
228 except (IOError, OSError) as err:
229 E5MessageBox.critical(self,
230 self.trUtf8("Save Diagram"),
231 self.trUtf8("""<p>The file <b>{0}</b> could not be saved.</p>"""
232 """<p>Reason: {1}</p>""").format(fname, str(err)))
233
234 self.__fileName = filename
235
236 @classmethod
237 def generateDialogFromFile(cls, project, parent=None):
238 """
239 Class method to generate a dialog reading data from a file.
240
241 @param project reference to the project object (Project)
242 @param parent parent widget of the dialog (QWidget)
243 @return generated dialog (UMLDialog)
244 """
245 # TODO: implement this
246 return None

eric ide

mercurial