annotate python/other/diagramitems.py @ 303:be7f60545368

Final fixups
author Windel Bouwman
date Fri, 06 Dec 2013 12:37:48 +0100
parents 534b94b40aa8
children b77f3290ac79
rev   line source
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
1 """
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
2 Contains all blocks that can be used to build models.
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
3 """
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
4
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
5 from PyQt4.QtGui import *
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
6 from PyQt4.QtCore import *
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
7
91
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
8 def uniqify(name, names):
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
9 newname, i = name, 1
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
10 while newname in names: newname, i = name + str(i), i + 1
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
11 return newname
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
12
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
13 def enum(**enums):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
14 return type('Enum', (), enums)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
15
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
16 Position = enum(TOP=0, TOP_RIGHT=1, RIGHT=2, BOTTOM_RIGHT=3, BOTTOM=4, BOTTOM_LEFT=5, LEFT=6, TOP_LEFT=7)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
17
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
18 def buildPath(pts):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
19 path = QPainterPath(pts[0])
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
20 for pt in pts[1:]: path.lineTo(pt)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
21 return path
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
22
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
23 def equalSpace(n, l, offset=15):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
24 if n == 1:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
25 return [l / 2]
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
26 elif n > 1:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
27 return [offset + (l - offset*2)/(n - 1)*i for i in range(n)]
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
28 return []
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
29
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
30 class Connection(QGraphicsPathItem):
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
31 """ A connection between blocks """
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
32 def __init__(self, fromPort=None, toPort=None):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
33 super(Connection, self).__init__()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
34 self.pos2 = self.fromPort = self.toPort = None
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
35 self.setFlags(self.ItemIsSelectable | self.ItemClipsToShape)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
36 pen = QPen(Qt.blue, 2, cap=Qt.RoundCap)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
37 self.setPen(pen)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
38 self.arrowhead = QGraphicsPathItem(self)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
39 self.arrowhead.setPath(buildPath([QPointF(0.0, 0.0), QPointF(-6.0, 10.0), QPointF(6.0, 10.0), QPointF(0.0, 0.0)]))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
40 self.arrowhead.setPen(pen)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
41 self.arrowhead.setBrush(QBrush(pen.color()))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
42 self.vias = []
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
43 self.setFromPort(fromPort)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
44 self.setToPort(toPort)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
45 def getDict(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
46 d = {}
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
47 d['fromBlock'] = self.fromPort.block.name
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
48 d['fromPort'] = self.fromPort.name
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
49 d['toBlock'] = self.toPort.block.name
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
50 d['toPort'] = self.toPort.name
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
51 return d
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
52 Dict = property(getDict)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
53 def myDelete(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
54 scene = self.scene()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
55 if scene:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
56 self.setFromPort(None)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
57 self.setToPort(None)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
58 scene.removeItem(self)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
59 def setFromPort(self, fromPort):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
60 if self.fromPort:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
61 self.fromPort.posCallbacks.remove(self.setBeginPos)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
62 self.fromPort.connection = None
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
63 self.fromPort = fromPort
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
64 if self.fromPort:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
65 self.fromPort.connection = self
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
66 self.updateLineStukken()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
67 self.fromPort.posCallbacks.append(self.setBeginPos)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
68 def setToPort(self, toPort):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
69 if self.toPort:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
70 self.toPort.posCallbacks.remove(self.setEndPos)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
71 self.toPort.connection = None
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
72 self.toPort = toPort
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
73 if self.toPort:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
74 self.setEndPos(toPort.scenePos())
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
75 self.toPort.connection = self
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
76 self.toPort.posCallbacks.append(self.setEndPos)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
77 def getPos1(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
78 if self.fromPort:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
79 return self.fromPort.scenePos()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
80 def setBeginPos(self, pos1): self.updateLineStukken()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
81 def setEndPos(self, endpos):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
82 self.pos2 = endpos
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
83 self.updateLineStukken()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
84 def itemChange(self, change, value):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
85 if change == self.ItemSelectedHasChanged:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
86 for via in self.vias:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
87 via.setVisible(value)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
88 return super(Connection, self).itemChange(change, value)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
89 def shape(self): return self.myshape
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
90 def updateLineStukken(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
91 """
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
92 This algorithm determines the optimal routing of all signals.
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
93 TODO: implement nice automatic line router
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
94 """
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
95 pos1 = self.getPos1()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
96 pos2 = self.pos2
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
97 if pos1 is None or pos2 is None:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
98 return
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
99 scene = self.scene()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
100 vias = [pos1 + QPointF(20, 0)] + self.vias + [pos2 + QPointF(-20, 0)]
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
101 if scene:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
102 litem = QGraphicsLineItem()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
103 litem.setFlags(self.ItemIsSelectable)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
104 scene.addItem(litem)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
105 for p1, p2 in zip(vias[:-1], vias[1:]):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
106 line = QLineF(p1, p2)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
107 litem.setLine(line)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
108 citems = scene.collidingItems(litem)
89
4b1892054744 Cleanup
windel
parents: 88
diff changeset
109 citems = [i for i in citems if type(i) is Block]
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
110 scene.removeItem(litem)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
111 pts = [pos1] + vias + [pos2]
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
112 self.arrowhead.setPos(pos2)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
113 self.arrowhead.setRotation(90)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
114 p = buildPath(pts)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
115 self.setPath(p)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
116 """ Create a shape outline using the path stroker """
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
117 s = super(Connection, self).shape()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
118 pps = QPainterPathStroker()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
119 pps.setWidth(10)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
120 self.myshape = pps.createStroke(s).simplified()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
121
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
122 class PortItem(QGraphicsPathItem):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
123 """ Represents a port to a subsystem """
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
124 def __init__(self, name, block):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
125 super(PortItem, self).__init__(block)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
126 self.textItem = QGraphicsTextItem(self)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
127 self.connection = None
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
128 self.block = block
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
129 self.setCursor(QCursor(Qt.CrossCursor))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
130 self.setPen(QPen(Qt.blue, 2, cap=Qt.RoundCap))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
131 self.name = name
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
132 self.posCallbacks = []
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
133 self.setFlag(self.ItemSendsScenePositionChanges, True)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
134 def getName(self): return self.textItem.toPlainText()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
135 def setName(self, name):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
136 self.textItem.setPlainText(name)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
137 rect = self.textItem.boundingRect()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
138 lw, lh = rect.width(), rect.height()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
139 lx = 3 if type(self) is InputPort else -3 - lw
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
140 self.textItem.setPos(lx, -lh / 2)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
141 name = property(getName, setName)
91
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
142 def getDict(self):
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
143 return {'name': self.name}
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
144 Dict = property(getDict)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
145 def itemChange(self, change, value):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
146 if change == self.ItemScenePositionHasChanged:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
147 for cb in self.posCallbacks: cb(value)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
148 return value
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
149 return super(PortItem, self).itemChange(change, value)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
150
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
151 class OutputPort(PortItem):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
152 def __init__(self, name, block, d=10.0):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
153 super(OutputPort, self).__init__(name, block)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
154 self.setPath(buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)]))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
155 def mousePressEvent(self, event):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
156 self.scene().startConnection(self)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
157
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
158 class InputPort(PortItem):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
159 def __init__(self, name, block, d=10.0):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
160 super(InputPort, self).__init__(name, block)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
161 self.setPath(buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)]))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
162
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
163 class Handle(QGraphicsEllipseItem):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
164 """ A handle that can be moved by the mouse """
89
4b1892054744 Cleanup
windel
parents: 88
diff changeset
165 def __init__(self, dx=10.0, parent=None):
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
166 super(Handle, self).__init__(QRectF(-0.5*dx,-0.5*dx,dx,dx), parent)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
167 self.setBrush(QBrush(Qt.white))
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
168 self.setFlags(self.ItemIsMovable)
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
169 self.setZValue(1)
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
170 self.setVisible(False)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
171 self.setCursor(QCursor(Qt.SizeFDiagCursor))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
172 def mouseMoveEvent(self, event):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
173 """ Move function without moving the other selected elements """
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
174 p = self.mapToParent(event.pos())
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
175 self.setPos(p)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
176
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
177 class ResizeSelectionHandle(Handle):
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
178 def __init__(self, position, block):
93
windel
parents: 92
diff changeset
179 super(ResizeSelectionHandle, self).__init__(dx=12, parent=block)
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
180 self.position = position
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
181 self.block = block
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
182 if position in [Position.TOP_LEFT, Position.BOTTOM_RIGHT]:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
183 self.setCursor(QCursor(Qt.SizeFDiagCursor))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
184 elif position in [Position.TOP_RIGHT, Position.BOTTOM_LEFT]:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
185 self.setCursor(QCursor(Qt.SizeBDiagCursor))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
186 elif position in [Position.TOP, Position.BOTTOM]:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
187 self.setCursor(QCursor(Qt.SizeVerCursor))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
188 elif position in [Position.LEFT, Position.RIGHT]:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
189 self.setCursor(QCursor(Qt.SizeHorCursor))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
190 def mouseMoveEvent(self, event):
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
191 self.block.sizerMoveEvent(self, event.scenePos())
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
192
89
4b1892054744 Cleanup
windel
parents: 88
diff changeset
193 class Block(QGraphicsRectItem):
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
194 """ Represents a block in the diagram. """
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
195 def __init__(self, name='Untitled', parent=None):
89
4b1892054744 Cleanup
windel
parents: 88
diff changeset
196 super(Block, self).__init__(parent)
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
197 self.selectionHandles = [ResizeSelectionHandle(i, self) for i in range(8)]
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
198 # Properties of the rectangle:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
199 self.setPen(QPen(Qt.blue, 2))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
200 self.setBrush(QBrush(Qt.lightGray))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
201 self.setFlags(self.ItemIsSelectable | self.ItemIsMovable | self.ItemSendsScenePositionChanges)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
202 self.setCursor(QCursor(Qt.PointingHandCursor))
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
203 self.setAcceptHoverEvents(True)
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
204 self.label = QGraphicsTextItem(name, self)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
205 self.name = name
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
206 # Create corner for resize:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
207 button = QPushButton('+in')
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
208 button.clicked.connect(self.newInputPort)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
209 self.buttonItemAddInput = QGraphicsProxyWidget(self)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
210 self.buttonItemAddInput.setWidget(button)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
211 self.buttonItemAddInput.setVisible(False)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
212 button = QPushButton('+out')
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
213 button.clicked.connect(self.newOutputPort)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
214 self.buttonItemAddOutput = QGraphicsProxyWidget(self)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
215 self.buttonItemAddOutput.setWidget(button)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
216 self.buttonItemAddOutput.setVisible(False)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
217 # Inputs and outputs of the block:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
218 self.inputs = []
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
219 self.outputs = []
91
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
220 self.changeSize(2,2)
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
221 def editParameters(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
222 pd = ParameterDialog(self, self.window())
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
223 pd.exec_()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
224 def newInputPort(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
225 names = [i.name for i in self.inputs + self.outputs]
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
226 self.addInput(InputPort(uniqify('in', names), self))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
227 def newOutputPort(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
228 names = [i.name for i in self.inputs + self.outputs]
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
229 self.addOutput(OutputPort(uniqify('out', names), self))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
230 def setName(self, name): self.label.setPlainText(name)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
231 def getName(self): return self.label.toPlainText()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
232 name = property(getName, setName)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
233 def getDict(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
234 d = {'x': self.scenePos().x(), 'y': self.scenePos().y()}
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
235 rect = self.rect()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
236 d.update({'width': rect.width(), 'height': rect.height()})
91
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
237 d['name'] = self.name
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
238 d['inputs'] = [inp.Dict for inp in self.inputs]
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
239 d['outputs'] = [outp.Dict for outp in self.outputs]
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
240 return d
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
241 def setDict(self, d):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
242 self.name = d['name']
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
243 self.setPos(d['x'], d['y'])
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
244 self.changeSize(d['width'], d['height'])
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
245 for inp in d['inputs']:
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
246 self.addInput(InputPort(inp['name'], self))
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
247 for outp in d['outputs']:
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
248 self.addOutput(OutputPort(outp['name'], self))
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
249 Dict = property(getDict, setDict)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
250 def addInput(self, i):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
251 self.inputs.append(i)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
252 self.updateSize()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
253 def addOutput(self, o):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
254 self.outputs.append(o)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
255 self.updateSize()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
256 def contextMenuEvent(self, event):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
257 menu = QMenu()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
258 pa = menu.addAction('Parameters')
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
259 pa.triggered.connect(self.editParameters)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
260 menu.exec_(event.screenPos())
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
261 def itemChange(self, change, value):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
262 if change == self.ItemSelectedHasChanged:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
263 for child in [self.buttonItemAddInput, self.buttonItemAddOutput]:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
264 child.setVisible(value)
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
265 if value:
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
266 self.repositionAndShowHandles()
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
267 else:
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
268 [h.setVisible(False) for h in self.selectionHandles]
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
269
89
4b1892054744 Cleanup
windel
parents: 88
diff changeset
270 return super(Block, self).itemChange(change, value)
91
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
271 def hoverEnterEvent(self, event):
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
272 if not self.isSelected():
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
273 self.repositionAndShowHandles()
91
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
274 super(Block, self).hoverEnterEvent(event)
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
275 def hoverLeaveEvent(self, event):
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
276 if not self.isSelected():
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
277 [h.setVisible(False) for h in self.selectionHandles]
91
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
278 super(Block, self).hoverLeaveEvent(event)
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
279 def myDelete(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
280 for p in self.inputs + self.outputs:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
281 if p.connection: p.connection.myDelete()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
282 self.scene().removeItem(self)
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
283 def repositionAndShowHandles(self):
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
284 r = self.rect()
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
285 self.selectionHandles[Position.TOP_LEFT].setPos(r.topLeft())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
286 self.selectionHandles[Position.TOP].setPos(r.center().x(), r.top())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
287 self.selectionHandles[Position.TOP_RIGHT].setPos(r.topRight())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
288 self.selectionHandles[Position.RIGHT].setPos(r.right(), r.center().y())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
289 self.selectionHandles[Position.BOTTOM_RIGHT].setPos(r.bottomRight())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
290 self.selectionHandles[Position.BOTTOM].setPos(r.center().x(), r.bottom())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
291 self.selectionHandles[Position.BOTTOM_LEFT].setPos(r.bottomLeft())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
292 self.selectionHandles[Position.LEFT].setPos(r.left(), r.center().y())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
293 for h in self.selectionHandles:
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
294 h.setVisible(True)
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
295 def sizerMoveEvent(self, handle, pos):
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
296 r = self.rect().translated(self.pos())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
297 if handle.position == Position.TOP_LEFT: r.setTopLeft(pos)
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
298 elif handle.position == Position.TOP: r.setTop(pos.y())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
299 elif handle.position == Position.TOP_RIGHT: r.setTopRight(pos)
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
300 elif handle.position == Position.RIGHT: r.setRight(pos.x())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
301 elif handle.position == Position.BOTTOM_RIGHT: r.setBottomRight(pos)
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
302 elif handle.position == Position.BOTTOM: r.setBottom(pos.y())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
303 elif handle.position == Position.BOTTOM_LEFT: r.setBottomLeft(pos)
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
304 elif handle.position == Position.LEFT: r.setLeft(pos.x())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
305 else:
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
306 print('invalid position')
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
307 self.setCenterAndSize(r.center(), r.size())
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
308 self.repositionAndShowHandles()
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
309 def updateSize(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
310 rect = self.rect()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
311 h, w = rect.height(), rect.width()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
312 self.buttonItemAddInput.setPos(0, h + 4)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
313 self.buttonItemAddOutput.setPos(w+10, h+4)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
314 for inp, y in zip(self.inputs, equalSpace(len(self.inputs), h)):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
315 inp.setPos(0.0, y)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
316 for outp, y in zip(self.outputs, equalSpace(len(self.outputs), h)):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
317 outp.setPos(w, y)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
318 def setCenterAndSize(self, center, size):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
319 self.changeSize(size.width(), size.height())
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
320 p = QPointF(size.width(), size.height())
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
321 self.setPos(center - p / 2)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
322 def changeSize(self, w, h):
99
windel
parents: 95
diff changeset
323 minw = 150
windel
parents: 95
diff changeset
324 minh = 50
windel
parents: 95
diff changeset
325 h = minh if h < minh else h
windel
parents: 95
diff changeset
326 w = minw if w < minw else w
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
327 self.setRect(0.0, 0.0, w, h)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
328 rect = self.label.boundingRect()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
329 self.label.setPos((w - rect.width()) / 2, (h - rect.height()) / 2)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
330 self.updateSize()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
331
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
332 class CodeBlock(Block):
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
333 def __init__(self, name='Untitled', parent=None):
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
334 super(CodeBlock, self).__init__(name, parent)
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
335 self.code = ''
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
336 def setDict(self, d):
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
337 super(CodeBlock, self).setDict(d)
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
338 self.code = d['code']
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
339 def getDict(self):
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
340 d = super(CodeBlock, self).getDict()
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
341 d['code'] = self.code
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
342 return d
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
343 def gencode(self):
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
344 c = ['def {0}():'.format(self.name)]
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
345 if self.code:
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
346 c += indent(self.code.split('\n'))
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
347 else:
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
348 c += indent(['pass'])
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
349 return c
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
350
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
351 class DiagramBlock(Block):
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
352 def __init__(self, name='Untitled', parent=None):
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
353 super(DiagramBlock, self).__init__(name, parent)
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
354 self.subModel = DiagramScene()
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
355 self.subModel.containingBlock = self
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
356 def setDict(self, d):
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
357 self.subModel.Dict = d['submodel']
93
windel
parents: 92
diff changeset
358 def mouseDoubleClickEvent(self, event):
windel
parents: 92
diff changeset
359 # descent into child diagram
windel
parents: 92
diff changeset
360 #self.editParameters()
windel
parents: 92
diff changeset
361 print('descent')
windel
parents: 92
diff changeset
362 scene = self.scene()
windel
parents: 92
diff changeset
363 if scene:
windel
parents: 92
diff changeset
364 for view in scene.views():
windel
parents: 92
diff changeset
365 view.diagram = self.subModel
windel
parents: 92
diff changeset
366 view.zoomAll()
90
499183b99c71 Fixed handles to block
windel
parents: 89
diff changeset
367
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
368 class DiagramScene(QGraphicsScene):
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
369 """ A diagram scene consisting of blocks and connections """
93
windel
parents: 92
diff changeset
370 structureChanged = pyqtSignal()
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
371 def __init__(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
372 super(DiagramScene, self).__init__()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
373 self.startedConnection = None
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
374
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
375 blocks = property(lambda sel: [i for i in sel.items() if isinstance(i, Block)])
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
376 connections = property(lambda sel: [i for i in sel.items() if type(i) is Connection])
93
windel
parents: 92
diff changeset
377 def addItem(self, item):
windel
parents: 92
diff changeset
378 super(DiagramScene, self).addItem(item)
windel
parents: 92
diff changeset
379 if isinstance(item, Block):
windel
parents: 92
diff changeset
380 self.structureChanged.emit()
windel
parents: 92
diff changeset
381 def removeItem(self, item):
windel
parents: 92
diff changeset
382 super(DiagramScene, self).removeItem(item)
windel
parents: 92
diff changeset
383 if isinstance(item, Block):
windel
parents: 92
diff changeset
384 self.structureChanged.emit()
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
385 def setDict(self, d):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
386 for block in d['blocks']:
89
4b1892054744 Cleanup
windel
parents: 88
diff changeset
387 b = Block()
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
388 self.addItem(b)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
389 b.Dict = block
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
390 for con in d['connections']:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
391 fromPort = self.findPort(con['fromBlock'], con['fromPort'])
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
392 toPort = self.findPort(con['toBlock'], con['toPort'])
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
393 self.addItem(Connection(fromPort, toPort))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
394 def getDict(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
395 return {'blocks': [b.Dict for b in self.blocks], 'connections': [c.Dict for c in self.connections]}
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
396 Dict = property(getDict, setDict)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
397 def gencode(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
398 c = []
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
399 for b in self.blocks:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
400 c += b.gencode()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
401 for b in self.blocks:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
402 c.append('{0}()'.format(b.name))
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
403 return c
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
404 def findPort(self, blockname, portname):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
405 block = self.findBlock(blockname)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
406 if block:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
407 for port in block.inputs + block.outputs:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
408 if port.name == portname: return port
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
409 def findBlock(self, blockname):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
410 for block in self.blocks:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
411 if block.name == blockname: return block
91
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
412 def uniqify(self, name):
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
413 blocknames = [item.name for item in self.blocks]
91
7ad4c66dd092 Fixed save function
windel
parents: 90
diff changeset
414 return uniqify(name, blocknames)
88
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
415 def mouseMoveEvent(self, event):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
416 if self.startedConnection:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
417 pos = event.scenePos()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
418 self.startedConnection.setEndPos(pos)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
419 super(DiagramScene, self).mouseMoveEvent(event)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
420 def mouseReleaseEvent(self, event):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
421 if self.startedConnection:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
422 for item in self.items(event.scenePos()):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
423 if type(item) is InputPort and item.connection == None:
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
424 self.startedConnection.setToPort(item)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
425 self.startedConnection = None
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
426 return
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
427 self.startedConnection.myDelete()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
428 self.startedConnection = None
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
429 super(DiagramScene, self).mouseReleaseEvent(event)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
430 def startConnection(self, port):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
431 self.startedConnection = Connection(port, None)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
432 pos = port.scenePos()
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
433 self.startedConnection.setEndPos(pos)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
434 self.addItem(self.startedConnection)
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
435 def deleteItems(self):
f3fe557be5ed Split off of items to reduce file size
windel
parents:
diff changeset
436 for item in list(self.selectedItems()): item.myDelete()
89
4b1892054744 Cleanup
windel
parents: 88
diff changeset
437