333
|
1 from qtwrapper import QtGui, QtCore, QtWidgets, pyqtSignal, get_icon
|
306
|
2 from ppci.c3 import Visitor, astnodes
|
1
|
3
|
165
|
4 class AstModelBuilder:
|
256
|
5 def __init__(self):
|
333
|
6 self.functionIco = get_icon('functionicon.png')
|
|
7 self.variableIco = get_icon('variableicon.png')
|
|
8 self.model = QtGui.QStandardItemModel()
|
|
9 self.model.setHorizontalHeaderLabels(['Object', 'Type'])
|
256
|
10
|
|
11 def build(self, pkg):
|
|
12 #self.model.clear()
|
|
13 c = self.model.rowCount()
|
|
14 self.model.removeRows(0, c)
|
|
15 self.curItem = self.model.invisibleRootItem()
|
|
16 if pkg:
|
|
17 visitor = Visitor()
|
|
18 visitor.visit(pkg, self.p1, self.p2)
|
|
19
|
|
20 def p1(self, node):
|
168
|
21 if type(node) is astnodes.Variable:
|
|
22 i = QStandardItem(self.variableIco, str(node))
|
|
23 elif type(node) is astnodes.Function:
|
|
24 i = QStandardItem(self.functionIco, str(node))
|
|
25 elif type(node) is astnodes.Package:
|
|
26 i = QStandardItem(str(node))
|
|
27 else:
|
|
28 return
|
165
|
29 typ = str(node.typ) if hasattr(node, 'typ') else ''
|
333
|
30 ti = QtGui.QStandardItem(str(typ))
|
165
|
31 ti.setData(node)
|
|
32 i.setData(node)
|
|
33 self.curItem.appendRow([i, ti])
|
|
34 self.curItem = i
|
256
|
35
|
|
36 def p2(self, node):
|
168
|
37 if type(node) in [astnodes.Variable, astnodes.Function, astnodes.Package]:
|
|
38 self.curItem = self.curItem.parent()
|
1
|
39
|
333
|
40
|
1
|
41 # The actual widget:
|
333
|
42 class AstViewer(QtWidgets.QTreeView):
|
256
|
43 sigNodeSelected = pyqtSignal(object)
|
|
44 def __init__(self, parent=None):
|
333
|
45 super().__init__(parent)
|
|
46 self.clicked.connect(self.selectHandler)
|
|
47 self.modelBuilder = AstModelBuilder()
|
|
48 self.setModel(self.modelBuilder.model)
|
1
|
49
|
256
|
50 def setAst(self, ast):
|
|
51 """ Create a new model and add all ast elements to it """
|
|
52 self.modelBuilder.build(ast)
|
|
53 self.expandAll()
|
1
|
54
|
256
|
55 def selectHandler(self, index):
|
333
|
56 if not index.isValid():
|
|
57 return
|
|
58 model = self.model()
|
|
59 item = model.itemFromIndex(index)
|
|
60 node = item.data()
|
|
61 self.sigNodeSelected.emit(node)
|