Source

codeaide / codeaide / modes / language / outline.py

Full commit
# -*- coding: utf-8 -*-
from PyQt4.QtCore import SIGNAL
from codeaide.base import ModeBase
from codeaide.utils import IdleBackgroundCall


class OutlineBase(ModeBase):


    def init(self):
        self.textedit.structure = []
        self.call = None
        self.init_outline()


    def init_outline(self):
        pass


    def contents_change_hook(self, block_number, block, text, user_data):
        # XXX: better call on QPlainTextEdit.blockCountChanged(int) or
        #      QAbstractTextDocumentLayout.updateBlock(QTextBlock) ?
        if not self.call:
            #self.call = IdleCall(self.textedit, self.update_structure)
            self.call = IdleBackgroundCall(
                self.textedit, self.update_structure, self.on_new_structure)


    def update_structure(self):
        """
        This method is called as a background thread and should return the nodes
        """
        raise NotImplementedError, "%s: %s" % (
            self.update_structure, self.update_structure.__doc__)


    def on_new_structure(self, nodes):
        self.call = None
        self.show_structure(nodes)


    def show_structure(self, nodes):
        """
        This called after update_structure.
        Implement your treeview-/list-update here.
        """
        self.textedit.structure = nodes
        self.textedit.emit(SIGNAL("structure_changed"), nodes)