comparison python/ide/astviewer.py @ 290:7b38782ed496

File moves
author Windel Bouwman
date Sun, 24 Nov 2013 11:24:15 +0100
parents python/astviewer.py@225f444019b1
children b145f8e6050b
comparison
equal deleted inserted replaced
289:bd2593de3ff8 290:7b38782ed496
1 from PyQt4.QtCore import *
2 from PyQt4.QtGui import *
3 from c3 import Visitor, astnodes
4
5 class AstModelBuilder:
6 def __init__(self):
7 self.functionIco = QIcon(QPixmap('icons/functionicon.png').scaled(32, 32))
8 self.variableIco = QIcon(QPixmap('icons/variableicon.png').scaled(32, 32))
9 self.model = QStandardItemModel()
10 self.model.setHorizontalHeaderLabels(['Object', 'Type'])
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):
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
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
36
37 def p2(self, node):
38 if type(node) in [astnodes.Variable, astnodes.Function, astnodes.Package]:
39 self.curItem = self.curItem.parent()
40
41 # The actual widget:
42 class AstViewer(QTreeView):
43 sigNodeSelected = pyqtSignal(object)
44 def __init__(self, parent=None):
45 super(AstViewer, self).__init__(parent)
46 self.clicked.connect(self.selectHandler)
47 self.modelBuilder = AstModelBuilder()
48 self.setModel(self.modelBuilder.model)
49
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()
54
55 def selectHandler(self, index):
56 if not index.isValid():
57 return
58 model = self.model()
59 item = model.itemFromIndex(index)
60 node = item.data()
61 self.sigNodeSelected.emit(node)
62
63