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 + "'");
238 // FIXME: setLastBookmark();
242 void BookView::onLoadFinished(bool ok)
246 qDebug() << "Not OK";
250 onSettingsChanged("scheme");
251 onSettingsChanged("zoom");
252 onSettingsChanged("font");
253 scheduleRestoreAfterLoad();
256 void BookView::scheduleRestoreAfterLoad()
259 if (restoreTimer->isActive()) {
260 // Ignore request if a restore is already in progress
264 disconnect(restoreTimer, SIGNAL(timeout()), this, 0);
265 connect(restoreTimer, SIGNAL(timeout()), this, SLOT(restoreAfterLoad()));
266 restoreTimer->setSingleShot(true);
267 restoreTimer->start(210);
270 void BookView::scheduleRestoreLastBookmark()
273 if (restoreTimer->isActive()) {
274 // Ignore request if a restore is already in progress
278 disconnect(restoreTimer, SIGNAL(timeout()), this, 0);
279 connect(restoreTimer, SIGNAL(timeout()), this,
280 SLOT(restoreLastBookmark()));
281 restoreTimer->setSingleShot(true);
282 restoreTimer->start(210);
285 void BookView::restoreAfterLoad()
288 if (restoreFragmentAfterLoad) {
289 qDebug() << "Restorint to fragment" << fragmentAfterLoad;
290 goToFragment(fragmentAfterLoad);
291 restoreFragmentAfterLoad = false;
292 } else if (restorePositionAfterLoad) {
293 qDebug() << "Restoring to position" << positionAfterLoad;
294 goToPosition(positionAfterLoad);
295 restorePositionAfterLoad = false;
298 emit partLoadEnd(contentIndex);
302 void BookView::onSettingsChanged(const QString &key)
304 Settings *s = Settings::instance();
305 Platform *p = Platform::instance();
308 int value = s->value(key, p->defaultZoom()).toInt();
309 qDebug() << "BookView::onSettingsChanged: zoom" << value;
310 setZoomFactor(value / 100.);
312 else if (key == "font") {
313 QString face = s->value(key, p->defaultFont()).toString();
314 qDebug() << "BookView::onSettingsChanged: font" << face;
315 settings()->setFontFamily(QWebSettings::StandardFont, face);
317 else if (key == "scheme") {
318 QWebFrame *frame = page()->mainFrame();
319 QString scheme = Settings::instance()->value("scheme").toString();
320 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
321 (scheme != "default")) {
324 qDebug() << "BookView::onSettingsChanged: scheme" << scheme;
325 QFile script(":/styles/" + scheme + ".js");
326 script.open(QFile::ReadOnly);
327 QString scriptText = script.readAll();
329 (void)frame->evaluateJavaScript(scriptText);
331 else if (key == "usevolumekeys") {
332 bool grab = s->value(key, false).toBool();
333 qDebug() << "BookView::onSettingsChanged: usevolumekeys" << grab;
334 grabVolumeKeys(grab);
338 void BookView::paintEvent(QPaintEvent *e)
340 QWebView::paintEvent(e);
341 if (!mBook || !loaded) {
346 QWebFrame *frame = page()->mainFrame();
347 int contentsHeight = frame->contentsSize().height();
348 QPoint scrollPos = frame->scrollPosition();
349 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
350 QPainter painter(this);
351 foreach (Book::Bookmark b, mBook->bookmarks()) {
352 if (b.part != contentIndex) {
355 int height = contentsHeight;
356 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
357 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
361 pen.setStyle(Qt::DotLine);
364 if (contentIndex > 0) {
365 painter.drawLine(0, -scrollPos.y(), width(), -scrollPos.y());
367 if (contentIndex < (mBook->parts.size() - 1)) {
368 int h = contentsHeight - scrollPos.y() - 1;
369 painter.drawLine(0, h, width(), h);
374 void BookView::mousePressEvent(QMouseEvent *e)
376 QWebView::mousePressEvent(e);
377 #if defined(Q_WS_MAEMO_5)
378 // Start monitoring kinetic scroll
379 if (scrollerMonitor) {
380 killTimer(scrollerMonitor);
384 scrollerMonitor = startTimer(500);
386 #elif defined(Q_OS_SYMBIAN)
389 // Handle mouse press on the scroll bar
390 QWebFrame *frame = page()->mainFrame();
391 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
399 void BookView::wheelEvent(QWheelEvent *e)
401 QWebView::wheelEvent(e);
405 void BookView::addBookmark(const QString ¬e)
411 int y = page()->mainFrame()->scrollPosition().y();
412 int height = page()->mainFrame()->contentsSize().height();
413 qDebug() << ((qreal)y / (qreal)height);
414 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height, note);
418 QString BookView::tmpPath()
420 return QDir::tempPath() + "/dorian";
423 bool BookView::eventFilter(QObject *o, QEvent *e)
426 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
427 if (e->type() == QEvent::Resize) {
428 qDebug() << "BookView::eventFilter QEvent::Resize to"
429 << page()->mainFrame()->contentsSize().height();
430 } else if (e->type() == QEvent::Timer) {
431 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
432 << ((QTimerEvent *)e)->timerId();
434 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
439 // Work around Qt bug that sometimes selects web view contents during swipe
441 case QEvent::MouseButtonPress:
444 case QEvent::MouseButtonRelease:
445 #ifndef Q_OS_SYMBIAN // Too heavy on Symbian
448 mousePressed = false;
450 case QEvent::MouseMove:
455 case QEvent::MouseButtonDblClick:
461 return QObject::eventFilter(o, e);
464 void BookView::addJavaScriptObjects()
467 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
470 void BookView::goToPosition(qreal position)
473 int contentsHeight = page()->mainFrame()->contentsSize().height();
474 int scrollPos = (int)((qreal)contentsHeight * position);
475 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
477 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
478 << (position * 100) << "%, height" << contentsHeight << ")";
481 void BookView::showProgress()
485 int contentsHeight = page()->mainFrame()->contentsSize().height();
486 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
487 (qreal)contentsHeight;
488 emit progress(mBook->getProgress(contentIndex, pos));
492 void BookView::timerEvent(QTimerEvent *e)
494 #if defined(Q_WS_MAEMO_5)
495 if (e->timerId() == scrollerMonitor) {
497 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
498 (scroller->state() == QAbstractKineticScroller::Pushing))) {
501 killTimer(scrollerMonitor);
502 scrollerMonitor = -1;
505 #endif // Q_WS_MAEMO_5
507 QWebView::timerEvent(e);
510 void BookView::hideEvent(QHideEvent *e)
512 Trace t("BookView::hideEvent");
514 #if defined(Q_OS_SYMBIAN)
518 QWebView::hideEvent(e);
521 void BookView::goPreviousPage()
523 QWebFrame *frame = page()->mainFrame();
524 int pos = frame->scrollPosition().y();
525 frame->scroll(0, -(height() - 19));
526 if (pos == frame->scrollPosition().y()) {
527 if (contentIndex > 0) {
528 Book::Bookmark bookmark(contentIndex - 1, 1.0);
529 mBook->setLastBookmark(contentIndex - 1, 1.0);
530 goToBookmark(bookmark);
537 void BookView::goNextPage()
540 QWebFrame *frame = page()->mainFrame();
541 int pos = frame->scrollPosition().y();
542 frame->scroll(0, height() - 19);
543 if (pos == frame->scrollPosition().y()) {
550 void BookView::grabVolumeKeys(bool grab)
553 grabbingVolumeKeys = grab;
558 void BookView::onMediaKeysPressed(MediaKeysObserver::MediaKeys key)
561 qDebug() << "Key" << (int)key;
562 if (grabbingVolumeKeys) {
563 if (key == MediaKeysObserver::EVolIncKey) {
564 qDebug() << "Volume up";
566 } else if (key == MediaKeysObserver::EVolDecKey){
567 qDebug() << "Volume down";
573 #endif // Q_OS_SYMBIAN