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

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