annotate python/astviewer.py @ 271:cf7d5fb7d9c8

Reorganization
author Windel Bouwman
date Tue, 20 Aug 2013 18:56:02 +0200
parents 225f444019b1
children
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:
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
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'])
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
11
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
12 def build(self, pkg):
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
13 #self.model.clear()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
14 c = self.model.rowCount()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
15 self.model.removeRows(0, c)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
16 self.curItem = self.model.invisibleRootItem()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
17 if pkg:
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
18 visitor = Visitor()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
19 visitor.visit(pkg, self.p1, self.p2)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
20
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
21 def p1(self, node):
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
22 if type(node) is astnodes.Variable:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
23 i = QStandardItem(self.variableIco, str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
24 elif type(node) is astnodes.Function:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
25 i = QStandardItem(self.functionIco, str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
26 elif type(node) is astnodes.Package:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
27 i = QStandardItem(str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
28 else:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
29 return
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
30 typ = str(node.typ) if hasattr(node, 'typ') else ''
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
31 ti = QStandardItem(str(typ))
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
32 ti.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
33 i.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
34 self.curItem.appendRow([i, ti])
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
35 self.curItem = i
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
36
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
37 def p2(self, node):
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
38 if type(node) in [astnodes.Variable, astnodes.Function, astnodes.Package]:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
39 self.curItem = self.curItem.parent()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 # The actual widget:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42 class AstViewer(QTreeView):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
43 sigNodeSelected = pyqtSignal(object)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
44 def __init__(self, parent=None):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45 super(AstViewer, self).__init__(parent)
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
46 self.clicked.connect(self.selectHandler)
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
47 self.modelBuilder = AstModelBuilder()
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
48 self.setModel(self.modelBuilder.model)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
49
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
50 def setAst(self, ast):
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
51 """ Create a new model and add all ast elements to it """
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
52 self.modelBuilder.build(ast)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
53 self.expandAll()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
54
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
55 def selectHandler(self, index):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
56 if not index.isValid():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
57 return
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
58 model = self.model()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
59 item = model.itemFromIndex(index)
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
60 node = item.data()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
61 self.sigNodeSelected.emit(node)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
62
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
63