annotate applications/lab/diagrameditor.py @ 53:6dd7d6a1737c

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