diff python/apps/diagrameditor.py @ 76:1084b433c278

Line reduction
author windel
date Sun, 11 Nov 2012 11:53:52 +0100
parents ba11d99b1d4a
children a5f3959bcab7
line wrap: on
line diff
--- a/python/apps/diagrameditor.py	Sun Nov 11 11:28:28 2012 +0100
+++ b/python/apps/diagrameditor.py	Sun Nov 11 11:53:52 2012 +0100
@@ -15,8 +15,7 @@
 
 def buildPath(pts):
    path = QPainterPath(pts[0])
-   for pt in pts[1:]:
-      path.lineTo(pt)
+   for pt in pts[1:]: path.lineTo(pt)
    return path
 
 def equalSpace(n, l):
@@ -28,11 +27,9 @@
    return []
 
 def uniqify(name, names):
-   newname = name
-   i = 1
+   newname, i = name, 1
    while newname in names:
-      newname = name + str(i)
-      i += 1
+      newname, i = name + str(i), i + 1
    return newname
 
 class Connection(QGraphicsPathItem):
@@ -40,10 +37,8 @@
    def __init__(self, fromPort, toPort):
       super(Connection, self).__init__()
       self.pos2 = self.fromPort = self.toPort = None
-      self.setFlag(self.ItemIsSelectable, True)
-      self.setFlag(self.ItemClipsToShape, True)
-      pen = QPen(Qt.blue, 2)
-      pen.setCapStyle(Qt.RoundCap)
+      self.setFlags(self.ItemIsSelectable | self.ItemClipsToShape)
+      pen = QPen(Qt.blue, 2, cap=Qt.RoundCap)
       self.setPen(pen)
       self.arrowhead = QGraphicsPathItem(self)
       self.arrowhead.setPath(buildPath([QPointF(0.0, 0.0), QPointF(-6.0, 10.0), QPointF(6.0, 10.0), QPointF(0.0, 0.0)]))
@@ -66,7 +61,6 @@
             tidx = idx
          idx += 1
       self.addHandle(pos, tidx)
-      
    def addHandle(self, pos, idx=None):
       hi = HandleItem(self)
       if idx:
@@ -106,8 +100,7 @@
    def getPos1(self):
       if self.fromPort:
          return self.fromPort.scenePos()
-   def setBeginPos(self, pos1):
-      self.updateLineStukken()
+   def setBeginPos(self, pos1): self.updateLineStukken()
    def setEndPos(self, endpos):
       self.pos2 = endpos
       self.updateLineStukken()
@@ -116,8 +109,7 @@
          for via in self.vias:
             via.setVisible(value)
       return super(Connection, self).itemChange(change, value)
-   def shape(self):
-      return self.myshape
+   def shape(self): return self.myshape
    def updateLineStukken(self):
       """
          This algorithm determines the optimal routing of all signals.
@@ -164,16 +156,13 @@
       self.connection = None
       d = 10.0
       if direction == 'input':
-         path = buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)])
+         self.setPath(buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)]))
       else:
-         path = buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)])
-      self.setPath(path)
+         self.setPath(buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)]))
       self.direction = direction
       self.block = block
       self.setCursor(QCursor(Qt.CrossCursor))
-      pen = QPen(Qt.blue, 2)
-      pen.setCapStyle(Qt.RoundCap)
-      self.setPen(pen)
+      self.setPen(QPen(Qt.blue, 2, cap=Qt.RoundCap))
       self.textItem = QGraphicsTextItem(self)
       self.name = name
       self.posCallbacks = []
@@ -183,10 +172,7 @@
       self.textItem.setPlainText(name)
       rect = self.textItem.boundingRect()
       lw, lh = rect.width(), rect.height()
-      if self.direction == 'input':
-         lx = 3
-      else:
-         lx = -3 - lw
+      lx = 3 if self.direction == 'input' else -3 - lw
       self.textItem.setPos(lx, -lh / 2)
    name = property(getName, setName)
    def itemChange(self, change, value):
@@ -211,8 +197,7 @@
       super(HandleItem, self).__init__(QRectF(-0.5*dx,-0.5*dx,dx,dx), parent)
       self.posChangeCallbacks = []
       self.setBrush(QBrush(Qt.white))
-      self.setFlag(self.ItemSendsScenePositionChanges, True)
-      self.setFlag(self.ItemIsMovable, True)
+      self.setFlags(self.ItemSendsScenePositionChanges | self.ItemIsMovable)
       self.setVisible(False)
       self.setCursor(QCursor(Qt.SizeFDiagCursor))
    def mouseMoveEvent(self, event):
@@ -488,12 +473,17 @@
                connection.addHandle(QPointF(x, y))
             self.addItem(connection)
    def findPort(self, blockname, portname):
-      items = self.items()
-      blocks = [item for item in items if type(item) is BlockItem]
-      for block in [b for b in blocks if b.name == blockname]:
+      block = self.findBlock(blockname)
+      if block:
          for port in block.inputs + block.outputs:
             if port.name == portname:
                return port
+   def findBlock(self, blockname):
+      items = self.items()
+      blocks = [item for item in items if type(item) is BlockItem]
+      for block in blocks:
+         if block.name == blockname:
+            return block
    def addNewBlock(self, pos, name):
       blocknames = [item.name for item in self.items() if type(item) is BlockItem]
       b1 = BlockItem(uniqify(name, blocknames))