Graphics/UMLGraphicsView.py

changeset 2033
4b99609f6a87
parent 2031
c36c2eb62a75
child 2034
8de0fc1f7fef
equal deleted inserted replaced
2032:704593d042fe 2033:4b99609f6a87
32 @signal relayout() emitted to indicate a relayout of the diagram 32 @signal relayout() emitted to indicate a relayout of the diagram
33 is requested 33 is requested
34 """ 34 """
35 relayout = pyqtSignal() 35 relayout = pyqtSignal()
36 36
37 def __init__(self, scene, diagramType, parent=None): 37 def __init__(self, scene, parent=None):
38 """ 38 """
39 Constructor 39 Constructor
40 40
41 @param scene reference to the scene object (QGraphicsScene) 41 @param scene reference to the scene object (QGraphicsScene)
42 @param diagramType type of the diagram (string)
43 @param parent parent widget of the view (QWidget) 42 @param parent parent widget of the view (QWidget)
44 """ 43 """
45 E5GraphicsView.__init__(self, scene, parent) 44 E5GraphicsView.__init__(self, scene, parent)
46 self.setObjectName("UMLGraphicsView") 45 self.setObjectName("UMLGraphicsView")
47 self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate) 46 self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
48 47
49 self.diagramName = "Unnamed" 48 self.diagramName = "Unnamed"
50 self.diagramType = diagramType
51
52 self.persistenceData = ""
53 self.__fileName = ""
54 self.__itemId = -1 49 self.__itemId = -1
55 50
56 self.border = 10 51 self.border = 10
57 self.deltaSize = 100.0 52 self.deltaSize = 100.0
58 53
71 66
72 self.deleteShapeAct = \ 67 self.deleteShapeAct = \
73 QAction(UI.PixmapCache.getIcon("deleteShape.png"), 68 QAction(UI.PixmapCache.getIcon("deleteShape.png"),
74 self.trUtf8("Delete shapes"), self) 69 self.trUtf8("Delete shapes"), self)
75 self.deleteShapeAct.triggered[()].connect(self.__deleteShape) 70 self.deleteShapeAct.triggered[()].connect(self.__deleteShape)
76
77 self.saveAct = \
78 QAction(UI.PixmapCache.getIcon("fileSave.png"),
79 self.trUtf8("Save"), self)
80 self.saveAct.triggered[()].connect(self.__save)
81
82 self.saveAsAct = \
83 QAction(UI.PixmapCache.getIcon("fileSaveAs.png"),
84 self.trUtf8("Save As..."), self)
85 self.saveAsAct.triggered[()].connect(self.__saveAs)
86
87 self.saveImageAct = \
88 QAction(UI.PixmapCache.getIcon("fileSavePixmap.png"),
89 self.trUtf8("Save as PNG"), self)
90 self.saveImageAct.triggered[()].connect(self.__saveImage)
91
92 self.printAct = \
93 QAction(UI.PixmapCache.getIcon("print.png"),
94 self.trUtf8("Print"), self)
95 self.printAct.triggered[()].connect(self.__printDiagram)
96
97 self.printPreviewAct = \
98 QAction(UI.PixmapCache.getIcon("printPreview.png"),
99 self.trUtf8("Print Preview"), self)
100 self.printPreviewAct.triggered[()].connect(self.__printPreviewDiagram)
101 71
102 self.zoomInAct = \ 72 self.zoomInAct = \
103 QAction(UI.PixmapCache.getIcon("zoomIn.png"), 73 QAction(UI.PixmapCache.getIcon("zoomIn.png"),
104 self.trUtf8("Zoom in"), self) 74 self.trUtf8("Zoom in"), self)
105 self.zoomInAct.triggered[()].connect(self.zoomIn) 75 self.zoomInAct.triggered[()].connect(self.zoomIn)
235 @return the populated toolBar (QToolBar) 205 @return the populated toolBar (QToolBar)
236 """ 206 """
237 toolBar = QToolBar(self.trUtf8("Graphics"), self) 207 toolBar = QToolBar(self.trUtf8("Graphics"), self)
238 toolBar.setIconSize(UI.Config.ToolBarIconSize) 208 toolBar.setIconSize(UI.Config.ToolBarIconSize)
239 toolBar.addAction(self.deleteShapeAct) 209 toolBar.addAction(self.deleteShapeAct)
240 toolBar.addSeparator()
241 toolBar.addAction(self.saveAct)
242 toolBar.addAction(self.saveAsAct)
243 toolBar.addAction(self.saveImageAct)
244 toolBar.addSeparator()
245 toolBar.addAction(self.printPreviewAct)
246 toolBar.addAction(self.printAct)
247 toolBar.addSeparator() 210 toolBar.addSeparator()
248 toolBar.addAction(self.zoomInAct) 211 toolBar.addAction(self.zoomInAct)
249 toolBar.addAction(self.zoomOutAct) 212 toolBar.addAction(self.zoomOutAct)
250 toolBar.addAction(self.zoomAct) 213 toolBar.addAction(self.zoomAct)
251 toolBar.addAction(self.zoomResetAct) 214 toolBar.addAction(self.zoomResetAct)
360 initial size (boolean) 323 initial size (boolean)
361 """ 324 """
362 super().autoAdjustSceneSize(limit=limit) 325 super().autoAdjustSceneSize(limit=limit)
363 self.__checkSizeActions() 326 self.__checkSizeActions()
364 327
365 def __saveImage(self): 328 def saveImage(self):
366 """ 329 """
367 Private method to handle the save context menu entry. 330 Public method to handle the save context menu entry.
368 """ 331 """
369 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 332 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
370 self, 333 self,
371 self.trUtf8("Save Diagram"), 334 self.trUtf8("Save Diagram"),
372 "", 335 "",
387 " Overwrite it?</p>").format(fname), 350 " Overwrite it?</p>").format(fname),
388 icon=E5MessageBox.Warning) 351 icon=E5MessageBox.Warning)
389 if not res: 352 if not res:
390 return 353 return
391 354
392 success = self.saveImage(fname, QFileInfo(fname).suffix().upper()) 355 success = super().saveImage(fname, QFileInfo(fname).suffix().upper())
393 if not success: 356 if not success:
394 E5MessageBox.critical(self, 357 E5MessageBox.critical(self,
395 self.trUtf8("Save Diagram"), 358 self.trUtf8("Save Diagram"),
396 self.trUtf8("""<p>The file <b>{0}</b> could not be saved.</p>""") 359 self.trUtf8("""<p>The file <b>{0}</b> could not be saved.</p>""")
397 .format(fname)) 360 .format(fname))
402 """ 365 """
403 self.__itemId = -1 366 self.__itemId = -1
404 self.scene().clear() 367 self.scene().clear()
405 self.relayout.emit() 368 self.relayout.emit()
406 369
407 def __printDiagram(self): 370 def printDiagram(self):
408 """ 371 """
409 Private slot called to print the diagram. 372 Public slot called to print the diagram.
410 """ 373 """
411 printer = QPrinter(mode=QPrinter.ScreenResolution) 374 printer = QPrinter(mode=QPrinter.ScreenResolution)
412 printer.setFullPage(True) 375 printer.setFullPage(True)
413 if Preferences.getPrinter("ColorMode"): 376 if Preferences.getPrinter("ColorMode"):
414 printer.setColorMode(QPrinter.Color) 377 printer.setColorMode(QPrinter.Color)
427 ) 390 )
428 printer.setPrinterName(Preferences.getPrinter("PrinterName")) 391 printer.setPrinterName(Preferences.getPrinter("PrinterName"))
429 392
430 printDialog = QPrintDialog(printer, self) 393 printDialog = QPrintDialog(printer, self)
431 if printDialog.exec_(): 394 if printDialog.exec_():
432 self.printDiagram(printer, self.diagramName) 395 super().printDiagram(printer, self.diagramName)
433 396
434 def __printPreviewDiagram(self): 397 def printPreviewDiagram(self):
435 """ 398 """
436 Private slot called to show a print preview of the diagram. 399 Public slot called to show a print preview of the diagram.
437 """ 400 """
438 from PyQt4.QtGui import QPrintPreviewDialog 401 from PyQt4.QtGui import QPrintPreviewDialog
439 402
440 printer = QPrinter(mode=QPrinter.ScreenResolution) 403 printer = QPrinter(mode=QPrinter.ScreenResolution)
441 printer.setFullPage(True) 404 printer.setFullPage(True)
455 QPrinter.Millimeter 418 QPrinter.Millimeter
456 ) 419 )
457 printer.setPrinterName(Preferences.getPrinter("PrinterName")) 420 printer.setPrinterName(Preferences.getPrinter("PrinterName"))
458 421
459 preview = QPrintPreviewDialog(printer, self) 422 preview = QPrintPreviewDialog(printer, self)
460 preview.paintRequested[QPrinter].connect( 423 preview.paintRequested[QPrinter].connect(self.__printPreviewPrint)
461 lambda x: self.printDiagram(x, self.diagramName))
462 preview.exec_() 424 preview.exec_()
425
426 def __printPreviewPrint(self, printer):
427 """
428 Private slot to generate a print preview.
429 """
430 super().printDiagram(printer, self.diagramName)
463 431
464 def __zoom(self): 432 def __zoom(self):
465 """ 433 """
466 Private method to handle the zoom context menu action. 434 Private method to handle the zoom context menu action.
467 """ 435 """
586 pinch.setScaleFactor(self.zoom()) 554 pinch.setScaleFactor(self.zoom())
587 else: 555 else:
588 self.setZoom(pinch.scaleFactor()) 556 self.setZoom(pinch.scaleFactor())
589 evt.accept() 557 evt.accept()
590 558
591 def setPersistenceData(self, data):
592 """
593 Public method to set additional persistence data.
594
595 @param data string of additional data to be made persistent (string)
596 """
597 self.persistenceData = data
598
599 def getPersistenceData(self):
600 """
601 Public method to get the additional persistence data.
602
603 @return additional persistence data (string)
604 """
605 return self.persistenceData
606
607 def getItemId(self): 559 def getItemId(self):
608 """ 560 """
609 Public method to get the ID to be assigned to an item. 561 Public method to get the ID to be assigned to an item.
610 562
611 @return item ID (integer) 563 @return item ID (integer)
629 if itemID == id: 581 if itemID == id:
630 return item 582 return item
631 583
632 return None 584 return None
633 585
634 def __save(self): 586 def getPersistenceData(self):
635 """ 587 """
636 Private slot to save the diagram with the current name. 588 Public method to get a list of data to be persisted.
637 """ 589
638 self.__saveAs(self.__fileName) 590 @return list of data to be persisted (list of strings)
639 591 """
640 def __saveAs(self, filename=""):
641 """
642 Private slot to save the diagram.
643
644 @param filename name of the file to write to (string)
645 """
646 if not filename:
647 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
648 self,
649 self.trUtf8("Save Diagram"),
650 "",
651 self.trUtf8("Eric5 Graphics File (*.e5g);;All Files (*)"),
652 "",
653 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
654 if not fname:
655 return
656 ext = QFileInfo(fname).suffix()
657 if not ext:
658 ex = selectedFilter.split("(*")[1].split(")")[0]
659 if ex:
660 fname += ex
661 if QFileInfo(fname).exists():
662 res = E5MessageBox.yesNo(self,
663 self.trUtf8("Save Diagram"),
664 self.trUtf8("<p>The file <b>{0}</b> already exists."
665 " Overwrite it?</p>").format(fname),
666 icon=E5MessageBox.Warning)
667 if not res:
668 return
669 filename = fname
670
671 lines = [ 592 lines = [
672 "version: 1.0",
673 "diagram_type: {0} ({1})".format(self.diagramType,
674 self.parent().diagramTypeToString(self.diagramType)),
675 "diagram_name: {0}".format(self.diagramName), 593 "diagram_name: {0}".format(self.diagramName),
676 "scene_size: {0};{1}".format(self.scene().width(), self.scene().height()),
677 ] 594 ]
678 if self.persistenceData: 595
679 lines.append("diagram_data: {0}".format(self.persistenceData))
680 for item in self.filteredItems(self.scene().items(), UMLItem): 596 for item in self.filteredItems(self.scene().items(), UMLItem):
681 lines.append("item: id={0}, x={1}, y={2}{3}".format( 597 lines.append("item: id={0}, x={1}, y={2}, item_type={3}{4}".format(
682 item.getId(), item.x(), item.y(), item.buildItemDataString())) 598 item.getId(), item.x(), item.y(), item.getItemType(),
599 item.buildItemDataString()))
600
683 for item in self.filteredItems(self.scene().items(), AssociationItem): 601 for item in self.filteredItems(self.scene().items(), AssociationItem):
684 lines.append("association: {0}".format(item.buildAssociationItemDataString())) 602 lines.append("association: {0}".format(item.buildAssociationItemDataString()))
685 603
686 try: 604 return lines
687 f = open(filename, "w", encoding="utf-8") 605
688 f.write("\n".join(lines)) 606 def parsePersistenceData(self, data):
689 f.close() 607 """
690 except (IOError, OSError) as err: 608 Public method to parse persisted data.
691 E5MessageBox.critical(self, 609
692 self.trUtf8("Save Diagram"), 610 @param dat persisted data to be parsed (string)
693 self.trUtf8("""<p>The file <b>{0}</b> could not be saved.</p>""" 611 """
694 """<p>Reason: {1}</p>""").format(fname, str(err))) 612 # TODO: implement this
695 613 return
696 self.__fileName = filename

eric ide

mercurial