Mercurial > lcfOS
annotate python/astviewer.py @ 192:6cd6260789a1
Added more tests for parser generator
author | Windel Bouwman |
---|---|
date | Sun, 26 May 2013 23:19:27 +0200 |
parents | ee0d30533dae |
children | 8b2f20aae086 |
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): | |
168 | 7 self.functionIco = QIcon(QPixmap('functionicon.png').scaled(32, 32)) |
8 self.variableIco = QIcon(QPixmap('variableicon.png').scaled(32, 32)) | |
165 | 9 self.visitor = Visitor(self.p1, self.p2) |
168 | 10 self.model = QStandardItemModel() |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
11 self.model.setHorizontalHeaderLabels(['Object', 'Type']) |
165 | 12 def build(self, pkg): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
13 #self.model.clear() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
14 c = self.model.rowCount() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
15 self.model.removeRows(0, c) |
168 | 16 self.curItem = self.model.invisibleRootItem() |
165 | 17 self.visitor.visit(pkg) |
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 |