annotate python/apps/diagrameditor.py @ 64:4a27c28c7d0f

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