E-book viewer: Add a paged mode that splits up the text into pages, like in a paper book instead of presenting it as a single column. To activate click the button with the yellow scroll icon in the top right corner.

This commit is contained in:
Kovid Goyal 2012-07-18 16:15:12 +05:30
parent ca9f58610d
commit b677bb15c9
5 changed files with 46 additions and 4 deletions

BIN
resources/images/scroll.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -22,7 +22,6 @@ from calibre.gui2.viewer.javascript import JavaScriptLoader
from calibre.gui2.viewer.position import PagePosition from calibre.gui2.viewer.position import PagePosition
from calibre.gui2.viewer.config import config, ConfigDialog from calibre.gui2.viewer.config import config, ConfigDialog
from calibre.ebooks.oeb.display.webview import load_html from calibre.ebooks.oeb.display.webview import load_html
from calibre.utils.config import tweaks
from calibre.constants import isxp from calibre.constants import isxp
# }}} # }}}
@ -60,7 +59,7 @@ class Document(QWebPage): # {{{
def __init__(self, shortcuts, parent=None, debug_javascript=False): def __init__(self, shortcuts, parent=None, debug_javascript=False):
QWebPage.__init__(self, parent) QWebPage.__init__(self, parent)
self.setObjectName("py_bridge") self.setObjectName("py_bridge")
self.in_paged_mode = tweaks.get('viewer_test_paged_mode', False) self.in_paged_mode = False
# Use this to pass arbitrary JSON encodable objects between python and # Use this to pass arbitrary JSON encodable objects between python and
# javascript. In python get/set the value as: self.bridge_value. In # javascript. In python get/set the value as: self.bridge_value. In
# javascript, get/set the value as: py_bridge.value # javascript, get/set the value as: py_bridge.value
@ -647,6 +646,7 @@ class DocumentView(QWebView): # {{{
def load_path(self, path, pos=0.0): def load_path(self, path, pos=0.0):
self.initial_pos = pos self.initial_pos = pos
self.last_loaded_path = path
def callback(lu): def callback(lu):
self.loading_url = lu self.loading_url = lu
@ -654,7 +654,7 @@ class DocumentView(QWebView): # {{{
self.manager.load_started() self.manager.load_started()
load_html(path, self, codec=getattr(path, 'encoding', 'utf-8'), mime_type=getattr(path, load_html(path, self, codec=getattr(path, 'encoding', 'utf-8'), mime_type=getattr(path,
'mime_type', None), pre_load_callback=callback) 'mime_type', 'text/html'), pre_load_callback=callback)
entries = set() entries = set()
for ie in getattr(path, 'index_entries', []): for ie in getattr(path, 'index_entries', []):
if ie.start_anchor: if ie.start_anchor:

View File

@ -152,6 +152,10 @@ class RecentAction(QAction):
class EbookViewer(MainWindow, Ui_EbookViewer): class EbookViewer(MainWindow, Ui_EbookViewer):
STATE_VERSION = 1 STATE_VERSION = 1
FLOW_MODE_TT = _('Switch to paged mode - where the text is broken up '
'into pages like a paper book')
PAGED_MODE_TT = _('Switch to flow mode - where the text is not broken up '
'into pages')
def __init__(self, pathtoebook=None, debug_javascript=False, open_at=None): def __init__(self, pathtoebook=None, debug_javascript=False, open_at=None):
MainWindow.__init__(self, None) MainWindow.__init__(self, None)
@ -168,6 +172,7 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
self.pending_anchor = None self.pending_anchor = None
self.pending_reference = None self.pending_reference = None
self.pending_bookmark = None self.pending_bookmark = None
self.pending_restore = False
self.existing_bookmarks= [] self.existing_bookmarks= []
self.selected_text = None self.selected_text = None
self.read_settings() self.read_settings()
@ -339,6 +344,22 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
self.addAction(action) self.addAction(action)
self.restore_state() self.restore_state()
self.action_toggle_paged_mode.toggled[bool].connect(self.toggle_paged_mode)
def toggle_paged_mode(self, checked, at_start=False):
in_paged_mode = not self.action_toggle_paged_mode.isChecked()
self.view.document.in_paged_mode = in_paged_mode
self.action_toggle_paged_mode.setToolTip(self.FLOW_MODE_TT if
self.action_toggle_paged_mode.isChecked() else
self.PAGED_MODE_TT)
if at_start: return
self.reload()
def reload(self):
if hasattr(self, 'current_index') and self.current_index > -1:
self.view.document.page_position.save(overwrite=False)
self.pending_restore = True
self.load_path(self.view.last_loaded_path)
def set_toc_visible(self, yes): def set_toc_visible(self, yes):
self.toc.setVisible(yes) self.toc.setVisible(yes)
@ -394,6 +415,7 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
vprefs.set('viewer_splitter_state', vprefs.set('viewer_splitter_state',
bytearray(self.splitter.saveState())) bytearray(self.splitter.saveState()))
vprefs['multiplier'] = self.view.multiplier vprefs['multiplier'] = self.view.multiplier
vprefs['in_paged_mode1'] = not self.action_toggle_paged_mode.isChecked()
def restore_state(self): def restore_state(self):
state = vprefs.get('viewer_toolbar_state', None) state = vprefs.get('viewer_toolbar_state', None)
@ -410,6 +432,10 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
# specific location, ensure they are visible. # specific location, ensure they are visible.
self.tool_bar.setVisible(True) self.tool_bar.setVisible(True)
self.tool_bar2.setVisible(True) self.tool_bar2.setVisible(True)
self.action_toggle_paged_mode.setChecked(not vprefs.get('in_paged_mode1',
False))
self.toggle_paged_mode(self.action_toggle_paged_mode.isChecked(),
at_start=True)
def lookup(self, word): def lookup(self, word):
self.dictionary_view.setHtml('<html><body><p>'+ \ self.dictionary_view.setHtml('<html><body><p>'+ \
@ -716,6 +742,8 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
if self.pending_bookmark is not None: if self.pending_bookmark is not None:
self.goto_bookmark(self.pending_bookmark) self.goto_bookmark(self.pending_bookmark)
self.pending_bookmark = None self.pending_bookmark = None
if self.pending_restore:
self.view.document.page_position.restore()
return self.current_index return self.current_index
def goto_next_section(self): def goto_next_section(self):

View File

@ -143,6 +143,7 @@
</attribute> </attribute>
<addaction name="action_find_next"/> <addaction name="action_find_next"/>
<addaction name="action_find_previous"/> <addaction name="action_find_previous"/>
<addaction name="action_toggle_paged_mode"/>
</widget> </widget>
<action name="action_back"> <action name="action_back">
<property name="icon"> <property name="icon">
@ -309,6 +310,18 @@
<string>Shift+F3</string> <string>Shift+F3</string>
</property> </property>
</action> </action>
<action name="action_toggle_paged_mode">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/scroll.png</normaloff>:/images/scroll.png</iconset>
</property>
<property name="text">
<string>Toggle Paged mode</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>

View File

@ -50,7 +50,8 @@ class PagePosition(object):
def __exit__(self, *args): def __exit__(self, *args):
self.restore() self.restore()
def save(self): def save(self, overwrite=True):
if not overwrite and self._cpos is not None: return
self._cpos = self.current_pos self._cpos = self.current_pos
def restore(self): def restore(self):