annotate python/other/diagramitems.py @ 390:b77f3290ac79

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