codeaide / codeaide / base.py

import re

from PyQt4.QtCore import (
    Qt, QString, SIGNAL, QVariant, QThread, QLine, QSize, QRect, QTimer,
    QAbstractListModel, QModelIndex)
from PyQt4.QtGui import (
    QApplication, QWidget, QHBoxLayout, QToolTip,
    QTextBlockUserData, QPlainTextEdit, QCompleter, QTextCursor, 
    QColor, QBrush, QPainter, QPen, QPalette, QTextEdit, QTextFormat)


    

class ModeBase(object):

    keyboard_hooks = {
        }
    paint_hook_before = None
    paint_hook = None
    paint_hook_after = None
    resize_hook = None
    
    state = 0

    requires = []



    @classmethod
    def find(cls, mode):
        for subcls in cls.__subclasses__():
            if isinstance(mode, basestring):
                if subcls.__name__ == mode:
                    return subcls
            else:
                if subcls is mode:
                    return subcls
            found = subcls.find(mode)
            if found:
                return found


    def __init__(self, textedit, *args, **kwargs):
        for required_mode in self.requires:
            textedit.require(required_mode)
        self.textedit = textedit
        self.init(*args, **kwargs)
        self.install_hooks()


    def init(self):
        pass


    def install_hooks(self):
        self.install_keyboard_hooks()
        self.install_paint_hook()
        self.install_resize_hook()


    def install_keyboard_hooks(self):
        import functools
        for key, callback in self.keyboard_hooks.items():
            if isinstance(key, tuple):
                key, modifiers = key
            else:
                modifiers = -1
            h = self.textedit.keyboard_hooks.setdefault(self.state, {})
            bound_method = functools.partial(callback, self)
            h.setdefault((key, int(modifiers)), []).append(bound_method)


    def install_paint_hook(self):
        callback = self.paint_hook_before or self.paint_hook 
        if callback:
            h = self.textedit.paint_hooks_before
            h.setdefault(self.state, []).append(callback)
        callback = self.paint_hook_after
        if callback:
            h = self.textedit.paint_hooks_after
            h.setdefault(self.state, []).append(callback)

        
    def install_resize_hook(self):
        callback = self.resize_hook
        if callback:
            h = self.textedit.resize_hooks
            h.setdefault(self.state, []).append(callback)




class TextCursor(QTextCursor):


    def __enter__(self, *args):
        self.beginEditBlock()


    def __exit__(self, *args):
        self.endEditBlock()


    def current_word(self):
        self.select(QTextCursor.WordUnderCursor)
        return unicode(self.selectedText())


    def current_line(self):
        return unicode(self.block().text())


    def endswith_word(self):
        word = self.current_word()
        pos = self.position()
        line = self.current_line()
        if line[:pos].endswith(word):
            return word


    def select_range(self, start_pos, end_pos):
        self.setPosition(start_pos)
        self.setPosition(end_pos, QTextCursor.KeepAnchor)


    def remove_range(self, start_pos, end_pos):
        self.select_range(start_pos, end_pos)
        self.removeSelectedText()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.