comparison python/astviewer.py @ 168:49f1ab80d040

Added awesome icons
author Windel Bouwman
date Fri, 22 Mar 2013 19:09:38 +0100
parents 598d3888a11c
children ee0d30533dae
comparison
equal deleted inserted replaced
167:0b5b2ee6b435 168:49f1ab80d040
1 from PyQt4.QtCore import * 1 from PyQt4.QtCore import *
2 from PyQt4.QtGui import * 2 from PyQt4.QtGui import *
3 from c3 import Visitor 3 from c3 import Visitor, astnodes
4 4
5 class AstModelBuilder: 5 class AstModelBuilder:
6 def __init__(self): 6 def __init__(self):
7 self.functionIco = QIcon(QPixmap('functionicon.png').scaled(32, 32))
8 self.variableIco = QIcon(QPixmap('variableicon.png').scaled(32, 32))
7 self.visitor = Visitor(self.p1, self.p2) 9 self.visitor = Visitor(self.p1, self.p2)
10 self.model = QStandardItemModel()
8 def build(self, pkg): 11 def build(self, pkg):
9 model = QStandardItemModel() 12 self.model.clear()
10 model.setHorizontalHeaderLabels(['Object', 'Type']) 13 self.model.setHorizontalHeaderLabels(['Object', 'Type'])
11 self.curItem = model.invisibleRootItem() 14 self.curItem = self.model.invisibleRootItem()
12 self.visitor.visit(pkg) 15 self.visitor.visit(pkg)
13 return model
14 def p1(self, node): 16 def p1(self, node):
15 i = QStandardItem(str(node)) 17 if type(node) is astnodes.Variable:
18 i = QStandardItem(self.variableIco, str(node))
19 elif type(node) is astnodes.Function:
20 i = QStandardItem(self.functionIco, str(node))
21 elif type(node) is astnodes.Package:
22 i = QStandardItem(str(node))
23 else:
24 return
16 typ = str(node.typ) if hasattr(node, 'typ') else '' 25 typ = str(node.typ) if hasattr(node, 'typ') else ''
17 ti = QStandardItem(str(typ)) 26 ti = QStandardItem(str(typ))
18 ti.setData(node) 27 ti.setData(node)
19 i.setData(node) 28 i.setData(node)
20 self.curItem.appendRow([i, ti]) 29 self.curItem.appendRow([i, ti])
21 self.curItem = i 30 self.curItem = i
22 def p2(self, node): 31 def p2(self, node):
23 self.curItem = self.curItem.parent() 32 if type(node) in [astnodes.Variable, astnodes.Function, astnodes.Package]:
33 self.curItem = self.curItem.parent()
24 34
25 # The actual widget: 35 # The actual widget:
26 class AstViewer(QTreeView): 36 class AstViewer(QTreeView):
27 sigNodeSelected = pyqtSignal(object) 37 sigNodeSelected = pyqtSignal(object)
28 def __init__(self, parent=None): 38 def __init__(self, parent=None):
29 super(AstViewer, self).__init__(parent) 39 super(AstViewer, self).__init__(parent)
30 self.clicked.connect(self.selectHandler) 40 self.clicked.connect(self.selectHandler)
31 self.modelBuilder = AstModelBuilder() 41 self.modelBuilder = AstModelBuilder()
42 self.setModel(self.modelBuilder.model)
32 43
33 def setAst(self, ast): 44 def setAst(self, ast):
34 """ Create a new model and add all ast elements to it """ 45 """ Create a new model and add all ast elements to it """
35 print(ast) 46 self.modelBuilder.build(ast)
36 if ast: 47 self.expandAll()
37 self.setModel(self.modelBuilder.build(ast))
38 self.expandAll()
39 48
40 def selectHandler(self, index): 49 def selectHandler(self, index):
41 if not index.isValid(): 50 if not index.isValid():
42 return 51 return
43 model = self.model() 52 model = self.model()