E-book viewer: Add page flip animations. Their duration can be controlled via the viewer Preferences

This commit is contained in:
Kovid Goyal 2010-12-15 22:39:04 -07:00
parent a7e22ca871
commit 3f3abd41bb
3 changed files with 209 additions and 23 deletions

View File

@ -7,14 +7,14 @@
<x>0</x>
<y>0</y>
<width>479</width>
<height>606</height>
<height>591</height>
</rect>
</property>
<property name="windowTitle">
<string>Configure Ebook viewer</string>
</property>
<property name="windowIcon">
<iconset>
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/config.png</normaloff>:/images/config.png</iconset>
</property>
<layout class="QGridLayout" name="gridLayout_4">
@ -164,14 +164,14 @@
</item>
</widget>
</item>
<item row="7" column="0" colspan="2">
<item row="8" column="0" colspan="2">
<widget class="QCheckBox" name="opt_remember_window_size">
<property name="text">
<string>Remember last used &amp;window size</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<item row="9" column="0" colspan="2">
<widget class="QCheckBox" name="opt_remember_current_page">
<property name="text">
<string>Remember the &amp;current page when quitting</string>
@ -201,21 +201,21 @@
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<item row="5" column="0" colspan="2">
<widget class="QCheckBox" name="hyphenate">
<property name="text">
<string>H&amp;yphenate (break line in the middle of large words)</string>
</property>
</widget>
</item>
<item row="5" column="1">
<item row="6" column="1">
<widget class="QComboBox" name="hyphenate_default_lang">
<property name="toolTip">
<string>The default language to use for hyphenation rules. If the book does not specify a language, this will be used.</string>
</property>
</widget>
</item>
<item row="5" column="0">
<item row="6" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Default &amp;language for hyphenation:</string>
@ -225,30 +225,66 @@
</property>
</widget>
</item>
<item row="6" column="0" colspan="2">
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="opt_fit_images">
<property name="text">
<string>&amp;Resize images larger than the viewer window (needs restart)</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_11">
<property name="text">
<string>Page flip &amp;duration:</string>
</property>
<property name="buddy">
<cstring>opt_page_flip_duration</cstring>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QDoubleSpinBox" name="opt_page_flip_duration">
<property name="specialValueText">
<string>disabled</string>
</property>
<property name="suffix">
<string> secs</string>
</property>
<property name="decimals">
<number>1</number>
</property>
<property name="minimum">
<double>0.100000000000000</double>
</property>
<property name="maximum">
<double>3.000000000000000</double>
</property>
<property name="singleStep">
<double>0.100000000000000</double>
</property>
<property name="value">
<double>0.500000000000000</double>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>&amp;User stylesheet</string>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="1" column="0">
<widget class="QPlainTextEdit" name="css"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
@ -268,6 +304,29 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>User &amp;Stylesheet</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label_10">
<property name="text">
<string>&lt;p&gt;A CSS stylesheet that can be used to control the look and feel of books. For examples, click &lt;a href=&quot;http://www.mobileread.com/forums/showthread.php?t=51500&quot;&gt;here&lt;/a&gt;.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="css"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
@ -281,7 +340,6 @@
<tabstop>standard_font</tabstop>
<tabstop>max_view_width</tabstop>
<tabstop>opt_remember_window_size</tabstop>
<tabstop>css</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources>

View File

@ -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

View File

@ -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 <kovid@kovidgoyal.net>'
__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
)