From 9c35237c50b9da0a6d3b3b15d0f40a6880faea68 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 2 Aug 2012 19:32:55 +0530 Subject: [PATCH] E-book viewer: Add simple settings for text and background colors --- src/calibre/gui2/viewer/config.py | 40 +++++++++++- src/calibre/gui2/viewer/config.ui | 81 +++++++++++++++++++++++-- src/calibre/gui2/viewer/documentview.py | 13 +++- 3 files changed, 126 insertions(+), 8 deletions(-) diff --git a/src/calibre/gui2/viewer/config.py b/src/calibre/gui2/viewer/config.py index 383699ff98..5bc4ae6a05 100644 --- a/src/calibre/gui2/viewer/config.py +++ b/src/calibre/gui2/viewer/config.py @@ -8,8 +8,9 @@ __copyright__ = '2012, Kovid Goyal ' __docformat__ = 'restructuredtext en' import zipfile +from functools import partial -from PyQt4.Qt import QFont, QVariant, QDialog +from PyQt4.Qt import QFont, QVariant, QDialog, Qt, QColor, QColorDialog from calibre.constants import iswindows, isxp from calibre.utils.config import Config, StringConfig @@ -58,6 +59,8 @@ def config(defaults=None): c.add_opt('top_margin', default=20) c.add_opt('side_margin', default=40) c.add_opt('bottom_margin', default=20) + c.add_opt('text_color', default=None) + c.add_opt('background_color', default=None) fonts = c.add_group('FONTS', _('Font options')) fonts('serif_family', default='Times New Roman' if iswindows else 'Liberation Serif', @@ -130,6 +133,39 @@ class ConfigDialog(QDialog, Ui_Dialog): for x in ('top', 'bottom', 'side'): getattr(self, 'opt_%s_margin'%x).setValue(getattr(opts, x+'_margin')) + for x in ('text', 'background'): + setattr(self, 'current_%s_color'%x, getattr(opts, '%s_color'%x)) + getattr(self, 'change_%s_color_button'%x).clicked.connect( + partial(self.change_color, x, reset=False)) + getattr(self, 'reset_%s_color_button'%x).clicked.connect( + partial(self.change_color, x, reset=True)) + self.update_sample_colors() + + def change_color(self, which, reset=False): + if reset: + setattr(self, 'current_%s_color'%which, None) + else: + initial = getattr(self, 'current_%s_color'%which) + if initial: + initial = QColor(initial) + else: + initial = Qt.black if which == 'text' else Qt.white + title = (_('Choose text color') if which == 'text' else + _('Choose background color')) + col = QColorDialog.getColor(initial, self, + title, QColorDialog.ShowAlphaChannel) + if col.isValid(): + name = unicode(col.name()) + setattr(self, 'current_%s_color'%which, name) + self.update_sample_colors() + + def update_sample_colors(self): + for x in ('text', 'background'): + val = getattr(self, 'current_%s_color'%x) + if not val: val = 'inherit' if x == 'text' else 'transparent' + ss = 'QLabel { %s: %s }'%('background-color' if x == 'background' + else 'color', val) + getattr(self, '%s_color_sample'%x).setStyleSheet(ss) def accept(self, *args): if self.shortcut_config.is_editing: @@ -165,6 +201,8 @@ class ConfigDialog(QDialog, Ui_Dialog): c.set('cols_per_screen', int(self.opt_cols_per_screen.value())) c.set('use_book_margins', not self.opt_override_book_margins.isChecked()) + c.set('text_color', self.current_text_color) + c.set('background_color', self.current_background_color) for x in ('top', 'bottom', 'side'): c.set(x+'_margin', int(getattr(self, 'opt_%s_margin'%x).value())) return QDialog.accept(self, *args) diff --git a/src/calibre/gui2/viewer/config.ui b/src/calibre/gui2/viewer/config.ui index a3c74681c1..19a118c7ba 100644 --- a/src/calibre/gui2/viewer/config.ui +++ b/src/calibre/gui2/viewer/config.ui @@ -58,7 +58,7 @@ QToolBox::tab:hover { 0 0 811 - 384 + 352 @@ -208,7 +208,7 @@ QToolBox::tab:hover { 0 0 811 - 384 + 352 @@ -338,7 +338,7 @@ QToolBox::tab:hover { 0 0 811 - 384 + 352 @@ -380,13 +380,84 @@ QToolBox::tab:hover { + + + Colors and backgrounds + + + + + + Background color: + + + + + + + + + Sample + + + + + + + Change + + + + + + + Reset + + + + + + + + + Text color: + + + + + + + + + Sample + + + + + + + Change + + + + + + + Reset + + + + + + + 0 0 811 - 384 + 352 @@ -457,7 +528,7 @@ QToolBox::tab:hover { 0 0 811 - 384 + 352 diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index 2b494d4ecd..7273ccd762 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -115,8 +115,17 @@ class Document(QWebPage): # {{{ mf.setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) def set_user_stylesheet(self): - raw = config().parse().user_css - raw = '::selection {background:#ffff00; color:#000;}\nbody {background-color: white;}\n'+raw + opts = config().parse() + bg = opts.background_color or 'white' + brules = ['background-color: %s !important'%bg] + if opts.text_color: + brules += ['color: %s !important'%opts.text_color] + prefix = ''' + body { %s } + '''%('; '.join(brules)) + print (prefix) + raw = prefix + opts.user_css + raw = '::selection {background:#ffff00; color:#000;}\n'+raw data = 'data:text/css;charset=utf-8;base64,' data += b64encode(raw.encode('utf-8')) self.settings().setUserStyleSheetUrl(QUrl(data))