annotate python/diagrameditor.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents 6efbeb903777
children
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
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
7 from diagramitems import Connection, ResizeSelectionHandle, Block, DiagramScene, CodeBlock
91
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
8 from icons import newicon, saveicon, loadicon
93
windel
parents: 92
diff changeset
9 import diagramitems
88
f3fe557be5ed Split off of items to reduce file size
windel
parents: 87
diff changeset
10
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
11 """
53
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
12 Author: Windel Bouwman
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
13 Year: 2012
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
14 Description: This script implements a diagram editor.
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
15 run with python 3.x as:
6dd7d6a1737c Added in and output port icons
windel
parents: 52
diff changeset
16 $ python [thisfile.py]
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
17 """
86
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
18 def indent(lines):
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
19 return [' ' + line for line in lines]
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
20
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
21 class ParameterDialog(QDialog):
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
22 def __init__(self, block, parent = None):
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
23 super(ParameterDialog, self).__init__(parent)
49
2b07ab2c0dc4 Added line saving
windel
parents: 48
diff changeset
24 self.block = block
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
25 self.button = QPushButton('Ok', self)
51
b3a65e154ab2 Added fancy arrow head
windel
parents: 50
diff changeset
26 self.nameEdit = QLineEdit(self.block.name)
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
27 self.codeEdit = QTextEdit(self)
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
28 self.codeEdit.setPlainText(self.block.code)
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
29 l = QFormLayout(self)
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
30 l.addRow('Name:', self.nameEdit)
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
31 l.addRow('Code:', self.codeEdit)
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
32 l.addWidget(self.button)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
33 self.button.clicked.connect(self.OK)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
34 def OK(self):
75
ba11d99b1d4a Added qformlayout..
windel
parents: 74
diff changeset
35 self.block.name = self.nameEdit.text()
60
b976546f5a60 Fixed code saving in XML
windel
parents: 59
diff changeset
36 self.block.code = self.codeEdit.toPlainText()
50
38ff8e178fe4 Fixed saving of connections
windel
parents: 49
diff changeset
37 self.close()
56
7a82f0d52e85 Cleanup of line selection
windel
parents: 55
diff changeset
38
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
39 class EditorGraphicsView(QGraphicsView):
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
40 def __init__(self, parent=None):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
41 QGraphicsView.__init__(self, parent)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
42 self.setDragMode(QGraphicsView.RubberBandDrag)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
43 self.delShort = QShortcut(QKeySequence.Delete, self)
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
44 self._model = None
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
45 self.treeView = QTreeView()
99
windel
parents: 95
diff changeset
46 self.treeView.clicked.connect(self.itemActivated)
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
47 def itemActivated(self, idx):
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
48 b = idx.internalPointer()
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
49 s = b.scene()
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
50 s.clearSelection()
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
51 b.setSelected(True)
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
52 def setDiagram(self, d):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
53 self.setScene(d)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
54 self.delShort.activated.connect(d.deleteItems)
91
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
55 def getModel(self):
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
56 return self._model
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
57 def setModel(self, m):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
58 self._model = m
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
59 if m:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
60 self.treeView.setModel(m)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
61 self.diagram = m.rootDiagram
83
762721284ec2 Added expand all
windel
parents: 82
diff changeset
62 self.model.modelReset.connect(self.treeView.expandAll)
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
63 model = property(getModel, setModel)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
64 diagram = property(lambda s: s.scene(), setDiagram)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
65 def save(self):
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
66 if self.model:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
67 if not self.model.filename:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
68 self.model.filename = QFileDialog.getSaveFileName(self)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
69 if self.model.filename:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
70 with open(self.model.filename, 'w') as f:
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
71 f.write(json.dumps(self.model.Dict, indent=2))
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
72 def load(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
73 filename = QFileDialog.getOpenFileName(self)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
74 if filename:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
75 self.model = loadModel(filename)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
76 def newModel(self):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
77 self.model = ModelHierarchyModel()
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
78 def goUp(self):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
79 if hasattr(self.diagram, 'containingBlock'):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
80 self.diagram = self.diagram.containingBlock.scene()
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
81 self.zoomAll()
86
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
82 def showCode(self):
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
83 if self.model:
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
84 c = self.model.gencode()
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
85 c = '\n'.join(c)
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
86 d = QDialog()
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
87 l = QFormLayout(d)
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
88 codeview = QTextEdit()
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
89 codeview.setPlainText(c)
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
90 l.addRow('code', codeview)
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
91 runButton = QPushButton('Run')
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
92 outputview = QTextEdit()
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
93 l.addRow('Output', outputview)
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
94 l.addWidget(runButton)
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
95 def print2(txt):
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
96 txt2 = outputview.toPlainText()
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
97 outputview.setPlainText(txt2 + '\n' + txt)
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
98 def runIt():
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
99 outputview.clear()
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
100 globs = {'print': print2}
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
101 exec(codeview.toPlainText(), globs)
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
102 runButton.clicked.connect(runIt)
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
103 d.exec_()
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
104 def zoomAll(self):
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
105 """ zoom to fit all items """
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
106 rect = self.diagram.itemsBoundingRect()
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
107 self.fitInView(rect, Qt.KeepAspectRatio)
55
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
108 def wheelEvent(self, event):
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
109 pos = event.pos()
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
110 posbefore = self.mapToScene(pos)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
111 degrees = event.delta() / 8.0
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
112 sx = (100.0 + degrees) / 100.0
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
113 self.scale(sx, sx)
9414bad225a8 Added nice resizing
windel
parents: 54
diff changeset
114 event.accept()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
115 def dragEnterEvent(self, event):
93
windel
parents: 92
diff changeset
116 if event.mimeData().hasFormat('component/name'):
windel
parents: 92
diff changeset
117 event.accept()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
118 def dragMoveEvent(self, event):
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
119 if event.mimeData().hasFormat('component/name'): event.accept()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
120 def dropEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
121 if event.mimeData().hasFormat('component/name'):
48
a309982ab4d5 Added cool rotate function
windel
parents: 47
diff changeset
122 name = bytes(event.mimeData().data('component/name')).decode()
93
windel
parents: 92
diff changeset
123 kind, name = name.split(':')
58
65acf6874a8d Removed auto routing
windel
parents: 57
diff changeset
124 pos = self.mapToScene(event.pos())
91
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
125 s = self.scene()
99
windel
parents: 95
diff changeset
126 if not s:
windel
parents: 95
diff changeset
127 return
93
windel
parents: 92
diff changeset
128 print(kind, 'name:', name)
windel
parents: 92
diff changeset
129 kind = getattr(diagramitems, kind)
windel
parents: 92
diff changeset
130 print(kind)
windel
parents: 92
diff changeset
131 b = kind(s.uniqify(name))
91
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
132 b.setPos(pos)
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
133 s.addItem(b)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
134
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
135 class LibraryModel(QStandardItemModel):
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
136 mimeTypes = lambda self: ['component/name']
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
137 def mimeData(self, idxs):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
138 mimedata = QMimeData()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
139 for idx in idxs:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
140 if idx.isValid():
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
141 txt = self.data(idx, Qt.DisplayRole)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
142 mimedata.setData('component/name', txt)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
143 return mimedata
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
144
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
145 class ModelHierarchyModel(QAbstractItemModel):
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
146 def __init__(self):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
147 super(ModelHierarchyModel, self).__init__()
79
44f075fe71eb Changed to use inputport and outputport
windel
parents: 78
diff changeset
148 self.rootDiagram = DiagramScene()
93
windel
parents: 92
diff changeset
149 self.rootDiagram.structureChanged.connect(self.handlechange)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
150 self.filename = None
93
windel
parents: 92
diff changeset
151 def handlechange(self):
windel
parents: 92
diff changeset
152 self.modelReset.emit()
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
153 def setDict(self, d):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
154 self.rootDiagram.Dict = d
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
155 self.modelReset.emit()
91
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
156 def getDict(self):
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
157 return self.rootDiagram.Dict
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
158 Dict = property(getDict, setDict)
86
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
159 def gencode(self):
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
160 c = ['def topLevel():']
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
161 c += indent(self.rootDiagram.gencode())
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
162 c.append('print("Running model")')
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
163 c.append('topLevel()')
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
164 c.append('print("Done")')
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
165 return c
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
166 def index(self, row, column, parent=None):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
167 if parent.isValid():
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
168 parent = parent.internalPointer().subModel
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
169 else:
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
170 parent = self.rootDiagram
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
171 blocks = sorted(parent.blocks, key=lambda b: b.name)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
172 block = blocks[row]
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
173 # Store the index to retrieve it later in the parent function.
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
174 # TODO: solve this in a better way.
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
175 block.index = self.createIndex(row, column, block)
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
176 return block.index
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
177 def parent(self, index):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
178 if index.isValid():
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
179 block = index.internalPointer()
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
180 if block.scene() == self.rootDiagram:
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
181 return QModelIndex()
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
182 else:
93
windel
parents: 92
diff changeset
183 print(block)
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
184 outerBlock = block.scene().containingBlock
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
185 return outerBlock.index
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
186 print('parent: No valid index')
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
187 def data(self, index, role):
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
188 if index.isValid() and role == Qt.DisplayRole:
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
189 b = index.internalPointer()
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
190 if index.column() == 0:
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
191 return b.name
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
192 elif index.column() == 1:
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
193 return str(type(b))
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
194 def headerData(self, section, orientation, role):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
195 if orientation == Qt.Horizontal and role == Qt.DisplayRole:
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
196 if section == 0:
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
197 return "Element"
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
198 elif section == 1:
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
199 return "Type"
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
200 else:
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
201 return "x"
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
202 def rowCount(self, parent):
91
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
203 if parent.column() > 0:
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
204 return 0
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
205 if parent.isValid():
91
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
206 block = parent.internalPointer()
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
207 if hasattr(block, 'subModel'):
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
208 return len(block.subModel.blocks)
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
209 else:
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
210 return 0
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
211 else:
91
7ad4c66dd092 Fixed save function
windel
parents: 89
diff changeset
212 return len(self.rootDiagram.blocks)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
213 def columnCount(self, parent):
92
0c4bdbf0aead improvements?
windel
parents: 91
diff changeset
214 return 2
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
215
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
216 class LibraryWidget(QListView):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
217 def __init__(self):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
218 super(LibraryWidget, self).__init__(None)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
219 self.libraryModel = LibraryModel(self)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
220 self.libraryModel.setColumnCount(1)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
221 # Create an icon with an icon:
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
222 pixmap = QPixmap(60, 60)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
223 pixmap.fill()
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
224 painter = QPainter(pixmap)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
225 painter.fillRect(10, 10, 40, 40, Qt.blue)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
226 painter.setBrush(Qt.yellow)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
227 painter.drawEllipse(20, 20, 20, 20)
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
228 painter.end()
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
229 # Fill library:
93
windel
parents: 92
diff changeset
230 for name in ['CodeBlock:codeBlock', 'DiagramBlock:submod', 'Block:blk']:
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
231 self.libraryModel.appendRow(QStandardItem(QIcon(pixmap), name))
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
232 self.setModel(self.libraryModel)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
233 self.setViewMode(self.IconMode)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
234 self.setDragDropMode(self.DragOnly)
54
d8163d2c3779 Rework of delete function.
windel
parents: 53
diff changeset
235
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
236 def warning(txt):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
237 QMessageBox.warning(None, "Warning", txt)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
238
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
239 def loadModel(filename):
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
240 try:
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
241 m = ModelHierarchyModel()
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
242 with open(filename, 'r') as f: data = f.read()
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
243 m.filename = filename
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
244 m.Dict = json.loads(data)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
245 return m
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
246 except KeyError:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
247 warning('Corrupt model: {0}'.format(filename))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
248 except ValueError:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
249 warning('Corrupt model: {0}'.format(filename))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
250 except FileNotFoundError:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
251 warning('File [{0}] not found'.format(filename))
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
252
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
253 class Main(QMainWindow):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
254 def __init__(self):
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
255 super(Main, self).__init__(None)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
256 self.editor = EditorGraphicsView()
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
257 self.setCentralWidget(self.editor)
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
258 self.setWindowTitle("Diagram editor")
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
259 def buildIcon(b64):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
260 icon = base64.decodestring(b64)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
261 pm = QPixmap()
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
262 pm.loadFromData(icon)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
263 return QIcon(pm)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
264 toolbar = self.addToolBar('Tools')
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
265 toolbar.setObjectName('Tools')
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
266 def act(name, shortcut, callback, icon=None):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
267 a = QAction(icon, name, self) if icon else QAction(name, self)
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
268 a.setShortcuts(shortcut)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
269 a.triggered.connect(callback)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
270 toolbar.addAction(a)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
271 act('New', QKeySequence.New, self.editor.newModel, buildIcon(newicon))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
272 act('Save', QKeySequence.Save, self.editor.save, buildIcon(saveicon))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
273 act('Load', QKeySequence.Open, self.editor.load, buildIcon(loadicon))
72
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
274 act('Full screen', QKeySequence("F11"), self.toggleFullScreen)
b01311fb3be7 Refinements to clean up
windel
parents: 63
diff changeset
275 act('Fit in view', QKeySequence("F8"), self.editor.zoomAll)
77
a5f3959bcab7 Added first hierarchy
windel
parents: 76
diff changeset
276 act('Go up', QKeySequence(Qt.Key_Up), self.editor.goUp)
86
317a73256bf0 Added code generator functions
windel
parents: 84
diff changeset
277 act('Model code', QKeySequence("F7"), self.editor.showCode)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
278 def addDock(name, widget):
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
279 dock = QDockWidget(name, self)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
280 dock.setObjectName(name)
78
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
281 dock.setWidget(widget)
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
282 self.addDockWidget(Qt.LeftDockWidgetArea, dock)
85bfa15c01f1 Changed to json
windel
parents: 77
diff changeset
283 addDock('Library', LibraryWidget())
80
d1925eb3bbd5 Added hierarchy first things
windel
parents: 79
diff changeset
284 addDock('Model tree', self.editor.treeView)
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
285 self.settings = QSettings('windelsoft', 'diagrameditor')
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
286 self.loadSettings()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
287 def toggleFullScreen(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
288 self.setWindowState(self.windowState() ^ Qt.WindowFullScreen)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
289 self.editor.zoomAll()
81
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
290 def loadSettings(self):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
291 if self.settings.contains('mainwindowstate'):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
292 self.restoreState(self.settings.value('mainwindowstate'))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
293 if self.settings.contains('mainwindowgeometry'):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
294 self.restoreGeometry(self.settings.value('mainwindowgeometry'))
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
295 if self.settings.contains('openedmodel'):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
296 modelfile = self.settings.value('openedmodel')
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
297 self.editor.model = loadModel(modelfile)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
298 def closeEvent(self, ev):
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
299 self.settings.setValue('mainwindowstate', self.saveState())
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
300 self.settings.setValue('mainwindowgeometry', self.saveGeometry())
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
301 if self.editor.model and self.editor.model.filename:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
302 self.settings.setValue('openedmodel', self.editor.model.filename)
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
303 # TODO: ask for save of opened files
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
304 else:
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
305 self.settings.remove('openedmodel')
49161141d765 Added icons and improved stability
windel
parents: 80
diff changeset
306 ev.accept()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
307
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
308 if __name__ == '__main__':
45
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
309 if sys.version_info.major != 3:
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
310 print('Please use python 3.x')
8a52263d67c4 Fixed width of library
windel
parents: 44
diff changeset
311 sys.exit(1)
46
7964065400b7 Added xml save function
windel
parents: 45
diff changeset
312 app = QApplication(sys.argv)
61
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
313 main = Main()
6fa41208a3a8 Rework into windowed system
windel
parents: 60
diff changeset
314 main.show()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
315 app.exec_()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
316