annotate python/astviewer.py @ 120:bb08633510bf

Added repostat script
author Windel Bouwman
date Sat, 12 Jan 2013 10:56:12 +0100
parents 5a965e9664f2
children e023d3ce1d63
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 from PyQt4.QtCore import *
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2 from PyQt4.QtGui import *
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
3
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
4 def astToNamedElement(astNode, parentNode):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
5 """ Helper to convert and AST tree to NamedElement tree: """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
6 item = QStandardItem(str(astNode))
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
7 item.setData(astNode)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
8 parentNode.appendRow(item)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
9 for c in astNode.getChildren():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10 astToNamedElement(c, item)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
12 # The actual widget:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13 class AstViewer(QTreeView):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
14 sigNodeSelected = pyqtSignal(object)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 def __init__(self, parent=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
16 super(AstViewer, self).__init__(parent)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17 self.setHeaderHidden(True)
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
18 self.clicked.connect(self.selectHandler)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
20 def setAst(self, ast):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 """ Create a new model and add all ast elements to it """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22 model = QStandardItemModel()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
23 if ast:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24 astToNamedElement(ast, model.invisibleRootItem())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 self.setModel( model )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 self.expandAll()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
28 def selectHandler(self, index):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 if not index.isValid():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30 return
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
31 model = self.model()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
32 item = model.itemFromIndex(index)
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
33 node = item.data()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
34 self.sigNodeSelected.emit(node)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36