annotate applications/lab/diagrameditor.py @ 47:c4ec95a588ea

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