annotate applications/lab/diagrameditor.py @ 46:7964065400b7

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