def highlightBlock(self, text): if not self.enabled: return fmt = QTextCharFormat() fmt.setUnderlineColor(Qt.red) fmt.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) for word_object in re.finditer(self.WORDS, text): if not self.spellcheck.check(word_object.group()): self.setFormat(word_object.start(), word_object.end() - word_object.start(), fmt)
def highlightBlock(self, text): if not self.dict: return text = unicode(text) format = QTextCharFormat() format.setUnderlineColor(Qt.red) 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(), format)
def highlightBlock(self, text): if not self.dict: return text = unicode(text) format = QTextCharFormat() #format.setUnderlineColor(Qt.red) format.setUnderlineStyle(QTextCharFormat.DotLine) 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(), format)
def mark_nbsp(state, text, nbsp_format): ans = [] fmt = None if state.bold or state.italic: fmt = QTextCharFormat() if state.bold: fmt.setFontWeight(QFont.Bold) if state.italic: fmt.setFontItalic(True) last = 0 for m in nbsp_pat.finditer(text): ans.extend([(m.start() - last, fmt), (m.end() - m.start(), nbsp_format)]) if not ans: ans = [(len(text), fmt)] return ans
def highlightBlock(self, text): if not self.dict: return if not text: return txt = unicode(text) #if len(txt.split())==1: # txt="" #else: # txt=txt.rsplit(' ',1)[0] format = QTextCharFormat() format.setUnderlineColor(Qt.red) format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) unicode_pattern=regex.compile(self.pattern,regex.UNICODE) for word_object in unicode_pattern.finditer(txt): if not self.dict.spell(word_object.group().encode('utf-8')): self.setFormat(word_object.start(), word_object.end() - word_object.start(), format) self.errorcount+=1 print self.errorcount
def check_spelling(text, tlen, fmt, locale, sfmt, store_locale): split_ans = [] ppos = 0 r, a = dictionaries.recognized, split_ans.append for start, length in split_into_words_and_positions(text, lang=locale.langcode): if start > ppos: a((start - ppos, fmt)) ppos = start + length recognized = r(text[start:ppos], locale) if recognized: a((length, fmt)) else: if store_locale: s = QTextCharFormat(sfmt) s.setProperty(SPELL_LOCALE_PROPERTY, locale) a((length, s)) else: a((length, sfmt)) if ppos < tlen: a((tlen - ppos, fmt)) return split_ans
def process_text(state, text, nbsp_format, spell_format, user_data): ans = [] fmt = None if state.is_bold or state.is_italic: fmt = syntax_text_char_format() if state.is_bold: fmt.setFontWeight(QFont.Bold) if state.is_italic: fmt.setFontItalic(True) last = 0 for m in nbsp_pat.finditer(text): ans.extend([(m.start() - last, fmt), (m.end() - m.start(), nbsp_format)]) last = m.end() if not ans: ans = [(len(text), fmt)] elif last < len(text): ans.append((len(text) - last, fmt)) if do_spell_check and state.tags and user_data.tag_ok_for_spell( state.tags[-1].name): split_ans = [] locale = state.current_lang or dictionaries.default_locale sfmt = QTextCharFormat(spell_format) if fmt is not None: sfmt.merge(fmt) tpos = 0 for tlen, fmt in ans: if fmt is nbsp_format: split_ans.append((tlen, fmt)) else: split_ans.extend( check_spelling(text[tpos:tpos + tlen], tlen, fmt, locale, sfmt, store_locale.enabled)) tpos += tlen ans = split_ans return ans
def check_spelling(text, tlen, fmt, locale, sfmt, store_locale): split_ans = [] ppos = 0 r, a = dictionaries.recognized, split_ans.append for start, length in split_into_words_and_positions( text, lang=locale.langcode): if start > ppos: a((start - ppos, fmt)) ppos = start + length recognized = r(text[start:ppos], locale) if recognized: a((length, fmt)) else: if store_locale: s = QTextCharFormat(sfmt) s.setProperty(SPELL_LOCALE_PROPERTY, locale) a((length, s)) else: a((length, sfmt)) if ppos < tlen: a((tlen - ppos, fmt)) return split_ans
def process_text(state, text, nbsp_format, spell_format, user_data): ans = [] fmt = None if state.is_bold or state.is_italic: fmt = syntax_text_char_format() if state.is_bold: fmt.setFontWeight(QFont.Bold) if state.is_italic: fmt.setFontItalic(True) last = 0 for m in nbsp_pat.finditer(text): ans.extend([(m.start() - last, fmt), (m.end() - m.start(), nbsp_format)]) last = m.end() if not ans: ans = [(len(text), fmt)] elif last < len(text): ans.append((len(text) - last, fmt)) if do_spell_check and state.tags and user_data.tag_ok_for_spell(state.tags[-1].name): split_ans = [] locale = state.current_lang or dictionaries.default_locale sfmt = QTextCharFormat(spell_format) if fmt is not None: sfmt.merge(fmt) tpos = 0 for tlen, fmt in ans: if fmt is nbsp_format: split_ans.append((tlen, fmt)) else: split_ans.extend(check_spelling(text[tpos:tpos+tlen], tlen, fmt, locale, sfmt, store_locale.enabled)) tpos += tlen ans = split_ans return ans
def highlightBlock(self, text): text = unicode(text) tformat = QTextCharFormat() tformat.setUnderlineColor(Qt.red) tformat.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) for word_object in re.finditer(self.WORDS, text): if not self.spell_dict.check(word_object.group()): self.setFormat(word_object.start(), word_object.end() - word_object.start(), tformat)
def highlightBlock(self, text): if not self.dict: return if not text: return txt = unicode(text) #if len(txt.split())==1: # txt="" #else: # txt=txt.rsplit(' ',1)[0] format = QTextCharFormat() format.setUnderlineColor(Qt.red) format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) unicode_pattern = regex.compile(self.pattern, regex.UNICODE) for word_object in unicode_pattern.finditer(txt): if not self.dict.spell(word_object.group().encode('utf-8')): self.setFormat(word_object.start(), word_object.end() - word_object.start(), format) self.errorcount += 1 print self.errorcount
def initializeFormats(cls): Config = cls.Config baseFormat = QTextCharFormat() baseFormat.setFontFamily(Config["fontfamily"]) baseFormat.setFontPointSize(Config["fontsize"]) for name in ("normal", "keyword", "builtin", "constant", "decorator", "comment", "string", "number", "error", "pyqt"): format = QTextCharFormat(baseFormat) format.setForeground(QColor(Config["%sfontcolor" % name])) if Config["%sfontbold" % name]: format.setFontWeight(QFont.Bold) format.setFontItalic(Config["%sfontitalic" % name]) PythonHighlighter.Formats[name] = format
def highlight_to_char_format(h): ans = QTextCharFormat() if h.bold: ans.setFontWeight(QFont.Bold) if h.italic: ans.setFontItalic(True) if h.fg is not None: ans.setForeground(h.fg) if h.bg is not None: ans.setBackground(h.bg) if h.underline is not None: ans.setUnderlineStyle(underline_styles[h.underline]) if h.underline_color is not None: ans.setUnderlineColor(h.underline_color.color()) return ans
def spell_property(sfmt, locale): s = QTextCharFormat(sfmt) s.setProperty(SPELL_LOCALE_PROPERTY, locale) return s
def setTheme(self, theme): self.theme = theme self.MARKDOWN_KWS_FORMAT = {} pal = self.parent.palette() pal.setColor(QPalette.Base, QColor(theme['background-color'])) self.parent.setPalette(pal) self.parent.setTextColor(QColor(theme['color'])) format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['bold']['color']))) format.setFontWeight(QFont.Bold if theme['bold']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['bold']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['Bold'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['bold']['color']))) format.setFontWeight(QFont.Bold if theme['bold']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['bold']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['uBold'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['emphasis']['color']))) format.setFontWeight(QFont.Bold if theme['emphasis']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['emphasis']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['Italic'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['emphasis']['color']))) format.setFontWeight(QFont.Bold if theme['emphasis']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['emphasis']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['uItalic'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['link']['color']))) format.setFontWeight(QFont.Bold if theme['link']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['link']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['Link'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['image']['color']))) format.setFontWeight(QFont.Bold if theme['image']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['image']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['Image'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['header']['color']))) format.setFontWeight(QFont.Bold if theme['header']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['header']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['Header'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['header']['color']))) format.setFontWeight(QFont.Bold if theme['header']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['header']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['HeaderAtx'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['unorderedlist']['color']))) format.setFontWeight(QFont.Bold if theme['unorderedlist']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['unorderedlist']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['UnorderedList'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['orderedlist']['color']))) format.setFontWeight(QFont.Bold if theme['orderedlist']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['orderedlist']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['OrderedList'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['blockquote']['color']))) format.setFontWeight(QFont.Bold if theme['blockquote']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['blockquote']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['BlockQuote'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['codespan']['color']))) format.setFontWeight(QFont.Bold if theme['codespan']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['codespan']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['CodeSpan'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['codeblock']['color']))) format.setFontWeight(QFont.Bold if theme['codeblock']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['codeblock']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['CodeBlock'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['line']['color']))) format.setFontWeight(QFont.Bold if theme['line']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['line']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['HR'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['line']['color']))) format.setFontWeight(QFont.Bold if theme['line']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['line']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['eHR'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['html']['color']))) format.setFontWeight(QFont.Bold if theme['html']['font-weight']=='bold' else QFont.Normal) format.setFontItalic(True if theme['html']['font-style']=='italic' else False) self.MARKDOWN_KWS_FORMAT['HTML'] = format self.rehighlight()
def __init__(self, right=False, parent=None, show_open_in_editor=False): PlainTextEdit.__init__(self, parent) self.setFrameStyle(0) self.show_open_in_editor = show_open_in_editor self.side_margin = 0 self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.show_context_menu) self.setFocusPolicy(Qt.NoFocus) self.right = right self.setReadOnly(True) w = self.fontMetrics() self.number_width = max(map(lambda x:w.width(str(x)), xrange(10))) self.space_width = w.width(' ') self.setLineWrapMode(self.WidgetWidth) self.setTabStopWidth(tprefs['editor_tab_stop_width'] * self.space_width) font = self.font() ff = tprefs['editor_font_family'] if ff is None: ff = default_font_family() font.setFamily(ff) font.setPointSize(tprefs['editor_font_size']) self.setFont(font) font = self.heading_font = QFont(self.font()) font.setPointSize(int(tprefs['editor_font_size'] * 1.5)) font.setBold(True) theme = get_theme() pal = self.palette() pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg')) pal.setColor(pal.AlternateBase, theme_color(theme, 'CursorLine', 'bg')) pal.setColor(pal.Text, theme_color(theme, 'Normal', 'fg')) pal.setColor(pal.Highlight, theme_color(theme, 'Visual', 'bg')) pal.setColor(pal.HighlightedText, theme_color(theme, 'Visual', 'fg')) self.setPalette(pal) self.viewport().setCursor(Qt.ArrowCursor) self.line_number_area = LineNumbers(self) self.blockCountChanged[int].connect(self.update_line_number_area_width) self.updateRequest.connect(self.update_line_number_area) self.line_number_palette = pal = QPalette() pal.setColor(pal.Base, theme_color(theme, 'LineNr', 'bg')) pal.setColor(pal.Text, theme_color(theme, 'LineNr', 'fg')) pal.setColor(pal.BrightText, theme_color(theme, 'LineNrC', 'fg')) self.line_number_map = LineNumberMap() self.search_header_pos = 0 self.changes, self.headers, self.images = [], [], OrderedDict() self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff), self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.diff_backgrounds = { 'replace' : theme_color(theme, 'DiffReplace', 'bg'), 'insert' : theme_color(theme, 'DiffInsert', 'bg'), 'delete' : theme_color(theme, 'DiffDelete', 'bg'), 'replacereplace': theme_color(theme, 'DiffReplaceReplace', 'bg'), 'boundary': QBrush(theme_color(theme, 'Normal', 'fg'), Qt.Dense7Pattern), } self.diff_foregrounds = { 'replace' : theme_color(theme, 'DiffReplace', 'fg'), 'insert' : theme_color(theme, 'DiffInsert', 'fg'), 'delete' : theme_color(theme, 'DiffDelete', 'fg'), 'boundary': QColor(0, 0, 0, 0), } for x in ('replacereplace', 'insert', 'delete'): f = QTextCharFormat() f.setBackground(self.diff_backgrounds[x]) setattr(self, '%s_format' % x, f)
def __init__(self, *args): QTextCharFormat.__init__(self, *args) self.setProperty(SYNTAX_PROPERTY, True)
def syntax_text_char_format(*args): ans = QTextCharFormat(*args) ans.setProperty(SYNTAX_PROPERTY, True) return ans
def __init__(self, right=False, parent=None, show_open_in_editor=False): PlainTextEdit.__init__(self, parent) self.setFrameStyle(0) self.show_open_in_editor = show_open_in_editor self.side_margin = 0 self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.show_context_menu) self.setFocusPolicy(Qt.NoFocus) self.right = right self.setReadOnly(True) w = self.fontMetrics() self.number_width = max(map(lambda x: w.width(str(x)), xrange(10))) self.space_width = w.width(' ') self.setLineWrapMode(self.WidgetWidth) self.setTabStopWidth(tprefs['editor_tab_stop_width'] * self.space_width) font = self.font() ff = tprefs['editor_font_family'] if ff is None: ff = default_font_family() font.setFamily(ff) font.setPointSize(tprefs['editor_font_size']) self.setFont(font) font = self.heading_font = QFont(self.font()) font.setPointSize(int(tprefs['editor_font_size'] * 1.5)) font.setBold(True) theme = get_theme(tprefs['editor_theme']) pal = self.palette() pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg')) pal.setColor(pal.AlternateBase, theme_color(theme, 'CursorLine', 'bg')) pal.setColor(pal.Text, theme_color(theme, 'Normal', 'fg')) pal.setColor(pal.Highlight, theme_color(theme, 'Visual', 'bg')) pal.setColor(pal.HighlightedText, theme_color(theme, 'Visual', 'fg')) self.setPalette(pal) self.viewport().setCursor(Qt.ArrowCursor) self.line_number_area = LineNumbers(self) self.blockCountChanged[int].connect(self.update_line_number_area_width) self.updateRequest.connect(self.update_line_number_area) self.line_number_palette = pal = QPalette() pal.setColor(pal.Base, theme_color(theme, 'LineNr', 'bg')) pal.setColor(pal.Text, theme_color(theme, 'LineNr', 'fg')) pal.setColor(pal.BrightText, theme_color(theme, 'LineNrC', 'fg')) self.line_number_map = LineNumberMap() self.search_header_pos = 0 self.changes, self.headers, self.images = [], [], OrderedDict() self.setVerticalScrollBarPolicy( Qt.ScrollBarAlwaysOff), self.setHorizontalScrollBarPolicy( Qt.ScrollBarAlwaysOff) self.diff_backgrounds = { 'replace': theme_color(theme, 'DiffReplace', 'bg'), 'insert': theme_color(theme, 'DiffInsert', 'bg'), 'delete': theme_color(theme, 'DiffDelete', 'bg'), 'replacereplace': theme_color(theme, 'DiffReplaceReplace', 'bg'), 'boundary': QBrush(theme_color(theme, 'Normal', 'fg'), Qt.Dense7Pattern), } self.diff_foregrounds = { 'replace': theme_color(theme, 'DiffReplace', 'fg'), 'insert': theme_color(theme, 'DiffInsert', 'fg'), 'delete': theme_color(theme, 'DiffDelete', 'fg'), 'boundary': QColor(0, 0, 0, 0), } for x in ('replacereplace', 'insert', 'delete'): f = QTextCharFormat() f.setBackground(self.diff_backgrounds[x]) setattr(self, '%s_format' % x, f)
from PyQt4.Qt import QTextDocument, QTextCursor, QTextCharFormat, QPlainTextDocumentLayout from calibre.gui2.tweak_book import tprefs from calibre.gui2.tweak_book.editor.text import get_highlighter as calibre_highlighter, SyntaxHighlighter from calibre.gui2.tweak_book.editor.themes import THEMES, default_theme, highlight_to_char_format def get_theme(): theme = THEMES.get(tprefs['editor_theme'], None) if theme is None: theme = THEMES[default_theme()] return theme NULL_FMT = QTextCharFormat() class QtHighlighter(QTextDocument): def __init__(self, parent, text, hlclass): QTextDocument.__init__(self, parent) self.l = QPlainTextDocumentLayout(self) self.setDocumentLayout(self.l) self.highlighter = hlclass(self) self.highlighter.apply_theme(get_theme()) self.highlighter.setDocument(self) self.setPlainText(text) def copy_lines(self, lo, hi, cursor): ''' Copy specified lines from the syntax highlighted buffer into the destination cursor, preserving all formatting created by the syntax
def initializeFormats(self): Config = self.Config Config["fontfamily"] = "monospace" #Config["fontsize"] = 10 for name, color, bold, italic in ( ("normal", "#000000", False, False), ("keyword", "#000080", True, False), ("builtin", "#0000A0", False, False), ("comment", "#007F00", False, True), ("string", "#808000", False, False), ("number", "#924900", False, False), ("lparen", "#000000", True, True), ("rparen", "#000000", True, True)): Config["%sfontcolor" % name] = color Config["%sfontbold" % name] = bold Config["%sfontitalic" % name] = italic baseFormat = QTextCharFormat() baseFormat.setFontFamily(Config["fontfamily"]) #baseFormat.setFontPointSize(Config["fontsize"]) for name in ("normal", "keyword", "builtin", "comment", "string", "number", "lparen", "rparen"): format = QTextCharFormat(baseFormat) format.setForeground(QColor(Config["%sfontcolor" % name])) if Config["%sfontbold" % name]: format.setFontWeight(QFont.Bold) format.setFontItalic(Config["%sfontitalic" % name]) self.Formats[name] = format
def base_fmt(self): fmt = QTextCharFormat() fmt.setFontFamily('monospace') return fmt