annotate python/other/diagrameditor.py @ 405:f381cea07fec

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