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 _create_info_cursor(self): cursor = self.cursorForPosition(self._mouse_position) cursor.select(QTextCursor.WordUnderCursor) format = QTextCharFormat() format.setFontUnderline(True) format.setForeground(QBrush(QColor('blue'))) cursor.setCharFormat(format) return cursor
def formatConverterFunction(format): if format == qutepart.syntax.TextFormat(): return None # Do not apply default format. Performance optimization qtFormat = QTextCharFormat() qtFormat.setForeground(QBrush(QColor(format.color))) qtFormat.setBackground(QBrush(QColor(format.background))) qtFormat.setFontItalic(format.italic) qtFormat.setFontWeight(QFont.Bold if format.bold else QFont.Normal) qtFormat.setFontUnderline(format.underline) qtFormat.setFontStrikeOut(format.strikeOut) return qtFormat
def formatConverterFunction(format): if format == qutepart.syntax.TextFormat(): return None # Do not apply default format. Performance optimization qtFormat = QTextCharFormat() qtFormat.setForeground(QBrush(QColor(format.color))) qtFormat.setBackground(QBrush(QColor(format.background))) qtFormat.setFontItalic(format.italic) qtFormat.setFontWeight(QFont.Bold if format.bold else QFont.Normal) qtFormat.setFontUnderline(format.underline) qtFormat.setFontStrikeOut(format.strikeOut) return qtFormat
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))
class TextFormat(): def __init__(self, selection, button): self.selection = selection self.cursor = QTextCursor(self.selection.textEdit.textCursor()) self.format = QTextCharFormat() self.button = button def bold_button_behavior(self): if self.selection.textEdit.textCursor().hasSelection(): return self.bold_selection() if self.selection.textEdit.fontWeight() == QFont.Bold: self.button.setDown(False) return self.selection.textEdit.setFontWeight(QFont.Normal) self.button.setDown(True) return self.selection.textEdit.setFontWeight(QFont.Bold) def bold_selection(self): if self.selection.bold_button.isDown(): self.selection.bold_button.setDown(False) return self.selection.textEdit.setFontWeight(QFont.Normal) if not self.selection.textEdit.textCursor().charFormat().fontWeight() == QFont.Bold: self.format.setFontWeight(QFont.Bold) self.selection.bold_button.setDown(True) return self.cursor.mergeCharFormat(self.format) return self.selection.textEdit.setFontWeight(QFont.Normal) def italic_button_behavior(self): if self.selection.textEdit.textCursor().hasSelection(): return self.italic_selection() if self.selection.textEdit.fontItalic(): self.button.setDown(False) return self.selection.textEdit.setFontItalic(False) self.button.setDown(True) return self.selection.textEdit.setFontItalic(True) def italic_selection(self): if self.selection.italic_button.isDown(): self.selection.italic_button.setDown(False) return self.selection.textEdit.setFontItalic() if not self.selection.textEdit.textCursor().charFormat().fontItalic(): self.format.setFontItalic(True) return self.cursor.mergeCharFormat(self.format) self.selection.italic_button.setDown(False) return self.selection.textEdit.setFontItalic(False) def underline_button_behavior(self): if self.selection.textEdit.textCursor().hasSelection(): return self.underline_selection() if self.selection.textEdit.fontUnderline(): self.button.setDown(False) return self.selection.textEdit.setFontUnderline(False) self.button.setDown(True) return self.selection.textEdit.setFontUnderline(True) def underline_selection(self): if self.selection.underline_button.isDown(): self.selection.underline_button.setDown(False) return self.selection.textEdit.setFontUnderline(False) if not self.selection.textEdit.textCursor().charFormat().fontUnderline(): self.format.setFontUnderline(True) return self.cursor.mergeCharFormat(self.format) return self.selection.textEdit.setFontUnderline(False) def strikeout_button_behavior(self): if self.selection.textEdit.textCursor().hasSelection(): return self.strikeout_selection() if self.selection.textEdit.textCursor().charFormat().fontStrikeOut(): self.button.setDown(False) return self.selection.textEdit.textCursor().charFormat().setFontStrikeOut(False) self.button.setDown(True) formatter = QTextCharFormat() formatter.setFontStrikeOut(True) return self.selection.textEdit.setCurrentCharFormat(formatter) def strikeout_selection(self): if self.selection.textEdit.textCursor().charFormat().fontStrikeOut(): self.selection.strikeout_button.setDown(False) self.format.setFontStrikeOut(False) return self.cursor.mergeCharFormat(self.format) if not self.selection.textEdit.textCursor().charFormat().fontStrikeOut(): self.format.setFontStrikeOut(True) return self.cursor.mergeCharFormat(self.format) return self.selection.textEdit.textCursor().charFormat()
class SyntaxHighlightSetting(): """ This class contains a single Setting for a code block in the SyntaxHighlighter. Variables: - expression: The regular expression of the syntax block - expression_end: If the expression has a start and an end expression - font_size - font_color - font_weight - font_style - font_underline - use_font_size """ def __init__(self, expression, font_family, font_size, font_color, font_weight, font_style, font_underline, use_font_size, expression_end=''): self.expression = expression if expression_end != '': self.expression_end = expression_end self.font_family = font_family self.font_size = font_size self.font_color = font_color self.font_weight = font_weight self.font_style = font_style self.font_underline = font_underline self.use_font_size = use_font_size self.createFormat() def createFormat(self): """ Create a QTextCharformat and saves it in self.class_format""" self.class_format = QTextCharFormat() self.class_format.setFontFamily(self.font_family) if self.use_font_size: self.class_format.setFontPointSize(self.font_size) self.class_format.setForeground(self.font_color) self.class_format.setFontWeight(self.font_weight) self.class_format.setFontItalic(self.font_style) self.class_format.setFontUnderline(self.font_underline) def get_format(self): return self.class_format def getValues(self): return [self.expression, self.font_color, self.font_weight] def serialize(self): str1 = "" str1 += self.expression + "//" str1 += str(self.font_color) + "//" str1 += str(self.font_weight) + "//" return str1 def deserialize(self, string): splitted = string.split("//") self.expression = splitted[0] self.font_color = splitted[1] self.font_weight = splitted[2]