annotate python/other/diagramitems.py @ 410:6aa9743ed362 tip

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