view python/apps/ide/astviewer.py @ 63:32078200cdd6

Several move action
author windel
date Sun, 07 Oct 2012 17:04:10 +0200
parents python/ide/ide/astviewer.py@fd7d5069734e
children
line wrap: on
line source

from PyQt4.QtCore import *
from PyQt4.QtGui import *

def astToNamedElement(astNode, parentNode):
   """ Helper to convert and AST tree to NamedElement tree: """
   item = QStandardItem(str(astNode))
   item.setData(astNode)
   parentNode.appendRow(item)
   for c in astNode.getChildren():
      astToNamedElement(c, item)

# The actual widget:
class AstViewer(QTreeView):
   sigNodeSelected = pyqtSignal(object)
   def __init__(self, parent=None):
      super(AstViewer, self).__init__(parent)
      self.setHeaderHidden(True)
      self.clicked.connect(self.selectHandler)

   def setAst(self, ast):
      """ Create a new model and add all ast elements to it """
      model = QStandardItemModel()
      if ast:
         astToNamedElement(ast, model.invisibleRootItem())
      self.setModel( model )
      self.expandAll()

   def selectHandler(self, index):
      if not index.isValid():
         return
      model = self.model()
      item = model.itemFromIndex(index)
      node = item.data()
      self.sigNodeSelected.emit(node)