From d1dadc3402cca5a4c297e6ab5ec1575fc6e40ec0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 18 Jun 2009 16:23:16 -0700 Subject: [PATCH] Ebook viewer: Fix scroll bar not updating after animated scrolling to bookmark or reference --- src/calibre/gui2/viewer/documentview.py | 32 ++++++++++++++++++++++++- src/calibre/gui2/viewer/js.py | 10 ++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index 99d6bc7111..4bca78bdc6 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -8,7 +8,8 @@ __docformat__ = 'restructuredtext en' import os, math, re from PyQt4.Qt import QWidget, QSize, QSizePolicy, QUrl, SIGNAL, Qt, QTimer, \ QPainter, QPalette, QBrush, QFontDatabase, QDialog, \ - QByteArray, QColor, QWheelEvent, QPoint, QImage, QRegion, QFont + QByteArray, QColor, QWheelEvent, QPoint, QImage, QRegion, \ + QFont, QObject, QApplication, pyqtSignature from PyQt4.QtWebKit import QWebPage, QWebView, QWebSettings from calibre.utils.config import Config, StringConfig @@ -72,6 +73,20 @@ def config(defaults=None): return c +class PythonJS(QObject): + + def __init__(self, callback): + QObject.__init__(self, QApplication.instance()) + self.setObjectName("py_bridge") + self._callback = callback + + @pyqtSignature("QString") + def callback(self, msg): + print "callback called" + self._callback(msg) + + + class ConfigDialog(QDialog, Ui_Dialog): def __init__(self, *args): @@ -89,6 +104,7 @@ class ConfigDialog(QDialog, Ui_Dialog): 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): c = config() c.set('serif_family', unicode(self.serif_family.currentFont().family())) @@ -125,7 +141,10 @@ class Document(QWebPage): def __init__(self, *args): QWebPage.__init__(self, *args) + self.setObjectName("py_bridge") self.debug_javascript = False + #self.js_bridge = PythonJS(self.js_callback) + self.setLinkDelegationPolicy(self.DelegateAllLinks) self.scroll_marks = [] pal = self.palette() @@ -160,12 +179,17 @@ class Document(QWebPage): self.settings().setUserStyleSheetUrl(QUrl.fromLocalFile(pt.name)) def load_javascript_libraries(self): + self.mainFrame().addToJavaScriptWindowObject("py_bridge", self) from calibre.resources import jquery, jquery_scrollTo self.javascript(jquery) self.javascript(jquery_scrollTo) self.javascript(bookmarks) self.javascript(referencing) + @pyqtSignature("") + def animated_scroll_done(self): + self.emit(SIGNAL('animated_scroll_done()')) + def reference_mode(self, enable): self.javascript(('enter' if enable else 'leave')+'_reference_mode()') @@ -302,6 +326,12 @@ class DocumentView(QWebView): self.connect(self.document, SIGNAL('linkClicked(QUrl)'), self.link_clicked) self.connect(self.document, SIGNAL('linkHovered(QString,QString,QString)'), self.link_hovered) self.connect(self.document, SIGNAL('selectionChanged()'), self.selection_changed) + self.connect(self.document, SIGNAL('animated_scroll_done()'), + self.animated_scroll_done, Qt.QueuedConnection) + + def animated_scroll_done(self): + if self.manager is not None: + self.manager.scrolled(self.document.scroll_fraction) def reference_mode(self, enable): self._reference_mode = enable diff --git a/src/calibre/gui2/viewer/js.py b/src/calibre/gui2/viewer/js.py index 54a622af73..5e46bcdcfa 100644 --- a/src/calibre/gui2/viewer/js.py +++ b/src/calibre/gui2/viewer/js.py @@ -54,10 +54,15 @@ function calculate_bookmark(y) { return sel + "|" + ratio; } +function animated_scrolling_done() { + window.py_bridge.animated_scroll_done(); +} + function scroll_to_bookmark(bookmark) { bm = bookmark.split("|"); var ratio = 0.7 * parseFloat(bm[1]); - $.scrollTo($(bm[0]), 1000, {over:ratio}); + $.scrollTo($(bm[0]), 1000, + {over:ratio, onAfter:function(){window.py_bridge.animated_scroll_done()}}); } ''' @@ -112,7 +117,8 @@ function goto_reference(ref) { if (num < 0) {alert("Invalid reference: "+ref); return;} var p = $("p"); if (num >= p.length) {alert("Reference not found: "+ref); return;} - $.scrollTo($(p[num]), 1000); + $.scrollTo($(p[num]), 1000, + {onAfter:function(){window.py_bridge.animated_scroll_done()}}); } '''