'Paint the line number'

def paintEvent(self, event):

    QPalette = QtGui.QPalette
    painter = QtGui.QPainter(self)
    palette = self.palette()
    painter.fillRect(event.rect(), palette.color(QPalette.Base))
    editor = self.editor
    content_offset = editor.contentOffset()
    block = editor.firstVisibleBlock()
    current_block_number = max(0, self.editor.textCursor().blockNumber())
    width = self.width()
    event_rect_bottom = event.rect().bottom()
    highlight = palette.color(QPalette.Highlight)
    window = palette.color(QPalette.Window)
    disabled = palette.color(QPalette.Disabled, QPalette.Text)
    painter.setPen(disabled)
    while block.isValid():
        block_geom = editor.blockBoundingGeometry(block)
        block_top = block_geom.translated(content_offset).top()
        if ((not block.isVisible()) or (block_top >= event_rect_bottom)):
            break
        rect = block_geom.translated(content_offset).toRect()
        block_number = block.blockNumber()
        if (block_number == self.highlight_line):
            painter.fillRect(rect.x(), rect.y(), width, rect.height(), highlight)
        elif (block_number == current_block_number):
            painter.fillRect(rect.x(), rect.y(), width, rect.height(), window)
        number = (u'%s' % (block_number + 1))
        painter.drawText(rect.x(), rect.y(), (self.width() - (defs.margin * 2)), rect.height(), (Qt.AlignRight | Qt.AlignVCenter), number)
        block = block.next()
