Mercurial > lcfOS
annotate python/astviewer.py @ 216:57c032c5e753
Fixed ast viewer
author | Windel Bouwman |
---|---|
date | Fri, 05 Jul 2013 13:26:51 +0200 |
parents | 8b2f20aae086 |
children | 225f444019b1 |
rev | line source |
---|---|
1 | 1 from PyQt4.QtCore import * |
2 from PyQt4.QtGui import * | |
168 | 3 from c3 import Visitor, astnodes |
1 | 4 |
165 | 5 class AstModelBuilder: |
6 def __init__(self): | |
207 | 7 self.functionIco = QIcon(QPixmap('icons/functionicon.png').scaled(32, 32)) |
8 self.variableIco = QIcon(QPixmap('icons/variableicon.png').scaled(32, 32)) | |
168 | 9 self.model = QStandardItemModel() |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
10 self.model.setHorizontalHeaderLabels(['Object', 'Type']) |
165 | 11 def build(self, pkg): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
12 #self.model.clear() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
13 c = self.model.rowCount() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
14 self.model.removeRows(0, c) |
168 | 15 self.curItem = self.model.invisibleRootItem() |
216 | 16 visitor = Visitor() |
17 visitor.visit(pkg, self.p1, self.p2) | |
165 | 18 def p1(self, node): |
168 | 19 if type(node) is astnodes.Variable: |
20 i = QStandardItem(self.variableIco, str(node)) | |
21 elif type(node) is astnodes.Function: | |
22 i = QStandardItem(self.functionIco, str(node)) | |
23 elif type(node) is astnodes.Package: | |
24 i = QStandardItem(str(node)) | |
25 else: | |
26 return | |
165 | 27 typ = str(node.typ) if hasattr(node, 'typ') else '' |
28 ti = QStandardItem(str(typ)) | |
29 ti.setData(node) | |
30 i.setData(node) | |
31 self.curItem.appendRow([i, ti]) | |
32 self.curItem = i | |
33 def p2(self, node): | |
168 | 34 if type(node) in [astnodes.Variable, astnodes.Function, astnodes.Package]: |
35 self.curItem = self.curItem.parent() | |
1 | 36 |
37 # The actual widget: | |
38 class AstViewer(QTreeView): | |
4 | 39 sigNodeSelected = pyqtSignal(object) |
1 | 40 def __init__(self, parent=None): |
41 super(AstViewer, self).__init__(parent) | |
4 | 42 self.clicked.connect(self.selectHandler) |
165 | 43 self.modelBuilder = AstModelBuilder() |
168 | 44 self.setModel(self.modelBuilder.model) |
1 | 45 |
46 def setAst(self, ast): | |
47 """ Create a new model and add all ast elements to it """ | |
168 | 48 self.modelBuilder.build(ast) |
49 self.expandAll() | |
1 | 50 |
4 | 51 def selectHandler(self, index): |
1 | 52 if not index.isValid(): |
53 return | |
4 | 54 model = self.model() |
55 item = model.itemFromIndex(index) | |
56 node = item.data() | |
57 self.sigNodeSelected.emit(node) | |
1 | 58 |
59 |