1
|
1 from PyQt4.QtCore import *
|
|
2 from PyQt4.QtGui import *
|
|
3
|
|
4 def astToNamedElement(astNode, parentNode):
|
|
5 """ Helper to convert and AST tree to NamedElement tree: """
|
|
6 item = QStandardItem(str(astNode))
|
4
|
7 item.setData(astNode)
|
1
|
8 parentNode.appendRow(item)
|
|
9 for c in astNode.getChildren():
|
|
10 astToNamedElement(c, item)
|
|
11
|
|
12 # The actual widget:
|
|
13 class AstViewer(QTreeView):
|
4
|
14 sigNodeSelected = pyqtSignal(object)
|
1
|
15 def __init__(self, parent=None):
|
|
16 super(AstViewer, self).__init__(parent)
|
|
17 self.setHeaderHidden(True)
|
4
|
18 self.clicked.connect(self.selectHandler)
|
1
|
19
|
|
20 def setAst(self, ast):
|
|
21 """ Create a new model and add all ast elements to it """
|
|
22 model = QStandardItemModel()
|
|
23 if ast:
|
|
24 astToNamedElement(ast, model.invisibleRootItem())
|
|
25 self.setModel( model )
|
|
26 self.expandAll()
|
|
27
|
4
|
28 def selectHandler(self, index):
|
1
|
29 if not index.isValid():
|
|
30 return
|
4
|
31 model = self.model()
|
|
32 item = model.itemFromIndex(index)
|
|
33 node = item.data()
|
|
34 self.sigNodeSelected.emit(node)
|
1
|
35
|
|
36
|