changeset 93:f7ec7517cabb

edits
author windel
date Mon, 24 Dec 2012 12:52:13 +0100
parents 0c4bdbf0aead
children 1be00bcfaabb
files python/apps/diagrameditor.py python/apps/diagramitems.py
diffstat 2 files changed, 32 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/python/apps/diagrameditor.py	Sat Dec 15 23:56:26 2012 +0100
+++ b/python/apps/diagrameditor.py	Mon Dec 24 12:52:13 2012 +0100
@@ -6,6 +6,7 @@
 
 from diagramitems import Connection, ResizeSelectionHandle, Block, DiagramScene, CodeBlock
 from icons import newicon, saveicon, loadicon
+import diagramitems
 
 """
  Author: Windel Bouwman
@@ -112,19 +113,22 @@
       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()
    def dropEvent(self, event):
       if event.mimeData().hasFormat('component/name'):
          name = bytes(event.mimeData().data('component/name')).decode()
+         kind, name = name.split(':')
          pos = self.mapToScene(event.pos())
          s = self.scene()
-         b = CodeBlock(s.uniqify(name))
+         print(kind, 'name:', name)
+         kind = getattr(diagramitems, kind)
+         print(kind)
+         b = kind(s.uniqify(name))
          b.setPos(pos)
          s.addItem(b)
-         # TODO: do this in a cleaner way:
-         self.model.modelReset.emit()
 
 class LibraryModel(QStandardItemModel):
    mimeTypes = lambda self: ['component/name']
@@ -140,10 +144,10 @@
    def __init__(self):
       super(ModelHierarchyModel, self).__init__()
       self.rootDiagram = DiagramScene()
-      self.rootDiagram.changed.connect(self.cha)
+      self.rootDiagram.structureChanged.connect(self.handlechange)
       self.filename = None
-   def cha(self, region):
-      print(region)
+   def handlechange(self):
+      self.modelReset.emit()
    def setDict(self, d):
       self.rootDiagram.Dict = d
       self.modelReset.emit()
@@ -174,6 +178,7 @@
          if block.scene() == self.rootDiagram:
             return QModelIndex()
          else:
+            print(block)
             outerBlock = block.scene().containingBlock
             return outerBlock.index
       print('parent: No valid index')
@@ -220,7 +225,7 @@
       painter.drawEllipse(20, 20, 20, 20)
       painter.end()
       # Fill library:
-      for name in ['Block', 'Uber unit', 'Device']:
+      for name in ['CodeBlock:codeBlock', 'DiagramBlock:submod', 'Block:blk']:
          self.libraryModel.appendRow(QStandardItem(QIcon(pixmap), name))
       self.setModel(self.libraryModel)
       self.setViewMode(self.IconMode)
--- a/python/apps/diagramitems.py	Sat Dec 15 23:56:26 2012 +0100
+++ b/python/apps/diagramitems.py	Mon Dec 24 12:52:13 2012 +0100
@@ -176,7 +176,7 @@
 
 class ResizeSelectionHandle(Handle):
    def __init__(self, position, block):
-      super(ResizeSelectionHandle, self).__init__(dx=6, parent=block)
+      super(ResizeSelectionHandle, self).__init__(dx=12, parent=block)
       self.position = position
       self.block = block
       if position in [Position.TOP_LEFT, Position.BOTTOM_RIGHT]:
@@ -221,13 +221,6 @@
    def editParameters(self):
       pd = ParameterDialog(self, self.window())
       pd.exec_()
-   def mouseDoubleClickEvent(self, event):
-      #self.editParameters()
-      scene = self.scene()
-      if scene:
-         for view in scene.views():
-            view.diagram = self.subModel
-            view.zoomAll()
    def newInputPort(self):
       names = [i.name for i in self.inputs + self.outputs]
       self.addInput(InputPort(uniqify('in', names), self))
@@ -360,15 +353,33 @@
       self.subModel.containingBlock = self
    def setDict(self, d):
       self.subModel.Dict = d['submodel']
+   def mouseDoubleClickEvent(self, event):
+      # descent into child diagram
+      #self.editParameters()
+      print('descent')
+      scene = self.scene()
+      if scene:
+         for view in scene.views():
+            view.diagram = self.subModel
+            view.zoomAll()
 
 class DiagramScene(QGraphicsScene):
    """ A diagram scene consisting of blocks and connections """
+   structureChanged = pyqtSignal()
    def __init__(self):
       super(DiagramScene, self).__init__()
       self.startedConnection = None
 
    blocks = property(lambda sel: [i for i in sel.items() if isinstance(i, Block)])
    connections = property(lambda sel: [i for i in sel.items() if type(i) is Connection])
+   def addItem(self, item):
+      super(DiagramScene, self).addItem(item)
+      if isinstance(item, Block):
+         self.structureChanged.emit()
+   def removeItem(self, item):
+      super(DiagramScene, self).removeItem(item)
+      if isinstance(item, Block):
+         self.structureChanged.emit()
    def setDict(self, d):
       for block in d['blocks']:
          b = Block()