-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_model.py
355 lines (272 loc) · 9.94 KB
/
tree_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from PyQt4.QtCore import Qt, QTimer, QPoint, pyqtWrapperType
from PyQt4.QtGui import QStandardItem, QAction, QTreeView, QMenu, QStandardItemModel, QApplication, QTableView, \
QItemDelegate, QDoubleSpinBox, QMainWindow, QDockWidget, QComboBox
def increment_name(base_name, existing):
i = 1
while base_name[-i:].isdigit():
i += 1
if i == 1:
n = 1
name = base_name
else:
n = int(base_name[-(i-1):])
name = base_name[:-(i-1)]
while name + str(n) in existing:
n += 1
return name + str(n)
class PropItem(QStandardItem):
def get(self):
return self.text()
def set(self, value):
self.setText(value)
def clone(self):
return type(self)(self.get())
def encode(self):
return self.get()
def decode(self, value):
self.set(value)
class MetaTreeObject(pyqtWrapperType):
def __new__(cls, name, parents, dct):
new_dct = dct.copy()
new_dct["_props"] = _props = {}
for key, val in dct.items():
if isinstance(val, PropItem):
def getter(self):
self._prop_items[key].get()
def setter(self, value):
self._prop_items[key].set(value)
def cloner():
val.clone()
new_dct[key] = property(getter, setter)
_props[key] = cloner
return super(MetaTreeObject, cls).__new__(cls, name, parents, new_dct)
class TreeObject(QStandardItem):
__metaclass__ = MetaTreeObject
standard_name = "TreeObject"
child_classes = []
def __init__(self, parent=None):
super(TreeObject, self).__init__()
if parent:
self.name = increment_name(self.standard_name, parent.child_names())
else:
self.name = self.standard_name
self.setText(self.name)
self.context_menu = QMenu()
for cls in self.child_classes:
action = QAction("Insert " + cls.standard_name, self.context_menu)
action.triggered.connect(lambda _, c=cls: self.appendRow(c(self)))
self.context_menu.addAction(action)
clone_action = QAction("Clone", self.context_menu)
self.context_menu.addAction(clone_action)
clone_action.triggered.connect(self.clone)
self.props_table = PropsTable()
self.props_widget = PropsWidget(self.props_table)
self._prop_items = {}
for name, factory in self._props.items():
self._prop_items[name] = prop_item = factory()
self.props_table.add_prop(name, prop_item)
def children(self):
return [self.child(n) for n in range(self.rowCount())]
def child_names(self):
return [c.name for c in self.children()]
def encode(self):
return {
'text': str(self.text()),
'children': [
(c.__class__.__name__, c.__module__, c.encode())
for c in self.children()
],
'props': self.props_table.encode()
}
def decode(self, d):
child_names = [
c.name for c in self.parent_or_model().children() if c is not self
]
self.setText(increment_name(d['text'], child_names))
self.props_table.decode(d['props'])
for class_name, module_name, child_dict in d['children']:
module = __import__(module_name)
cls = getattr(module, class_name)
inst = cls(self.parent())
self.appendRow(inst)
inst.decode(child_dict)
def parent_or_model(self):
if self.parent() is None:
return self.model()
return self.parent()
def clone(self):
d = self.encode()
new_inst = type(self)(self.parent())
self.parent_or_model().appendRow(new_inst)
new_inst.decode(d)
class PropsTable(QStandardItemModel):
def __init__(self):
super(PropsTable, self).__init__()
self._items_dict = {}
def flags(self, idx):
sflags = super(PropsTable, self).flags(idx)
if idx.column() == 0:
return sflags & ~Qt.ItemIsEditable
return sflags
def add_prop(self, name, item):
self.appendRow([QStandardItem(name), item])
self._items_dict[name] = item
def encode(self):
return {name: item.encode() for name, item in self._items_dict.items()}
def decode(self, d):
for name, value in d.items():
self._items_dict[name].decode(value)
class PropsWidget(QTableView):
def __init__(self, model):
super(PropsWidget, self).__init__()
self.setModel(model)
self.setItemDelegate(PropDelegate(model))
class FloatProp(PropItem):
def __init__(self, init):
super(FloatProp, self).__init__(str(init))
def create_editor(self):
w = QDoubleSpinBox()
w.setValue(float(self.text()))
return w
def set_data(self, widget):
"""
:type widget: QDoubleSpinBox
"""
self.setText(str(widget.value()))
def get(self):
return float(self.text())
def set(self, val):
self.setText(str(val))
class ObjectProp(PropItem):
def __init__(self, cls_or_item):
if isinstance(cls_or_item, TreeObject):
super(ObjectProp, self).__init__(cls_or_item.name)
self.object_class = type(cls_or_item)
self.current_item = cls_or_item
else:
super(ObjectProp, self).__init__("None")
self.object_class = cls_or_item
self.current_item = None
def set_parent_instance(self, parent):
self.parent_instance = parent
pv = self.potential_values()
if pv:
self.set(pv[0])
def potential_values(self):
return [o for o in self.parent_instance.children()
if isinstance(o, self.object_class)]
def create_editor(self):
w = QComboBox()
items = self.potential_values()
names = [o.name for o in items]
w.addItems(names)
if self.current_item is not None and self.current_item.name in names:
w.setCurrentIndex(names.index(self.current_item.name))
return w
def set_data(self, widget):
"""
:type widget: QComboBox
"""
name = widget.currentText()
self.set(self.item_from_name(name))
def get(self):
return self.current_item
def set(self, item):
self.current_item = item
if item is None:
self.setText("None")
else:
self.setText(item.name)
def encode(self):
item = self.current_item
item_name = item.name if item is not None else None
return (
item_name,
self.object_class.__name__,
self.object_class.__module__
)
def decode(self, val):
item_name, class_name, module_name = val
module = __import__(module_name)
self.object_class = getattr(module, class_name)
if item_name is not None:
self.set(self.item_from_name(item_name))
def item_from_name(self, name):
for item in self.potential_values():
if item.name == name:
return item
def clone(self):
if self.current_item is None:
return ObjectProp(self.object_class)
else:
return ObjectProp(self.current_item)
class PropDelegate(QItemDelegate):
def __init__(self, model):
super(PropDelegate, self).__init__()
self.model = model
def createEditor(self, widget, style, idx):
item = self.model.itemFromIndex(idx)
w = item.create_editor()
w.setParent(widget)
return w
def setModelData(self, widget, model, index):
model.itemFromIndex(index).set_data(widget)
class TreeModel(QStandardItemModel):
def children(self):
return [self.item(n, 0) for n in range(self.rowCount())]
def child_names(self):
return [c.name for c in self.children()]
class TreeWidget(QTreeView):
def __init__(self, **kwargs):
super(TreeWidget, self).__init__(**kwargs)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.popup_context_menu)
def popup_context_menu(self, pos):
item = self.model().itemFromIndex(self.indexAt(pos))
if item:
item.context_menu.exec_(self.mapToGlobal(pos) + QPoint(0, 23))
return True
return False
def setModel(self, model):
super(TreeWidget, self).setModel(model)
model.rowsInserted.connect(lambda parent, i, j: self.expand(parent))
class MainWindow(QMainWindow):
def __init__(self, model):
super(MainWindow, self).__init__()
self.tree_dock = QDockWidget()
self.props_dock = QDockWidget()
self.tree_view = TreeWidget()
self.tree_view.setModel(model)
self.tree_dock.setWidget(self.tree_view)
self.addDockWidget(Qt.LeftDockWidgetArea, self.tree_dock)
self.addDockWidget(Qt.LeftDockWidgetArea, self.props_dock)
self.tree_view.clicked.connect(self.change_props_widget)
def change_props_widget(self, idx):
self.props_dock.setWidget(
self.tree_view.model().itemFromIndex(idx).props_widget
)
if __name__ == '__main__':
class C(TreeObject):
standard_name = "C"
class B(TreeObject):
standard_name = "B"
test_float = FloatProp(3.3)
test_object = ObjectProp(C)
def __init__(self, parent):
super(B, self).__init__(parent)
print self.test_float
self.test_float = 4.4
print self.test_float
self._prop_items['test_object'].set_parent_instance(parent)
class A(TreeObject):
standard_name = "A"
child_classes = [B, C]
app = QApplication([])
model = TreeModel()
root1 = A(model)
model.appendRow(root1)
widget = MainWindow(model)
widget.show()
timer = QTimer()
timer.singleShot(1, lambda: widget.raise_())
app.exec_()