changeset 77:a5f3959bcab7

Added first hierarchy
author windel
date Sun, 11 Nov 2012 14:08:51 +0100
parents 1084b433c278
children 85bfa15c01f1
files python/apps/diagrameditor.py
diffstat 1 files changed, 32 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/python/apps/diagrameditor.py	Sun Nov 11 11:53:52 2012 +0100
+++ b/python/apps/diagrameditor.py	Sun Nov 11 14:08:51 2012 +0100
@@ -18,8 +18,7 @@
    for pt in pts[1:]: path.lineTo(pt)
    return path
 
-def equalSpace(n, l):
-   offset = 15
+def equalSpace(n, l, offset=15):
    if n == 1:
       return [l / 2]
    elif n > 1:
@@ -28,8 +27,7 @@
 
 def uniqify(name, names):
    newname, i = name, 1
-   while newname in names:
-      newname, i = name + str(i), i + 1
+   while newname in names: newname, i = name + str(i), i + 1
    return newname
 
 class Connection(QGraphicsPathItem):
@@ -226,6 +224,8 @@
    """
    def __init__(self, name='Untitled', parent=None):
       super(BlockItem, self).__init__(parent)
+      self.subModel = DiagramScene()
+      self.subModel.containingBlock = self
       # Properties of the rectangle:
       self.setPen(QPen(Qt.blue, 2))
       self.setBrush(QBrush(Qt.lightGray))
@@ -256,7 +256,12 @@
       pd = ParameterDialog(self, self.window())
       pd.exec_()
    def mouseDoubleClickEvent(self, event):
-      self.editParameters()
+      #self.editParameters()
+      scene = self.scene()
+      if scene:
+         for view in scene.views():
+            view.dScene = self.subModel
+            view.zoomAll()
    def newInputPort(self):
       names = [i.name for i in self.inputs + self.outputs]
       self.addInput(PortItem(uniqify('in', names), self, 'input'))
@@ -310,27 +315,28 @@
 class EditorGraphicsView(QGraphicsView):
    def __init__(self, parent=None):
       QGraphicsView.__init__(self, parent)
-      scene = DiagramScene(self)
-      self.diagramScene = scene
-      self.setScene(scene)
+      self.dScene = DiagramScene(self)
       self.setDragMode(QGraphicsView.RubberBandDrag)
-      testShortcut = QShortcut(QKeySequence("F12"), self)
-      testShortcut.activated.connect(self.test)
       delShort = QShortcut(QKeySequence.Delete, self)
-      delShort.activated.connect(scene.deleteItems)
-      self.loadDiagram = scene.loadDiagram
+      delShort.activated.connect(self.dScene.deleteItems)
+   dScene = property(lambda s: s.scene(), lambda s, v: s.setScene(v))
    def save(self):
       self.diagramScene.saveDiagram('diagram2.usd')
    def load(self):
       filename = QFileDialog.getOpenFileName(self)
       self.diagramScene.loadDiagram(filename)
+   def goDown(self):
+      print('Down!')
+      block = self.dScene.selected
+   def goUp(self):
+      print('Up!')
+      if hasattr(self.dScene, 'containingBlock'):
+         self.dScene = self.dScene.containingBlock.scene()
+         self.zoomAll()
    def zoomAll(self):
       """ zoom to fit all items """
-      rect = self.diagramScene.itemsBoundingRect()
+      rect = self.dScene.itemsBoundingRect()
       self.fitInView(rect, Qt.KeepAspectRatio)
-   def test(self):
-      self.rotate(30)
-      self.zoomAll()
    def wheelEvent(self, event):
       pos = event.pos()
       posbefore = self.mapToScene(pos)
@@ -339,11 +345,9 @@
       self.scale(sx, sx)
       event.accept()
    def dragEnterEvent(self, event):
-      if event.mimeData().hasFormat('component/name'):
-         event.accept()
+      if event.mimeData().hasFormat('component/name'): event.accept()
    def dragMoveEvent(self, event):
-      if event.mimeData().hasFormat('component/name'):
-         event.accept()
+      if event.mimeData().hasFormat('component/name'): event.accept()
    def dropEvent(self, event):
       if event.mimeData().hasFormat('component/name'):
          name = bytes(event.mimeData().data('component/name')).decode()
@@ -353,8 +357,7 @@
 class LibraryModel(QStandardItemModel):
    def __init__(self, parent=None):
       QStandardItemModel.__init__(self, parent)
-   def mimeTypes(self):
-      return ['component/name']
+   mimeTypes = lambda self: ['component/name']
    def mimeData(self, idxs):
       mimedata = QMimeData()
       for idx in idxs:
@@ -368,10 +371,11 @@
    def __init__(self, parent=None):
       super(DiagramScene, self).__init__(parent)
       self.startedConnection = None
+   blocks = property(lambda sel: [i for i in sel.items() if type(i) is BlockItem])
+   connections = property(lambda sel: [i for i in sel.items() if type(i) is Connection])
    def saveDiagram(self, filename):
-      items = self.items()
-      blocks = [item for item in items if type(item) is BlockItem]
-      connections = [item for item in items if type(item) is Connection]
+      blocks = self.blocks
+      connections = self.connections
       doc = md.Document()
       modelElement = doc.createElement('system')
       doc.appendChild(modelElement)
@@ -416,7 +420,6 @@
          modelElement.appendChild(connectionElement)
       with open(filename, 'w') as f:
          f.write(doc.toprettyxml())
-         
    def loadDiagram(self, filename):
       try:
          doc = md.parse(filename)
@@ -555,11 +558,13 @@
       act('Load', QKeySequence.Open, self.editor.load)
       act('Full screen', QKeySequence("F11"), self.toggleFullScreen)
       act('Fit in view', QKeySequence("F8"), self.editor.zoomAll)
+      act('Go down', QKeySequence(Qt.Key_Down), self.editor.goDown)
+      act('Go up', QKeySequence(Qt.Key_Up), self.editor.goUp)
       self.library = LibraryWidget() 
       libraryDock = QDockWidget('Library', self)
       libraryDock.setWidget(self.library)
       self.addDockWidget(Qt.LeftDockWidgetArea, libraryDock)
-      self.editor.loadDiagram('diagram2.usd')
+      self.editor.scene().loadDiagram('diagram2.usd')
    def toggleFullScreen(self):
       self.setWindowState(self.windowState() ^ Qt.WindowFullScreen)
       self.editor.zoomAll()