annotate python/ide/astviewer.py @ 363:396e5cefba13

Removed debuginfo instruction
author Windel Bouwman
date Sun, 16 Mar 2014 11:28:47 +0100
parents dcae6574c974
children
rev   line source
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
1 from qtwrapper import QtGui, QtCore, QtWidgets, pyqtSignal, get_icon
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 290
diff changeset
2 from ppci.c3 import Visitor, astnodes
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
3
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
4 class AstModelBuilder:
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
5 def __init__(self):
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
6 self.functionIco = get_icon('functionicon.png')
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
7 self.variableIco = get_icon('variableicon.png')
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
8 self.model = QtGui.QStandardItemModel()
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
9 self.model.setHorizontalHeaderLabels(['Object', 'Type'])
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
10
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
11 def build(self, pkg):
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
12 #self.model.clear()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
13 c = self.model.rowCount()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
14 self.model.removeRows(0, c)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
15 self.curItem = self.model.invisibleRootItem()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
16 if pkg:
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
17 visitor = Visitor()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
18 visitor.visit(pkg, self.p1, self.p2)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
19
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
20 def p1(self, node):
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
21 if type(node) is astnodes.Variable:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
22 i = QStandardItem(self.variableIco, str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
23 elif type(node) is astnodes.Function:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
24 i = QStandardItem(self.functionIco, str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
25 elif type(node) is astnodes.Package:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
26 i = QStandardItem(str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
27 else:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
28 return
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
29 typ = str(node.typ) if hasattr(node, 'typ') else ''
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
30 ti = QtGui.QStandardItem(str(typ))
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
31 ti.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
32 i.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
33 self.curItem.appendRow([i, ti])
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
34 self.curItem = i
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
35
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 216
diff changeset
36 def p2(self, node):
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
37 if type(node) in [astnodes.Variable, astnodes.Function, astnodes.Package]:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
38 self.curItem = self.curItem.parent()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
40
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 # The actual widget:
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
42 class AstViewer(QtWidgets.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):
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
45 super().__init__(parent)
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
46 self.clicked.connect(self.selectHandler)
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
47 self.modelBuilder = AstModelBuilder()
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
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):
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
56 if not index.isValid():
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
57 return
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
58 model = self.model()
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
59 item = model.itemFromIndex(index)
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
60 node = item.data()
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 306
diff changeset
61 self.sigNodeSelected.emit(node)