Example #1
0
def html_copy(cursor, scheme='editor', number_lines=False):
    """Return a new QTextDocument with highlighting set as HTML textcharformats.

    The cursor is a cursor of a document.Document instance. If the cursor
    has a selection, only the selection is put in the new document.

    If number_lines is True, line numbers are added.

    """
    data = textformats.formatData(scheme)
    doc = QTextDocument()
    doc.setDefaultFont(data.font)
    doc.setPlainText(cursor.document().toPlainText())
    if metainfo.info(cursor.document()).highlighting:
        highlight(doc, mapping(data),
                  ly.lex.state(documentinfo.mode(cursor.document())))
    if cursor.hasSelection():
        # cut out not selected text
        start, end = cursor.selectionStart(), cursor.selectionEnd()
        cur1 = QTextCursor(doc)
        cur1.setPosition(start, QTextCursor.KeepAnchor)
        cur2 = QTextCursor(doc)
        cur2.setPosition(end)
        cur2.movePosition(QTextCursor.End, QTextCursor.KeepAnchor)
        cur2.removeSelectedText()
        cur1.removeSelectedText()
    if number_lines:
        c = QTextCursor(doc)
        f = QTextCharFormat()
        f.setBackground(QColor('#eeeeee'))
        if cursor.hasSelection():
            num = cursor.document().findBlock(
                cursor.selectionStart()).blockNumber() + 1
            last = cursor.document().findBlock(cursor.selectionEnd())
        else:
            num = 1
            last = cursor.document().lastBlock()
        lastnum = last.blockNumber() + 1
        padding = len(format(lastnum))
        block = doc.firstBlock()
        while block.isValid():
            c.setPosition(block.position())
            c.setCharFormat(f)
            c.insertText('{0:>{1}d} '.format(num, padding))
            block = block.next()
            num += 1
    return doc
Example #2
0
    def __init__(self, document: QtGui.QTextDocument,
                 get_palette: Callable[[], QtGui.QPalette],
                 check_word: Callable[[str], bool],
                 settings: Settings) -> None:
        super().__init__(document)
        KalpanaObject.init(self, settings)
        self.italic_marker = self.settings.italic_marker.value
        self.bold_marker = self.settings.bold_marker.value
        self.underline_marker = self.settings.underline_marker.value
        self.hr_marker = self.settings.horizontal_ruler_marker.value
        self.get_palette = get_palette
        self.check_word = check_word
        self.chapter_keyword = self.settings.chapter_keyword.value
        self.spellcheck_active = self.settings.spellcheck_active.value
        self.active_block = document.firstBlock()
        self.last_block = self.active_block
        self.init_is_done = False

        def update_setting_bold_marker(new_marker: str) -> None:
            self.bold_marker = new_marker

        def update_setting_chapter_keyword(new_keyword: str) -> None:
            self.chapter_keyword = new_keyword

        def update_setting_horizontal_ruler_marker(new_marker: str) -> None:
            self.hr_marker = new_marker

        def update_setting_italic_marker(new_marker: str) -> None:
            self.italic_marker = new_marker

        def update_setting_spellcheck_active(active: bool) -> None:
            if self.spellcheck_active != active:
                self.spellcheck_active = active
                self.rehighlight()

        def update_setting_underline_marker(new_marker: str) -> None:
            self.underline_marker = new_marker

        s = self.settings
        s.bold_marker.changed.connect(update_setting_bold_marker)
        s.chapter_keyword.changed.connect(update_setting_chapter_keyword)
        s.horizontal_ruler_marker.changed.connect(update_setting_horizontal_ruler_marker)
        s.italic_marker.changed.connect(update_setting_italic_marker)
        s.underline_marker.changed.connect(update_setting_underline_marker)
        s.spellcheck_active.changed.connect(update_setting_spellcheck_active)
Example #3
0
def html_copy(cursor, scheme='editor', number_lines=False):
    """Return a new QTextDocument with highlighting set as HTML textcharformats.

    The cursor is a cursor of a document.Document instance. If the cursor
    has a selection, only the selection is put in the new document.

    If number_lines is True, line numbers are added.

    """
    data = textformats.formatData(scheme)
    doc = QTextDocument()
    doc.setDefaultFont(data.font)
    doc.setPlainText(cursor.document().toPlainText())
    if metainfo.info(cursor.document()).highlighting:
        highlight(doc, mapping(data), ly.lex.state(documentinfo.mode(cursor.document())))
    if cursor.hasSelection():
        # cut out not selected text
        start, end = cursor.selectionStart(), cursor.selectionEnd()
        cur1 = QTextCursor(doc)
        cur1.setPosition(start, QTextCursor.KeepAnchor)
        cur2 = QTextCursor(doc)
        cur2.setPosition(end)
        cur2.movePosition(QTextCursor.End, QTextCursor.KeepAnchor)
        cur2.removeSelectedText()
        cur1.removeSelectedText()
    if number_lines:
        c = QTextCursor(doc)
        f = QTextCharFormat()
        f.setBackground(QColor('#eeeeee'))
        if cursor.hasSelection():
            num = cursor.document().findBlock(cursor.selectionStart()).blockNumber() + 1
            last = cursor.document().findBlock(cursor.selectionEnd())
        else:
            num = 1
            last = cursor.document().lastBlock()
        lastnum = last.blockNumber() + 1
        padding = len(format(lastnum))
        block = doc.firstBlock()
        while block.isValid():
            c.setPosition(block.position())
            c.setCharFormat(f)
            c.insertText('{0:>{1}d} '.format(num, padding))
            block = block.next()
            num += 1
    return doc