Source

codeaide / codeaide / modes / visualhelper.py

Full commit
from codeaide.base import *


class WrapIndicator(ModeBase):


    def init(self, width=80):
        self.x = self.textedit.fm.width("0"*width)
        self.pen = QPen(QColor(self.textedit.scheme.wrap_indicator_color))
        self.brush = QBrush(QColor(self.textedit.scheme.wrap_indicator_background))


    def resize_hook(self, event):
        cr = self.textedit.contentsRect()
        y = cr.top()
        x = cr.left() + self.x
        h = cr.height()
        w = cr.width()
        self.line = QLine(x, y, x, h)
        self.rect = QRect(x+1, y, w, h)

    
    def paint_hook_before(self, event, painter, viewport):
        rect = event.rect()
        if rect.right() < self.x:
            return
        painter.fillRect(self.rect, self.brush)


    def paint_hook_after(self, event, painter, viewport):
        rect = event.rect()
        if rect.right() < self.x:
            return
        painter.setPen(self.pen)
        painter.drawLine(self.line)



        
class HighlightLine(ModeBase):
    """
    Originally drawing was done in a paint event with fillRect.
    Now this mode uses an ExtraSelection (Is this better?)
    """


    def init(self):
        self.old_row = -1
        self.selection = None
        self.brush = QBrush(QColor(self.textedit.scheme.highlight_line_background))
        self.textedit.connect(self.textedit, 
                              SIGNAL("cursorPositionChanged()"),
                              self.update)



    def update(self):
        cursor = self.textedit.textCursor()
        row = cursor.blockNumber()
        if row != self.old_row:
            self.old_row = row
            sel = self.selection
            if sel:
                self.textedit.remove_extra_selections([sel])
            sel = QTextEdit.ExtraSelection()
            sel.cursor = QTextCursor(self.textedit.textCursor())
            sel.format.setBackground(self.brush)
            sel.format.setProperty(QTextFormat.FullWidthSelection, QVariant(True))
            self.textedit.add_extra_selections([sel])
            self.selection = sel

    
    #def paint_hook(self, event, painter, viewport):
    #    r = self.textedit.cursorRect()
    #    r.setX(0)
    #    r.setWidth(viewport.width())
    #    painter.fillRect(r, self.brush)