5 #if defined(Q_OS_SYMBIAN)
6 # include "mediakeysobserver.h"
7 # include "flickcharm.h"
16 #include "progressdialog.h"
19 BookView::BookView(QWidget *parent): QWebView(parent), contentIndex(-1),
20 mBook(0), restorePositionAfterLoad(false), positionAfterLoad(0),
21 restoreFragmentAfterLoad(false), loaded(false), grabbingVolumeKeys(false)
25 // Create timer for scheduling restores
26 restoreTimer = new QTimer(this);
28 // Set up web view defaults
29 settings()->setAttribute(QWebSettings::AutoLoadImages, true);
30 settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
31 settings()->setAttribute(QWebSettings::JavaEnabled, false);
32 settings()->setAttribute(QWebSettings::PluginsEnabled, false);
33 settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
34 settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
35 settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard,
37 settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled,
39 settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled,
41 settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
42 settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
43 settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
45 settings()->setDefaultTextEncoding("utf-8");
46 page()->setContentEditable(false);
47 QWebFrame *frame = page()->mainFrame();
48 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
49 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
51 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
52 connect(this, SIGNAL(loadFinished(bool)),
53 this, SLOT(onLoadFinished(bool)));
54 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
55 this, SLOT(addJavaScriptObjects()));
57 // Suppress unwanted text selections on Maemo and Symbian
58 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
59 installEventFilter(this);
62 // Pre-load bookmark icon
63 bookmarkImage = QImage(":/icons/bookmark.png");
65 // Handle settings changes, force handling initial settings
66 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
67 this, SLOT(onSettingsChanged(const QString &)));
70 // Enable kinetic scrolling
71 #if defined(Q_WS_MAEMO_5)
73 scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
74 #elif defined(Q_OS_SYMBIAN)
76 charm = new FlickCharm(this);
77 charm->activateOn(this);
80 // Observe media keys on Symbian
82 MediaKeysObserver *observer = MediaKeysObserver::instance();
83 connect(observer, SIGNAL(mediaKeyPressed(MediaKeysObserver::MediaKeys)),
84 this, SLOT(onMediaKeysPressed(MediaKeysObserver::MediaKeys)));
88 void BookView::loadContent(int index)
95 if ((index < 0) || (index >= mBook->parts.size())) {
99 QString contentFile(mBook->content[mBook->parts[index]].href);
100 if (mBook->parts[index] == "error") {
101 setHtml(contentFile);
104 emit partLoadStart(index);
105 QUrl u = QUrl::fromLocalFile(QDir(mBook->rootPath()).
106 absoluteFilePath(contentFile));
107 qDebug() << "Loading" << u;
110 contentIndex = index;
113 void BookView::setBook(Book *book)
117 // Bail out if new book is the same as current book
122 // Save reading position of current book
125 // Set new book as the current book
128 // Bail out if new book is null
131 setHtml(tr("No book"));
137 // Restore last reading position - this will force
140 restoreLastBookmark();
144 setHtml(tr("Failed to open book"));
148 Book *BookView::book()
153 void BookView::goPrevious()
156 if (mBook && (contentIndex > 0)) {
157 mBook->setLastBookmark(contentIndex - 1, 0);
158 loadContent(contentIndex - 1);
162 void BookView::goNext()
165 if (mBook && (contentIndex < (mBook->parts.size() - 1))) {
166 mBook->setLastBookmark(contentIndex + 1, 0);
167 loadContent(contentIndex + 1);
171 void BookView::setLastBookmark(bool fast)
175 QWebFrame *frame = page()->mainFrame();
176 int height = frame->contentsSize().height();
177 int pos = frame->scrollPosition().y();
178 qDebug() << QString("At %1 (%2%, height %3)").
179 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
180 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height, fast);
182 qDebug() << "(no book)";
186 void BookView::restoreLastBookmark()
190 goToBookmark(mBook->lastBookmark());
194 void BookView::goToBookmark(const Book::Bookmark &bookmark)
198 if (bookmark.part != contentIndex) {
199 qDebug () << "Loading new part" << bookmark.part;
200 mBook->setLastBookmark(bookmark.part, bookmark.pos);
201 restorePositionAfterLoad = true;
202 positionAfterLoad = bookmark.pos;
203 loadContent(bookmark.part);
205 emit partLoadEnd(contentIndex);
206 goToPosition(bookmark.pos);
211 void BookView::goToPart(int part, const QString &fragment)
215 if (fragment.isEmpty()) {
216 goToBookmark(Book::Bookmark(part, 0));
218 if (part != contentIndex) {
219 qDebug() << "Loading new part" << part;
220 restoreFragmentAfterLoad = true;
221 fragmentAfterLoad = fragment;
224 goToFragment(fragment);
231 void BookView::goToFragment(const QString &fragment)
234 if (!fragment.isEmpty()) {
235 QVariant ret = page()->mainFrame()->evaluateJavaScript(
236 QString("window.location='") + fragment + "'");
241 void BookView::onLoadFinished(bool ok)
245 qDebug() << "Not OK";
249 onSettingsChanged("scheme");
250 onSettingsChanged("zoom");
251 onSettingsChanged("font");
252 scheduleRestoreAfterLoad();
255 void BookView::scheduleRestoreAfterLoad()
258 if (restoreTimer->isActive()) {
259 // Ignore request if a restore is already in progress
263 disconnect(restoreTimer, SIGNAL(timeout()), this, 0);
264 connect(restoreTimer, SIGNAL(timeout()), this, SLOT(restoreAfterLoad()));
265 restoreTimer->setSingleShot(true);
266 restoreTimer->start(210);
269 void BookView::scheduleRestoreLastBookmark()
272 if (restoreTimer->isActive()) {
273 // Ignore request if a restore is already in progress
277 disconnect(restoreTimer, SIGNAL(timeout()), this, 0);
278 connect(restoreTimer, SIGNAL(timeout()), this,
279 SLOT(restoreLastBookmark()));
280 restoreTimer->setSingleShot(true);
281 restoreTimer->start(210);
284 void BookView::restoreAfterLoad()
287 if (restoreFragmentAfterLoad) {
288 qDebug() << "Restorint to fragment" << fragmentAfterLoad;
289 goToFragment(fragmentAfterLoad);
290 restoreFragmentAfterLoad = false;
291 } else if (restorePositionAfterLoad) {
292 qDebug() << "Restoring to position" << positionAfterLoad;
293 goToPosition(positionAfterLoad);
294 restorePositionAfterLoad = false;
297 emit partLoadEnd(contentIndex);
301 void BookView::onSettingsChanged(const QString &key)
303 Settings *s = Settings::instance();
304 Platform *p = Platform::instance();
307 int value = s->value(key, p->defaultZoom()).toInt();
308 qDebug() << "BookView::onSettingsChanged: zoom" << value;
309 setZoomFactor(value / 100.);
311 else if (key == "font") {
312 QString face = s->value(key, p->defaultFont()).toString();
313 qDebug() << "BookView::onSettingsChanged: font" << face;
314 settings()->setFontFamily(QWebSettings::StandardFont, face);
316 else if (key == "scheme") {
317 QWebFrame *frame = page()->mainFrame();
318 QString scheme = Settings::instance()->value("scheme").toString();
319 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
320 (scheme != "default")) {
323 qDebug() << "BookView::onSettingsChanged: scheme" << scheme;
324 QFile script(":/styles/" + scheme + ".js");
325 script.open(QFile::ReadOnly);
326 QString scriptText = script.readAll();
328 (void)frame->evaluateJavaScript(scriptText);
330 else if (key == "usevolumekeys") {
331 bool grab = s->value(key, false).toBool();
332 qDebug() << "BookView::onSettingsChanged: usevolumekeys" << grab;
333 grabVolumeKeys(grab);
337 void BookView::paintEvent(QPaintEvent *e)
339 QWebView::paintEvent(e);
340 if (!mBook || !loaded) {
345 QWebFrame *frame = page()->mainFrame();
346 int contentsHeight = frame->contentsSize().height();
347 QPoint scrollPos = frame->scrollPosition();
348 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
349 QPainter painter(this);
350 foreach (Book::Bookmark b, mBook->bookmarks()) {
351 if (b.part != contentIndex) {
354 int height = contentsHeight;
355 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
356 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
360 pen.setStyle(Qt::DotLine);
363 if (contentIndex > 0) {
364 painter.drawLine(0, -scrollPos.y(), width(), -scrollPos.y());
366 if (contentIndex < (mBook->parts.size() - 1)) {
367 int h = contentsHeight - scrollPos.y() - 1;
368 painter.drawLine(0, h, width(), h);
373 void BookView::mousePressEvent(QMouseEvent *e)
375 QWebView::mousePressEvent(e);
376 #if defined(Q_WS_MAEMO_5)
377 // Start monitoring kinetic scroll
378 if (scrollerMonitor) {
379 killTimer(scrollerMonitor);
383 scrollerMonitor = startTimer(500);
385 #elif defined(Q_OS_SYMBIAN)
388 // Handle mouse press on the scroll bar
389 QWebFrame *frame = page()->mainFrame();
390 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
398 void BookView::wheelEvent(QWheelEvent *e)
400 QWebView::wheelEvent(e);
404 void BookView::addBookmark(const QString ¬e)
410 int y = page()->mainFrame()->scrollPosition().y();
411 int height = page()->mainFrame()->contentsSize().height();
412 qDebug() << ((qreal)y / (qreal)height);
413 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height, note);
417 QString BookView::tmpPath()
419 return QDir::tempPath() + "/dorian";
422 bool BookView::eventFilter(QObject *o, QEvent *e)
425 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
426 if (e->type() == QEvent::Resize) {
427 qDebug() << "BookView::eventFilter QEvent::Resize to"
428 << page()->mainFrame()->contentsSize().height();
429 } else if (e->type() == QEvent::Timer) {
430 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
431 << ((QTimerEvent *)e)->timerId();
433 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
438 // Work around Qt bug that sometimes selects web view contents during swipe
440 case QEvent::MouseButtonPress:
443 case QEvent::MouseButtonRelease:
444 #ifndef Q_OS_SYMBIAN // Too heavy on Symbian
447 mousePressed = false;
449 case QEvent::MouseMove:
454 case QEvent::MouseButtonDblClick:
460 return QObject::eventFilter(o, e);
463 void BookView::addJavaScriptObjects()
466 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
469 void BookView::goToPosition(qreal position)
472 int contentsHeight = page()->mainFrame()->contentsSize().height();
473 int scrollPos = (int)((qreal)contentsHeight * position);
474 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
476 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
477 << (position * 100) << "%, height" << contentsHeight << ")";
480 void BookView::showProgress()
484 int contentsHeight = page()->mainFrame()->contentsSize().height();
485 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
486 (qreal)contentsHeight;
487 emit progress(mBook->getProgress(contentIndex, pos));
491 void BookView::timerEvent(QTimerEvent *e)
493 #if defined(Q_WS_MAEMO_5)
494 if (e->timerId() == scrollerMonitor) {
496 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
497 (scroller->state() == QAbstractKineticScroller::Pushing))) {
500 killTimer(scrollerMonitor);
501 scrollerMonitor = -1;
504 #endif // Q_WS_MAEMO_5
506 QWebView::timerEvent(e);
509 void BookView::hideEvent(QHideEvent *e)
511 Trace t("BookView::hideEvent");
513 #if defined(Q_OS_SYMBIAN)
517 QWebView::hideEvent(e);
520 void BookView::goPreviousPage()
522 QWebFrame *frame = page()->mainFrame();
523 const int pos = frame->scrollPosition().y();
524 frame->scroll(0, -1);
525 if (pos == frame->scrollPosition().y()) {
526 if (contentIndex > 0) {
527 Book::Bookmark bookmark(contentIndex - 1, 1.0);
528 mBook->setLastBookmark(contentIndex - 1, 1.0);
529 goToBookmark(bookmark);
533 QPropertyAnimation *slide =
534 new QPropertyAnimation(frame, "scrollPosition");
535 const QPoint *offset = new QPoint(0, height() - 18);
536 slide->setDuration(400);
537 slide->setStartValue(frame->scrollPosition());
538 slide->setEndValue(frame->scrollPosition() - *offset);
539 slide->setEasingCurve(QEasingCurve::OutQuad);
540 slide->start(QAbstractAnimation::DeleteWhenStopped);
545 void BookView::goNextPage()
548 QWebFrame *frame = page()->mainFrame();
549 const int pos = frame->scrollPosition().y();
551 if (pos == frame->scrollPosition().y()) {
555 QPropertyAnimation *slide =
556 new QPropertyAnimation(frame, "scrollPosition");
557 const QPoint *offset = new QPoint(0, (height() - 18));
558 slide->setDuration(400);
559 slide->setStartValue(frame->scrollPosition());
560 slide->setEndValue(frame->scrollPosition() + *offset);
561 slide->setEasingCurve(QEasingCurve::OutQuad);
562 slide->start(QAbstractAnimation::DeleteWhenStopped);
567 void BookView::grabVolumeKeys(bool grab)
570 grabbingVolumeKeys = grab;
575 void BookView::onMediaKeysPressed(MediaKeysObserver::MediaKeys key)
578 qDebug() << "Key" << (int)key;
579 if (grabbingVolumeKeys) {
580 if (key == MediaKeysObserver::EVolIncKey) {
581 qDebug() << "Volume up";
583 } else if (key == MediaKeysObserver::EVolDecKey){
584 qDebug() << "Volume down";
590 #endif // Q_OS_SYMBIAN