comparison python/apps/diagramitems.py @ 88:f3fe557be5ed

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