Mercurial > lcfOS
annotate python/astviewer.py @ 280:02385f62f250
Rework from str interface to Instruction interface
author | Windel Bouwman |
---|---|
date | Sat, 02 Nov 2013 10:03:26 +0100 |
parents | 225f444019b1 |
children |
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: |
256 | 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']) |
256 | 11 |
12 def build(self, pkg): | |
13 #self.model.clear() | |
14 c = self.model.rowCount() | |
15 self.model.removeRows(0, c) | |
16 self.curItem = self.model.invisibleRootItem() | |
17 if pkg: | |
18 visitor = Visitor() | |
19 visitor.visit(pkg, self.p1, self.p2) | |
20 | |
21 def p1(self, node): | |
168 | 22 if type(node) is astnodes.Variable: |
23 i = QStandardItem(self.variableIco, str(node)) | |
24 elif type(node) is astnodes.Function: | |
25 i = QStandardItem(self.functionIco, str(node)) | |
26 elif type(node) is astnodes.Package: | |
27 i = QStandardItem(str(node)) | |
28 else: | |
29 return | |
165 | 30 typ = str(node.typ) if hasattr(node, 'typ') else '' |
31 ti = QStandardItem(str(typ)) | |
32 ti.setData(node) | |
33 i.setData(node) | |
34 self.curItem.appendRow([i, ti]) | |
35 self.curItem = i | |
256 | 36 |
37 def p2(self, node): | |
168 | 38 if type(node) in [astnodes.Variable, astnodes.Function, astnodes.Package]: |
39 self.curItem = self.curItem.parent() | |
1 | 40 |
41 # The actual widget: | |
42 class AstViewer(QTreeView): | |
256 | 43 sigNodeSelected = pyqtSignal(object) |
44 def __init__(self, parent=None): | |
1 | 45 super(AstViewer, self).__init__(parent) |
4 | 46 self.clicked.connect(self.selectHandler) |
165 | 47 self.modelBuilder = AstModelBuilder() |
168 | 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): |
1 | 56 if not index.isValid(): |
57 return | |
4 | 58 model = self.model() |
59 item = model.itemFromIndex(index) | |
60 node = item.data() | |
61 self.sigNodeSelected.emit(node) | |
1 | 62 |
63 |