annotate python/apps/diagrameditor.py @ 74:f506f6b74697

Cleanup
author windel
date Sun, 11 Nov 2012 10:49:46 +0100
parents 191d70a0ed52
children ba11d99b1d4a
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 *
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
5 import sys, xml
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
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
8 """
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
9 Author: Windel Bouwman
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
10 Year: 2012
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
11 Description: This script implements a diagram editor.
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
12 run with python 3.x as:
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
13 $ python [thisfile.py]
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
14 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
15
73
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
16 def buildPath(pts):
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
17 path = QPainterPath(pts[0])
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
18 for pt in pts[1:]:
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
19 path.lineTo(pt)
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
20 return path
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
21
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
22 def equalSpace(n, l):
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
23 offset = 15
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
24 if n == 1:
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
25 return [l / 2]
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
26 elif n > 1:
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
27 return [offset + (l - offset*2)/(n - 1)*i for i in range(n)]
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
28 return []
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
29
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
30 class Connection(QGraphicsPathItem):
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
31 """ Implementation of a connection between blocks """
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
32 def __init__(self, fromPort, toPort):
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
33 super(Connection, self).__init__()
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
34 self.pos2 = self.fromPort = self.toPort = None
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
35 self.setFlag(self.ItemIsSelectable, True)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
36 self.setFlag(self.ItemClipsToShape, True)
73
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
37 pen = QPen(Qt.blue, 2)
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
38 pen.setCapStyle(Qt.RoundCap)
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
39 self.setPen(pen)
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
40 self.arrowhead = QGraphicsPathItem(self)
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
41 self.arrowhead.setPath(buildPath([QPointF(0.0, 0.0), QPointF(-6.0, 10.0), QPointF(6.0, 10.0), QPointF(0.0, 0.0)]))
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
42 self.arrowhead.setPen(pen)
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
43 self.arrowhead.setBrush(QBrush(pen.color()))
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
44 self.vias = []
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
45 self.setFromPort(fromPort)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
46 self.setToPort(toPort)
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
47 def mouseDoubleClickEvent(self, event):
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
48 pos = event.scenePos()
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
49 pts = [self.getPos1()] + [v.pos() for v in self.vias] + [self.pos2]
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
50 idx = 0
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
51 tidx = 0
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
52 for p1, p2 in zip(pts[0:-1], pts[1:]):
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
53 l1 = QLineF(p1, p2)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
54 l2 = QLineF(p1, pos)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
55 l3 = QLineF(pos, p2)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
56 d = l2.length() + l3.length() - l1.length()
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
57 if d < 5:
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
58 tidx = idx
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
59 idx += 1
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
60 self.addHandle(pos, tidx)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
61
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
62 def addHandle(self, pos, idx=None):
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
63 hi = HandleItem(self)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
64 if idx:
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
65 self.vias.insert(idx, hi)
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
66 else:
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
67 self.vias.append(hi)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
68 def callback(p):
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
69 self.updateLineStukken()
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
70 return p
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
71 hi.posChangeCallbacks.append(callback)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
72 hi.setPos(pos)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
73 self.updateLineStukken()
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
74 def myDelete(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
75 scene = self.scene()
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
76 if scene:
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
77 self.setFromPort(None)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
78 self.setToPort(None)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
79 scene.removeItem(self)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
80 def setFromPort(self, fromPort):
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
81 if self.fromPort:
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
82 self.fromPort.posCallbacks.remove(self.setBeginPos)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
83 self.fromPort.connection = None
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
84 self.fromPort = fromPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
85 if self.fromPort:
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
86 self.fromPort.connection = self
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
87 self.updateLineStukken()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
88 self.fromPort.posCallbacks.append(self.setBeginPos)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
89 def setToPort(self, toPort):
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
90 if self.toPort:
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
91 self.toPort.posCallbacks.remove(self.setEndPos)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
92 self.toPort.connection = None
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
93 self.toPort = toPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
94 if self.toPort:
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
95 self.setEndPos(toPort.scenePos())
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
96 self.toPort.connection = self
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
97 self.toPort.posCallbacks.append(self.setEndPos)
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
98 def getPos1(self):
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
99 if self.fromPort:
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
100 return self.fromPort.scenePos()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
101 def setBeginPos(self, pos1):
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
102 self.updateLineStukken()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
103 def setEndPos(self, endpos):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
104 self.pos2 = endpos
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
105 self.updateLineStukken()
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
106 def itemChange(self, change, value):
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
107 if change == self.ItemSelectedHasChanged:
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
108 for via in self.vias:
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
109 via.setVisible(value)
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
110 return super(Connection, self).itemChange(change, value)
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
111 def shape(self):
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
112 return self.myshape
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
113 def updateLineStukken(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
114 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
115 This algorithm determines the optimal routing of all signals.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
116 TODO: implement nice automatic line router
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
117 """
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
118 pos1 = self.getPos1()
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
119 if pos1 is None or self.pos2 is None:
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
120 return
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
121 pts = [pos1] + [v.pos() for v in self.vias] + [self.pos2]
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
122 self.arrowhead.setPos(self.pos2)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
123 if pts[-1].x() < pts[-2].x():
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
124 self.arrowhead.setRotation(-90)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
125 else:
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
126 self.arrowhead.setRotation(90)
73
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
127 self.setPath(buildPath(pts))
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
128 """ Create a shape outline using the path stroker """
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
129 s = super(Connection, self).shape()
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
130 pps = QPainterPathStroker()
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
131 pps.setWidth(10)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
132 self.myshape = pps.createStroke(s).simplified()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
133
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
134 class ParameterDialog(QDialog):
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
135 def __init__(self, block, parent = None):
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
136 super(ParameterDialog, self).__init__(parent)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
137 self.block = block
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
138 self.button = QPushButton('Ok', self)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
139 l = QGridLayout(self)
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
140 l.addWidget(QLabel('Name:', self), 0, 0)
51
b3a65e154ab2 Added fancy arrow head
windel
parents: 50
diff changeset
141 self.nameEdit = QLineEdit(self.block.name)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
142 l.addWidget(self.nameEdit, 0, 1)
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
143 l.addWidget(QLabel('Code:', self), 1, 0)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
144 self.codeEdit = QTextEdit(self)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
145 self.codeEdit.setPlainText(self.block.code)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
146 l.addWidget(self.codeEdit, 1, 1)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
147 l.addWidget(self.button, 2, 0, 1, 2)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
148 self.button.clicked.connect(self.OK)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
149 def OK(self):
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
150 self.block.setName(self.nameEdit.text())
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
151 self.block.code = self.codeEdit.toPlainText()
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
152 self.close()
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
153
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
154 class PortItem(QGraphicsPathItem):
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
155 """ Represents a port to a subsystem """
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
156 def __init__(self, name, block, direction):
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
157 super(PortItem, self).__init__(block)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
158 self.connection = None
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
159 path = QPainterPath()
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
160 d = 10.0
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
161 if direction == 'input':
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
162 path.moveTo(-d, -d)
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
163 path.lineTo(0.0, 0.0)
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
164 path.lineTo(-d, d)
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
165 else:
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
166 path.moveTo(0.0, -d)
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
167 path.lineTo(d, 0.0)
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
168 path.lineTo(0.0, d)
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
169 self.setPath(path)
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
170 self.direction = direction
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
171 self.block = block
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
172 self.setCursor(QCursor(Qt.CrossCursor))
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
173 pen = QPen(Qt.blue, 2)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
174 pen.setCapStyle(Qt.RoundCap)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
175 self.setPen(pen)
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
176 self.textItem = QGraphicsTextItem(name, self)
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
177 self.setName(name)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
178 self.posCallbacks = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
179 self.setFlag(self.ItemSendsScenePositionChanges, True)
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
180 def setName(self, name):
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
181 self.name = name
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
182 self.textItem.setPlainText(name)
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
183 rect = self.textItem.boundingRect()
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
184 lw, lh = rect.width(), rect.height()
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
185 if self.direction == 'input':
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
186 lx = 3
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
187 else:
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
188 lx = -3 - lw
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
189 self.textItem.setPos(lx, -lh / 2)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
190 def itemChange(self, change, value):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
191 if change == self.ItemScenePositionHasChanged:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
192 for cb in self.posCallbacks:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
193 cb(value)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
194 return value
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
195 return super(PortItem, self).itemChange(change, value)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
196 def mousePressEvent(self, event):
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
197 if self.direction == 'output':
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
198 self.scene().startConnection(self)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
199
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
200 class OutputPort(PortItem):
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
201 # TODO: create a subclass OR make a member porttype
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
202 pass
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
203
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
204 # Block part:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
205 class HandleItem(QGraphicsEllipseItem):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
206 """ A handle that can be moved by the mouse """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
207 def __init__(self, parent=None):
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
208 dx = 13.0
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
209 super(HandleItem, self).__init__(QRectF(-0.5*dx,-0.5*dx,dx,dx), parent)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
210 self.posChangeCallbacks = []
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
211 self.setBrush(QBrush(Qt.white))
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
212 self.setFlag(self.ItemSendsScenePositionChanges, True)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
213 self.setFlag(self.ItemIsMovable, True)
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
214 self.setVisible(False)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
215 self.setCursor(QCursor(Qt.SizeFDiagCursor))
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
216 def mouseMoveEvent(self, event):
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
217 """ Move function without moving the other selected elements """
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
218 p = self.mapToParent(event.pos())
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
219 self.setPos(p)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
220 def itemChange(self, change, value):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
221 if change == self.ItemPositionChange:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
222 for cb in self.posChangeCallbacks:
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
223 res = cb(value)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
224 if res:
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
225 value = res
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
226 return value
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
227 # Call superclass method:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
228 return super(HandleItem, self).itemChange(change, value)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
229
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
230 def uniqify(name, names):
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
231 newname = name
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
232 i = 1
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
233 while newname in names:
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
234 newname = name + str(i)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
235 i += 1
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
236 return newname
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
237
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
238 class BlockItem(QGraphicsRectItem):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
239 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
240 Represents a block in the diagram
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
241 Has an x and y and width and height
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
242 width and height can only be adjusted with a tip in the lower right corner.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
243
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
244 - in and output ports
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
245 - parameters
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
246 - description
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
247 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
248 def __init__(self, name='Untitled', parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
249 super(BlockItem, self).__init__(parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
250 # Properties of the rectangle:
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
251 self.setPen(QPen(Qt.blue, 2))
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
252 self.setBrush(QBrush(Qt.lightGray))
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
253 self.setFlags(self.ItemIsSelectable | self.ItemIsMovable)
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
254 self.setFlag(self.ItemSendsScenePositionChanges, True)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
255 self.setCursor(QCursor(Qt.PointingHandCursor))
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
256 self.label = QGraphicsTextItem(name, self)
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
257 self.name = name
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
258 self.code = ''
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
259 # Create corner for resize:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
260 self.sizer = HandleItem(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
261 self.sizer.posChangeCallbacks.append(self.changeSize) # Connect the callback
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
262 button = QPushButton('+in')
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
263 button.clicked.connect(self.newInputPort)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
264 self.buttonItemAddInput = QGraphicsProxyWidget(self)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
265 self.buttonItemAddInput.setWidget(button)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
266 self.buttonItemAddInput.setVisible(False)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
267 button = QPushButton('+out')
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
268 button.clicked.connect(self.newOutputPort)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
269 self.buttonItemAddOutput = QGraphicsProxyWidget(self)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
270 self.buttonItemAddOutput.setWidget(button)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
271 self.buttonItemAddOutput.setVisible(False)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
272 # Inputs and outputs of the block:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
273 self.inputs = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
274 self.outputs = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
275 # Update size:
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
276 self.sizer.setPos(QPointF(60, 40)) # This is a better resize function
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
277 def editParameters(self):
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
278 pd = ParameterDialog(self, self.window())
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
279 pd.exec_()
59
f6ea630c4057 Cleanup
windel
parents: 58
diff changeset
280 def mouseDoubleClickEvent(self, event):
f6ea630c4057 Cleanup
windel
parents: 58
diff changeset
281 self.editParameters()
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
282 def newInputPort(self):
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
283 names = [i.name for i in self.inputs + self.outputs]
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
284 self.addInput(PortItem(uniqify('in', names), self, 'input'))
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
285 def newOutputPort(self):
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
286 names = [i.name for i in self.inputs + self.outputs]
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
287 self.addOutput(PortItem(uniqify('out', names), self, 'output'))
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
288 def setName(self, name):
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
289 self.name = name
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
290 self.label.setPlainText(name)
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
291 def addInput(self, i):
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
292 self.inputs.append(i)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
293 self.updateSize()
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
294 def addOutput(self, o):
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
295 self.outputs.append(o)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
296 self.updateSize()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
297 def contextMenuEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
298 menu = QMenu()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
299 pa = menu.addAction('Parameters')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
300 pa.triggered.connect(self.editParameters)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
301 menu.exec_(event.screenPos())
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
302 def itemChange(self, change, value):
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
303 if change == self.ItemSelectedHasChanged:
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
304 for child in [self.sizer, self.buttonItemAddInput, self.buttonItemAddOutput]:
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
305 child.setVisible(value)
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
306 return super(BlockItem, self).itemChange(change, value)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
307 def myDelete(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
308 scene = self.scene()
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
309 if scene:
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
310 for p in self.inputs + self.outputs:
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
311 if p.connection:
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
312 p.connection.myDelete()
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
313 scene.removeItem(self)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
314 def updateSize(self):
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
315 rect = self.rect()
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
316 h, w = rect.height(), rect.width()
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
317 self.buttonItemAddInput.setPos(0, h + 4)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
318 self.buttonItemAddOutput.setPos(w+10, h+4)
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
319 for inp, y in zip(self.inputs, equalSpace(len(self.inputs), h)):
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
320 inp.setPos(0.0, y)
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
321 for outp, y in zip(self.outputs, equalSpace(len(self.outputs), h)):
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
322 outp.setPos(w, y)
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
323 def changeSize(self, p):
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
324 h = 20 if p.y() < 20 else p.y()
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
325 w = 40 if p.x() < 40 else p.x()
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
326 self.setRect(0.0, 0.0, w, h)
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
327 rect = self.label.boundingRect()
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
328 self.label.setPos((w - rect.width()) / 2, (h - rect.height()) / 2)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
329 self.updateSize()
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
330 return QPointF(w, h)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
331
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
332 class EditorGraphicsView(QGraphicsView):
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
333 def __init__(self, parent=None):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
334 QGraphicsView.__init__(self, parent)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
335 scene = DiagramScene(self)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
336 self.diagramScene = scene
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
337 self.setScene(scene)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
338 self.setDragMode(QGraphicsView.RubberBandDrag)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
339 testShortcut = QShortcut(QKeySequence("F12"), self)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
340 testShortcut.activated.connect(self.test)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
341 delShort = QShortcut(QKeySequence.Delete, self)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
342 delShort.activated.connect(scene.deleteItems)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
343 self.loadDiagram = scene.loadDiagram
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
344 def save(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
345 self.diagramScene.saveDiagram('diagram2.usd')
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
346 def load(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
347 filename = QFileDialog.getOpenFileName(self)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
348 self.diagramScene.loadDiagram(filename)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
349 def zoomAll(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
350 """ zoom to fit all items """
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
351 rect = self.diagramScene.itemsBoundingRect()
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
352 self.fitInView(rect, Qt.KeepAspectRatio)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
353 def test(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
354 self.rotate(30)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
355 self.zoomAll()
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
356 def wheelEvent(self, event):
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
357 pos = event.pos()
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
358 posbefore = self.mapToScene(pos)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
359 degrees = event.delta() / 8.0
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
360 sx = (100.0 + degrees) / 100.0
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
361 self.scale(sx, sx)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
362 event.accept()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
363 def dragEnterEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
364 if event.mimeData().hasFormat('component/name'):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
365 event.accept()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
366 def dragMoveEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
367 if event.mimeData().hasFormat('component/name'):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
368 event.accept()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
369 def dropEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
370 if event.mimeData().hasFormat('component/name'):
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
371 name = bytes(event.mimeData().data('component/name')).decode()
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
372 pos = self.mapToScene(event.pos())
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
373 self.scene().addNewBlock(pos, name)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
374
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
375 class LibraryModel(QStandardItemModel):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
376 def __init__(self, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
377 QStandardItemModel.__init__(self, parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
378 def mimeTypes(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
379 return ['component/name']
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
380 def mimeData(self, idxs):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
381 mimedata = QMimeData()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
382 for idx in idxs:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
383 if idx.isValid():
45
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
384 txt = self.data(idx, Qt.DisplayRole) # python 3
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
385 mimedata.setData('component/name', txt)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
386 return mimedata
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
387
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
388 class DiagramScene(QGraphicsScene):
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
389 """ Save and load and deletion of item"""
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
390 def __init__(self, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
391 super(DiagramScene, self).__init__(parent)
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
392 self.startedConnection = None
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
393 def saveDiagram(self, filename):
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
394 items = self.items()
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
395 blocks = [item for item in items if type(item) is BlockItem]
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
396 connections = [item for item in items if type(item) is Connection]
47
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
397 doc = md.Document()
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
398 modelElement = doc.createElement('system')
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
399 doc.appendChild(modelElement)
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
400 for block in blocks:
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
401 blockElement = doc.createElement("block")
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
402 x, y = block.scenePos().x(), block.scenePos().y()
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
403 rect = block.rect()
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
404 w, h = rect.width(), rect.height()
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
405 blockElement.setAttribute("name", block.name)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
406 blockElement.setAttribute("x", str(int(x)))
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
407 blockElement.setAttribute("y", str(int(y)))
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
408 blockElement.setAttribute("width", str(int(w)))
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
409 blockElement.setAttribute("height", str(int(h)))
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
410 codeNode = doc.createCDATASection(block.code)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
411 codeElement = doc.createElement('code')
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
412 codeElement.appendChild(codeNode)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
413 blockElement.appendChild(codeElement)
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
414 for inp in block.inputs:
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
415 portElement = doc.createElement("input")
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
416 portElement.setAttribute("name", inp.name)
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
417 blockElement.appendChild(portElement)
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
418 for outp in block.outputs:
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
419 portElement = doc.createElement("output")
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
420 portElement.setAttribute("name", outp.name)
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
421 blockElement.appendChild(portElement)
47
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
422 modelElement.appendChild(blockElement)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
423 for connection in connections:
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
424 connectionElement = doc.createElement("connection")
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
425 fromPort = connection.fromPort.name
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
426 toPort = connection.toPort.name
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
427 fromBlock = connection.fromPort.block.name
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
428 toBlock = connection.toPort.block.name
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
429 connectionElement.setAttribute("fromBlock", fromBlock)
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
430 connectionElement.setAttribute("fromPort", fromPort)
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
431 connectionElement.setAttribute("toBlock", toBlock)
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
432 connectionElement.setAttribute("toPort", toPort)
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
433 for via in connection.vias:
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
434 viaElement = doc.createElement('via')
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
435 viaElement.setAttribute('x', str(int(via.x())))
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
436 viaElement.setAttribute('y', str(int(via.y())))
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
437 connectionElement.appendChild(viaElement)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
438 modelElement.appendChild(connectionElement)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
439 with open(filename, 'w') as f:
47
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
440 f.write(doc.toprettyxml())
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
441
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
442 def loadDiagram(self, filename):
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
443 try:
47
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
444 doc = md.parse(filename)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
445 except IOError as e:
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
446 print('{0} not found'.format(filename))
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
447 return
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
448 except xml.parsers.expat.ExpatError as e:
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
449 print('{0}'.format(e))
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
450 return
47
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
451 sysElements = doc.getElementsByTagName('system')
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
452 blockElements = doc.getElementsByTagName('block')
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
453 for sysElement in sysElements:
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
454 blockElements = sysElement.getElementsByTagName('block')
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
455 for blockElement in blockElements:
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
456 x = float(blockElement.getAttribute('x'))
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
457 y = float(blockElement.getAttribute('y'))
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
458 w = float(blockElement.getAttribute('width'))
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
459 h = float(blockElement.getAttribute('height'))
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
460 name = blockElement.getAttribute('name')
c4ec95a588ea Fixed save to xml
windel
parents: 46
diff changeset
461 block = BlockItem(name)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
462 self.addItem(block)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
463 block.setPos(x, y)
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
464 block.sizer.setPos(w, h)
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
465 codeElements = blockElement.getElementsByTagName('code')
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
466 if codeElements:
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
467 cn = codeElements[0].childNodes
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
468 cdatas = [cd for cd in cn if type(cd) is md.CDATASection]
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
469 if len(cdatas) > 0:
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
470 block.code = cdatas[0].data
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
471 # Load ports:
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
472 portElements = blockElement.getElementsByTagName('input')
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
473 for portElement in portElements:
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
474 name = portElement.getAttribute('name')
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
475 inp = PortItem(name, block, 'input')
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
476 block.addInput(inp)
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
477 portElements = blockElement.getElementsByTagName('output')
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
478 for portElement in portElements:
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
479 name = portElement.getAttribute('name')
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
480 outp = PortItem(name, block, 'output')
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
481 block.addOutput(outp)
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
482 connectionElements = sysElement.getElementsByTagName('connection')
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
483 for connectionElement in connectionElements:
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
484 fromBlock = connectionElement.getAttribute('fromBlock')
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
485 fromPort = connectionElement.getAttribute('fromPort')
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
486 toBlock = connectionElement.getAttribute('toBlock')
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
487 toPort = connectionElement.getAttribute('toPort')
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
488 viaElements = connectionElement.getElementsByTagName('via')
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
489 fromPort = self.findPort(fromBlock, fromPort)
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
490 toPort = self.findPort(toBlock, toPort)
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
491 connection = Connection(fromPort, toPort)
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
492 for viaElement in viaElements:
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
493 x = int(viaElement.getAttribute('x'))
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
494 y = int(viaElement.getAttribute('y'))
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
495 connection.addHandle(QPointF(x, y))
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
496 self.addItem(connection)
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
497 def findPort(self, blockname, portname):
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
498 items = self.items()
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
499 blocks = [item for item in items if type(item) is BlockItem]
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
500 for block in [b for b in blocks if b.name == blockname]:
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
501 for port in block.inputs + block.outputs:
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
502 if port.name == portname:
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
503 return port
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
504 def addNewBlock(self, pos, name):
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
505 blocknames = [item.name for item in self.items() if type(item) is BlockItem]
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
506 b1 = BlockItem(uniqify(name, blocknames))
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
507 b1.setPos(pos)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
508 self.addItem(b1)
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
509 def mouseMoveEvent(self, event):
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
510 if self.startedConnection:
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
511 pos = event.scenePos()
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
512 self.startedConnection.setEndPos(pos)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
513 super(DiagramScene, self).mouseMoveEvent(event)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
514 def mouseReleaseEvent(self, event):
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
515 if self.startedConnection:
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
516 for item in self.items(event.scenePos()):
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
517 if type(item) is PortItem:
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
518 if item.direction == 'input' and item.connection == None:
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
519 self.startedConnection.setToPort(item)
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
520 self.startedConnection = None
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
521 return
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
522 self.startedConnection.myDelete()
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
523 self.startedConnection = None
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
524 super(DiagramScene, self).mouseReleaseEvent(event)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
525 def startConnection(self, port):
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
526 self.startedConnection = Connection(port, None)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
527 pos = port.scenePos()
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
528 self.startedConnection.setEndPos(pos)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
529 self.addItem(self.startedConnection)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
530 def deleteItems(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
531 for item in list(self.selectedItems()):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
532 item.myDelete()
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
533
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
534 class LibraryWidget(QListView):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
535 def __init__(self):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
536 super(LibraryWidget, self).__init__(None)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
537 self.libraryModel = LibraryModel(self)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
538 self.libraryModel.setColumnCount(1)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
539 # Create an icon with an icon:
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
540 pixmap = QPixmap(60, 60)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
541 pixmap.fill()
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
542 painter = QPainter(pixmap)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
543 painter.fillRect(10, 10, 40, 40, Qt.blue)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
544 painter.setBrush(Qt.yellow)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
545 painter.drawEllipse(20, 20, 20, 20)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
546 painter.end()
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
547
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
548 # Fill library:
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
549 self.libItems = []
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
550 self.libItems.append( QStandardItem(QIcon(pixmap), 'Block') )
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
551 self.libItems.append( QStandardItem(QIcon(pixmap), 'Uber Unit') )
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
552 self.libItems.append( QStandardItem(QIcon(pixmap), 'Device') )
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
553 for i in self.libItems:
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
554 self.libraryModel.appendRow(i)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
555 self.setModel(self.libraryModel)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
556 self.setViewMode(self.IconMode)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
557 self.setDragDropMode(self.DragOnly)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
558
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
559 class Main(QMainWindow):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
560 def __init__(self):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
561 super(Main, self).__init__(None)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
562 self.editor = EditorGraphicsView()
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
563 self.setCentralWidget(self.editor)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
564 self.setWindowTitle("Diagram editor")
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
565 toolbar = self.addToolBar('Tools')
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
566 def act(name, shortcut, callback):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
567 a = QAction(name, self)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
568 a.setShortcuts(shortcut)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
569 a.triggered.connect(callback)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
570 toolbar.addAction(a)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
571 act('Save', QKeySequence.Save, self.editor.save)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
572 act('Load', QKeySequence.Open, self.editor.load)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
573 act('Full screen', QKeySequence("F11"), self.toggleFullScreen)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
574 act('Fit in view', QKeySequence("F8"), self.editor.zoomAll)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
575 self.library = LibraryWidget()
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
576 libraryDock = QDockWidget('Library', self)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
577 libraryDock.setWidget(self.library)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
578 self.addDockWidget(Qt.LeftDockWidgetArea, libraryDock)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
579 self.editor.loadDiagram('diagram2.usd')
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
580 def toggleFullScreen(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
581 self.setWindowState(self.windowState() ^ Qt.WindowFullScreen)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
582 self.editor.zoomAll()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
583
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
584 if __name__ == '__main__':
45
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
585 if sys.version_info.major != 3:
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
586 print('Please use python 3.x')
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
587 sys.exit(1)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
588 app = QApplication(sys.argv)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
589 main = Main()
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
590 main.show()
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
591 main.resize(700, 500)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
592 main.editor.zoomAll()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
593 app.exec_()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
594