E-book viewer: Fix viewer losing place in very long single file documents when window resized. Fixes #745001 (Book jumps ahead several pages when closing the table of contents window)

This commit is contained in:
Kovid Goyal 2011-03-29 12:09:00 -06:00
parent 22b7eb2ed1
commit 49a8d773c0
4 changed files with 41 additions and 14 deletions

View File

@ -483,7 +483,6 @@ class Amazon(Source):
log.exception('Failed to download cover from:', cached_url) log.exception('Failed to download cover from:', cached_url)
# }}} # }}}
if __name__ == '__main__': # tests {{{ if __name__ == '__main__': # tests {{{
# To run these test use: calibre-debug -e # To run these test use: calibre-debug -e
# src/calibre/ebooks/metadata/sources/amazon.py # src/calibre/ebooks/metadata/sources/amazon.py

View File

@ -76,9 +76,9 @@ class xISBN(object):
xisbn = xISBN() xisbn = xISBN()
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys, pprint
isbn = sys.argv[-1] isbn = sys.argv[-1]
print xisbn.get_data(isbn) print pprint.pprint(xisbn.get_data(isbn))
print print
print xisbn.get_associated_isbns(isbn) print xisbn.get_associated_isbns(isbn)

View File

@ -171,10 +171,11 @@ class Document(QWebPage): # {{{
self.misc_config() self.misc_config()
self.after_load() self.after_load()
def __init__(self, shortcuts, parent=None): def __init__(self, shortcuts, parent=None, resize_callback=lambda: None):
QWebPage.__init__(self, parent) QWebPage.__init__(self, parent)
self.setObjectName("py_bridge") self.setObjectName("py_bridge")
self.debug_javascript = False self.debug_javascript = False
self.resize_callback = resize_callback
self.current_language = None self.current_language = None
self.loaded_javascript = False self.loaded_javascript = False
@ -237,6 +238,12 @@ class Document(QWebPage): # {{{
if self.loaded_javascript: if self.loaded_javascript:
return return
self.loaded_javascript = True self.loaded_javascript = True
self.javascript(
'''
window.onresize = function(event) {
window.py_bridge.window_resized();
}
''')
if jquery is None: if jquery is None:
jquery = P('content_server/jquery.js', data=True) jquery = P('content_server/jquery.js', data=True)
self.javascript(jquery) self.javascript(jquery)
@ -298,6 +305,10 @@ class Document(QWebPage): # {{{
def debug(self, msg): def debug(self, msg):
prints(msg) prints(msg)
@pyqtSignature('')
def window_resized(self):
self.resize_callback()
def reference_mode(self, enable): def reference_mode(self, enable):
self.javascript(('enter' if enable else 'leave')+'_reference_mode()') self.javascript(('enter' if enable else 'leave')+'_reference_mode()')
@ -424,12 +435,19 @@ class Document(QWebPage): # {{{
def xpos(self): def xpos(self):
return self.mainFrame().scrollPosition().x() return self.mainFrame().scrollPosition().x()
@property @dynamic_property
def scroll_fraction(self): def scroll_fraction(self):
def fget(self):
try: try:
return float(self.ypos)/(self.height-self.window_height) return float(self.ypos)/(self.height-self.window_height)
except ZeroDivisionError: except ZeroDivisionError:
return 0. return 0.
def fset(self, val):
npos = val * (self.height - self.window_height)
if npos < 0:
npos = 0
self.scroll_to(x=self.xpos, y=npos)
return property(fget=fget, fset=fset)
@property @property
def hscroll_fraction(self): def hscroll_fraction(self):
@ -493,7 +511,8 @@ class DocumentView(QWebView): # {{{
self._size_hint = QSize(510, 680) self._size_hint = QSize(510, 680)
self.initial_pos = 0.0 self.initial_pos = 0.0
self.to_bottom = False self.to_bottom = False
self.document = Document(self.shortcuts, parent=self) self.document = Document(self.shortcuts, parent=self,
resize_callback=self.viewport_resized)
self.setPage(self.document) self.setPage(self.document)
self.manager = None self.manager = None
self._reference_mode = False self._reference_mode = False
@ -630,9 +649,13 @@ class DocumentView(QWebView): # {{{
def sizeHint(self): def sizeHint(self):
return self._size_hint return self._size_hint
@property @dynamic_property
def scroll_fraction(self): def scroll_fraction(self):
def fget(self):
return self.document.scroll_fraction return self.document.scroll_fraction
def fset(self, val):
self.document.scroll_fraction = float(val)
return property(fget=fget, fset=fset)
@property @property
def hscroll_fraction(self): def hscroll_fraction(self):
@ -968,9 +991,11 @@ class DocumentView(QWebView): # {{{
def resizeEvent(self, event): def resizeEvent(self, event):
ret = QWebView.resizeEvent(self, event) ret = QWebView.resizeEvent(self, event)
QTimer.singleShot(10, self.initialize_scrollbar) QTimer.singleShot(10, self.initialize_scrollbar)
return ret
def viewport_resized(self):
if self.manager is not None: if self.manager is not None:
self.manager.viewport_resized(self.scroll_fraction) self.manager.viewport_resized(self.scroll_fraction)
return ret
def event(self, ev): def event(self, ev):
typ = ev.type() typ = ev.type()

View File

@ -240,7 +240,7 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
self.connect(self.action_reference_mode, SIGNAL('triggered(bool)'), self.connect(self.action_reference_mode, SIGNAL('triggered(bool)'),
lambda x: self.view.reference_mode(x)) lambda x: self.view.reference_mode(x))
self.connect(self.action_metadata, SIGNAL('triggered(bool)'), lambda x:self.metadata.setVisible(x)) self.connect(self.action_metadata, SIGNAL('triggered(bool)'), lambda x:self.metadata.setVisible(x))
self.connect(self.action_table_of_contents, SIGNAL('toggled(bool)'), lambda x:self.toc.setVisible(x)) self.action_table_of_contents.toggled[bool].connect(self.set_toc_visible)
self.connect(self.action_copy, SIGNAL('triggered(bool)'), self.copy) self.connect(self.action_copy, SIGNAL('triggered(bool)'), self.copy)
self.connect(self.action_font_size_larger, SIGNAL('triggered(bool)'), self.connect(self.action_font_size_larger, SIGNAL('triggered(bool)'),
self.font_size_larger) self.font_size_larger)
@ -310,6 +310,9 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
self.restore_state() self.restore_state()
def set_toc_visible(self, yes):
self.toc.setVisible(yes)
def clear_recent_history(self, *args): def clear_recent_history(self, *args):
vprefs.set('viewer_open_history', []) vprefs.set('viewer_open_history', [])
self.build_recent_menu() self.build_recent_menu()