annotate applications/lab/diagrameditor.py @ 51:b3a65e154ab2

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