-
Notifications
You must be signed in to change notification settings - Fork 0
/
qanytreemodel.py
341 lines (242 loc) · 10.5 KB
/
qanytreemodel.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
from PyQt5.QtCore import Qt, QAbstractItemModel, QModelIndex, QVariant
from PyQt5.QtWidgets import QUndoStack, QUndoCommand
from anytree.importer import DictImporter
from .qanytreeitem import QAnyTreeItem
class QAnyTreeModel(QAbstractItemModel):
def __init__(self, data, parent=None):
super().__init__(parent)
importer = DictImporter(nodecls=QAnyTreeItem)
self.root = importer.import_(data)
self.undoStack = QUndoStack(self)
######################
# Overridden functions
######################
def index(self, row, column, parent=QModelIndex()):
if parent.isValid() and parent.column() != 0:
return QModelIndex()
parentItem = self.getItem(parent)
if not parentItem:
return QModelIndex()
childItem = parentItem.getChild(row)
if childItem:
return self.createIndex(row, column, childItem)
return QModelIndex()
def parent(self, index):
if not index.isValid():
return QModelIndex()
childItem = self.getItem(index)
parentItem = childItem.parent
if not parentItem or parentItem == self.root:
return QModelIndex()
return self.createIndex(parentItem.childNumber(), 0, parentItem)
def rowCount(self, parent=QModelIndex()):
parentItem = self.getItem(parent)
if parentItem:
return parentItem.childCount()
return 0
def insertRows(self, position, rows, parent=QModelIndex()):
addCommand = AddCommand(position, rows, parent, self)
self.undoStack.push(addCommand)
return addCommand.result
def _insertRows(self, position, rows, parent=QModelIndex()):
parentItem = self.getItem(parent)
if not parentItem:
return False
self.beginInsertRows(parent, position, position + rows - 1)
success = parentItem.insertChildren(position, rows, self.root.columnCount())
self.endInsertRows()
return success
def moveRows(self, sourceParent, sourceRow, count, destinationParent, destinationChild):
moveCommand = self.MoveCommand(sourceParent, sourceRow, count, destinationParent, destinationChild, self)
self.undoStack.push(moveCommand)
return moveCommand.result
def _moveRows(self, sourceParent, sourceRow, count, destinationParent, destinationChild):
newDestinationChild = destinationChild
if sourceParent == destinationParent:
if sourceRow == destinationChild:
return True
elif sourceRow < destinationChild:
newDestinationChild += 1
destinationItem = self.getItem(destinationParent)
self.beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, newDestinationChild)
for row in range(sourceRow, sourceRow + count):
index = self.index(row, 0, sourceParent)
item = self.getItem(index)
item.parent = destinationItem
destinationItem.moveChild(item.childNumber(), destinationChild)
self.endMoveRows()
return True
def removeRows(self, position, rows, parent=QModelIndex()):
deleteCommand = DeleteCommand(position, rows, parent, self)
self.undoStack.push(deleteCommand)
return deleteCommand.result
def _removeRows(self, position, rows, parent=QModelIndex()):
parentItem = self.getItem(parent)
if not parentItem:
return False
self.beginRemoveRows(parent, position, position + rows - 1)
success = parentItem.removeChildren(position, rows)
self.endRemoveRows()
return success
def columnCount(self, parent=QModelIndex()):
return self.root.columnCount()
def insertColumns(self, position, columns, parent=QModelIndex()):
self.beginInsertColumns(parent, position, position + columns - 1)
success = self.root.insertColumns(position, columns)
self.endInsertColumns()
return success
def removeColumns(self, position, columns, parent=QModelIndex()):
self.beginRemoveColumns(parent, position, position + columns - 1)
success = self.root.removeColumns(position, columns)
self.endRemoveColumns()
if self.root.columnCount() == 0:
self.removeRows(0, self.rowCount())
return success
def data(self, index, role=Qt.DisplayRole):
if not index.isValid() or (role != Qt.DisplayRole and role != Qt.EditRole):
return QVariant()
item = self.getItem(index)
return item.getData(index.column())
def setData(self, index, value, role=Qt.EditRole):
if role != Qt.EditRole:
return False
oldValue = self.data(index, role)
modifyCommand = ModifyCommand(oldValue, value, index, self)
self.undoStack.push(modifyCommand)
return modifyCommand.result
def _setData(self, index, value):
item = self.getItem(index)
result = item.setData(index.column(), value)
if result:
self.dataChanged.emit(index, index, [Qt.DisplayRole, Qt.EditRole])
return result
def headerData(self, section, orientation, role=Qt.DisplayRole):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.root.getData(section)
return QVariant()
def setHeaderData(self, section, orientation, value, role=Qt.EditRole):
if role != Qt.EditRole or orientation != Qt.Horizontal:
return False
result = self.root.setData(section, value)
if result:
self.headerDataChanged.emit(orientation, section, section)
return result
def flags(self, index):
if not index.isValid():
return Qt.ItemIsDropEnabled
return Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled | Qt.ItemIsEditable | super().flags(index)
def supportedDropActions(self):
return Qt.MoveAction
##################
# Helper functions
##################
def copyRow(self, sourceParent, sourceRow, destinationParent, destinationChild):
columns = self.columnCount()
for column in range(columns):
destinationIndex = self.index(destinationChild, column, destinationParent)
sourceIndex = self.index(sourceRow, column, sourceParent)
self.setData(destinationIndex, self.data(sourceIndex))
def getItem(self, index):
if index.isValid():
item = index.internalPointer()
if item:
return item
return self.root
def toDict(self):
return self.root.toDict()
class MoveCommand(QUndoCommand):
def __init__(self, sourceParent, sourceRow, count, destinationParent, destinationChild, model, parent=None):
super().__init__(parent)
self.sourceParent = sourceParent
self.sourceRow = sourceRow
self.count = count
self.destinationParent = destinationParent
self.destinationChild = destinationChild
self.model = model
self.result = False
self.setText('(move item)')
def undo(self):
self.result = self.model._moveRows(self.destinationParent, self.destinationChild, self.count, self.sourceParent,
self.sourceRow)
def redo(self):
self.result = self.model._moveRows(self.sourceParent, self.sourceRow, self.count, self.destinationParent,
self.destinationChild)
class AddCommand(QUndoCommand):
def __init__(self, position, rows, parent, model):
super().__init__()
self.position = position
self.rows = rows
self.parent = parent
self.model = model
self.result = False
if rows > 1:
self.setText('(add item)')
else:
self.setText('(add items)')
def undo(self):
self.result = self.model._removeRows(self.position, self.rows, self.parent)
def redo(self):
self.result = self.model._insertRows(self.position, self.rows, self.parent)
class DeleteCommand(QUndoCommand):
def __init__(self, position, rows, parent, model):
super().__init__()
self.position = position
self.rows = rows
self.parent = parent
self.model = model
self.indexLocations = getIndexLocations(parent)
self.result = False
# Backup data from deleted items
self.data = []
for row in range(rows):
index = model.index(position + row, 0, parent)
item = model.getItem(index)
self.data.append(item.toDict())
if rows > 1:
self.setText('(delete items)')
else:
self.setText('(delete item)')
def undo(self):
importer = DictImporter(nodecls=QAnyTreeItem)
parent = getIndexFromLocations(self.indexLocations, self.model)
parentItem = self.model.getItem(parent)
self.result = self.model.beginInsertRows(parent, self.position, self.position + self.rows - 1)
for row, data in enumerate(self.data):
print(data)
item = importer.import_(data)
# Reconstruct branch
item.parent = parentItem
parentItem.moveChild(parentItem.childCount() - 1, self.position + row)
self.model.endInsertRows()
def redo(self):
parent = getIndexFromLocations(self.indexLocations, self.model)
self.result = self.model._removeRows(self.position, self.rows, parent)
class ModifyCommand(QUndoCommand):
def __init__(self, oldValue, newValue, index, model):
super().__init__()
self.oldValue = oldValue
self.newValue = newValue
self.index = index
self.model = model
self.indexLocations = getIndexLocations(index)
self.result = False
self.setText('(modify item)')
def undo(self):
index = getIndexFromLocations(self.indexLocations, self.model)
self.result = self.model._setData(index, self.oldValue)
def redo(self):
index = getIndexFromLocations(self.indexLocations, self.model)
self.result = self.model._setData(index, self.newValue)
def getIndexFromLocations(indexLocations, model, parent=QModelIndex()):
index = QModelIndex()
for row, column in indexLocations:
index = model.index(row, column, parent)
parent = index
return index
def getIndexLocations(index):
indexLocations = []
while index.row() != -1:
indexLocations.append((index.row(), index.column()))
index = index.parent()
indexLocations.reverse()
return indexLocations