forked from BlueQuartzSoftware/MetaForge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usetreemodel.py
80 lines (64 loc) · 3.45 KB
/
usetreemodel.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
# 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, QMessageBox
from treeitem import TreeItem
from treemodel import TreeModel
class TreeModelU(TreeModel):
checkChanged = Signal(int, str)
def __init__(self, headers, data, tablemodel, editables,parent=None):
super(TreeModel, self).__init__(parent)
rootData = [header for header in headers]
self.rootItem = TreeItem(rootData)
self.treeDict= data
self.tablemodel = tablemodel
self.editableKeys = editables
self.setupModelData(data, self.rootItem)
self.checkList()
def checkList(self):
for i in range(len(self.tablemodel.templatesources)):
if "Custom Input" not in self.tablemodel.templatesources[i]:
if self.tablemodel.templatesources[i] not in self.tablemodel.newmetadatasources:
QMessageBox.warning(None, QApplication.applicationDisplayName(), "Bad stuff happens. " + "The file extracted is missing Source: \n\n"+ self.tablemodel.templatesources[i] + "\n\nPlease try a different file")
self.tablemodel.metadataList = []
self.tablemodel.metadatasources = []
self.tablemodel.newmetadataList = []
self.tablemodel.newmetadatasources = []
return
for i in range(len(self.tablemodel.newmetadataList)):
self.tablemodel.addRow(self.tablemodel.newmetadataList[i])
self.tablemodel.newmetadataList = []
self.tablemodel.newmetadatasources = []
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)
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:
self.tablemodel.prepRow(curDict,tempSource,key)