mirror of
				https://github.com/kovidgoyal/calibre.git
				synced 2025-11-03 19:17:02 -05:00 
			
		
		
		
	E-book viewer: Add simple settings for text and background colors
This commit is contained in:
		
							parent
							
								
									4887214c68
								
							
						
					
					
						commit
						9c35237c50
					
				@ -8,8 +8,9 @@ __copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
				
			|||||||
__docformat__ = 'restructuredtext en'
 | 
					__docformat__ = 'restructuredtext en'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import zipfile
 | 
					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.constants import iswindows, isxp
 | 
				
			||||||
from calibre.utils.config import Config, StringConfig
 | 
					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('top_margin', default=20)
 | 
				
			||||||
    c.add_opt('side_margin', default=40)
 | 
					    c.add_opt('side_margin', default=40)
 | 
				
			||||||
    c.add_opt('bottom_margin', default=20)
 | 
					    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 = c.add_group('FONTS', _('Font options'))
 | 
				
			||||||
    fonts('serif_family', default='Times New Roman' if iswindows else 'Liberation Serif',
 | 
					    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'):
 | 
					        for x in ('top', 'bottom', 'side'):
 | 
				
			||||||
            getattr(self, 'opt_%s_margin'%x).setValue(getattr(opts,
 | 
					            getattr(self, 'opt_%s_margin'%x).setValue(getattr(opts,
 | 
				
			||||||
                x+'_margin'))
 | 
					                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):
 | 
					    def accept(self, *args):
 | 
				
			||||||
        if self.shortcut_config.is_editing:
 | 
					        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('cols_per_screen', int(self.opt_cols_per_screen.value()))
 | 
				
			||||||
        c.set('use_book_margins', not
 | 
					        c.set('use_book_margins', not
 | 
				
			||||||
                self.opt_override_book_margins.isChecked())
 | 
					                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'):
 | 
					        for x in ('top', 'bottom', 'side'):
 | 
				
			||||||
            c.set(x+'_margin', int(getattr(self, 'opt_%s_margin'%x).value()))
 | 
					            c.set(x+'_margin', int(getattr(self, 'opt_%s_margin'%x).value()))
 | 
				
			||||||
        return QDialog.accept(self, *args)
 | 
					        return QDialog.accept(self, *args)
 | 
				
			||||||
 | 
				
			|||||||
@ -58,7 +58,7 @@ QToolBox::tab:hover {
 | 
				
			|||||||
            <x>0</x>
 | 
					            <x>0</x>
 | 
				
			||||||
            <y>0</y>
 | 
					            <y>0</y>
 | 
				
			||||||
            <width>811</width>
 | 
					            <width>811</width>
 | 
				
			||||||
            <height>384</height>
 | 
					            <height>352</height>
 | 
				
			||||||
           </rect>
 | 
					           </rect>
 | 
				
			||||||
          </property>
 | 
					          </property>
 | 
				
			||||||
          <attribute name="label">
 | 
					          <attribute name="label">
 | 
				
			||||||
@ -208,7 +208,7 @@ QToolBox::tab:hover {
 | 
				
			|||||||
            <x>0</x>
 | 
					            <x>0</x>
 | 
				
			||||||
            <y>0</y>
 | 
					            <y>0</y>
 | 
				
			||||||
            <width>811</width>
 | 
					            <width>811</width>
 | 
				
			||||||
            <height>384</height>
 | 
					            <height>352</height>
 | 
				
			||||||
           </rect>
 | 
					           </rect>
 | 
				
			||||||
          </property>
 | 
					          </property>
 | 
				
			||||||
          <attribute name="label">
 | 
					          <attribute name="label">
 | 
				
			||||||
@ -338,7 +338,7 @@ QToolBox::tab:hover {
 | 
				
			|||||||
            <x>0</x>
 | 
					            <x>0</x>
 | 
				
			||||||
            <y>0</y>
 | 
					            <y>0</y>
 | 
				
			||||||
            <width>811</width>
 | 
					            <width>811</width>
 | 
				
			||||||
            <height>384</height>
 | 
					            <height>352</height>
 | 
				
			||||||
           </rect>
 | 
					           </rect>
 | 
				
			||||||
          </property>
 | 
					          </property>
 | 
				
			||||||
          <attribute name="label">
 | 
					          <attribute name="label">
 | 
				
			||||||
@ -380,13 +380,84 @@ QToolBox::tab:hover {
 | 
				
			|||||||
           </item>
 | 
					           </item>
 | 
				
			||||||
          </layout>
 | 
					          </layout>
 | 
				
			||||||
         </widget>
 | 
					         </widget>
 | 
				
			||||||
 | 
					         <widget class="QWidget" name="page_6">
 | 
				
			||||||
 | 
					          <attribute name="label">
 | 
				
			||||||
 | 
					           <string>Colors and backgrounds</string>
 | 
				
			||||||
 | 
					          </attribute>
 | 
				
			||||||
 | 
					          <layout class="QFormLayout" name="formLayout_6">
 | 
				
			||||||
 | 
					           <item row="0" column="0">
 | 
				
			||||||
 | 
					            <widget class="QLabel" name="label_14">
 | 
				
			||||||
 | 
					             <property name="text">
 | 
				
			||||||
 | 
					              <string>Background color:</string>
 | 
				
			||||||
 | 
					             </property>
 | 
				
			||||||
 | 
					            </widget>
 | 
				
			||||||
 | 
					           </item>
 | 
				
			||||||
 | 
					           <item row="0" column="1">
 | 
				
			||||||
 | 
					            <layout class="QHBoxLayout" name="horizontalLayout">
 | 
				
			||||||
 | 
					             <item>
 | 
				
			||||||
 | 
					              <widget class="QLabel" name="background_color_sample">
 | 
				
			||||||
 | 
					               <property name="text">
 | 
				
			||||||
 | 
					                <string>Sample</string>
 | 
				
			||||||
 | 
					               </property>
 | 
				
			||||||
 | 
					              </widget>
 | 
				
			||||||
 | 
					             </item>
 | 
				
			||||||
 | 
					             <item>
 | 
				
			||||||
 | 
					              <widget class="QPushButton" name="change_background_color_button">
 | 
				
			||||||
 | 
					               <property name="text">
 | 
				
			||||||
 | 
					                <string>Change</string>
 | 
				
			||||||
 | 
					               </property>
 | 
				
			||||||
 | 
					              </widget>
 | 
				
			||||||
 | 
					             </item>
 | 
				
			||||||
 | 
					             <item>
 | 
				
			||||||
 | 
					              <widget class="QPushButton" name="reset_background_color_button">
 | 
				
			||||||
 | 
					               <property name="text">
 | 
				
			||||||
 | 
					                <string>Reset</string>
 | 
				
			||||||
 | 
					               </property>
 | 
				
			||||||
 | 
					              </widget>
 | 
				
			||||||
 | 
					             </item>
 | 
				
			||||||
 | 
					            </layout>
 | 
				
			||||||
 | 
					           </item>
 | 
				
			||||||
 | 
					           <item row="1" column="0">
 | 
				
			||||||
 | 
					            <widget class="QLabel" name="label_16">
 | 
				
			||||||
 | 
					             <property name="text">
 | 
				
			||||||
 | 
					              <string>Text color:</string>
 | 
				
			||||||
 | 
					             </property>
 | 
				
			||||||
 | 
					            </widget>
 | 
				
			||||||
 | 
					           </item>
 | 
				
			||||||
 | 
					           <item row="1" column="1">
 | 
				
			||||||
 | 
					            <layout class="QHBoxLayout" name="horizontalLayout_2">
 | 
				
			||||||
 | 
					             <item>
 | 
				
			||||||
 | 
					              <widget class="QLabel" name="text_color_sample">
 | 
				
			||||||
 | 
					               <property name="text">
 | 
				
			||||||
 | 
					                <string>Sample</string>
 | 
				
			||||||
 | 
					               </property>
 | 
				
			||||||
 | 
					              </widget>
 | 
				
			||||||
 | 
					             </item>
 | 
				
			||||||
 | 
					             <item>
 | 
				
			||||||
 | 
					              <widget class="QPushButton" name="change_text_color_button">
 | 
				
			||||||
 | 
					               <property name="text">
 | 
				
			||||||
 | 
					                <string>Change</string>
 | 
				
			||||||
 | 
					               </property>
 | 
				
			||||||
 | 
					              </widget>
 | 
				
			||||||
 | 
					             </item>
 | 
				
			||||||
 | 
					             <item>
 | 
				
			||||||
 | 
					              <widget class="QPushButton" name="reset_text_color_button">
 | 
				
			||||||
 | 
					               <property name="text">
 | 
				
			||||||
 | 
					                <string>Reset</string>
 | 
				
			||||||
 | 
					               </property>
 | 
				
			||||||
 | 
					              </widget>
 | 
				
			||||||
 | 
					             </item>
 | 
				
			||||||
 | 
					            </layout>
 | 
				
			||||||
 | 
					           </item>
 | 
				
			||||||
 | 
					          </layout>
 | 
				
			||||||
 | 
					         </widget>
 | 
				
			||||||
         <widget class="QWidget" name="page_3">
 | 
					         <widget class="QWidget" name="page_3">
 | 
				
			||||||
          <property name="geometry">
 | 
					          <property name="geometry">
 | 
				
			||||||
           <rect>
 | 
					           <rect>
 | 
				
			||||||
            <x>0</x>
 | 
					            <x>0</x>
 | 
				
			||||||
            <y>0</y>
 | 
					            <y>0</y>
 | 
				
			||||||
            <width>811</width>
 | 
					            <width>811</width>
 | 
				
			||||||
            <height>384</height>
 | 
					            <height>352</height>
 | 
				
			||||||
           </rect>
 | 
					           </rect>
 | 
				
			||||||
          </property>
 | 
					          </property>
 | 
				
			||||||
          <attribute name="label">
 | 
					          <attribute name="label">
 | 
				
			||||||
@ -457,7 +528,7 @@ QToolBox::tab:hover {
 | 
				
			|||||||
            <x>0</x>
 | 
					            <x>0</x>
 | 
				
			||||||
            <y>0</y>
 | 
					            <y>0</y>
 | 
				
			||||||
            <width>811</width>
 | 
					            <width>811</width>
 | 
				
			||||||
            <height>384</height>
 | 
					            <height>352</height>
 | 
				
			||||||
           </rect>
 | 
					           </rect>
 | 
				
			||||||
          </property>
 | 
					          </property>
 | 
				
			||||||
          <attribute name="label">
 | 
					          <attribute name="label">
 | 
				
			||||||
 | 
				
			|||||||
@ -115,8 +115,17 @@ class Document(QWebPage): # {{{
 | 
				
			|||||||
        mf.setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
 | 
					        mf.setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def set_user_stylesheet(self):
 | 
					    def set_user_stylesheet(self):
 | 
				
			||||||
        raw = config().parse().user_css
 | 
					        opts = config().parse()
 | 
				
			||||||
        raw = '::selection {background:#ffff00; color:#000;}\nbody {background-color: white;}\n'+raw
 | 
					        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 = 'data:text/css;charset=utf-8;base64,'
 | 
				
			||||||
        data += b64encode(raw.encode('utf-8'))
 | 
					        data += b64encode(raw.encode('utf-8'))
 | 
				
			||||||
        self.settings().setUserStyleSheetUrl(QUrl(data))
 | 
					        self.settings().setUserStyleSheetUrl(QUrl(data))
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user