mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-08-05 08:40:13 -04:00
Add support for user stylesheets to ebook-viewer
This commit is contained in:
parent
5d4159690f
commit
69f79f163d
@ -804,7 +804,7 @@ class Processor(Parser):
|
|||||||
# Workaround for anchor rendering bug in ADE
|
# Workaround for anchor rendering bug in ADE
|
||||||
css += '\n\na { color: inherit; text-decoration: inherit; cursor: default; }\na[href] { color: blue; text-decoration: underline; cursor:pointer; }'
|
css += '\n\na { color: inherit; text-decoration: inherit; cursor: default; }\na[href] { color: blue; text-decoration: underline; cursor:pointer; }'
|
||||||
if self.opts.remove_paragraph_spacing:
|
if self.opts.remove_paragraph_spacing:
|
||||||
css += '\n\np {text-indent: 2.1em; margin-top:1pt; margin-bottom:1pt; padding:0pt; border:0pt;}'
|
css += '\n\np {text-indent: 2em; margin-top:1pt; margin-bottom:1pt; padding:0pt; border:0pt;}'
|
||||||
self.override_css = self.css_parser.parseString(self.preprocess_css(css))
|
self.override_css = self.css_parser.parseString(self.preprocess_css(css))
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>479</width>
|
<width>479</width>
|
||||||
<height>436</height>
|
<height>574</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle" >
|
<property name="windowTitle" >
|
||||||
@ -145,6 +145,18 @@
|
|||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
|
<item row="2" column="0" >
|
||||||
|
<widget class="QGroupBox" name="groupBox_2" >
|
||||||
|
<property name="title" >
|
||||||
|
<string>&User stylesheet</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QGridLayout" name="gridLayout_5" >
|
||||||
|
<item row="0" column="0" >
|
||||||
|
<widget class="QPlainTextEdit" name="css" />
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
@ -14,6 +14,7 @@ from PyQt4.QtWebKit import QWebPage, QWebView, QWebSettings
|
|||||||
from calibre.utils.config import Config, StringConfig
|
from calibre.utils.config import Config, StringConfig
|
||||||
from calibre.gui2.viewer.config_ui import Ui_Dialog
|
from calibre.gui2.viewer.config_ui import Ui_Dialog
|
||||||
from calibre.gui2.viewer.js import bookmarks, referencing
|
from calibre.gui2.viewer.js import bookmarks, referencing
|
||||||
|
from calibre.ptempfile import PersistentTemporaryFile
|
||||||
|
|
||||||
def load_builtin_fonts():
|
def load_builtin_fonts():
|
||||||
from calibre.ebooks.lrf.fonts.liberation import LiberationMono_BoldItalic
|
from calibre.ebooks.lrf.fonts.liberation import LiberationMono_BoldItalic
|
||||||
@ -50,7 +51,10 @@ def config(defaults=None):
|
|||||||
c = Config('viewer', desc)
|
c = Config('viewer', desc)
|
||||||
else:
|
else:
|
||||||
c = StringConfig(defaults, desc)
|
c = StringConfig(defaults, desc)
|
||||||
|
|
||||||
|
c.add_opt('user_css', default='',
|
||||||
|
help=_('Set the user CSS stylesheet. This can be used to customize the look of all books.'))
|
||||||
|
|
||||||
fonts = c.add_group('FONTS', _('Font options'))
|
fonts = c.add_group('FONTS', _('Font options'))
|
||||||
fonts('serif_family', default='Liberation Serif', help=_('The serif font family'))
|
fonts('serif_family', default='Liberation Serif', help=_('The serif font family'))
|
||||||
fonts('sans_family', default='Liberation Sans', help=_('The sans-serif font family'))
|
fonts('sans_family', default='Liberation Sans', help=_('The sans-serif font family'))
|
||||||
@ -74,6 +78,8 @@ class ConfigDialog(QDialog, Ui_Dialog):
|
|||||||
self.default_font_size.setValue(opts.default_font_size)
|
self.default_font_size.setValue(opts.default_font_size)
|
||||||
self.mono_font_size.setValue(opts.mono_font_size)
|
self.mono_font_size.setValue(opts.mono_font_size)
|
||||||
self.standard_font.setCurrentIndex({'serif':0, 'sans':1, 'mono':2}[opts.standard_font])
|
self.standard_font.setCurrentIndex({'serif':0, 'sans':1, 'mono':2}[opts.standard_font])
|
||||||
|
self.css.setPlainText(opts.user_css)
|
||||||
|
self.css.setToolTip(_('Set the user CSS stylesheet. This can be used to customize the look of all books.'))
|
||||||
|
|
||||||
def accept(self, *args):
|
def accept(self, *args):
|
||||||
c = config()
|
c = config()
|
||||||
@ -83,6 +89,7 @@ class ConfigDialog(QDialog, Ui_Dialog):
|
|||||||
c.set('default_font_size', self.default_font_size.value())
|
c.set('default_font_size', self.default_font_size.value())
|
||||||
c.set('mono_font_size', self.mono_font_size.value())
|
c.set('mono_font_size', self.mono_font_size.value())
|
||||||
c.set('standard_font', {0:'serif', 1:'sans', 2:'mono'}[self.standard_font.currentIndex()])
|
c.set('standard_font', {0:'serif', 1:'sans', 2:'mono'}[self.standard_font.currentIndex()])
|
||||||
|
c.set('user_css', unicode(self.css.toPlainText()))
|
||||||
return QDialog.accept(self, *args)
|
return QDialog.accept(self, *args)
|
||||||
|
|
||||||
|
|
||||||
@ -104,6 +111,7 @@ class Document(QWebPage):
|
|||||||
d = ConfigDialog(parent)
|
d = ConfigDialog(parent)
|
||||||
if d.exec_() == QDialog.Accepted:
|
if d.exec_() == QDialog.Accepted:
|
||||||
self.set_font_settings()
|
self.set_font_settings()
|
||||||
|
self.set_user_stylesheet()
|
||||||
self.triggerAction(QWebPage.Reload)
|
self.triggerAction(QWebPage.Reload)
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
@ -128,11 +136,19 @@ class Document(QWebPage):
|
|||||||
|
|
||||||
# Miscellaneous
|
# Miscellaneous
|
||||||
settings.setAttribute(QWebSettings.LinksIncludedInFocusChain, True)
|
settings.setAttribute(QWebSettings.LinksIncludedInFocusChain, True)
|
||||||
|
self.set_user_stylesheet()
|
||||||
|
|
||||||
# Load jQuery
|
# Load jQuery
|
||||||
self.connect(self.mainFrame(), SIGNAL('javaScriptWindowObjectCleared()'),
|
self.connect(self.mainFrame(), SIGNAL('javaScriptWindowObjectCleared()'),
|
||||||
self.load_javascript_libraries)
|
self.load_javascript_libraries)
|
||||||
|
|
||||||
|
def set_user_stylesheet(self):
|
||||||
|
raw = config().parse().user_css
|
||||||
|
pt = PersistentTemporaryFile('_user_stylesheet.css')
|
||||||
|
pt.write(raw.encode('utf-8'))
|
||||||
|
pt.close()
|
||||||
|
self.settings().setUserStyleSheetUrl(QUrl.fromLocalFile(pt.name))
|
||||||
|
|
||||||
def load_javascript_libraries(self):
|
def load_javascript_libraries(self):
|
||||||
from calibre.resources import jquery, jquery_scrollTo
|
from calibre.resources import jquery, jquery_scrollTo
|
||||||
self.javascript(jquery)
|
self.javascript(jquery)
|
||||||
|
@ -419,8 +419,7 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
|
|||||||
path = self.view.path()
|
path = self.view.path()
|
||||||
try:
|
try:
|
||||||
index = self.iterator.spine.index(path)
|
index = self.iterator.spine.index(path)
|
||||||
except ValueError:
|
except (ValueError, AttributeError):
|
||||||
print path
|
|
||||||
return -1
|
return -1
|
||||||
self.current_page = self.iterator.spine[index]
|
self.current_page = self.iterator.spine[index]
|
||||||
self.current_index = index
|
self.current_index = index
|
||||||
|
Loading…
x
Reference in New Issue
Block a user