diff --git a/src/calibre/gui2/viewer/config.ui b/src/calibre/gui2/viewer/config.ui
index 4066daada2..8f2650d814 100644
--- a/src/calibre/gui2/viewer/config.ui
+++ b/src/calibre/gui2/viewer/config.ui
@@ -7,14 +7,14 @@
0
0
479
- 606
+ 591
Configure Ebook viewer
-
+
:/images/config.png:/images/config.png
@@ -164,14 +164,14 @@
- -
+
-
Remember last used &window size
- -
+
-
Remember the ¤t page when quitting
@@ -201,21 +201,21 @@
- -
+
-
H&yphenate (break line in the middle of large words)
- -
+
-
The default language to use for hyphenation rules. If the book does not specify a language, this will be used.
- -
+
-
Default &language for hyphenation:
@@ -225,30 +225,66 @@
- -
+
-
&Resize images larger than the viewer window (needs restart)
+ -
+
+
+ Page flip &duration:
+
+
+ opt_page_flip_duration
+
+
+
+ -
+
+
+ disabled
+
+
+ secs
+
+
+ 1
+
+
+ 0.100000000000000
+
+
+ 3.000000000000000
+
+
+ 0.100000000000000
+
+
+ 0.500000000000000
+
+
+
- -
-
-
- &User stylesheet
-
-
-
-
-
-
-
-
-
+ -
+
+
+ Qt::Vertical
+
+
+
+ 20
+ 40
+
+
+
+
@@ -268,6 +304,29 @@
+
+
+ User &Stylesheet
+
+
+ -
+
+
+ <p>A CSS stylesheet that can be used to control the look and feel of books. For examples, click <a href="http://www.mobileread.com/forums/showthread.php?t=51500">here</a>.
+
+
+ true
+
+
+ true
+
+
+
+ -
+
+
+
+
@@ -281,7 +340,6 @@
standard_font
max_view_width
opt_remember_window_size
- css
buttonBox
diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py
index afaea41bc6..d033ae133b 100644
--- a/src/calibre/gui2/viewer/documentview.py
+++ b/src/calibre/gui2/viewer/documentview.py
@@ -18,6 +18,7 @@ from PyQt4.QtWebKit import QWebPage, QWebView, QWebSettings
from calibre.utils.config import Config, StringConfig
from calibre.utils.localization import get_language
from calibre.gui2.viewer.config_ui import Ui_Dialog
+from calibre.gui2.viewer.flip import SlideFlip
from calibre.gui2.shortcuts import Shortcuts, ShortcutConfig
from calibre.constants import iswindows
from calibre import prints, guess_type
@@ -52,6 +53,9 @@ def config(defaults=None):
help=_('Default language for hyphenation rules'))
c.add_opt('remember_current_page', default=True,
help=_('Save the current position in the document, when quitting'))
+ c.add_opt('page_flip_duration', default=0.5,
+ help=_('The time, in seconds, for the page flip animation. Default'
+ ' is half a second.'))
fonts = c.add_group('FONTS', _('Font options'))
fonts('serif_family', default='Times New Roman' if iswindows else 'Liberation Serif',
@@ -75,6 +79,7 @@ class ConfigDialog(QDialog, Ui_Dialog):
opts = config().parse()
self.opt_remember_window_size.setChecked(opts.remember_window_size)
self.opt_remember_current_page.setChecked(opts.remember_current_page)
+ self.opt_page_flip_duration.setValue(opts.page_flip_duration)
self.serif_family.setCurrentFont(QFont(opts.serif_family))
self.sans_family.setCurrentFont(QFont(opts.sans_family))
self.mono_family.setCurrentFont(QFont(opts.mono_family))
@@ -122,6 +127,7 @@ class ConfigDialog(QDialog, Ui_Dialog):
c.set('max_view_width', int(self.max_view_width.value()))
c.set('hyphenate', self.hyphenate.isChecked())
c.set('remember_current_page', self.opt_remember_current_page.isChecked())
+ c.set('page_flip_duration', self.opt_page_flip_duration.value())
idx = self.hyphenate_default_lang.currentIndex()
c.set('hyphenate_default_lang',
str(self.hyphenate_default_lang.itemData(idx).toString()))
@@ -197,6 +203,8 @@ class Document(QWebPage):
self.hyphenate = opts.hyphenate
self.hyphenate_default_lang = opts.hyphenate_default_lang
self.do_fit_images = opts.fit_images
+ self.page_flip_duration = opts.page_flip_duration
+ self.enable_page_flip = self.page_flip_duration > 0.1
def fit_images(self):
if self.do_fit_images:
@@ -453,6 +461,7 @@ class DocumentView(QWebView):
def __init__(self, *args):
QWebView.__init__(self, *args)
+ self.flipper = SlideFlip(self)
self.debug_javascript = False
self.shortcuts = Shortcuts(SHORTCUTS, 'shortcuts/viewer')
self.self_closing_pat = re.compile(r'<([a-z1-6]+)\s+([^>]+)/>',
@@ -708,12 +717,17 @@ class DocumentView(QWebView):
return False
return True
- def find_next_blank_line(self, overlap):
+ def current_page_image(self, overlap=-1):
+ if overlap < 0:
+ overlap = self.height()
img = QImage(self.width(), overlap, QImage.Format_ARGB32)
painter = QPainter(img)
- # Render a region of width x overlap pixels atthe bottom of the current viewport
self.document.mainFrame().render(painter, QRegion(0, 0, self.width(), overlap))
painter.end()
+ return img
+
+ def find_next_blank_line(self, overlap):
+ img = self.current_page_image(overlap)
for i in range(overlap-1, -1, -1):
if self.test_line(img, i):
self.scroll_by(y=i, notify=False)
@@ -721,6 +735,10 @@ class DocumentView(QWebView):
self.scroll_by(y=overlap)
def previous_page(self):
+ if self.flipper.running:
+ return
+ epf = self.document.enable_page_flip
+
delta_y = self.document.window_height - 25
if self.document.at_top:
if self.manager is not None:
@@ -732,11 +750,20 @@ class DocumentView(QWebView):
if upper_limit < 0:
upper_limit = 0
if upper_limit < opos:
+ if epf:
+ self.flipper.initialize(self.current_page_image())
self.document.scroll_to(self.document.xpos, upper_limit)
+ if epf:
+ self.flipper(self.current_page_image(), forwards=False,
+ duration=self.document.page_flip_duration)
if self.manager is not None:
self.manager.scrolled(self.scroll_fraction)
def next_page(self):
+ if self.flipper.running:
+ return
+ epf = self.document.enable_page_flip
+
window_height = self.document.window_height
document_height = self.document.height
ddelta = document_height - window_height
@@ -770,6 +797,10 @@ class DocumentView(QWebView):
return
#print 'Setting padding to:', lower_limit - max_y
self.document.set_bottom_padding(lower_limit - max_y)
+ before_img = None
+ if epf:
+ before_img = self.current_page_image()
+ self.flipper.initialize(before_img)
#print 'Document height:', self.document.height
max_y = self.document.height - window_height
lower_limit = min(max_y, lower_limit)
@@ -780,6 +811,9 @@ class DocumentView(QWebView):
#print 'After scroll pos:', self.document.ypos
self.find_next_blank_line(window_height - actually_scrolled)
#print 'After blank line pos:', self.document.ypos
+ if epf:
+ self.flipper(self.current_page_image(),
+ duration=self.document.page_flip_duration)
if self.manager is not None:
self.manager.scrolled(self.scroll_fraction)
#print 'After all:', self.document.ypos
diff --git a/src/calibre/gui2/viewer/flip.py b/src/calibre/gui2/viewer/flip.py
new file mode 100644
index 0000000000..d12d3f56fe
--- /dev/null
+++ b/src/calibre/gui2/viewer/flip.py
@@ -0,0 +1,94 @@
+#!/usr/bin/env python
+# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
+
+__license__ = 'GPL v3'
+__copyright__ = '2010, Kovid Goyal '
+__docformat__ = 'restructuredtext en'
+
+from PyQt4.Qt import QWidget, QPainter, QPropertyAnimation, QEasingCurve, \
+ QRect, QPixmap, Qt, pyqtProperty
+
+class SlideFlip(QWidget):
+
+ def __init__(self, parent):
+ QWidget.__init__(self, parent)
+
+ self.setGeometry(0, 0, 1, 1)
+ self._current_width = 0
+ self.before_image = self.after_image = None
+ self.animation = QPropertyAnimation(self, 'current_width', self)
+ self.setVisible(False)
+ self.animation.valueChanged.connect(self.update)
+ self.animation.finished.connect(self.finished)
+ self.flip_forwards = True
+ self.setAttribute(Qt.WA_OpaquePaintEvent)
+
+ @property
+ def running(self):
+ return self.animation.state() == self.animation.Running
+
+ def initialize(self, image):
+ self.before_image = QPixmap.fromImage(image)
+ self.after_image = None
+ self.setGeometry(0, 0, image.width(), image.height())
+ self.setVisible(True)
+
+ def __call__(self, image, duration=0.5, forwards=True):
+ if self.running:
+ return
+ self.flip_forwards = forwards
+ self.after_image = QPixmap.fromImage(image)
+
+ if self.flip_forwards:
+ self.animation.setStartValue(image.width())
+ self.animation.setEndValue(0)
+ t = self.before_image
+ self.before_image = self.after_image
+ self.after_image = t
+ self.animation.setEasingCurve(QEasingCurve(QEasingCurve.InExpo))
+ else:
+ self.animation.setStartValue(0)
+ self.animation.setEndValue(image.width())
+ self.animation.setEasingCurve(QEasingCurve(QEasingCurve.OutExpo))
+
+ self.animation.setDuration(duration * 1000)
+ self.animation.start()
+
+ def finished(self):
+ self.setVisible(False)
+ self.before_image = self.after_image = None
+
+ def paintEvent(self, ev):
+ if self.before_image is None:
+ return
+ canvas_size = self.rect()
+ p = QPainter(self)
+ p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
+
+ p.drawPixmap(canvas_size, self.before_image,
+ self.before_image.rect())
+ if self.after_image is not None:
+ width = self._current_width
+ iw = self.after_image.width()
+ sh = min(self.after_image.height(), canvas_size.height())
+
+ if self.flip_forwards:
+ source = QRect(max(0, iw - width), 0, width, sh)
+ else:
+ source = QRect(0, 0, width, sh)
+
+ target = QRect(source)
+ target.moveLeft(0)
+ p.drawPixmap(target, self.after_image, source)
+
+ p.end()
+
+ def set_current_width(self, val):
+ self._current_width = val
+
+ current_width = pyqtProperty('int',
+ fget=lambda self: self._current_width,
+ fset=set_current_width
+ )
+
+