def _get_format_from_style(self, token, style): """ Returns a QTextCharFormat for token by reading a Pygments style. """ result = QtGui.QTextCharFormat() items = list(style.style_for_token(token).items()) for key, value in items: if value is None and key == 'color': # make sure to use a default visible color for the foreground # brush value = drift_color(self.background, 1000).name() if value: if key == 'color': result.setForeground(self._get_brush(value)) elif key == 'bgcolor': result.setBackground(self._get_brush(value)) elif key == 'bold': result.setFontWeight(QtGui.QFont.Bold) elif key == 'italic': result.setFontItalic(value) elif key == 'underline': result.setUnderlineStyle( QtGui.QTextCharFormat.SingleUnderline) elif key == 'sans': result.setFontStyleHint(QtGui.QFont.SansSerif) elif key == 'roman': result.setFontStyleHint(QtGui.QFont.Times) elif key == 'mono': result.setFontStyleHint(QtGui.QFont.TypeWriter) if token in [ Token.Literal.String, Token.Literal.String.Doc, Token.Comment ]: # mark strings, comments and docstrings regions for further queries result.setObjectType(result.UserObject) return result
def highlight_disabled_columns(self, text): fmt = QtGui.QTextCharFormat() fmt.setForeground(QtGui.QBrush(self.editor.whitespaces_foreground)) try: self.setFormat(0, 6 if text[6] in ['*', '-'] else 7, fmt) except IndexError: self.setFormat(0, len(text), fmt)
def _get_format_from_style(self, token, style): """ Returns a QTextCharFormat for token by reading a Pygments style. """ result = QtGui.QTextCharFormat() try: style = style.style_for_token(token) except KeyError: # fallback to plain text style = style.style_for_token(Text) for key, value in list(style.items()): if value: if key == 'color': result.setForeground(self._get_brush(value)) elif key == 'bgcolor': result.setBackground(self._get_brush(value)) elif key == 'bold': result.setFontWeight(QtGui.QFont.Bold) elif key == 'italic': result.setFontItalic(True) elif key == 'underline': result.setUnderlineStyle( QtGui.QTextCharFormat.SingleUnderline) elif key == 'sans': result.setFontStyleHint(QtGui.QFont.SansSerif) elif key == 'roman': result.setFontStyleHint(QtGui.QFont.Times) elif key == 'mono': result.setFontStyleHint(QtGui.QFont.TypeWriter) return result
def write(text_edit, text, color): text_edit.moveCursor(QtGui.QTextCursor.End) fmt = QtGui.QTextCharFormat() fmt.setUnderlineStyle(QtGui.QTextCharFormat.NoUnderline) fmt.setForeground(QtGui.QBrush(color)) text_edit.setCurrentCharFormat(fmt) text_edit.insertPlainText(text) text_edit.moveCursor(QtGui.QTextCursor.End)
def _check_formats(self): """ Make sure italic/bold formats exist (the format map may be reset if user changed color scheme). """ base_fmt = self.formats['normal'] try: self.formats['italic'] except KeyError: italic_fmt = QtGui.QTextCharFormat(base_fmt) italic_fmt.setFontItalic(True) self.formats['italic'] = italic_fmt try: self.formats['bold'] except KeyError: bold_fmt = QtGui.QTextCharFormat(base_fmt) bold_fmt.setFontWeight(QtGui.QFont.Bold) self.formats['bold'] = bold_fmt
def _highlight_whitespaces(self, text): fmt = QtGui.QTextCharFormat() fmt.setForeground(QtGui.QBrush(self.editor.whitespaces_foreground)) index = self.WHITESPACES.indexIn(text, 0) while index >= 0: index = self.WHITESPACES.pos(0) length = len(self.WHITESPACES.cap(0)) self.setFormat(index, length, self.formats['whitespace']) index = self.WHITESPACES.indexIn(text, index + length)
def highlight_block(self, text, block): if text.startswith('>>>') or text.startswith('...'): super(SyntaxHighlighter, self).highlight_block(text, block) else: match = self.editor.link_regex.search(text) if match: start, end = match.span('url') fmt = QtGui.QTextCharFormat() fmt.setForeground(QtWidgets.qApp.palette().highlight().color()) fmt.setUnderlineStyle(fmt.SingleUnderline) self.setFormat(start, end - start, fmt)
def write_with_underline(text_edit, text, color, line, start, end): text_edit.moveCursor(QtGui.QTextCursor.End) text_edit.setTextColor(color) fmt = QtGui.QTextCharFormat() fmt.setUnderlineColor(color) fmt.setUnderlineStyle(QtGui.QTextCharFormat.SingleUnderline) fmt.setForeground(QtGui.QBrush(color)) text_edit.setCurrentCharFormat(fmt) text_edit.insertPlainText(text) text_edit.moveCursor(QtGui.QTextCursor.End) block = self.document().lastBlock() data = self.UserData(text, line, start, end) block.setUserData(data)
def _get_format_from_color(self, color): fmt = QtGui.QTextCharFormat() fmt.setBackground(self._get_brush(color)) return fmt