annotate python/astviewer.py @ 244:58155c7c4a8e

Add hexutil
author Windel Bouwman
date Wed, 24 Jul 2013 19:47:13 +0200
parents 57c032c5e753
children 225f444019b1
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 from PyQt4.QtCore import *
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2 from PyQt4.QtGui import *
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
3 from c3 import Visitor, astnodes
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
4
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
5 class AstModelBuilder:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
6 def __init__(self):
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 169
diff changeset
7 self.functionIco = QIcon(QPixmap('icons/functionicon.png').scaled(32, 32))
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 169
diff changeset
8 self.variableIco = QIcon(QPixmap('icons/variableicon.png').scaled(32, 32))
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
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
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
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
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
15 self.curItem = self.model.invisibleRootItem()
216
57c032c5e753 Fixed ast viewer
Windel Bouwman
parents: 207
diff changeset
16 visitor = Visitor()
57c032c5e753 Fixed ast viewer
Windel Bouwman
parents: 207
diff changeset
17 visitor.visit(pkg, self.p1, self.p2)
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
18 def p1(self, node):
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
19 if type(node) is astnodes.Variable:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
20 i = QStandardItem(self.variableIco, str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
21 elif type(node) is astnodes.Function:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
22 i = QStandardItem(self.functionIco, str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
23 elif type(node) is astnodes.Package:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
24 i = QStandardItem(str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
25 else:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
26 return
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
27 typ = str(node.typ) if hasattr(node, 'typ') else ''
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
28 ti = QStandardItem(str(typ))
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
29 ti.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
30 i.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
31 self.curItem.appendRow([i, ti])
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
32 self.curItem = i
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
33 def p2(self, node):
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
34 if type(node) in [astnodes.Variable, astnodes.Function, astnodes.Package]:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
35 self.curItem = self.curItem.parent()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 # The actual widget:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 class AstViewer(QTreeView):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
39 sigNodeSelected = pyqtSignal(object)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 def __init__(self, parent=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 super(AstViewer, self).__init__(parent)
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
42 self.clicked.connect(self.selectHandler)
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
43 self.modelBuilder = AstModelBuilder()
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
44 self.setModel(self.modelBuilder.model)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
46 def setAst(self, ast):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
47 """ Create a new model and add all ast elements to it """
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
48 self.modelBuilder.build(ast)
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
49 self.expandAll()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
50
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
51 def selectHandler(self, index):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
52 if not index.isValid():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
53 return
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
54 model = self.model()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
55 item = model.itemFromIndex(index)
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
56 node = item.data()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
57 self.sigNodeSelected.emit(node)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
58
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
59