1
|
1 from PyQt4.QtCore import *
|
|
2 from PyQt4.QtGui import *
|
164
|
3 from c3 import Visitor
|
1
|
4
|
165
|
5 class AstModelBuilder:
|
|
6 def __init__(self):
|
|
7 self.visitor = Visitor(self.p1, self.p2)
|
|
8 def build(self, pkg):
|
|
9 model = QStandardItemModel()
|
|
10 model.setHorizontalHeaderLabels(['Object', 'Type'])
|
|
11 self.curItem = model.invisibleRootItem()
|
|
12 self.visitor.visit(pkg)
|
|
13 return model
|
|
14 def p1(self, node):
|
|
15 i = QStandardItem(str(node))
|
|
16 typ = str(node.typ) if hasattr(node, 'typ') else ''
|
|
17 ti = QStandardItem(str(typ))
|
|
18 ti.setData(node)
|
|
19 i.setData(node)
|
|
20 self.curItem.appendRow([i, ti])
|
|
21 self.curItem = i
|
|
22 def p2(self, node):
|
|
23 self.curItem = self.curItem.parent()
|
1
|
24
|
|
25 # The actual widget:
|
|
26 class AstViewer(QTreeView):
|
4
|
27 sigNodeSelected = pyqtSignal(object)
|
1
|
28 def __init__(self, parent=None):
|
|
29 super(AstViewer, self).__init__(parent)
|
4
|
30 self.clicked.connect(self.selectHandler)
|
165
|
31 self.modelBuilder = AstModelBuilder()
|
1
|
32
|
|
33 def setAst(self, ast):
|
|
34 """ Create a new model and add all ast elements to it """
|
164
|
35 print(ast)
|
1
|
36 if ast:
|
165
|
37 self.setModel(self.modelBuilder.build(ast))
|
|
38 self.expandAll()
|
1
|
39
|
4
|
40 def selectHandler(self, index):
|
1
|
41 if not index.isValid():
|
|
42 return
|
4
|
43 model = self.model()
|
|
44 item = model.itemFromIndex(index)
|
|
45 node = item.data()
|
|
46 self.sigNodeSelected.emit(node)
|
1
|
47
|
|
48
|