Ejemplo n.º 1
0
    def format(self, rgb, style=''):
        """
        Return a QtGui.QTextCharFormat with the given attributes.
        """
        color = QtGui.QColor(*rgb)
        textFormat = QtGui.QTextCharFormat()
        textFormat.setForeground(color)

        if 'bold' in style:
            textFormat.setFontWeight(QtGui.QFont.Bold)
        if 'italic' in style:
            textFormat.setFontItalic(True)

        return textFormat
Ejemplo n.º 2
0
    def highlight_selected_word(self, text):
        """
        Highlight other occurences of
        the selected word in the text.
        """
        if not self.selected_word:
            return
        word = self.selected_word
        if word not in text:
            return

        extraSelections = self.editor.extraSelections()
        rgb, weight = self.theme['selected_word']
        color = QtGui.QColor(*rgb)

        block = self.currentBlock()
        if not block.isVisible():
            return

        pos = block.position()

        cursor = self.editor.textCursor()
        for m in re.finditer(word, text):
            start = m.start() + pos
            end = m.end() + pos

            selection = QtWidgets.QTextEdit.ExtraSelection()
            cursor.setPosition(start, QtGui.QTextCursor.MoveAnchor)
            cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor)
            selection.cursor = cursor

            selection.format.setForeground(QtCore.Qt.white)
            selection.format.setBackground(color)
            extraSelections.append(selection)

        self.editor.setExtraSelections(extraSelections)