diff --git a/src/calibre/gui2/tweak_book/diff/highlight.py b/src/calibre/gui2/tweak_book/diff/highlight.py index cf9524788a..eeac053fb8 100644 --- a/src/calibre/gui2/tweak_book/diff/highlight.py +++ b/src/calibre/gui2/tweak_book/diff/highlight.py @@ -12,13 +12,7 @@ from PyQt4.Qt import QTextDocument, QTextCursor, QTextCharFormat, QPlainTextDocu 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 +from calibre.gui2.tweak_book.editor.themes import get_theme, highlight_to_char_format NULL_FMT = QTextCharFormat() @@ -29,7 +23,7 @@ class QtHighlighter(QTextDocument): self.l = QPlainTextDocumentLayout(self) self.setDocumentLayout(self.l) self.highlighter = hlclass() - self.highlighter.apply_theme(get_theme()) + self.highlighter.apply_theme(get_theme(tprefs['editor_theme'])) self.highlighter.set_document(self) self.setPlainText(text) @@ -118,7 +112,7 @@ def format_for_token(theme, cache, token): class PygmentsHighlighter(object): def __init__(self, text, lexer): - theme, cache = get_theme(), {} + 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): diff --git a/src/calibre/gui2/tweak_book/diff/view.py b/src/calibre/gui2/tweak_book/diff/view.py index e814e99e4b..1b705a2abd 100644 --- a/src/calibre/gui2/tweak_book/diff/view.py +++ b/src/calibre/gui2/tweak_book/diff/view.py @@ -26,9 +26,9 @@ from calibre import human_readable, fit_image from calibre.gui2 import info_dialog from calibre.gui2.tweak_book import tprefs from calibre.gui2.tweak_book.editor.text import PlainTextEdit, default_font_family, LineNumbers -from calibre.gui2.tweak_book.editor.themes import theme_color +from calibre.gui2.tweak_book.editor.themes import theme_color, get_theme from calibre.gui2.tweak_book.diff import get_sequence_matcher -from calibre.gui2.tweak_book.diff.highlight import get_theme, get_highlighter +from calibre.gui2.tweak_book.diff.highlight import get_highlighter Change = namedtuple('Change', 'ltop lbot rtop rbot kind') @@ -119,7 +119,7 @@ class TextBrowser(PlainTextEdit): # {{{ font = self.heading_font = QFont(self.font()) font.setPointSize(int(tprefs['editor_font_size'] * 1.5)) font.setBold(True) - theme = get_theme() + 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')) diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index c0f2b59cdd..9c0c7b1778 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -19,7 +19,7 @@ from PyQt4.Qt import ( from calibre import prepare_string_for_xml, xml_entity_to_unicode from calibre.gui2.tweak_book import tprefs, TOP from calibre.gui2.tweak_book.editor import SYNTAX_PROPERTY, SPELL_PROPERTY -from calibre.gui2.tweak_book.editor.themes import THEMES, default_theme, theme_color, theme_format +from calibre.gui2.tweak_book.editor.themes import get_theme, theme_color, theme_format from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter from calibre.gui2.tweak_book.editor.syntax.html import HTMLHighlighter, XMLHighlighter from calibre.gui2.tweak_book.editor.syntax.css import CSSHighlighter @@ -163,9 +163,7 @@ class TextEdit(PlainTextEdit): 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 = THEMES.get(prefs['editor_theme'], None) - if theme is None: - theme = THEMES[default_theme()] + theme = get_theme(prefs['editor_theme']) self.apply_theme(theme) w = self.fontMetrics() self.space_width = w.width(' ') diff --git a/src/calibre/gui2/tweak_book/editor/themes.py b/src/calibre/gui2/tweak_book/editor/themes.py index 0b666007fa..ec4d102875 100644 --- a/src/calibre/gui2/tweak_book/editor/themes.py +++ b/src/calibre/gui2/tweak_book/editor/themes.py @@ -226,6 +226,12 @@ def u(x): return x + 'Underline' underline_styles = {x:getattr(SyntaxTextCharFormat, u(x)) for x in underline_styles} +def get_theme(name): + try: + return THEMES[name] + except KeyError: + return THEMES[default_theme()] + def highlight_to_char_format(h): ans = SyntaxTextCharFormat() if h.bold: diff --git a/src/calibre/gui2/tweak_book/live_css.py b/src/calibre/gui2/tweak_book/live_css.py index d3dcf815f6..9273eb9949 100644 --- a/src/calibre/gui2/tweak_book/live_css.py +++ b/src/calibre/gui2/tweak_book/live_css.py @@ -16,7 +16,7 @@ from PyQt4.Qt import ( from calibre.constants import iswindows from calibre.gui2.tweak_book import editors, actions, current_container, tprefs -from calibre.gui2.tweak_book.editor.themes import THEMES, default_theme, theme_color +from calibre.gui2.tweak_book.editor.themes import get_theme, theme_color from calibre.gui2.tweak_book.editor.text import default_font_family class Heading(QWidget): # {{{ @@ -359,9 +359,7 @@ class LiveCSS(QWidget): f.setFamily(tprefs['editor_font_family'] or default_font_family()) f.setPointSize(tprefs['editor_font_size']) self.setFont(f) - theme = THEMES.get(tprefs['editor_theme'], None) - if theme is None: - theme = THEMES[default_theme()] + 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'))