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