def profile(): import sys from qt.core import QTextDocument from calibre.gui2 import Application from calibre.gui2.tweak_book import set_book_locale from calibre.gui2.tweak_book.editor.themes import get_theme app = Application([]) set_book_locale('en') with open(sys.argv[-2], 'rb') as f: raw = f.read().decode('utf-8') doc = QTextDocument() doc.setPlainText(raw) h = Highlighter() theme = get_theme(tprefs['editor_theme']) h.apply_theme(theme) h.set_document(doc) h.join() import cProfile print('Running profile on', sys.argv[-2]) h.rehighlight() cProfile.runctx('h.join()', {}, {'h': h}, sys.argv[-1]) print('Stats saved to:', sys.argv[-1]) del h del doc del app
def profile(): import sys from PyQt5.Qt import QTextDocument from calibre.gui2 import Application from calibre.gui2.tweak_book import set_book_locale from calibre.gui2.tweak_book.editor.themes import get_theme app = Application([]) set_book_locale("en") raw = open(sys.argv[-2], "rb").read().decode("utf-8") doc = QTextDocument() doc.setPlainText(raw) h = HTMLHighlighter() theme = get_theme(tprefs["editor_theme"]) h.apply_theme(theme) h.set_document(doc) h.join() import cProfile print("Running profile on", sys.argv[-2]) h.rehighlight() cProfile.runctx("h.join()", {}, {"h": h}, sys.argv[-1]) print("Stats saved to:", sys.argv[-1]) del h del doc del app
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) self.setLineWrapMode(self.WidgetWidth) 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) self.calculate_metrics() self.setTabStopWidth(tprefs['editor_tab_stop_width'] * self.space_width) 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)
def __init__(self, parent, text, hlclass): QTextDocument.__init__(self, parent) self.l = QPlainTextDocumentLayout(self) self.setDocumentLayout(self.l) self.highlighter = hlclass() self.highlighter.apply_theme(get_theme(tprefs['editor_theme'])) self.highlighter.set_document(self) self.setPlainText(text)
def apply_settings(self, prefs=None, dictionaries_changed=False): # {{{ prefs = prefs or tprefs self.setLineWrapMode(QPlainTextEdit.WidgetWidth if prefs["editor_line_wrap"] else QPlainTextEdit.NoWrap) theme = get_theme(prefs["editor_theme"]) self.apply_theme(theme) w = self.fontMetrics() self.space_width = w.width(" ") self.setTabStopWidth(prefs["editor_tab_stop_width"] * self.space_width) if dictionaries_changed: self.highlighter.rehighlight()
def apply_settings(self, prefs=None, dictionaries_changed=False): # {{{ prefs = prefs or tprefs self.setLineWrapMode(QPlainTextEdit.WidgetWidth if prefs['editor_line_wrap'] else QPlainTextEdit.NoWrap) theme = get_theme(prefs['editor_theme']) self.apply_theme(theme) w = self.fontMetrics() self.space_width = w.width(' ') self.setTabStopWidth(prefs['editor_tab_stop_width'] * self.space_width) if dictionaries_changed: self.highlighter.rehighlight()
def apply_settings(self, prefs=None, dictionaries_changed=False): # {{{ prefs = prefs or tprefs self.setLineWrapMode(QPlainTextEdit.WidgetWidth if prefs['editor_line_wrap'] else QPlainTextEdit.NoWrap) theme = get_theme(prefs['editor_theme']) self.apply_theme(theme) w = self.fontMetrics() self.space_width = w.width(' ') self.tw = self.smarts.override_tab_stop_width if self.smarts.override_tab_stop_width is not None else prefs['editor_tab_stop_width'] self.setTabStopWidth(self.tw * self.space_width) if dictionaries_changed: self.highlighter.rehighlight()
def apply_settings(self, prefs=None, dictionaries_changed=False): # {{{ prefs = prefs or tprefs self.setAcceptDrops(prefs.get('editor_accepts_drops', True)) self.setLineWrapMode(QPlainTextEdit.WidgetWidth if prefs['editor_line_wrap'] else QPlainTextEdit.NoWrap) theme = get_theme(prefs['editor_theme']) self.apply_theme(theme) w = self.fontMetrics() self.space_width = w.width(' ') self.tw = self.smarts.override_tab_stop_width if self.smarts.override_tab_stop_width is not None else prefs['editor_tab_stop_width'] self.setTabStopWidth(self.tw * self.space_width) if dictionaries_changed: self.highlighter.rehighlight()
def apply_theme(self): f = self.font() f.setFamily(tprefs['editor_font_family'] or default_font_family()) f.setPointSize(tprefs['editor_font_size']) self.setFont(f) theme = get_theme(tprefs['editor_theme']) pal = self.palette() pal.setColor(pal.Window, theme_color(theme, 'Normal', 'bg')) pal.setColor(pal.WindowText, theme_color(theme, 'Normal', 'fg')) pal.setColor(pal.AlternateBase, theme_color(theme, 'HighlightRegion', 'bg')) pal.setColor(pal.LinkVisited, theme_color(theme, 'Keyword', 'fg')) self.setPalette(pal) if hasattr(self, 'box'): self.box.relayout() self.update()
def apply_settings(self, prefs=None, dictionaries_changed=False): # {{{ prefs = prefs or tprefs self.setAcceptDrops(prefs.get("editor_accepts_drops", True)) self.setLineWrapMode(QPlainTextEdit.WidgetWidth if prefs["editor_line_wrap"] else QPlainTextEdit.NoWrap) theme = get_theme(prefs["editor_theme"]) self.apply_theme(theme) w = self.fontMetrics() self.space_width = w.width(" ") self.tw = ( self.smarts.override_tab_stop_width if self.smarts.override_tab_stop_width is not None else prefs["editor_tab_stop_width"] ) self.setTabStopWidth(self.tw * self.space_width) if dictionaries_changed: self.highlighter.rehighlight()
def apply_theme(self): f = self.font() f.setFamily(tprefs["editor_font_family"] or default_font_family()) f.setPointSize(tprefs["editor_font_size"]) self.setFont(f) theme = get_theme(tprefs["editor_theme"]) pal = self.palette() pal.setColor(pal.Window, theme_color(theme, "Normal", "bg")) pal.setColor(pal.WindowText, theme_color(theme, "Normal", "fg")) pal.setColor(pal.AlternateBase, theme_color(theme, "HighlightRegion", "bg")) pal.setColor(pal.Link, theme_color(theme, "Link", "fg")) pal.setColor(pal.LinkVisited, theme_color(theme, "Keyword", "fg")) self.setPalette(pal) if hasattr(self, "box"): self.box.relayout() self.update()
def __init__(self, text, lexer): theme, cache = get_theme(tprefs['editor_theme']), {} theme = {k:highlight_to_char_format(v) for k, v in theme.iteritems()} theme[None] = NULL_FMT def fmt(token): return format_for_token(theme, cache, token) from pygments import lex lines = self.lines = [[]] current_line = lines[0] for token, val in lex(text, lexer): for v in val.splitlines(True): current_line.append((fmt(token), v)) if v[-1] in '\n\r': lines.append([]) current_line = lines[-1] continue
def __init__(self, text, lexer): theme, cache = get_theme(tprefs['editor_theme']), {} theme = {k:highlight_to_char_format(v) for k, v in theme.iteritems()} theme[None] = NULL_FMT def fmt(token): return format_for_pygments_token(theme, cache, token) from pygments import lex lines = self.lines = [[]] current_line = lines[0] for token, val in lex(text, lexer): for v in val.splitlines(True): current_line.append((fmt(token), v)) if v[-1] in '\n\r': lines.append([]) current_line = lines[-1] continue
def __init__(self, parent): QPlainTextEdit.__init__(self, parent) # Use the default theme from the book editor theme = get_theme(None) self.line_number_palette = pal = QPalette() pal.setColor(QPalette.ColorRole.Base, theme_color(theme, 'LineNr', 'bg')) pal.setColor(QPalette.ColorRole.Text, theme_color(theme, 'LineNr', 'fg')) pal.setColor(QPalette.ColorRole.BrightText, theme_color(theme, 'LineNrC', 'fg')) self.line_number_area = LineNumberArea(self) self.blockCountChanged.connect(self.update_line_number_area_width) self.updateRequest.connect(self.update_line_number_area) self.cursorPositionChanged.connect(self.highlight_cursor_line) self.update_line_number_area_width(0) self.highlight_cursor_line() self.clicked_line_numbers = set()
def profile(): import sys from PyQt5.Qt import QTextDocument from calibre.gui2 import Application from calibre.gui2.tweak_book import set_book_locale from calibre.gui2.tweak_book.editor.themes import get_theme app = Application([]) set_book_locale('en') raw = open(sys.argv[-2], 'rb').read().decode('utf-8') doc = QTextDocument() doc.setPlainText(raw) h = Highlighter() theme = get_theme(tprefs['editor_theme']) h.apply_theme(theme) h.set_document(doc) h.join() import cProfile print ('Running profile on', sys.argv[-2]) h.rehighlight() cProfile.runctx('h.join()', {}, {'h':h}, sys.argv[-1]) print ('Stats saved to:', sys.argv[-1]) del h del doc del app