44
|
1 #!/usr/bin/python
|
|
2
|
|
3 from PyQt4.QtGui import *
|
|
4 from PyQt4.QtCore import *
|
|
5 import sys
|
47
|
6 import xml.dom.minidom as md
|
44
|
7
|
53
|
8 """
|
|
9 Author: Windel Bouwman
|
|
10 Year: 2012
|
|
11 Description: This script implements a diagram editor.
|
|
12 run with python 3.x as:
|
|
13 $ python [thisfile.py]
|
44
|
14 """
|
|
15
|
49
|
16 class Connection(QGraphicsPathItem):
|
53
|
17 """ Implementation of a connection between blocks """
|
44
|
18 def __init__(self, fromPort, toPort):
|
49
|
19 super(Connection, self).__init__()
|
44
|
20 self.pos1 = None
|
|
21 self.pos2 = None
|
53
|
22 self.fromPort = None
|
|
23 self.toPort = None
|
51
|
24 #self.setFlags(self.ItemIsSelectable | self.ItemIsMovable)
|
|
25 self.setFlags(self.ItemIsSelectable)
|
53
|
26 pen = QPen(Qt.blue, 2)
|
46
|
27 pen.setCapStyle(Qt.RoundCap)
|
49
|
28 self.setPen(pen)
|
51
|
29 arrowPath = QPainterPath(QPointF(0.0, 0.0))
|
|
30 arrowPath.lineTo(-6.0, 10.0)
|
|
31 arrowPath.lineTo(6.0, 10.0)
|
|
32 arrowPath.lineTo(0.0, 0.0)
|
|
33 self.arrowhead = QGraphicsPathItem(arrowPath, self)
|
|
34 self.arrowhead.setPen(pen)
|
|
35 self.arrowhead.setBrush(QBrush(pen.color()))
|
44
|
36 self.setFromPort(fromPort)
|
46
|
37 self.setToPort(toPort)
|
53
|
38 #def shape(self):
|
|
39 # return self.path()
|
44
|
40 def setFromPort(self, fromPort):
|
53
|
41 if self.fromPort:
|
|
42 self.fromPort.posCallbacks.remove(self.setBeginPos)
|
44
|
43 self.fromPort = fromPort
|
|
44 if self.fromPort:
|
46
|
45 self.setBeginPos(fromPort.scenePos())
|
44
|
46 self.fromPort.posCallbacks.append(self.setBeginPos)
|
|
47 def setToPort(self, toPort):
|
53
|
48 if self.toPort:
|
|
49 self.toPort.posCallbacks.remove(self.setEndPos)
|
44
|
50 self.toPort = toPort
|
|
51 if self.toPort:
|
46
|
52 self.setEndPos(toPort.scenePos())
|
44
|
53 self.toPort.posCallbacks.append(self.setEndPos)
|
53
|
54 def releasePorts(self):
|
|
55 self.setFromPort(None)
|
|
56 self.setToPort(None)
|
44
|
57 def setBeginPos(self, pos1):
|
|
58 self.pos1 = pos1
|
46
|
59 self.updateLineStukken()
|
44
|
60 def setEndPos(self, endpos):
|
|
61 self.pos2 = endpos
|
46
|
62 self.updateLineStukken()
|
44
|
63 def updateLineStukken(self):
|
|
64 """
|
|
65 This algorithm determines the optimal routing of all signals.
|
|
66 TODO: implement nice automatic line router
|
|
67 """
|
46
|
68 if self.pos1 is None or self.pos2 is None:
|
|
69 return
|
50
|
70 # TODO: do not get the scene here?
|
44
|
71 ds = editor.diagramScene
|
51
|
72 self.arrowhead.setPos(self.pos2)
|
44
|
73
|
46
|
74 # TODO: create pieces of lines.
|
|
75
|
|
76 # Determine the begin and end positions:
|
|
77 p1 = self.pos1
|
|
78 p4 = self.pos2
|
|
79 x1, y1 = self.pos1.x(), self.pos1.y()
|
|
80 x4, y4 = self.pos2.x(), self.pos2.y()
|
|
81
|
|
82 def stripHits(hits):
|
|
83 """ Helper function that removes object hits """
|
|
84 hits = [hit for hit in hits if type(hit) is BlockItem]
|
49
|
85 if self in hits:
|
|
86 hits.remove(self)
|
46
|
87 if self.fromPort in hits:
|
|
88 hits.remove(self.fromPort)
|
|
89 if self.toPort in hits:
|
|
90 hits.remove(self.toPort)
|
|
91 return hits
|
|
92
|
|
93 def viaPoints(pA, pB):
|
|
94 # Construct intermediate points:
|
|
95
|
|
96 pAB1 = QPointF(pA.x(), pB.y())
|
|
97 pAB2 = QPointF(pB.x(), pA.y())
|
|
98 path1 = QPainterPath(pA)
|
|
99 path1.lineTo(pAB1)
|
|
100 path2 = QPainterPath(pA)
|
|
101 path2.lineTo(pAB2)
|
|
102 if len(stripHits(ds.items(path1))) > len(stripHits(ds.items(path2))):
|
|
103 pAB = pAB2
|
|
104 else:
|
|
105 pAB = pAB1
|
|
106 return [pAB]
|
|
107
|
|
108 # Determine left or right:
|
51
|
109 dx = QPointF(20, 0)
|
46
|
110 if len(stripHits(ds.items(p1 + dx))) > len(stripHits(ds.items(p1 - dx))):
|
|
111 p2 = p1 - dx
|
44
|
112 else:
|
46
|
113 p2 = p1 + dx
|
|
114
|
|
115 if len(stripHits(ds.items(p4 + dx))) > len(stripHits(ds.items(p4 - dx))):
|
|
116 p3 = p4 - dx
|
51
|
117 self.arrowhead.setRotation(90)
|
46
|
118 else:
|
|
119 p3 = p4 + dx
|
51
|
120 self.arrowhead.setRotation(-90)
|
46
|
121
|
|
122 path = QPainterPath(p1)
|
|
123 path.lineTo(p2)
|
|
124 # Now move from p2 to p3 without hitting blocks:
|
|
125 pts = viaPoints(p2, p3)
|
|
126 for pt in pts:
|
|
127 path.lineTo(pt)
|
|
128 path.lineTo(p3)
|
|
129 path.lineTo(p4)
|
|
130
|
|
131 hits = stripHits(ds.items(path))
|
49
|
132 self.setPath(path)
|
44
|
133
|
|
134 def delete(self):
|
49
|
135 editor.diagramScene.removeItem(self)
|
44
|
136 # Remove position update callbacks:
|
|
137
|
|
138 class ParameterDialog(QDialog):
|
49
|
139 def __init__(self, block, parent = None):
|
44
|
140 super(ParameterDialog, self).__init__(parent)
|
49
|
141 self.block = block
|
44
|
142 self.button = QPushButton('Ok', self)
|
49
|
143 l = QGridLayout(self)
|
|
144 l.addWidget(QLabel('Name:', self), 0, 0)
|
51
|
145 self.nameEdit = QLineEdit(self.block.name)
|
49
|
146 l.addWidget(self.nameEdit, 0, 1)
|
|
147 l.addWidget(self.button, 1, 0)
|
44
|
148 self.button.clicked.connect(self.OK)
|
|
149 def OK(self):
|
50
|
150 self.block.setName(self.nameEdit.text())
|
|
151 self.close()
|
|
152 # TODO: merge dialogs?
|
|
153
|
|
154 class AddPortDialog(QDialog):
|
|
155 def __init__(self, block, parent = None):
|
|
156 super(AddPortDialog, self).__init__(parent)
|
|
157 self.block = block
|
|
158 l = QGridLayout(self)
|
|
159 l.addWidget(QLabel('Name:', self), 0, 0)
|
|
160 self.nameEdit = QLineEdit('bla')
|
|
161 l.addWidget(self.nameEdit, 0, 1)
|
|
162 self.button = QPushButton('Ok', self)
|
|
163 l.addWidget(self.button, 1, 0)
|
|
164 self.button.clicked.connect(self.OK)
|
|
165
|
|
166 def OK(self):
|
|
167 name = self.nameEdit.text()
|
53
|
168 self.block.addOutput(PortItem(name, self.block, 'output'))
|
44
|
169 self.close()
|
|
170
|
53
|
171 class PortItem(QGraphicsPathItem):
|
44
|
172 """ Represents a port to a subsystem """
|
53
|
173 def __init__(self, name, block, direction):
|
|
174 super(PortItem, self).__init__(block)
|
|
175 #QRectF(-6,-6,12.0,12.0)
|
|
176 path = QPainterPath()
|
|
177 if direction == 'input':
|
|
178 d = 5.0
|
|
179 path.moveTo(-d, -d)
|
|
180 path.lineTo(0.0, 0.0)
|
|
181 path.lineTo(-d, d)
|
|
182 else:
|
|
183 d = 5.0
|
|
184 path.moveTo(0.0, -d)
|
|
185 path.lineTo(d, 0.0)
|
|
186 path.lineTo(0.0, d)
|
|
187 self.setPath(path)
|
|
188 self.direction = direction
|
50
|
189 self.block = block
|
46
|
190 self.setCursor(QCursor(Qt.CrossCursor))
|
53
|
191 #self.setBrush(QBrush(Qt.red))
|
|
192 self.setPen(QPen(Qt.blue, 2))
|
44
|
193 self.name = name
|
50
|
194 self.textItem = QGraphicsTextItem(name, self)
|
53
|
195 self.setName(name)
|
44
|
196 self.posCallbacks = []
|
|
197 self.setFlag(self.ItemSendsScenePositionChanges, True)
|
53
|
198 def setName(self, name):
|
|
199 self.name = name
|
|
200 self.textItem.setPlainText(name)
|
|
201 rect = self.textItem.boundingRect()
|
|
202 lw, lh = rect.width(), rect.height()
|
|
203 if self.direction == 'input':
|
|
204 lx = 3
|
|
205 else:
|
|
206 lx = -3 - lw
|
|
207 self.textItem.setPos(lx, -lh / 2) # TODO
|
44
|
208 def itemChange(self, change, value):
|
|
209 if change == self.ItemScenePositionHasChanged:
|
|
210 for cb in self.posCallbacks:
|
|
211 cb(value)
|
|
212 return value
|
|
213 return super(PortItem, self).itemChange(change, value)
|
|
214 def mousePressEvent(self, event):
|
|
215 editor.startConnection(self)
|
|
216
|
53
|
217 class OutputPort(PortItem):
|
|
218 # TODO: create a subclass OR make a member porttype
|
|
219 pass
|
|
220
|
44
|
221 # Block part:
|
|
222 class HandleItem(QGraphicsEllipseItem):
|
|
223 """ A handle that can be moved by the mouse """
|
|
224 def __init__(self, parent=None):
|
|
225 super(HandleItem, self).__init__(QRectF(-4.0,-4.0,8.0,8.0), parent)
|
|
226 self.posChangeCallbacks = []
|
46
|
227 self.setBrush(QBrush(Qt.white))
|
|
228 self.setFlags(self.ItemIsMovable | self.ItemSendsScenePositionChanges)
|
|
229 self.setCursor(QCursor(Qt.SizeFDiagCursor))
|
44
|
230
|
|
231 def itemChange(self, change, value):
|
|
232 if change == self.ItemPositionChange:
|
45
|
233 #value = value.toPointF()
|
44
|
234 x, y = value.x(), value.y()
|
|
235 # TODO: make this a signal?
|
|
236 # This cannot be a signal because this is not a QObject
|
|
237 for cb in self.posChangeCallbacks:
|
|
238 res = cb(x, y)
|
|
239 if res:
|
|
240 x, y = res
|
|
241 value = QPointF(x, y)
|
|
242 return value
|
|
243 # Call superclass method:
|
|
244 return super(HandleItem, self).itemChange(change, value)
|
|
245
|
|
246 class BlockItem(QGraphicsRectItem):
|
|
247 """
|
|
248 Represents a block in the diagram
|
|
249 Has an x and y and width and height
|
|
250 width and height can only be adjusted with a tip in the lower right corner.
|
|
251
|
|
252 - in and output ports
|
|
253 - parameters
|
|
254 - description
|
|
255 """
|
|
256 def __init__(self, name='Untitled', parent=None):
|
|
257 super(BlockItem, self).__init__(parent)
|
|
258 # Properties of the rectangle:
|
46
|
259 self.setPen(QPen(Qt.blue, 2))
|
|
260 self.setBrush(QBrush(Qt.lightGray))
|
44
|
261 self.setFlags(self.ItemIsSelectable | self.ItemIsMovable)
|
46
|
262 self.setCursor(QCursor(Qt.PointingHandCursor))
|
44
|
263 self.label = QGraphicsTextItem(name, self)
|
50
|
264 self.name = name
|
44
|
265 # Create corner for resize:
|
|
266 self.sizer = HandleItem(self)
|
|
267 self.sizer.posChangeCallbacks.append(self.changeSize) # Connect the callback
|
|
268 #self.sizer.setVisible(False)
|
|
269 self.sizer.setFlag(self.sizer.ItemIsSelectable, True)
|
|
270
|
|
271 # Inputs and outputs of the block:
|
|
272 self.inputs = []
|
|
273 self.outputs = []
|
|
274 # Update size:
|
50
|
275 self.changeSize(60, 40)
|
44
|
276 def editParameters(self):
|
49
|
277 pd = ParameterDialog(self, self.window())
|
44
|
278 pd.exec_()
|
50
|
279 def addPort(self):
|
|
280 pd = AddPortDialog(self, self.window())
|
|
281 pd.exec_()
|
|
282 def setName(self, name):
|
|
283 self.name = name
|
|
284 self.label.setPlainText(name)
|
48
|
285 def addInput(self, i):
|
|
286 self.inputs.append(i)
|
49
|
287 self.updateSize()
|
48
|
288 def addOutput(self, o):
|
|
289 self.outputs.append(o)
|
49
|
290 self.updateSize()
|
44
|
291
|
|
292 def contextMenuEvent(self, event):
|
|
293 menu = QMenu()
|
46
|
294 da = menu.addAction('Delete')
|
|
295 da.triggered.connect(self.delete)
|
44
|
296 pa = menu.addAction('Parameters')
|
|
297 pa.triggered.connect(self.editParameters)
|
50
|
298 pa = menu.addAction('Add port')
|
|
299 pa.triggered.connect(self.addPort)
|
44
|
300 menu.exec_(event.screenPos())
|
|
301
|
46
|
302 def delete(self):
|
|
303 editor.diagramScene.removeItem(self)
|
|
304 # TODO: remove connections
|
|
305
|
49
|
306 def updateSize(self):
|
|
307 rect = self.rect()
|
|
308 h, w = rect.height(), rect.width()
|
44
|
309 if len(self.inputs) == 1:
|
53
|
310 self.inputs[0].setPos(0.0, h / 2)
|
44
|
311 elif len(self.inputs) > 1:
|
53
|
312 y = 15
|
|
313 dy = (h - 30) / (len(self.inputs) - 1)
|
44
|
314 for inp in self.inputs:
|
53
|
315 inp.setPos(0.0, y)
|
44
|
316 y += dy
|
|
317 if len(self.outputs) == 1:
|
53
|
318 self.outputs[0].setPos(w, h / 2)
|
44
|
319 elif len(self.outputs) > 1:
|
53
|
320 y = 15
|
|
321 dy = (h - 30) / (len(self.outputs) - 1)
|
44
|
322 for outp in self.outputs:
|
53
|
323 outp.setPos(w, y)
|
44
|
324 y += dy
|
49
|
325
|
|
326 def changeSize(self, w, h):
|
|
327 """ Resize block function """
|
|
328 # Limit the block size:
|
|
329 if h < 20:
|
|
330 h = 20
|
|
331 if w < 40:
|
|
332 w = 40
|
|
333 self.setRect(0.0, 0.0, w, h)
|
|
334 # center label:
|
|
335 rect = self.label.boundingRect()
|
|
336 lw, lh = rect.width(), rect.height()
|
|
337 lx = (w - lw) / 2
|
|
338 ly = (h - lh) / 2
|
|
339 self.label.setPos(lx, ly)
|
|
340 # Update port positions:
|
|
341 self.updateSize()
|
44
|
342 return w, h
|
|
343
|
|
344 class EditorGraphicsView(QGraphicsView):
|
|
345 def __init__(self, scene, parent=None):
|
|
346 QGraphicsView.__init__(self, scene, parent)
|
|
347 def dragEnterEvent(self, event):
|
|
348 if event.mimeData().hasFormat('component/name'):
|
|
349 event.accept()
|
|
350 def dragMoveEvent(self, event):
|
|
351 if event.mimeData().hasFormat('component/name'):
|
|
352 event.accept()
|
|
353 def dropEvent(self, event):
|
|
354 if event.mimeData().hasFormat('component/name'):
|
48
|
355 name = bytes(event.mimeData().data('component/name')).decode()
|
44
|
356 b1 = BlockItem(name)
|
|
357 b1.setPos(self.mapToScene(event.pos()))
|
|
358 self.scene().addItem(b1)
|
|
359
|
|
360 class LibraryModel(QStandardItemModel):
|
|
361 def __init__(self, parent=None):
|
|
362 QStandardItemModel.__init__(self, parent)
|
|
363 def mimeTypes(self):
|
|
364 return ['component/name']
|
|
365 def mimeData(self, idxs):
|
|
366 mimedata = QMimeData()
|
|
367 for idx in idxs:
|
|
368 if idx.isValid():
|
45
|
369 txt = self.data(idx, Qt.DisplayRole) # python 3
|
44
|
370 mimedata.setData('component/name', txt)
|
|
371 return mimedata
|
|
372
|
|
373 class DiagramScene(QGraphicsScene):
|
|
374 def __init__(self, parent=None):
|
|
375 super(DiagramScene, self).__init__(parent)
|
|
376 def mouseMoveEvent(self, mouseEvent):
|
|
377 editor.sceneMouseMoveEvent(mouseEvent)
|
|
378 super(DiagramScene, self).mouseMoveEvent(mouseEvent)
|
|
379 def mouseReleaseEvent(self, mouseEvent):
|
|
380 editor.sceneMouseReleaseEvent(mouseEvent)
|
|
381 super(DiagramScene, self).mouseReleaseEvent(mouseEvent)
|
|
382
|
|
383 class DiagramEditor(QWidget):
|
|
384 def __init__(self, parent=None):
|
46
|
385 QWidget.__init__(self, parent)
|
44
|
386 self.setWindowTitle("Diagram editor")
|
|
387
|
|
388 # Widget layout and child widgets:
|
46
|
389 self.horizontalLayout = QHBoxLayout(self)
|
|
390 self.libraryBrowserView = QListView(self)
|
45
|
391 self.libraryBrowserView.setMaximumWidth(160)
|
44
|
392 self.libraryModel = LibraryModel(self)
|
|
393 self.libraryModel.setColumnCount(1)
|
|
394 # Create an icon with an icon:
|
|
395 pixmap = QPixmap(60, 60)
|
|
396 pixmap.fill()
|
|
397 painter = QPainter(pixmap)
|
|
398 painter.fillRect(10, 10, 40, 40, Qt.blue)
|
|
399 painter.setBrush(Qt.red)
|
|
400 painter.drawEllipse(36, 2, 20, 20)
|
|
401 painter.setBrush(Qt.yellow)
|
|
402 painter.drawEllipse(20, 20, 20, 20)
|
|
403 painter.end()
|
|
404
|
52
|
405 # Fill library:
|
44
|
406 self.libItems = []
|
46
|
407 self.libItems.append( QStandardItem(QIcon(pixmap), 'Block') )
|
|
408 self.libItems.append( QStandardItem(QIcon(pixmap), 'Uber Unit') )
|
|
409 self.libItems.append( QStandardItem(QIcon(pixmap), 'Device') )
|
44
|
410 for i in self.libItems:
|
|
411 self.libraryModel.appendRow(i)
|
|
412 self.libraryBrowserView.setModel(self.libraryModel)
|
|
413 self.libraryBrowserView.setViewMode(self.libraryBrowserView.IconMode)
|
|
414 self.libraryBrowserView.setDragDropMode(self.libraryBrowserView.DragOnly)
|
|
415
|
|
416 self.diagramScene = DiagramScene(self)
|
|
417 self.diagramView = EditorGraphicsView(self.diagramScene, self)
|
|
418 self.horizontalLayout.addWidget(self.libraryBrowserView)
|
|
419 self.horizontalLayout.addWidget(self.diagramView)
|
|
420
|
|
421 self.startedConnection = None
|
|
422 fullScreenShortcut = QShortcut(QKeySequence("F11"), self)
|
|
423 fullScreenShortcut.activated.connect(self.toggleFullScreen)
|
46
|
424 saveShortcut = QShortcut(QKeySequence(QKeySequence.Save), self)
|
|
425 saveShortcut.activated.connect(self.save)
|
48
|
426 testShortcut = QShortcut(QKeySequence("F12"), self)
|
|
427 testShortcut.activated.connect(self.test)
|
|
428 zoomShortcut = QShortcut(QKeySequence("F8"), self)
|
|
429 zoomShortcut.activated.connect(self.zoomAll)
|
52
|
430 delShort = QShortcut(QKeySequence.Delete, self)
|
|
431 delShort.activated.connect(self.deleteItem)
|
46
|
432
|
48
|
433 def test(self):
|
|
434 self.diagramView.rotate(11)
|
46
|
435 def save(self):
|
47
|
436 self.saveDiagram('diagram2.usd')
|
46
|
437
|
|
438 def saveDiagram(self, filename):
|
|
439 items = self.diagramScene.items()
|
|
440 blocks = [item for item in items if type(item) is BlockItem]
|
49
|
441 connections = [item for item in items if type(item) is Connection]
|
47
|
442
|
|
443 doc = md.Document()
|
|
444 modelElement = doc.createElement('system')
|
|
445 doc.appendChild(modelElement)
|
|
446 for block in blocks:
|
|
447 blockElement = doc.createElement("block")
|
|
448 x, y = block.scenePos().x(), block.scenePos().y()
|
|
449 rect = block.rect()
|
|
450 w, h = rect.width(), rect.height()
|
50
|
451 blockElement.setAttribute("name", block.name)
|
49
|
452 blockElement.setAttribute("x", str(int(x)))
|
|
453 blockElement.setAttribute("y", str(int(y)))
|
|
454 blockElement.setAttribute("width", str(int(w)))
|
|
455 blockElement.setAttribute("height", str(int(h)))
|
48
|
456 for inp in block.inputs:
|
|
457 portElement = doc.createElement("input")
|
|
458 portElement.setAttribute("name", inp.name)
|
|
459 blockElement.appendChild(portElement)
|
|
460 for outp in block.outputs:
|
|
461 portElement = doc.createElement("output")
|
|
462 portElement.setAttribute("name", outp.name)
|
|
463 blockElement.appendChild(portElement)
|
47
|
464 modelElement.appendChild(blockElement)
|
49
|
465 for connection in connections:
|
|
466 connectionElement = doc.createElement("connection")
|
|
467 fromPort = connection.fromPort.name
|
|
468 toPort = connection.toPort.name
|
50
|
469 fromBlock = connection.fromPort.block.name
|
|
470 toBlock = connection.toPort.block.name
|
49
|
471 connectionElement.setAttribute("fromBlock", fromBlock)
|
|
472 connectionElement.setAttribute("fromPort", fromPort)
|
|
473 connectionElement.setAttribute("toBlock", toBlock)
|
|
474 connectionElement.setAttribute("toPort", toPort)
|
|
475 modelElement.appendChild(connectionElement)
|
46
|
476 with open(filename, 'w') as f:
|
47
|
477 f.write(doc.toprettyxml())
|
46
|
478
|
|
479 def loadDiagram(self, filename):
|
|
480 try:
|
47
|
481 doc = md.parse(filename)
|
46
|
482 except IOError as e:
|
|
483 print('{0} not found'.format(filename))
|
|
484 return
|
47
|
485 sysElements = doc.getElementsByTagName('system')
|
|
486 blockElements = doc.getElementsByTagName('block')
|
|
487 for sysElement in sysElements:
|
|
488 blockElements = sysElement.getElementsByTagName('block')
|
|
489 for blockElement in blockElements:
|
|
490 x = float(blockElement.getAttribute('x'))
|
|
491 y = float(blockElement.getAttribute('y'))
|
|
492 w = float(blockElement.getAttribute('width'))
|
|
493 h = float(blockElement.getAttribute('height'))
|
|
494 name = blockElement.getAttribute('name')
|
|
495 block = BlockItem(name)
|
46
|
496 self.diagramScene.addItem(block)
|
|
497 block.setPos(x, y)
|
|
498 block.sizer.setPos(w, h)
|
48
|
499 # Load ports:
|
49
|
500 portElements = blockElement.getElementsByTagName('input')
|
48
|
501 for portElement in portElements:
|
49
|
502 name = portElement.getAttribute('name')
|
53
|
503 inp = PortItem(name, block, 'input')
|
49
|
504 block.addInput(inp)
|
|
505 portElements = blockElement.getElementsByTagName('output')
|
|
506 for portElement in portElements:
|
|
507 name = portElement.getAttribute('name')
|
53
|
508 outp = PortItem(name, block, 'output')
|
49
|
509 block.addOutput(outp)
|
48
|
510 connectionElements = sysElement.getElementsByTagName('connection')
|
|
511 for connectionElement in connectionElements:
|
50
|
512 fromBlock = connectionElement.getAttribute('fromBlock')
|
|
513 fromPort = connectionElement.getAttribute('fromPort')
|
|
514 toBlock = connectionElement.getAttribute('toBlock')
|
|
515 toPort = connectionElement.getAttribute('toPort')
|
|
516 fromPort = self.findPort(fromBlock, fromPort)
|
|
517 toPort = self.findPort(toBlock, toPort)
|
|
518 connection = Connection(fromPort, toPort)
|
|
519 self.diagramScene.addItem(connection)
|
|
520
|
48
|
521 self.zoomAll()
|
47
|
522
|
50
|
523 def findPort(self, blockname, portname):
|
|
524 items = self.diagramScene.items()
|
|
525 blocks = [item for item in items if type(item) is BlockItem]
|
|
526 for block in [b for b in blocks if b.name == blockname]:
|
|
527 for port in block.inputs + block.outputs:
|
|
528 if port.name == portname:
|
|
529 return port
|
|
530
|
44
|
531 def toggleFullScreen(self):
|
|
532 self.setWindowState(self.windowState() ^ Qt.WindowFullScreen)
|
|
533 def startConnection(self, port):
|
|
534 self.startedConnection = Connection(port, None)
|
50
|
535 self.diagramScene.addItem(self.startedConnection)
|
48
|
536 def zoomAll(self):
|
|
537 """ zoom to fit all items """
|
|
538 rect = self.diagramScene.itemsBoundingRect()
|
|
539 self.diagramView.fitInView(rect, Qt.KeepAspectRatio)
|
52
|
540 def deleteItem(self):
|
|
541 items = self.diagramScene.selectedItems()
|
|
542 for item in items:
|
53
|
543 if type(item) is Connection:
|
|
544 item.releasePorts()
|
|
545 item.delete()
|
|
546 if type(item) is Block:
|
|
547 pass
|
|
548
|
44
|
549 def sceneMouseMoveEvent(self, event):
|
|
550 if self.startedConnection:
|
|
551 pos = event.scenePos()
|
|
552 self.startedConnection.setEndPos(pos)
|
|
553 def sceneMouseReleaseEvent(self, event):
|
|
554 # Clear the actual connection:
|
|
555 if self.startedConnection:
|
46
|
556 items = self.diagramScene.items(event.scenePos())
|
44
|
557 for item in items:
|
|
558 if type(item) is PortItem:
|
|
559 self.startedConnection.setToPort(item)
|
46
|
560 self.startedConnection = None
|
|
561 return
|
|
562 self.startedConnection.delete()
|
|
563 del self.startedConnection
|
44
|
564 self.startedConnection = None
|
|
565
|
|
566 if __name__ == '__main__':
|
45
|
567 if sys.version_info.major != 3:
|
|
568 print('Please use python 3.x')
|
|
569 sys.exit(1)
|
|
570
|
46
|
571 app = QApplication(sys.argv)
|
44
|
572 global editor
|
|
573 editor = DiagramEditor()
|
50
|
574 editor.loadDiagram('diagram2.usd')
|
44
|
575 editor.show()
|
48
|
576 editor.resize(700, 500)
|
50
|
577 editor.zoomAll()
|
44
|
578 app.exec_()
|
|
579
|