annotate applications/lab/diagrameditor.py @ 57:143ab220aa27

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