annotate python/apps/diagrameditor.py @ 81:49161141d765

Added icons and improved stability
author windel
date Fri, 16 Nov 2012 12:10:34 +0100
parents d1925eb3bbd5
children a47f6fec5435
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 *
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
5 import sys, json, base64
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
6
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
7 """
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
8 Author: Windel Bouwman
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
9 Year: 2012
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
10 Description: This script implements a diagram editor.
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
11 run with python 3.x as:
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
12 $ python [thisfile.py]
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
13 """
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
14 saveicon = b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAGGklEQVR42s2XeWxURRjAv7d3L9ay\nlJBCbAx/SIIpCWilhNLSFsrR0qUgyCmnnKVFa/xDjNbUKGIUSrlaLhEREWlZaQoC24VymBivoGJB\n/MuEtEpjoNt2z/H7Zt+8fdPdGk2MOu23MzvvzXy/+Y6ZWQX+46L8bwC2vLTlZte9e6MMRiMYjQYY\nPWYsZGXngM1iBkUxoERej1RYayJmYGrF1Dbj/31+P1xtuwQ3vvmaP6M/h8Nx69Xq6kclgDWrn2W3\nbt+G5KQkSE9Ph/E5eTB1xixIQID+yIraEMoVJYLGay4KGNS2ty8Aza4muHqpVZvClmCDHbW1igSw\neuUq1tPTA2EWRohkmJCbD9OLS+Fu14M4ZlP6M8lwSvSdEUNTwNXYCFc87iiAzQY7d9XJACuXr2Dd\n3d1gNpvBarXCxMmFUDzLCUk2S3zfKTrlqmIlYgqtTfWDXh80NZ6CNvdFnQUSYNfuXTLAimXLmV5B\nTn4hlDrLICUxHoAC+tHSTNz1jIcCw48HvX60AAK0RgGsaIE9e/fIAMuWPiMB5BZMAWfZHBiUaI1r\nAaZ+Mi32VKVqX5h/ZzwGzpxuRAtckFywt36fDLB08RIZoHAqlDhnQ0eXdwDlosFAP1APRGVoahK0\nYBBedp+XLNCwv0EGWLxwUT+AIphZ6ozNghhlEQCtrZqeqRC+QAjOftqEFogCkAX2HzwgAyxasFAC\nmFQwFaaVEIAJ5P2K6T5ln2uu0LnEHwzBZ2ea4Epr1AUU5AcPH5IBFsx/Wg5CBCgqdoLVbBxg5Xov\nDGyFQCgMF5tPI8B5CeDwkfdkgPlPzYsBmDLTCffu98b3f78OzS4s+g7Vg5Ot4G5xwdV+AEeOvi8D\nzJszV5p7IgLkT3equ9zAAPo4EMpFAIpnnrMuuKYDsCDA0WMfyABzy+bIAPlTIG9aKfzZecV0y5dc\nIdosMvzyORdc90RjwGKxwLHjH8oAZc7ZEoBj6DAYOeoxPJiMYKIDymTkbYPBoB5CkRpABByDcDjM\nJUQSDEIwFIIwyk83b0DXrx0SwPETH8kAs2eVxrhXKsrfPLnZwNOZEeDEyY9lgNLiEvYXpv1HCgGc\nPPWJDFAys/jfA8AD71RTowxQPH3GgAAK+t2IQv7X4oC+q5cSKiIORCyEyP9qLfr1AI2u0zLAjKJp\ncQHWblgPGRkZ0Q7G4uwFch8d6xXlm3jw0qEUCARgOF5yRDGZTOBqPiMDFBUUxsxLh8aa9evAbrfz\nVYuVRvVG2uKZ6COrvFj1Ao92fiL2eME+yK4pM6EFms+2yAAFeZNjAawWWF9eDqmpqVBT/ZqWZn1+\nH7y5dat2LxRmp1qY/pUtL/NgY9ju6e3lVz29Bc5dOC8D5OVMigNghfLKCg7wXOVm3kd53e31Qv3+\nBq6clAUx5/v7/I2a17mvOXBfHyTgLUgDQNdc8LTKADnZE2IAiHRz1fPgGDIEKjeW8z7aZPwBP9TW\n1WlK9QAC4p1tb2sAPp8PbLgYunFb8HgPBILgabssA0zIejIGgHy4uaoKARywacPGCABO7sOr9rs7\ntsdEOwnBkNRu34EAJnQBAz++TwB8V0WoIAZl2/VrMkDWuMdjAJISE2EjuoCCsBKjOuJrVISTbt32\nlqZMrJyiXQDt2VnHldGYYDCA7rSBGS0awvEmowmufH5dBhibOeYu+nSYHiABAdZhGg4igIoKbkqa\nnLKjpqZGWrneErTihn31XGEEIAgJOIZixqim5hdffRkFSElOUfDX0LgR6cNbMECGaBbAyF21dg2v\n+UBdkFGu6wH0z0jhgfp6vlIeuPiMrmGiUNr+0P5jcp/P5xUWoDoN/bT8kYczqjH4rCILFi5ZAvaH\n7JKPRZtMLtJPCE1Oz44cOsSh1X0KrBaLHiD47fffjcTmL/ojLhFlNCovtJjNDmRi2dnjk57IyhqM\nsWBBf1pxQjMJzmgke6rKmULJwVgQm36E8nd2dNxv9Xh+//nOHR9Ivxj4WjGRQu24+mb88psegNqE\nSQlrJH9lZmYacnNzjWlpaQpmBBdKLXXHU9QNiIkdEa0T9nq94c7OzpDb7Q63t7crTD6WFRXEj0J3\nveAfetNmUUsM6bsAAAAASUVORK5CYII=\n'
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
15
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
16 loadicon = b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACsElEQVR42u2Xf0hTURTHv++ZczlD\n6SeUREVRiRT9gFWM1rbAwr+iYAathPqjMosIQQqJQAhLLDI0CMwflRa1vyQst0oNLKxhCGWR9UfB\nwFCRaqC2vc6d7+1tr/7YnuwW5IEv95y79875vHvP7t4E/GUTpgGmAaL8vaRzpDlx3iuRXpEOkz5N\nFWDhrHR8bL+LtGXLKUqRJcoKyeUk1Q8Fges3gLIKeGlm+1QBHA4rPJ7WxG4eHqLlWoJRcvfJaPHY\nV1LPbwB5Dnja7if+BFUVQEdX7OqETdL48vD+C/DODze5e9iUCmAjgDt6FzJ+m/gG5GyD9OEz1lPY\nqwLQFrQ1JR+Amc0JPO0O941XBbAQQB0nABcBvNACbCGAWk4ABwngpRbATACXEksU7rsJGlnzUSZB\nkEdAbW9lXgnJtx0jAJ8WYCMBnE8MIESFx7+rRQRRLiaPESANmL2EAF6jgaLSCIA9Bx53sZokJpEY\nm4zFzP85BgTn0mUZUL9y7CPpz8CCvAJdA0DrE6D6NnxsLp9UaV2KVfW7Jy8QWcGUyUJhP0piFJA0\nmw6iAn19EAgAmdkICpRw5EEdsjabgXQDYo9gRZENVxVeIJO8Gjqs7y2wxoJ+IXUGxkcHkDrTqC+R\nXrt5D3AVoYU9yJs+L1bnruQLUFJO+34NpxlAc2MVCly7+ALkHQAedSKfAZwq3o/KK2X8ikvUT4ss\nkPyDWMwAtm5ai47uZn4Ag8PAAgvoxxzzGUCGMQ1DI89gMBr4ADymtwHHIXSSa1UOIt/zBqwz5/IB\nqG4Bjl9EDblFCkDN5ZM4csLJB+DoBaDWDTp3cVUBcDntaLx1hg/AzlKgvQc7yH2oAGSbjOg/WwjT\nvMzkFvdT65U34UdgDCtYGP1avoFUSMpK8gKw7q8n9bLgn/pj8n8C/ALihrxpNKi7hQAAAABJRU5E\nrkJggg==\n'
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
17
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
18 newicon = b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADSUlEQVR42r2XO0xaURjHLzYmdXRi\nYXBwdDScJl3djQNLw0TSAR88FR9JU6P4QKMCaTqQdGgXsQQMQXFwcGXhCCG+4iNEo6mDMZL45kK/\n76THUO+9Qi/efskJcm4O3+/+v8f51Al/LBaLmTs6Or43gAkq7eHhoXh2dmZva2v7WusZHf9jZGTE\nPz4+blfjuFwuC6IoCsViUbi8vCwDhM1oNH75V4AAANjUAJRKJeYc183NjXB1dVU+Pj7+CIp+qxlg\neHg44PV6VQHg2z8+PjKAu7s7oampSQAVxL29PUtnZ+eP/waA6/r6WtDr9UyJ09NTcXd319LV1aUI\n8QQwNDQUmJiYqBsA5BcMBgPLC4Q4OTkRt7e3LSaTSRbiCWBwcDAwOTmpOgfQOVSBcHFxIbS0tLB9\nDgH5IGYyGYvZbJZAvAoAOsL4IwTEngGxH9fp2MJnhUJBTKfT5t7e3rAsgMfjYQB4oB4V+OJAuI+A\naGtra6nz8/P3o6OjJQnAwMBAYGpqSjUAh8Aw8JLE3OBqIMTGxgbNZrPE5/MVFQFUe6+A4M5xoWMe\nIg4ASksB+vv761aAG3eKMFwBBFhfX6ebm5sEyl0eAHOgjqvgRSBUA3KAghHouFIAt9vNFNACgIci\nmUxSKEcyNjYmBXC5XK8Wgudvz8OAAJgDigD1lGE155UAsiFwOp2vUgVKzisBZJMQAdR2wlqco62s\nrNBcLkfgzpECOByOuhXg5cc733NLJBIMQLYP2O12BqB0uJrjyk8li8fjdGtri4AfeQAMAUpXayJW\ne2MlANlWbLPZGAB2LPagCoQapZaXl+nOzg6ZmZmRB8CBhANoYTB5U5iQyOzsrDwAjmR4hWpl0WiU\nwpxI5ubmpAAwKLAQ4HWqlUUiEbq/v0/m5+flATAEWiqwtLREDw4OyMLCgjyA1iEIh8P08PCQ+P1+\nKUBPTw8D0DIEi4uL9OjoiASDQSlAd3c3A7i/v9cUAKsgFApJAaxWa2B6etp2e3v71yGlen++X60v\n4EwAAOlUKvUO+oEUoLW19UNfX9/nxsbGhsoOxy+Wl75X2+drdXX1J/zX9AkmI+lU3N7e/hYSxAAT\n0Rs+FdX69rXsA1y5ubn5Vz6fL1Q++w30VO4/0/9IewAAAABJRU5ErkJggg==\n'
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
19
73
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
20 def buildPath(pts):
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
21 path = QPainterPath(pts[0])
76
1084b433c278 Line reduction
windel
parents: 75
diff changeset
22 for pt in pts[1:]: path.lineTo(pt)
73
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
23 return path
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
24
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
25 def equalSpace(n, l, offset=15):
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
26 if n == 1:
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
27 return [l / 2]
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
28 elif n > 1:
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
29 return [offset + (l - offset*2)/(n - 1)*i for i in range(n)]
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
30 return []
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
31
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
32 def uniqify(name, names):
76
1084b433c278 Line reduction
windel
parents: 75
diff changeset
33 newname, i = name, 1
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
34 while newname in names: newname, i = name + str(i), i + 1
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
35 return newname
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
36
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
37 class Connection(QGraphicsPathItem):
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
38 """ Implementation of a connection between blocks """
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
39 def __init__(self, fromPort=None, toPort=None):
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
40 super(Connection, self).__init__()
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
41 self.pos2 = self.fromPort = self.toPort = None
76
1084b433c278 Line reduction
windel
parents: 75
diff changeset
42 self.setFlags(self.ItemIsSelectable | self.ItemClipsToShape)
1084b433c278 Line reduction
windel
parents: 75
diff changeset
43 pen = QPen(Qt.blue, 2, cap=Qt.RoundCap)
73
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
44 self.setPen(pen)
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
45 self.arrowhead = QGraphicsPathItem(self)
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
46 self.arrowhead.setPath(buildPath([QPointF(0.0, 0.0), QPointF(-6.0, 10.0), QPointF(6.0, 10.0), QPointF(0.0, 0.0)]))
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
47 self.arrowhead.setPen(pen)
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
48 self.arrowhead.setBrush(QBrush(pen.color()))
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
49 self.vias = []
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
50 self.setFromPort(fromPort)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
51 self.setToPort(toPort)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
52 def getDict(self):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
53 d = {}
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
54 d['fromBlock'] = self.fromPort.block.name
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
55 d['fromPort'] = self.fromPort.name
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
56 d['toBlock'] = self.toPort.block.name
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
57 d['toPort'] = self.toPort.name
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
58 return d
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
59 def setDict(self, d):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
60 print(d)
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
61 Dict = property(getDict, setDict)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
62 def myDelete(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
63 scene = self.scene()
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
64 if scene:
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
65 self.setFromPort(None)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
66 self.setToPort(None)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
67 scene.removeItem(self)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
68 def setFromPort(self, fromPort):
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
69 if self.fromPort:
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
70 self.fromPort.posCallbacks.remove(self.setBeginPos)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
71 self.fromPort.connection = None
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
72 self.fromPort = fromPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
73 if self.fromPort:
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
74 self.fromPort.connection = self
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
75 self.updateLineStukken()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
76 self.fromPort.posCallbacks.append(self.setBeginPos)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
77 def setToPort(self, toPort):
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
78 if self.toPort:
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
79 self.toPort.posCallbacks.remove(self.setEndPos)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
80 self.toPort.connection = None
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
81 self.toPort = toPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
82 if self.toPort:
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
83 self.setEndPos(toPort.scenePos())
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
84 self.toPort.connection = self
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
85 self.toPort.posCallbacks.append(self.setEndPos)
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
86 def getPos1(self):
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
87 if self.fromPort:
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
88 return self.fromPort.scenePos()
76
1084b433c278 Line reduction
windel
parents: 75
diff changeset
89 def setBeginPos(self, pos1): self.updateLineStukken()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
90 def setEndPos(self, endpos):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
91 self.pos2 = endpos
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
92 self.updateLineStukken()
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
93 def itemChange(self, change, value):
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
94 if change == self.ItemSelectedHasChanged:
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
95 for via in self.vias:
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
96 via.setVisible(value)
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
97 return super(Connection, self).itemChange(change, value)
76
1084b433c278 Line reduction
windel
parents: 75
diff changeset
98 def shape(self): return self.myshape
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
99 def updateLineStukken(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
100 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
101 This algorithm determines the optimal routing of all signals.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
102 TODO: implement nice automatic line router
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
103 """
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
104 pos1 = self.getPos1()
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
105 if pos1 is None or self.pos2 is None:
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
106 return
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
107 pts = [pos1] + [v.pos() for v in self.vias] + [self.pos2]
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
108 self.arrowhead.setPos(self.pos2)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
109 if pts[-1].x() < pts[-2].x():
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
110 self.arrowhead.setRotation(-90)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
111 else:
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
112 self.arrowhead.setRotation(90)
73
191d70a0ed52 Removed arrowhead class
windel
parents: 72
diff changeset
113 self.setPath(buildPath(pts))
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
114 """ Create a shape outline using the path stroker """
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
115 s = super(Connection, self).shape()
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
116 pps = QPainterPathStroker()
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
117 pps.setWidth(10)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
118 self.myshape = pps.createStroke(s).simplified()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
119
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
120 class ParameterDialog(QDialog):
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
121 def __init__(self, block, parent = None):
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
122 super(ParameterDialog, self).__init__(parent)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
123 self.block = block
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
124 self.button = QPushButton('Ok', self)
51
b3a65e154ab2 Added fancy arrow head
windel
parents: 50
diff changeset
125 self.nameEdit = QLineEdit(self.block.name)
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
126 self.codeEdit = QTextEdit(self)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
127 self.codeEdit.setPlainText(self.block.code)
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
128 l = QFormLayout(self)
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
129 l.addRow('Name:', self.nameEdit)
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
130 l.addRow('Code:', self.codeEdit)
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
131 l.addWidget(self.button)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
132 self.button.clicked.connect(self.OK)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
133 def OK(self):
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
134 self.block.name = self.nameEdit.text()
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
135 self.block.code = self.codeEdit.toPlainText()
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
136 self.close()
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
137
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
138 class PortItem(QGraphicsPathItem):
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
139 """ Represents a port to a subsystem """
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
140 def __init__(self, name, block):
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
141 super(PortItem, self).__init__(block)
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
142 self.textItem = QGraphicsTextItem(self)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
143 self.connection = None
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
144 self.block = block
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
145 self.setCursor(QCursor(Qt.CrossCursor))
76
1084b433c278 Line reduction
windel
parents: 75
diff changeset
146 self.setPen(QPen(Qt.blue, 2, cap=Qt.RoundCap))
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
147 self.name = name
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
148 self.posCallbacks = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
149 self.setFlag(self.ItemSendsScenePositionChanges, True)
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
150 def getName(self): return self.textItem.toPlainText()
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
151 def setName(self, name):
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
152 self.textItem.setPlainText(name)
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
153 rect = self.textItem.boundingRect()
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
154 lw, lh = rect.width(), rect.height()
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
155 lx = 3 if type(self) is InputPort else -3 - lw
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
156 self.textItem.setPos(lx, -lh / 2)
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
157 name = property(getName, setName)
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
158 def getDict(self): return {'name': self.name}
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
159 Dict = property(getDict)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
160 def itemChange(self, change, value):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
161 if change == self.ItemScenePositionHasChanged:
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
162 for cb in self.posCallbacks: cb(value)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
163 return value
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
164 return super(PortItem, self).itemChange(change, value)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
165
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
166 class OutputPort(PortItem):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
167 def __init__(self, name, block, d=10.0):
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
168 super(OutputPort, self).__init__(name, block)
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
169 self.setPath(buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)]))
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
170 def mousePressEvent(self, event):
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
171 self.scene().startConnection(self)
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
172
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
173 class InputPort(PortItem):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
174 def __init__(self, name, block, d=10.0):
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
175 super(InputPort, self).__init__(name, block)
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
176 self.setPath(buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)]))
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
177
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
178 class HandleItem(QGraphicsEllipseItem):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
179 """ A handle that can be moved by the mouse """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
180 def __init__(self, parent=None):
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
181 dx = 13.0
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
182 super(HandleItem, self).__init__(QRectF(-0.5*dx,-0.5*dx,dx,dx), parent)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
183 self.posChangeCallbacks = []
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
184 self.setBrush(QBrush(Qt.white))
76
1084b433c278 Line reduction
windel
parents: 75
diff changeset
185 self.setFlags(self.ItemSendsScenePositionChanges | self.ItemIsMovable)
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
186 self.setVisible(False)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
187 self.setCursor(QCursor(Qt.SizeFDiagCursor))
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
188 def mouseMoveEvent(self, event):
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
189 """ Move function without moving the other selected elements """
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
190 p = self.mapToParent(event.pos())
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
191 self.setPos(p)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
192 def itemChange(self, change, value):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
193 if change == self.ItemPositionChange:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
194 for cb in self.posChangeCallbacks:
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
195 res = cb(value)
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
196 if res: value = res
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
197 return value
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
198 return super(HandleItem, self).itemChange(change, value)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
199
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
200 class BlockItem(QGraphicsRectItem):
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
201 """ Represents a block in the diagram """
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
202 def __init__(self, name='Untitled', parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
203 super(BlockItem, self).__init__(parent)
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
204 self.subModel = DiagramScene()
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
205 self.subModel.containingBlock = self
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
206 # Properties of the rectangle:
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
207 self.setPen(QPen(Qt.blue, 2))
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
208 self.setBrush(QBrush(Qt.lightGray))
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
209 self.setFlags(self.ItemIsSelectable | self.ItemIsMovable | self.ItemSendsScenePositionChanges)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
210 self.setCursor(QCursor(Qt.PointingHandCursor))
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
211 self.label = QGraphicsTextItem(name, self)
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
212 self.name = name
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
213 self.code = ''
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
214 # Create corner for resize:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
215 self.sizer = HandleItem(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
216 self.sizer.posChangeCallbacks.append(self.changeSize) # Connect the callback
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
217 button = QPushButton('+in')
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
218 button.clicked.connect(self.newInputPort)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
219 self.buttonItemAddInput = QGraphicsProxyWidget(self)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
220 self.buttonItemAddInput.setWidget(button)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
221 self.buttonItemAddInput.setVisible(False)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
222 button = QPushButton('+out')
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
223 button.clicked.connect(self.newOutputPort)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
224 self.buttonItemAddOutput = QGraphicsProxyWidget(self)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
225 self.buttonItemAddOutput.setWidget(button)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
226 self.buttonItemAddOutput.setVisible(False)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
227 # Inputs and outputs of the block:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
228 self.inputs = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
229 self.outputs = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
230 # Update size:
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
231 self.sizer.setPos(QPointF(60, 40)) # This is a better resize function
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
232 def editParameters(self):
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
233 pd = ParameterDialog(self, self.window())
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
234 pd.exec_()
59
f6ea630c4057 Cleanup
windel
parents: 58
diff changeset
235 def mouseDoubleClickEvent(self, event):
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
236 #self.editParameters()
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
237 scene = self.scene()
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
238 if scene:
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
239 for view in scene.views():
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
240 view.diagram = self.subModel
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
241 view.zoomAll()
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
242 def newInputPort(self):
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
243 names = [i.name for i in self.inputs + self.outputs]
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
244 self.addInput(InputPort(uniqify('in', names), self))
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
245 def newOutputPort(self):
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
246 names = [i.name for i in self.inputs + self.outputs]
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
247 self.addOutput(OutputPort(uniqify('out', names), self))
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
248 def setName(self, name): self.label.setPlainText(name)
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
249 def getName(self): return self.label.toPlainText()
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
250 name = property(getName, setName)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
251 def getDict(self):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
252 d = {'x': self.scenePos().x(), 'y': self.scenePos().y()}
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
253 rect = self.rect()
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
254 d.update({'width': rect.width(), 'height': rect.height()})
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
255 d.update({'name': self.name, 'code': self.code})
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
256 d['inputs'] = [inp.Dict for inp in self.inputs]
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
257 d['outputs'] = [outp.Dict for outp in self.outputs]
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
258 d['submodel'] = self.subModel.Dict
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
259 return d
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
260 def setDict(self, d):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
261 self.name = d['name']
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
262 self.code = d['code']
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
263 self.setPos(d['x'], d['y'])
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
264 self.sizer.setPos(d['width'], d['height'])
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
265 for inp in d['inputs']: self.addInput(InputPort(inp['name'], self))
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
266 for outp in d['outputs']: self.addOutput(OutputPort(outp['name'], self))
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
267 self.subModel.Dict = d['submodel']
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
268 Dict = property(getDict, setDict)
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
269 def addInput(self, i):
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
270 self.inputs.append(i)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
271 self.updateSize()
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
272 def addOutput(self, o):
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
273 self.outputs.append(o)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
274 self.updateSize()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
275 def contextMenuEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
276 menu = QMenu()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
277 pa = menu.addAction('Parameters')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
278 pa.triggered.connect(self.editParameters)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
279 menu.exec_(event.screenPos())
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
280 def itemChange(self, change, value):
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
281 if change == self.ItemSelectedHasChanged:
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
282 for child in [self.sizer, self.buttonItemAddInput, self.buttonItemAddOutput]:
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
283 child.setVisible(value)
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
284 return super(BlockItem, self).itemChange(change, value)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
285 def myDelete(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
286 scene = self.scene()
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
287 if scene:
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
288 for p in self.inputs + self.outputs:
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
289 if p.connection: p.connection.myDelete()
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
290 scene.removeItem(self)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
291 def updateSize(self):
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
292 rect = self.rect()
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
293 h, w = rect.height(), rect.width()
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
294 self.buttonItemAddInput.setPos(0, h + 4)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
295 self.buttonItemAddOutput.setPos(w+10, h+4)
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
296 for inp, y in zip(self.inputs, equalSpace(len(self.inputs), h)):
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
297 inp.setPos(0.0, y)
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
298 for outp, y in zip(self.outputs, equalSpace(len(self.outputs), h)):
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
299 outp.setPos(w, y)
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
300 def changeSize(self, p):
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
301 h = 20 if p.y() < 20 else p.y()
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
302 w = 40 if p.x() < 40 else p.x()
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
303 self.setRect(0.0, 0.0, w, h)
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
304 rect = self.label.boundingRect()
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
305 self.label.setPos((w - rect.width()) / 2, (h - rect.height()) / 2)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
306 self.updateSize()
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
307 return QPointF(w, h)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
308
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
309 class EditorGraphicsView(QGraphicsView):
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
310 def __init__(self, parent=None):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
311 QGraphicsView.__init__(self, parent)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
312 self.setDragMode(QGraphicsView.RubberBandDrag)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
313 self.delShort = QShortcut(QKeySequence.Delete, self)
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
314 self._model = None
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
315 self.treeView = QTreeView()
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
316 def setDiagram(self, d):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
317 self.setScene(d)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
318 self.delShort.activated.connect(d.deleteItems)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
319 def getModel(self): return self._model
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
320 def setModel(self, m):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
321 self._model = m
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
322 if m:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
323 self.treeView.setModel(m)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
324 self.diagram = m.rootDiagram
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
325 model = property(getModel, setModel)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
326 diagram = property(lambda s: s.scene(), setDiagram)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
327 def save(self):
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
328 if self.model:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
329 if not self.model.filename:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
330 self.model.filename = QFileDialog.getSaveFileName(self)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
331 if self.model.filename:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
332 with open(self.model.filename, 'w') as f:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
333 f.write(json.dumps(self.model.Dict))
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
334 def load(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
335 filename = QFileDialog.getOpenFileName(self)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
336 if filename:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
337 self.model = loadModel(filename)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
338 def newModel(self):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
339 self.model = ModelHierarchyModel()
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
340 def goUp(self):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
341 if hasattr(self.diagram, 'containingBlock'):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
342 self.diagram = self.diagram.containingBlock.scene()
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
343 self.zoomAll()
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
344 def zoomAll(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
345 """ zoom to fit all items """
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
346 rect = self.diagram.itemsBoundingRect()
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
347 self.fitInView(rect, Qt.KeepAspectRatio)
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
348 def wheelEvent(self, event):
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
349 pos = event.pos()
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
350 posbefore = self.mapToScene(pos)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
351 degrees = event.delta() / 8.0
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
352 sx = (100.0 + degrees) / 100.0
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
353 self.scale(sx, sx)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
354 event.accept()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
355 def dragEnterEvent(self, event):
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
356 if event.mimeData().hasFormat('component/name'): event.accept()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
357 def dragMoveEvent(self, event):
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
358 if event.mimeData().hasFormat('component/name'): event.accept()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
359 def dropEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
360 if event.mimeData().hasFormat('component/name'):
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
361 name = bytes(event.mimeData().data('component/name')).decode()
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
362 pos = self.mapToScene(event.pos())
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
363 self.scene().addNewBlock(pos, name)
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
364 # TODO: do this in a cleaner way:
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
365 self.model.modelReset.emit()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
366
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
367 class LibraryModel(QStandardItemModel):
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
368 mimeTypes = lambda self: ['component/name']
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
369 def mimeData(self, idxs):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
370 mimedata = QMimeData()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
371 for idx in idxs:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
372 if idx.isValid():
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
373 txt = self.data(idx, Qt.DisplayRole)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
374 mimedata.setData('component/name', txt)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
375 return mimedata
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
376
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
377 class ModelHierarchyModel(QAbstractItemModel):
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
378 def __init__(self):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
379 super(ModelHierarchyModel, self).__init__()
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
380 self.rootDiagram = DiagramScene()
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
381 self.filename = None
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
382 def setDict(self, d):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
383 self.rootDiagram.Dict = d
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
384 self.modelReset.emit()
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
385 Dict = property(lambda s: s.rootDiagram.Dict, setDict)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
386 def index(self, row, column, parent=None):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
387 if parent.isValid():
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
388 parent = parent.internalPointer().subModel
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
389 else:
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
390 parent = self.rootDiagram
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
391 blocks = sorted(parent.blocks, key=lambda b: b.name)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
392 block = blocks[row]
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
393 # Store the index to retrieve it later in the parent function.
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
394 # TODO: solve this in a better way.
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
395 block.index = self.createIndex(row, column, block)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
396 return block.index
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
397 def parent(self, index):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
398 if index.isValid():
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
399 block = index.internalPointer()
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
400 if block.scene() == self.rootDiagram:
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
401 return QModelIndex()
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
402 else:
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
403 outerBlock = block.scene().containingBlock
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
404 return outerBlock.index
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
405 print('parent: No valid index')
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
406 def data(self, index, role):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
407 if index.isValid() and role == Qt.DisplayRole:
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
408 b = index.internalPointer()
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
409 return b.name
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
410 def headerData(self, section, orientation, role):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
411 if orientation == Qt.Horizontal and role == Qt.DisplayRole:
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
412 return "Element"
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
413 def rowCount(self, parent):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
414 if parent.column() > 0: return 0
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
415 if parent.isValid():
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
416 parent = parent.internalPointer().subModel
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
417 else:
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
418 parent = self.rootDiagram
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
419 return len(parent.blocks)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
420 def columnCount(self, parent):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
421 return 1
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
422
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
423 class DiagramScene(QGraphicsScene):
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
424 def __init__(self):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
425 super(DiagramScene, self).__init__()
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
426 self.startedConnection = None
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
427 blocks = property(lambda sel: [i for i in sel.items() if type(i) is BlockItem])
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
428 connections = property(lambda sel: [i for i in sel.items() if type(i) is Connection])
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
429 def setDict(self, d):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
430 for block in d['blocks']:
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
431 b = BlockItem()
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
432 self.addItem(b)
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
433 b.Dict = block
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
434 for con in d['connections']:
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
435 fromPort = self.findPort(con['fromBlock'], con['fromPort'])
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
436 toPort = self.findPort(con['toBlock'], con['toPort'])
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
437 self.addItem(Connection(fromPort, toPort))
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
438 def getDict(self):
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
439 return {'blocks': [b.Dict for b in self.blocks], 'connections': [c.Dict for c in self.connections]}
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
440 Dict = property(getDict, setDict)
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
441 def findPort(self, blockname, portname):
76
1084b433c278 Line reduction
windel
parents: 75
diff changeset
442 block = self.findBlock(blockname)
1084b433c278 Line reduction
windel
parents: 75
diff changeset
443 if block:
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
444 for port in block.inputs + block.outputs:
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
445 if port.name == portname: return port
76
1084b433c278 Line reduction
windel
parents: 75
diff changeset
446 def findBlock(self, blockname):
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
447 for block in self.blocks:
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
448 if block.name == blockname: return block
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
449 def addNewBlock(self, pos, name):
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
450 blocknames = [item.name for item in self.blocks]
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
451 b1 = BlockItem(uniqify(name, blocknames))
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
452 b1.setPos(pos)
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
453 self.addItem(b1)
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:
74
f506f6b74697 Cleanup
windel
parents: 73
diff changeset
461 for item in self.items(event.scenePos()):
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
462 if type(item) is InputPort and item.connection == None:
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
463 self.startedConnection.setToPort(item)
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
464 self.startedConnection = None
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
465 return
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
466 self.startedConnection.myDelete()
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
467 self.startedConnection = None
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
468 super(DiagramScene, self).mouseReleaseEvent(event)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
469 def startConnection(self, port):
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
470 self.startedConnection = Connection(port, None)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
471 pos = port.scenePos()
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
472 self.startedConnection.setEndPos(pos)
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
473 self.addItem(self.startedConnection)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
474 def deleteItems(self):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
475 for item in list(self.selectedItems()): item.myDelete()
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
476
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
477 class LibraryWidget(QListView):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
478 def __init__(self):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
479 super(LibraryWidget, self).__init__(None)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
480 self.libraryModel = LibraryModel(self)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
481 self.libraryModel.setColumnCount(1)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
482 # Create an icon with an icon:
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
483 pixmap = QPixmap(60, 60)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
484 pixmap.fill()
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
485 painter = QPainter(pixmap)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
486 painter.fillRect(10, 10, 40, 40, Qt.blue)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
487 painter.setBrush(Qt.yellow)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
488 painter.drawEllipse(20, 20, 20, 20)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
489 painter.end()
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
490 # Fill library:
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
491 for name in ['Block', 'Uber unit', 'Device']:
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
492 self.libraryModel.appendRow(QStandardItem(QIcon(pixmap), name))
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
493 self.setModel(self.libraryModel)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
494 self.setViewMode(self.IconMode)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
495 self.setDragDropMode(self.DragOnly)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
496
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
497 def warning(txt):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
498 QMessageBox.warning(None, "Warning", txt)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
499
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
500 def loadModel(filename):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
501 try:
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
502 m = ModelHierarchyModel()
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
503 with open(filename, 'r') as f: data = f.read()
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
504 m.filename = filename
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
505 m.Dict = json.loads(data)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
506 return m
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
507 except KeyError:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
508 warning('Corrupt model: {0}'.format(filename))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
509 except ValueError:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
510 warning('Corrupt model: {0}'.format(filename))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
511 except FileNotFoundError:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
512 warning('File [{0}] not found'.format(filename))
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
513
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
514 class Main(QMainWindow):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
515 def __init__(self):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
516 super(Main, self).__init__(None)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
517 self.editor = EditorGraphicsView()
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
518 self.setCentralWidget(self.editor)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
519 self.setWindowTitle("Diagram editor")
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
520 def buildIcon(b64):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
521 icon = base64.decodestring(b64)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
522 pm = QPixmap()
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
523 pm.loadFromData(icon)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
524 return QIcon(pm)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
525 toolbar = self.addToolBar('Tools')
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
526 toolbar.setObjectName('Tools')
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
527 def act(name, shortcut, callback, icon=None):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
528 a = QAction(icon, name, self) if icon else QAction(name, self)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
529 a.setShortcuts(shortcut)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
530 a.triggered.connect(callback)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
531 toolbar.addAction(a)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
532 act('New', QKeySequence.New, self.editor.newModel, buildIcon(newicon))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
533 act('Save', QKeySequence.Save, self.editor.save, buildIcon(saveicon))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
534 act('Load', QKeySequence.Open, self.editor.load, buildIcon(loadicon))
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
535 act('Full screen', QKeySequence("F11"), self.toggleFullScreen)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
536 act('Fit in view', QKeySequence("F8"), self.editor.zoomAll)
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
537 act('Go up', QKeySequence(Qt.Key_Up), self.editor.goUp)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
538 def addDock(name, widget):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
539 dock = QDockWidget(name, self)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
540 dock.setObjectName(name)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
541 dock.setWidget(widget)
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
542 self.addDockWidget(Qt.LeftDockWidgetArea, dock)
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
543 addDock('Library', LibraryWidget())
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
544 addDock('Model tree', self.editor.treeView)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
545 self.settings = QSettings('windelsoft', 'diagrameditor')
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
546 self.loadSettings()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
547 def toggleFullScreen(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
548 self.setWindowState(self.windowState() ^ Qt.WindowFullScreen)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
549 self.editor.zoomAll()
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
550 def loadSettings(self):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
551 if self.settings.contains('mainwindowstate'):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
552 self.restoreState(self.settings.value('mainwindowstate'))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
553 if self.settings.contains('mainwindowgeometry'):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
554 self.restoreGeometry(self.settings.value('mainwindowgeometry'))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
555 if self.settings.contains('openedmodel'):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
556 modelfile = self.settings.value('openedmodel')
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
557 self.editor.model = loadModel(modelfile)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
558 def closeEvent(self, ev):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
559 self.settings.setValue('mainwindowstate', self.saveState())
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
560 self.settings.setValue('mainwindowgeometry', self.saveGeometry())
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
561 if self.editor.model and self.editor.model.filename:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
562 self.settings.setValue('openedmodel', self.editor.model.filename)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
563 # TODO: ask for save of opened files
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
564 else:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
565 self.settings.remove('openedmodel')
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
566 ev.accept()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
567
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
568 if __name__ == '__main__':
45
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
569 if sys.version_info.major != 3:
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
570 print('Please use python 3.x')
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
571 sys.exit(1)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
572 app = QApplication(sys.argv)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
573 main = Main()
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
574 main.show()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
575 app.exec_()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
576