class YouTubeHighlighter(QSyntaxHighlighter): def __init__(self, parent=None): """ @param parent QObject or QTextDocument or QTextEdit or None """ super(YouTubeHighlighter, self).__init__(parent) self._format = QTextCharFormat() self._format.setForeground(Qt.blue) #self._format.setFontWeight(QFont.Bold) self._format.setFontUnderline(True) self._format.setUnderlineColor(Qt.red) self._format.setUnderlineStyle(QTextCharFormat.DashUnderline) def highlightBlock(self, text): """@reimp @protected""" for vid in self._itervids(text): index = text.index(vid) length = len(vid) self.setFormat(index, length, self._format) @staticmethod def _itervids(text): """ @param text unicode @yield str """ if text: for it in 'http://', 'www.', 'youtu.be', 'youtube.com': text = text.replace(it, '') for word in re.split(r'\s', text): vid = re.sub(r'.*v=([0-9a-zA-Z_-]+).*', r'\1', word) if re.match(r'[0-9a-zA-Z_-]+', vid): yield vid
def highlightBlock(self, text): if not self.dict: return text = unicode(text) error_format = QTextCharFormat() error_format.setUnderlineColor(Qt.red) error_format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) for word_object in re.finditer(self.WORDS, text): if not self.dict.check(word_object.group()): self.setFormat(word_object.start(), word_object.end() - word_object.start(), error_format)
def highlightBlock(self, text): if not self.dict: return text = unicode(text) error_format = QTextCharFormat() error_format.setUnderlineColor(Qt.red) error_format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) for word_object in re.finditer(self.WORDS, text): if not self.dict.check(word_object.group()): self.setFormat(word_object.start(), word_object.end() - word_object.start(), error_format)
def highlightBlock(self, text): """@reimp @public""" dic = self.__d.dic if not dic: return WORDS = "(?iu)[\w']+" #WORDS = "(?iu)[\w]+" # ' is not considered as part of a world fmt = QTextCharFormat() fmt.setUnderlineColor(Qt.red) fmt.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) for word_object in re.finditer(WORDS, text): if not dic.check(word_object.group()): self.setFormat(word_object.start(), word_object.end() - word_object.start(), fmt)
def mouseMoveEvent(self, event): if event.modifiers() & Qt.ControlModifier: self.clear_underline() cursor, line = self._get_line_cursor(event.pos()) for goto in self.gotos: if line.strip().startswith(goto): fmt = QTextCharFormat() fmt.setFontUnderline(True) fmt.setUnderlineStyle(QTextCharFormat.WaveUnderline) fmt.setUnderlineColor(QtGui.QColor('blue')) # cursor.clearSelection() cursor.select(QTextCursor.BlockUnderCursor) cursor.beginEditBlock() cursor.setCharFormat(fmt) cursor.endEditBlock() break super(myCodeWidget, self).mouseMoveEvent(event)
def mouseMoveEvent(self, event): if event.modifiers() & Qt.ControlModifier: self.clear_underline() cursor, line = self._get_line_cursor(event.pos()) for goto in self.gotos: if line.strip().startswith(goto): fmt = QTextCharFormat() fmt.setFontUnderline(True) fmt.setUnderlineStyle(QTextCharFormat.WaveUnderline) fmt.setUnderlineColor(QtGui.QColor('blue')) # cursor.clearSelection() cursor.select(QTextCursor.BlockUnderCursor) cursor.beginEditBlock() cursor.setCharFormat(fmt) cursor.endEditBlock() break super(myCodeWidget, self).mouseMoveEvent(event)
class Highlighter(QSyntaxHighlighter): def __init__(self, state, parent=None): super().__init__(parent) self.state = state self.unknownWords = [] self.wordsToIgnore = set() self.spellFormat = QTextCharFormat() self.spellFormat.setFontUnderline(True) self.spellFormat.setUnderlineColor(Qt.red) self.spellFormat.setUnderlineStyle(QTextCharFormat.WaveUnderline) def highlightBlock(self, text): self.unknownWords = [] language = self.state.language.value for match in WORD_LIKE_RX.finditer(text): word = match.group() start = match.start() length = match.end() - match.start() if (word not in self.wordsToIgnore and not Spell.check(word, language)): self.setFormat(start, length, self.spellFormat) self.unknownWords.append((start, word))