Graphics/UMLGraphicsView.py

changeset 2030
db11a2fe9bbc
parent 2028
30247d523fdb
child 2031
c36c2eb62a75
equal deleted inserted replaced
2028:30247d523fdb 2030:db11a2fe9bbc
13 from E5Graphics.E5GraphicsView import E5GraphicsView 13 from E5Graphics.E5GraphicsView import E5GraphicsView
14 14
15 from E5Gui import E5MessageBox, E5FileDialog 15 from E5Gui import E5MessageBox, E5FileDialog
16 16
17 from .UMLItem import UMLItem 17 from .UMLItem import UMLItem
18 from .AssociationItem import AssociationItem
18 from .UMLSceneSizeDialog import UMLSceneSizeDialog 19 from .UMLSceneSizeDialog import UMLSceneSizeDialog
19 from .ZoomDialog import ZoomDialog 20 from .ZoomDialog import ZoomDialog
20 21
21 import UI.Config 22 import UI.Config
22 import UI.PixmapCache 23 import UI.PixmapCache
31 @signal relayout() emitted to indicate a relayout of the diagram 32 @signal relayout() emitted to indicate a relayout of the diagram
32 is requested 33 is requested
33 """ 34 """
34 relayout = pyqtSignal() 35 relayout = pyqtSignal()
35 36
36 def __init__(self, scene, diagramName="Unnamed", parent=None, name=None): 37 def __init__(self, scene, diagramType, diagramName="Unnamed", parent=None,
38 name=None):
37 """ 39 """
38 Constructor 40 Constructor
39 41
40 @param scene reference to the scene object (QGraphicsScene) 42 @param scene reference to the scene object (QGraphicsScene)
43 @param diagramType type of the diagram (string)
41 @param diagramName name of the diagram (string) 44 @param diagramName name of the diagram (string)
42 @param parent parent widget of the view (QWidget) 45 @param parent parent widget of the view (QWidget)
43 @param name name of the view widget (string) 46 @param name name of the view widget (string)
44 """ 47 """
45 E5GraphicsView.__init__(self, scene, parent) 48 E5GraphicsView.__init__(self, scene, parent)
46 if name: 49 if name:
47 self.setObjectName(name) 50 self.setObjectName(name)
48 self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate) 51 self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
49 52
50 self.diagramName = diagramName 53 self.diagramName = diagramName
54 self.diagramType = diagramType
55
56 self.persistenceData = ""
57 self.__fileName = ""
58 self.__itemId = -1
51 59
52 self.border = 10 60 self.border = 10
53 self.deltaSize = 100.0 61 self.deltaSize = 100.0
54 62
55 self.__initActions() 63 self.__initActions()
70 self.trUtf8("Delete shapes"), self) 78 self.trUtf8("Delete shapes"), self)
71 self.deleteShapeAct.triggered[()].connect(self.__deleteShape) 79 self.deleteShapeAct.triggered[()].connect(self.__deleteShape)
72 80
73 self.saveAct = \ 81 self.saveAct = \
74 QAction(UI.PixmapCache.getIcon("fileSave.png"), 82 QAction(UI.PixmapCache.getIcon("fileSave.png"),
83 self.trUtf8("Save"), self)
84 self.saveAct.triggered[()].connect(self.__save)
85
86 self.saveAsAct = \
87 QAction(UI.PixmapCache.getIcon("fileSaveAs.png"),
88 self.trUtf8("Save As..."), self)
89 self.saveAsAct.triggered[()].connect(self.__saveAs)
90
91 self.saveImageAct = \
92 QAction(UI.PixmapCache.getIcon("fileSavePixmap.png"),
75 self.trUtf8("Save as PNG"), self) 93 self.trUtf8("Save as PNG"), self)
76 self.saveAct.triggered[()].connect(self.__saveImage) 94 self.saveImageAct.triggered[()].connect(self.__saveImage)
77 95
78 self.printAct = \ 96 self.printAct = \
79 QAction(UI.PixmapCache.getIcon("print.png"), 97 QAction(UI.PixmapCache.getIcon("print.png"),
80 self.trUtf8("Print"), self) 98 self.trUtf8("Print"), self)
81 self.printAct.triggered[()].connect(self.__printDiagram) 99 self.printAct.triggered[()].connect(self.__printDiagram)
223 toolBar = QToolBar(self.trUtf8("Graphics"), self) 241 toolBar = QToolBar(self.trUtf8("Graphics"), self)
224 toolBar.setIconSize(UI.Config.ToolBarIconSize) 242 toolBar.setIconSize(UI.Config.ToolBarIconSize)
225 toolBar.addAction(self.deleteShapeAct) 243 toolBar.addAction(self.deleteShapeAct)
226 toolBar.addSeparator() 244 toolBar.addSeparator()
227 toolBar.addAction(self.saveAct) 245 toolBar.addAction(self.saveAct)
246 toolBar.addAction(self.saveAsAct)
247 toolBar.addAction(self.saveImageAct)
228 toolBar.addSeparator() 248 toolBar.addSeparator()
229 toolBar.addAction(self.printPreviewAct) 249 toolBar.addAction(self.printPreviewAct)
230 toolBar.addAction(self.printAct) 250 toolBar.addAction(self.printAct)
231 toolBar.addSeparator() 251 toolBar.addSeparator()
232 toolBar.addAction(self.zoomInAct) 252 toolBar.addAction(self.zoomInAct)
249 toolBar.addSeparator() 269 toolBar.addSeparator()
250 toolBar.addAction(self.relayoutAct) 270 toolBar.addAction(self.relayoutAct)
251 271
252 return toolBar 272 return toolBar
253 273
254 def filteredItems(self, items): 274 def filteredItems(self, items, itemType=UMLItem):
255 """ 275 """
256 Public method to filter a list of items. 276 Public method to filter a list of items.
257 277
258 @param items list of items as returned by the scene object 278 @param items list of items as returned by the scene object
259 (QGraphicsItem) 279 (QGraphicsItem)
280 @param itemType type to be filtered (class)
260 @return list of interesting collision items (QGraphicsItem) 281 @return list of interesting collision items (QGraphicsItem)
261 """ 282 """
262 return [itm for itm in items if isinstance(itm, UMLItem)] 283 return [itm for itm in items if isinstance(itm, itemType)]
263 284
264 def selectItems(self, items): 285 def selectItems(self, items):
265 """ 286 """
266 Public method to select the given items. 287 Public method to select the given items.
267 288
381 402
382 def __relayout(self): 403 def __relayout(self):
383 """ 404 """
384 Private method to handle the re-layout context menu entry. 405 Private method to handle the re-layout context menu entry.
385 """ 406 """
407 self.__itemId = -1
386 self.scene().clear() 408 self.scene().clear()
387 self.relayout.emit() 409 self.relayout.emit()
388 410
389 def __printDiagram(self): 411 def __printDiagram(self):
390 """ 412 """
567 if pinch.state() == Qt.GestureStarted: 589 if pinch.state() == Qt.GestureStarted:
568 pinch.setScaleFactor(self.zoom()) 590 pinch.setScaleFactor(self.zoom())
569 else: 591 else:
570 self.setZoom(pinch.scaleFactor()) 592 self.setZoom(pinch.scaleFactor())
571 evt.accept() 593 evt.accept()
594
595 def setPersistenceData(self, data):
596 """
597 Public method to set additional persistence data.
598
599 @param data string of additional data to be made persistent (string)
600 """
601 self.persistenceData = data
602
603 def getPersistenceData(self):
604 """
605 Public method to get the additional persistence data.
606
607 @return additional persistence data (string)
608 """
609 return self.persistenceData
610
611 def getItemId(self):
612 """
613 Public method to get the ID to be assigned to an item.
614
615 @return item ID (integer)
616 """
617 self.__itemId += 1
618 return self.__itemId
619
620 def findItem(self, id):
621 """
622 Public method to find an UML item based on the ID.
623
624 @param id of the item to search for (integer)
625 @return item found (UMLItem) or None
626 """
627 for item in self.scene().items():
628 try:
629 itemID = item.getId()
630 except AttributeError:
631 continue
632
633 if itemID == id:
634 return item
635
636 return None
637
638 def __save(self):
639 """
640 Private slot to save the diagram with the current name.
641 """
642 self.__saveAs(self.__fileName)
643
644 def __saveAs(self, filename=""):
645 """
646 Private slot to save the diagram.
647
648 @param filename name of the file to write to (string)
649 """
650 if not filename:
651 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
652 self,
653 self.trUtf8("Save Diagram"),
654 "",
655 self.trUtf8("Eric5 Graphics File (*.e5g);;All Files (*)"),
656 "",
657 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
658 if not fname:
659 return
660 ext = QFileInfo(fname).suffix()
661 if not ext:
662 ex = selectedFilter.split("(*")[1].split(")")[0]
663 if ex:
664 fname += ex
665 if QFileInfo(fname).exists():
666 res = E5MessageBox.yesNo(self,
667 self.trUtf8("Save Diagram"),
668 self.trUtf8("<p>The file <b>{0}</b> already exists."
669 " Overwrite it?</p>").format(fname),
670 icon=E5MessageBox.Warning)
671 if not res:
672 return
673 filename = fname
674
675 lines = [
676 "version: 1.0",
677 "diagram_type: {0}".format(self.diagramType),
678 "diagram_name: {0}".format(self.diagramName),
679 "scene_size: {0};{1}".format(self.scene().width(), self.scene().height()),
680 ]
681 if self.persistenceData:
682 lines.append("diagram_data: {0}".format(self.persistenceData))
683 for item in self.filteredItems(self.scene().items(), UMLItem):
684 lines.append("item: id={0}, x={1}, y={2}{3}".format(
685 item.getId(), item.x(), item.y(), item.buildItemDataString()))
686 for item in self.filteredItems(self.scene().items(), AssociationItem):
687 lines.append("association: {0}".format(item.buildAssociationItemDataString()))
688
689 try:
690 f = open(filename, "w", encoding="utf-8")
691 f.write("\n".join(lines))
692 f.close()
693 except (IOError, OSError) as err:
694 E5MessageBox.critical(self,
695 self.trUtf8("Save Diagram"),
696 self.trUtf8("""<p>The file <b>{0}</b> could not be saved.</p>"""
697 """<p>Reason: {1}</p>""").format(fname, str(err)))
698
699 self.__fileName = filename

eric ide

mercurial