forked from BlueQuartzSoftware/MetaForge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treemodelrestore.py
72 lines (57 loc) · 3.03 KB
/
treemodelrestore.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
# This Python file uses the following encoding: utf-8
from PySide2.QtCore import QAbstractItemModel, QFile, QIODevice, QItemSelectionModel, QModelIndex, QObject ,Qt, Signal, Slot
from PySide2.QtWidgets import QApplication, QMainWindow
from treeitem import TreeItem
from treemodel import TreeModel
class TreeModelR(TreeModel):
checkChanged = Signal(int, str)
def __init__(self, headers, data, tablemodel, newList, filterModel,parent=None):
super(TreeModel, self).__init__(parent)
rootData = [header for header in headers]
self.rootItem = TreeItem(rootData)
self.treeDict = data
self.tablemodel = tablemodel
self.newList = newList
self.filterModel = filterModel
self.setupModelData(data, self.rootItem)
def setupModelData(self, data, parent):
visited={}
queue=[]
grandParents = {}
for key in data.keys():
visited[(parent.itemData[0])]=[key]
queue.append((key,parent,""))
grandParents[key] = (data[key],parent)
curDict = data
tempSource= ""
while queue:
poppedItem = queue.pop(0)
child = poppedItem[0]
parentOfChild = poppedItem[1]
childSource = poppedItem[2]
parent = parentOfChild
parent.insertChildren(parent.childCount(),1,self.rootItem.columnCount())
parent.child(parent.childCount() -1).setData(0,child)
for i in range(len(self.newList)):
if child == self.newList[i]["Key"]:
self.tablemodel.beginInsertRows(self.tablemodel.index(len(self.tablemodel.metadataList), 0), i, i)
self.tablemodel.metadataList.append(self.newList[i])
self.tablemodel.endInsertRows()
parent.child(parent.childCount() -1).checked = self.newList[i]["Checked"]
self.filterModel.checkList(self.newList[i]["Checked"],self.newList[i]["Source"])
if child in grandParents:
curDict = grandParents[child][0]
tempSource = childSource+child+"/"
for curChild in range(grandParents[child][1].childCount()):
if child == grandParents[child][1].child(curChild).itemData[0]:
parent = grandParents[child][1].child(curChild)
visited[(parent.itemData[0])]=[]
if isinstance(curDict, dict):
for key in curDict.keys():
if key not in visited[(parent.itemData[0])]:
visited[(parent.itemData[0])].append(key)
queue.append((key,parent,tempSource))
if (isinstance(curDict[key],dict)):
grandParents[key]= (curDict[key],parent)
else:
pass