Refactor editor theme management

This commit is contained in:
Kovid Goyal 2014-05-25 09:29:35 +05:30
parent 9254dfeb0d
commit 5a155f3fe5
5 changed files with 16 additions and 20 deletions

View File

@ -12,13 +12,7 @@ from PyQt4.Qt import QTextDocument, QTextCursor, QTextCharFormat, QPlainTextDocu
from calibre.gui2.tweak_book import tprefs 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.text import get_highlighter as calibre_highlighter, SyntaxHighlighter
from calibre.gui2.tweak_book.editor.themes import THEMES, default_theme, highlight_to_char_format from calibre.gui2.tweak_book.editor.themes import get_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() NULL_FMT = QTextCharFormat()
@ -29,7 +23,7 @@ class QtHighlighter(QTextDocument):
self.l = QPlainTextDocumentLayout(self) self.l = QPlainTextDocumentLayout(self)
self.setDocumentLayout(self.l) self.setDocumentLayout(self.l)
self.highlighter = hlclass() self.highlighter = hlclass()
self.highlighter.apply_theme(get_theme()) self.highlighter.apply_theme(get_theme(tprefs['editor_theme']))
self.highlighter.set_document(self) self.highlighter.set_document(self)
self.setPlainText(text) self.setPlainText(text)
@ -118,7 +112,7 @@ def format_for_token(theme, cache, token):
class PygmentsHighlighter(object): class PygmentsHighlighter(object):
def __init__(self, text, lexer): 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 = {k:highlight_to_char_format(v) for k, v in theme.iteritems()}
theme[None] = NULL_FMT theme[None] = NULL_FMT
def fmt(token): def fmt(token):

View File

@ -26,9 +26,9 @@ from calibre import human_readable, fit_image
from calibre.gui2 import info_dialog from calibre.gui2 import info_dialog
from calibre.gui2.tweak_book import tprefs 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.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 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') Change = namedtuple('Change', 'ltop lbot rtop rbot kind')
@ -119,7 +119,7 @@ class TextBrowser(PlainTextEdit): # {{{
font = self.heading_font = QFont(self.font()) font = self.heading_font = QFont(self.font())
font.setPointSize(int(tprefs['editor_font_size'] * 1.5)) font.setPointSize(int(tprefs['editor_font_size'] * 1.5))
font.setBold(True) font.setBold(True)
theme = get_theme() theme = get_theme(tprefs['editor_theme'])
pal = self.palette() pal = self.palette()
pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg')) pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg'))
pal.setColor(pal.AlternateBase, theme_color(theme, 'CursorLine', 'bg')) pal.setColor(pal.AlternateBase, theme_color(theme, 'CursorLine', 'bg'))

View File

@ -19,7 +19,7 @@ from PyQt4.Qt import (
from calibre import prepare_string_for_xml, xml_entity_to_unicode from calibre import prepare_string_for_xml, xml_entity_to_unicode
from calibre.gui2.tweak_book import tprefs, TOP 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 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.base import SyntaxHighlighter
from calibre.gui2.tweak_book.editor.syntax.html import HTMLHighlighter, XMLHighlighter from calibre.gui2.tweak_book.editor.syntax.html import HTMLHighlighter, XMLHighlighter
from calibre.gui2.tweak_book.editor.syntax.css import CSSHighlighter 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): # {{{ def apply_settings(self, prefs=None, dictionaries_changed=False): # {{{
prefs = prefs or tprefs prefs = prefs or tprefs
self.setLineWrapMode(QPlainTextEdit.WidgetWidth if prefs['editor_line_wrap'] else QPlainTextEdit.NoWrap) self.setLineWrapMode(QPlainTextEdit.WidgetWidth if prefs['editor_line_wrap'] else QPlainTextEdit.NoWrap)
theme = THEMES.get(prefs['editor_theme'], None) theme = get_theme(prefs['editor_theme'])
if theme is None:
theme = THEMES[default_theme()]
self.apply_theme(theme) self.apply_theme(theme)
w = self.fontMetrics() w = self.fontMetrics()
self.space_width = w.width(' ') self.space_width = w.width(' ')

View File

@ -226,6 +226,12 @@ def u(x):
return x + 'Underline' return x + 'Underline'
underline_styles = {x:getattr(SyntaxTextCharFormat, u(x)) for x in underline_styles} 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): def highlight_to_char_format(h):
ans = SyntaxTextCharFormat() ans = SyntaxTextCharFormat()
if h.bold: if h.bold:

View File

@ -16,7 +16,7 @@ from PyQt4.Qt import (
from calibre.constants import iswindows from calibre.constants import iswindows
from calibre.gui2.tweak_book import editors, actions, current_container, tprefs 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 from calibre.gui2.tweak_book.editor.text import default_font_family
class Heading(QWidget): # {{{ class Heading(QWidget): # {{{
@ -359,9 +359,7 @@ class LiveCSS(QWidget):
f.setFamily(tprefs['editor_font_family'] or default_font_family()) f.setFamily(tprefs['editor_font_family'] or default_font_family())
f.setPointSize(tprefs['editor_font_size']) f.setPointSize(tprefs['editor_font_size'])
self.setFont(f) self.setFont(f)
theme = THEMES.get(tprefs['editor_theme'], None) theme = get_theme(tprefs['editor_theme'])
if theme is None:
theme = THEMES[default_theme()]
pal = self.palette() pal = self.palette()
pal.setColor(pal.Window, theme_color(theme, 'Normal', 'bg')) pal.setColor(pal.Window, theme_color(theme, 'Normal', 'bg'))
pal.setColor(pal.WindowText, theme_color(theme, 'Normal', 'fg')) pal.setColor(pal.WindowText, theme_color(theme, 'Normal', 'fg'))