Graphics/UMLDialog.py

branch
Py2 comp.
changeset 3484
645c12de6b0c
parent 3178
f25fc1364c88
parent 3345
071afe8be2a1
child 3656
441956d8fce5
equal deleted inserted replaced
3456:96232974dcdb 3484:645c12de6b0c
7 Module implementing a dialog showing UML like diagrams. 7 Module implementing a dialog showing UML like diagrams.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 from PyQt4.QtCore import Qt, QFileInfo 12 from PyQt4.QtCore import pyqtSlot, Qt, QFileInfo
13 from PyQt4.QtGui import QAction, QToolBar, QGraphicsScene 13 from PyQt4.QtGui import QAction, QToolBar, QGraphicsScene
14 14
15 from E5Gui import E5MessageBox, E5FileDialog 15 from E5Gui import E5MessageBox, E5FileDialog
16 from E5Gui.E5MainWindow import E5MainWindow 16 from E5Gui.E5MainWindow import E5MainWindow
17 17
74 """ 74 """
75 Private slot to initialize the actions. 75 Private slot to initialize the actions.
76 """ 76 """
77 self.closeAct = \ 77 self.closeAct = \
78 QAction(UI.PixmapCache.getIcon("close.png"), 78 QAction(UI.PixmapCache.getIcon("close.png"),
79 self.trUtf8("Close"), self) 79 self.tr("Close"), self)
80 self.closeAct.triggered[()].connect(self.close) 80 self.closeAct.triggered.connect(self.close)
81 81
82 self.openAct = \ 82 self.openAct = \
83 QAction(UI.PixmapCache.getIcon("open.png"), 83 QAction(UI.PixmapCache.getIcon("open.png"),
84 self.trUtf8("Load"), self) 84 self.tr("Load"), self)
85 self.openAct.triggered[()].connect(self.load) 85 self.openAct.triggered.connect(self.load)
86 86
87 self.saveAct = \ 87 self.saveAct = \
88 QAction(UI.PixmapCache.getIcon("fileSave.png"), 88 QAction(UI.PixmapCache.getIcon("fileSave.png"),
89 self.trUtf8("Save"), self) 89 self.tr("Save"), self)
90 self.saveAct.triggered[()].connect(self.__save) 90 self.saveAct.triggered.connect(self.__save)
91 91
92 self.saveAsAct = \ 92 self.saveAsAct = \
93 QAction(UI.PixmapCache.getIcon("fileSaveAs.png"), 93 QAction(UI.PixmapCache.getIcon("fileSaveAs.png"),
94 self.trUtf8("Save As..."), self) 94 self.tr("Save As..."), self)
95 self.saveAsAct.triggered[()].connect(self.__saveAs) 95 self.saveAsAct.triggered.connect(self.__saveAs)
96 96
97 self.saveImageAct = \ 97 self.saveImageAct = \
98 QAction(UI.PixmapCache.getIcon("fileSavePixmap.png"), 98 QAction(UI.PixmapCache.getIcon("fileSavePixmap.png"),
99 self.trUtf8("Save as Image"), self) 99 self.tr("Save as Image"), self)
100 self.saveImageAct.triggered[()].connect(self.umlView.saveImage) 100 self.saveImageAct.triggered.connect(self.umlView.saveImage)
101 101
102 self.printAct = \ 102 self.printAct = \
103 QAction(UI.PixmapCache.getIcon("print.png"), 103 QAction(UI.PixmapCache.getIcon("print.png"),
104 self.trUtf8("Print"), self) 104 self.tr("Print"), self)
105 self.printAct.triggered[()].connect(self.umlView.printDiagram) 105 self.printAct.triggered.connect(self.umlView.printDiagram)
106 106
107 self.printPreviewAct = \ 107 self.printPreviewAct = \
108 QAction(UI.PixmapCache.getIcon("printPreview.png"), 108 QAction(UI.PixmapCache.getIcon("printPreview.png"),
109 self.trUtf8("Print Preview"), self) 109 self.tr("Print Preview"), self)
110 self.printPreviewAct.triggered[()].connect( 110 self.printPreviewAct.triggered.connect(
111 self.umlView.printPreviewDiagram) 111 self.umlView.printPreviewDiagram)
112 112
113 def __initToolBars(self): 113 def __initToolBars(self):
114 """ 114 """
115 Private slot to initialize the toolbars. 115 Private slot to initialize the toolbars.
116 """ 116 """
117 self.windowToolBar = QToolBar(self.trUtf8("Window"), self) 117 self.windowToolBar = QToolBar(self.tr("Window"), self)
118 self.windowToolBar.setIconSize(UI.Config.ToolBarIconSize) 118 self.windowToolBar.setIconSize(UI.Config.ToolBarIconSize)
119 self.windowToolBar.addAction(self.closeAct) 119 self.windowToolBar.addAction(self.closeAct)
120 120
121 self.fileToolBar = QToolBar(self.trUtf8("File"), self) 121 self.fileToolBar = QToolBar(self.tr("File"), self)
122 self.fileToolBar.setIconSize(UI.Config.ToolBarIconSize) 122 self.fileToolBar.setIconSize(UI.Config.ToolBarIconSize)
123 self.fileToolBar.addAction(self.openAct) 123 self.fileToolBar.addAction(self.openAct)
124 self.fileToolBar.addSeparator() 124 self.fileToolBar.addSeparator()
125 self.fileToolBar.addAction(self.saveAct) 125 self.fileToolBar.addAction(self.saveAct)
126 self.fileToolBar.addAction(self.saveAsAct) 126 self.fileToolBar.addAction(self.saveAsAct)
182 return ApplicationDiagramBuilder( 182 return ApplicationDiagramBuilder(
183 self, self.umlView, self.__project, **kwargs) 183 self, self.umlView, self.__project, **kwargs)
184 elif diagramType == UMLDialog.NoDiagram: 184 elif diagramType == UMLDialog.NoDiagram:
185 return None 185 return None
186 else: 186 else:
187 raise ValueError(self.trUtf8( 187 raise ValueError(self.tr(
188 "Illegal diagram type '{0}' given.").format(diagramType)) 188 "Illegal diagram type '{0}' given.").format(diagramType))
189 189
190 def __diagramTypeString(self): 190 def __diagramTypeString(self):
191 """ 191 """
192 Private method to generate a readable string for the diagram type. 192 Private method to generate a readable string for the diagram type.
208 """ 208 """
209 Private slot to save the diagram with the current name. 209 Private slot to save the diagram with the current name.
210 """ 210 """
211 self.__saveAs(self.__fileName) 211 self.__saveAs(self.__fileName)
212 212
213 @pyqtSlot()
213 def __saveAs(self, filename=""): 214 def __saveAs(self, filename=""):
214 """ 215 """
215 Private slot to save the diagram. 216 Private slot to save the diagram.
216 217
217 @param filename name of the file to write to (string) 218 @param filename name of the file to write to (string)
218 """ 219 """
219 if not filename: 220 if not filename:
220 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 221 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
221 self, 222 self,
222 self.trUtf8("Save Diagram"), 223 self.tr("Save Diagram"),
223 "", 224 "",
224 self.trUtf8("Eric5 Graphics File (*.e5g);;All Files (*)"), 225 self.tr("Eric5 Graphics File (*.e5g);;All Files (*)"),
225 "", 226 "",
226 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) 227 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
227 if not fname: 228 if not fname:
228 return 229 return
229 ext = QFileInfo(fname).suffix() 230 ext = QFileInfo(fname).suffix()
232 if ex: 233 if ex:
233 fname += ex 234 fname += ex
234 if QFileInfo(fname).exists(): 235 if QFileInfo(fname).exists():
235 res = E5MessageBox.yesNo( 236 res = E5MessageBox.yesNo(
236 self, 237 self,
237 self.trUtf8("Save Diagram"), 238 self.tr("Save Diagram"),
238 self.trUtf8("<p>The file <b>{0}</b> already exists." 239 self.tr("<p>The file <b>{0}</b> already exists."
239 " Overwrite it?</p>").format(fname), 240 " Overwrite it?</p>").format(fname),
240 icon=E5MessageBox.Warning) 241 icon=E5MessageBox.Warning)
241 if not res: 242 if not res:
242 return 243 return
243 filename = fname 244 filename = fname
244 245
259 f.write("\n".join(lines)) 260 f.write("\n".join(lines))
260 f.close() 261 f.close()
261 except (IOError, OSError) as err: 262 except (IOError, OSError) as err:
262 E5MessageBox.critical( 263 E5MessageBox.critical(
263 self, 264 self,
264 self.trUtf8("Save Diagram"), 265 self.tr("Save Diagram"),
265 self.trUtf8( 266 self.tr(
266 """<p>The file <b>{0}</b> could not be saved.</p>""" 267 """<p>The file <b>{0}</b> could not be saved.</p>"""
267 """<p>Reason: {1}</p>""").format(filename, str(err))) 268 """<p>Reason: {1}</p>""").format(filename, str(err)))
268 return 269 return
269 270
270 self.__fileName = filename 271 self.__fileName = filename
275 276
276 @return flag indicating success (boolean) 277 @return flag indicating success (boolean)
277 """ 278 """
278 filename = E5FileDialog.getOpenFileName( 279 filename = E5FileDialog.getOpenFileName(
279 self, 280 self,
280 self.trUtf8("Load Diagram"), 281 self.tr("Load Diagram"),
281 "", 282 "",
282 self.trUtf8("Eric5 Graphics File (*.e5g);;All Files (*)")) 283 self.tr("Eric5 Graphics File (*.e5g);;All Files (*)"))
283 if not filename: 284 if not filename:
284 # Cancelled by user 285 # Cancelled by user
285 return False 286 return False
286 287
287 try: 288 try:
289 data = f.read() 290 data = f.read()
290 f.close() 291 f.close()
291 except (IOError, OSError) as err: 292 except (IOError, OSError) as err:
292 E5MessageBox.critical( 293 E5MessageBox.critical(
293 self, 294 self,
294 self.trUtf8("Load Diagram"), 295 self.tr("Load Diagram"),
295 self.trUtf8( 296 self.tr(
296 """<p>The file <b>{0}</b> could not be read.</p>""" 297 """<p>The file <b>{0}</b> could not be read.</p>"""
297 """<p>Reason: {1}</p>""").format(filename, str(err))) 298 """<p>Reason: {1}</p>""").format(filename, str(err)))
298 return False 299 return False
299 300
300 lines = data.splitlines() 301 lines = data.splitlines()
371 372
372 @param filename name of the file containing the invalid data (string) 373 @param filename name of the file containing the invalid data (string)
373 @param linenum number of the invalid line (integer) 374 @param linenum number of the invalid line (integer)
374 """ 375 """
375 if linenum < 0: 376 if linenum < 0:
376 msg = self.trUtf8("""<p>The file <b>{0}</b> does not contain""" 377 msg = self.tr("""<p>The file <b>{0}</b> does not contain"""
377 """ valid data.</p>""").format(filename) 378 """ valid data.</p>""").format(filename)
378 else: 379 else:
379 msg = self.trUtf8("""<p>The file <b>{0}</b> does not contain""" 380 msg = self.tr("""<p>The file <b>{0}</b> does not contain"""
380 """ valid data.</p><p>Invalid line: {1}</p>""" 381 """ valid data.</p><p>Invalid line: {1}</p>"""
381 ).format(filename, linenum + 1) 382 ).format(filename, linenum + 1)
382 E5MessageBox.critical(self, self.trUtf8("Load Diagram"), msg) 383 E5MessageBox.critical(self, self.tr("Load Diagram"), msg)

eric ide

mercurial