-
Notifications
You must be signed in to change notification settings - Fork 1
/
treeview.py
158 lines (126 loc) · 4.5 KB
/
treeview.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
"""
Reworked code based on
http://trevorius.com/scrapbook/uncategorized/pyqt-custom-abstractitemmodel/
Adapted to Qt5 and fixed column/row bug.
TODO: handle changing data.
"""
import sys
import csv
from pathlib import Path
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt
class CustomNode(object):
def __init__(self, data):
self._data = data
if type(data) == tuple:
self._data = list(data)
if type(data) is str or not hasattr(data, '__getitem__'):
self._data = [data]
self._columncount = len(self._data)
self._children = []
self._parent = None
self._row = 0
def data(self, column):
if column >= 0 and column < len(self._data):
return self._data[column]
def columnCount(self):
return self._columncount
def childCount(self):
return len(self._children)
def child(self, row):
if row >= 0 and row < self.childCount():
return self._children[row]
def parent(self):
return self._parent
def row(self):
return self._row
def addChild(self, child):
child._parent = self
child._row = len(self._children)
self._children.append(child)
self._columncount = max(child.columnCount(), self._columncount)
class CustomModel(QtCore.QAbstractItemModel):
def __init__(self, nodes, headers):
QtCore.QAbstractItemModel.__init__(self)
self._root = CustomNode(None)
for node in nodes:
self._root.addChild(node)
self.headers = headers
def rowCount(self, index):
if index.isValid():
return index.internalPointer().childCount()
return self._root.childCount()
def addChild(self, node, _parent):
if not _parent or not _parent.isValid():
parent = self._root
else:
parent = _parent.internalPointer()
parent.addChild(node)
def index(self, row, column, _parent=None):
if not _parent or not _parent.isValid():
parent = self._root
else:
parent = _parent.internalPointer()
if not QtCore.QAbstractItemModel.hasIndex(self, row, column, _parent):
return QtCore.QModelIndex()
child = parent.child(row)
if child:
return QtCore.QAbstractItemModel.createIndex(self, row, column, child)
else:
return QtCore.QModelIndex()
def parent(self, index):
if index.isValid():
p = index.internalPointer().parent()
if p:
return QtCore.QAbstractItemModel.createIndex(self, p.row(), 0, p)
return QtCore.QModelIndex()
def columnCount(self, index):
if index.isValid():
return index.internalPointer().columnCount()
return self._root.columnCount()
def data(self, index, role):
if not index.isValid():
return None
node = index.internalPointer()
if role == QtCore.Qt.DisplayRole:
return node.data(index.column())
return None
def headerData(self, section, orientation, role):
if (orientation == Qt.Horizontal and
role == Qt.DisplayRole):
assert 0 <= section <= len(self.headers)
return self.headers[section]
return
# def asRecord(self, index):
# leaf = self.nodeFromIndex(index)
# if leaf is not None and isinstance(leaf, LeafNode):
# return leaf.asRecord()
# return []
class MyTree():
"""
"""
def __init__(self):
self.items = {}
# Set some random data:
dir_ = r'G:\software\my_software\projects\cfactor_asce7'
csv_path = Path(dir_) / 'systems.csv'
with open(csv_path, 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
if row[0][0] in 'ABCDEFGH':
i = row[0]
# root = self.items.get(i, None)
# if root is None:
root = CustomNode(i)
self.items[i] = root
if row[0][0] not in 'FH':
continue
root.addChild(CustomNode(row))
self.tw = QtWidgets.QTreeView()
self.tw.setModel(CustomModel(list(self.items.values()), headers=('System', 'ASCE 7-16 Section', 'R', 'Omega', 'Cd', 'B', 'C', 'D')))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mytree = MyTree()
mytree.tw.show()
sys.exit(app.exec_())