changeset 75:ba11d99b1d4a

Added qformlayout..
author windel
date Sun, 11 Nov 2012 11:28:28 +0100
parents f506f6b74697
children 1084b433c278
files python/apps/diagrameditor.py
diffstat 1 files changed, 23 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/python/apps/diagrameditor.py	Sun Nov 11 10:49:46 2012 +0100
+++ b/python/apps/diagrameditor.py	Sun Nov 11 11:28:28 2012 +0100
@@ -27,6 +27,14 @@
       return [offset + (l - offset*2)/(n - 1)*i for i in range(n)]
    return []
 
+def uniqify(name, names):
+   newname = name
+   i = 1
+   while newname in names:
+      newname = name + str(i)
+      i += 1
+   return newname
+
 class Connection(QGraphicsPathItem):
    """ Implementation of a connection between blocks """
    def __init__(self, fromPort, toPort):
@@ -136,18 +144,16 @@
       super(ParameterDialog, self).__init__(parent)
       self.block = block
       self.button = QPushButton('Ok', self)
-      l = QGridLayout(self)
-      l.addWidget(QLabel('Name:', self), 0, 0)
       self.nameEdit = QLineEdit(self.block.name)
-      l.addWidget(self.nameEdit, 0, 1)
-      l.addWidget(QLabel('Code:', self), 1, 0)
       self.codeEdit = QTextEdit(self)
       self.codeEdit.setPlainText(self.block.code)
-      l.addWidget(self.codeEdit, 1, 1)
-      l.addWidget(self.button, 2, 0, 1, 2)
+      l = QFormLayout(self)
+      l.addRow('Name:', self.nameEdit)
+      l.addRow('Code:', self.codeEdit)
+      l.addWidget(self.button)
       self.button.clicked.connect(self.OK)
    def OK(self):
-      self.block.setName(self.nameEdit.text())
+      self.block.name = self.nameEdit.text()
       self.block.code = self.codeEdit.toPlainText()
       self.close()
 
@@ -156,16 +162,11 @@
    def __init__(self, name, block, direction):
       super(PortItem, self).__init__(block)
       self.connection = None
-      path = QPainterPath()
       d = 10.0
       if direction == 'input':
-         path.moveTo(-d, -d)
-         path.lineTo(0.0, 0.0)
-         path.lineTo(-d, d)
+         path = buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)])
       else:
-         path.moveTo(0.0, -d)
-         path.lineTo(d, 0.0)
-         path.lineTo(0.0, d)
+         path = buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)])
       self.setPath(path)
       self.direction = direction
       self.block = block
@@ -173,12 +174,12 @@
       pen = QPen(Qt.blue, 2)
       pen.setCapStyle(Qt.RoundCap)
       self.setPen(pen)
-      self.textItem = QGraphicsTextItem(name, self)
-      self.setName(name)
+      self.textItem = QGraphicsTextItem(self)
+      self.name = name
       self.posCallbacks = []
       self.setFlag(self.ItemSendsScenePositionChanges, True)
+   def getName(self): return self.textItem.toPlainText()
    def setName(self, name):
-      self.name = name
       self.textItem.setPlainText(name)
       rect = self.textItem.boundingRect()
       lw, lh = rect.width(), rect.height()
@@ -187,6 +188,7 @@
       else:
          lx = -3 - lw
       self.textItem.setPos(lx, -lh / 2)
+   name = property(getName, setName)
    def itemChange(self, change, value):
       if change == self.ItemScenePositionHasChanged:
          for cb in self.posCallbacks:
@@ -227,14 +229,6 @@
       # Call superclass method:
       return super(HandleItem, self).itemChange(change, value)
 
-def uniqify(name, names):
-   newname = name
-   i = 1
-   while newname in names:
-      newname = name + str(i)
-      i += 1
-   return newname
-
 class BlockItem(QGraphicsRectItem):
    """ 
       Represents a block in the diagram
@@ -250,8 +244,7 @@
       # Properties of the rectangle:
       self.setPen(QPen(Qt.blue, 2))
       self.setBrush(QBrush(Qt.lightGray))
-      self.setFlags(self.ItemIsSelectable | self.ItemIsMovable)
-      self.setFlag(self.ItemSendsScenePositionChanges, True)
+      self.setFlags(self.ItemIsSelectable | self.ItemIsMovable | self.ItemSendsScenePositionChanges)
       self.setCursor(QCursor(Qt.PointingHandCursor))
       self.label = QGraphicsTextItem(name, self)
       self.name = name
@@ -285,9 +278,9 @@
    def newOutputPort(self):
       names = [i.name for i in self.inputs + self.outputs]
       self.addOutput(PortItem(uniqify('out', names), self, 'output'))
-   def setName(self, name):
-      self.name = name
-      self.label.setPlainText(name)
+   def setName(self, name): self.label.setPlainText(name)
+   def getName(self): return self.label.toPlainText()
+   name = property(getName, setName)
    def addInput(self, i):
       self.inputs.append(i)
       self.updateSize()