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 // Set up web view defaults
26 settings()->setAttribute(QWebSettings::AutoLoadImages, true);
27 settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
28 settings()->setAttribute(QWebSettings::JavaEnabled, false);
29 settings()->setAttribute(QWebSettings::PluginsEnabled, false);
30 settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
31 settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
32 settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard,
34 settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled,
36 settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled,
38 settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
39 settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
40 settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
42 settings()->setDefaultTextEncoding("utf-8");
43 page()->setContentEditable(false);
44 QWebFrame *frame = page()->mainFrame();
45 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
46 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
48 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
49 connect(this, SIGNAL(loadFinished(bool)),
50 this, SLOT(onLoadFinished(bool)));
51 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
52 this, SLOT(addJavaScriptObjects()));
54 // Suppress unwanted text selections on Maemo and Symbian
55 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
56 installEventFilter(this);
59 // Pre-load bookmark icon
60 bookmarkImage = QImage(":/icons/bookmark.png");
62 // Handle settings changes, force handling initial settings
63 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
64 this, SLOT(onSettingsChanged(const QString &)));
67 // Enable kinetic scrolling
68 #if defined(Q_WS_MAEMO_5)
70 scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
71 #elif defined(Q_OS_SYMBIAN)
73 charm = new FlickCharm(this);
74 charm->activateOn(this);
77 // Observe media keys on Symbian
79 MediaKeysObserver *observer = MediaKeysObserver::instance();
80 connect(observer, SIGNAL(mediaKeyPressed(MediaKeysObserver::MediaKeys)),
81 this, SLOT(onMediaKeysPressed(MediaKeysObserver::MediaKeys)));
85 void BookView::loadContent(int index)
92 if ((index < 0) || (index >= mBook->parts.size())) {
96 QString contentFile(mBook->content[mBook->parts[index]].href);
97 if (mBook->parts[index] == "error") {
101 emit partLoadStart(index);
102 QUrl u = QUrl::fromLocalFile(QDir(mBook->rootPath()).
103 absoluteFilePath(contentFile));
104 qDebug() << "Loading" << u;
107 contentIndex = index;
110 void BookView::setBook(Book *book)
114 // Save position in current book
117 // Open new book, restore last position
123 restoreLastBookmark();
127 setHtml(tr("Failed to open book"));
132 setHtml(tr("No book"));
137 Book *BookView::book()
142 void BookView::goPrevious()
145 if (mBook && (contentIndex > 0)) {
146 mBook->setLastBookmark(contentIndex - 1, 0);
147 loadContent(contentIndex - 1);
151 void BookView::goNext()
154 if (mBook && (contentIndex < (mBook->parts.size() - 1))) {
155 mBook->setLastBookmark(contentIndex + 1, 0);
156 loadContent(contentIndex + 1);
160 void BookView::setLastBookmark(bool fast)
164 QWebFrame *frame = page()->mainFrame();
165 int height = frame->contentsSize().height();
166 int pos = frame->scrollPosition().y();
167 qDebug() << QString("At %1 (%2%, height %3)").
168 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
169 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height, fast);
173 void BookView::restoreLastBookmark()
177 goToBookmark(mBook->lastBookmark());
181 void BookView::goToBookmark(const Book::Bookmark &bookmark)
185 if (bookmark.part != contentIndex) {
186 qDebug () << "Loading new part" << bookmark.part;
187 mBook->setLastBookmark(bookmark.part, bookmark.pos);
188 restorePositionAfterLoad = true;
189 positionAfterLoad = bookmark.pos;
190 loadContent(bookmark.part);
192 goToPosition(bookmark.pos);
197 void BookView::goToPart(int part, const QString &fragment)
201 if (fragment.isEmpty()) {
202 goToBookmark(Book::Bookmark(part, 0));
204 if (part != contentIndex) {
205 qDebug() << "Loading new part" << part;
206 restoreFragmentAfterLoad = true;
207 fragmentAfterLoad = fragment;
210 goToFragment(fragment);
217 void BookView::goToFragment(const QString &fragment)
220 if (!fragment.isEmpty()) {
221 QVariant ret = page()->mainFrame()->evaluateJavaScript(
222 QString("window.location='") + fragment + "'");
224 // FIXME: setLastBookmark();
228 void BookView::onLoadFinished(bool ok)
232 qDebug() << "Not OK";
236 onSettingsChanged("scheme");
237 onSettingsChanged("zoom");
238 onSettingsChanged("font");
240 QTimer::singleShot(210, this, SLOT(restoreAfterLoad()));
243 void BookView::restoreAfterLoad()
246 if (restoreFragmentAfterLoad) {
247 qDebug() << "Restorint to fragment" << fragmentAfterLoad;
248 goToFragment(fragmentAfterLoad);
249 restoreFragmentAfterLoad = false;
250 } else if (restorePositionAfterLoad) {
251 qDebug() << "Restoring to position" << positionAfterLoad;
252 goToPosition(positionAfterLoad);
253 restorePositionAfterLoad = false;
256 emit partLoadEnd(contentIndex);
260 void BookView::onSettingsChanged(const QString &key)
262 Settings *s = Settings::instance();
263 Platform *p = Platform::instance();
266 int value = s->value(key, p->defaultZoom()).toInt();
267 qDebug() << "BookView::onSettingsChanged: zoom" << value;
268 setZoomFactor(value / 100.);
270 else if (key == "font") {
271 QString face = s->value(key, p->defaultFont()).toString();
272 qDebug() << "BookView::onSettingsChanged: font" << face;
273 settings()->setFontFamily(QWebSettings::StandardFont, face);
275 else if (key == "scheme") {
276 QWebFrame *frame = page()->mainFrame();
277 QString scheme = Settings::instance()->value("scheme").toString();
278 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
279 (scheme != "default")) {
282 qDebug() << "BookView::onSettingsChanged: scheme" << scheme;
283 QFile script(":/styles/" + scheme + ".js");
284 script.open(QFile::ReadOnly);
285 QString scriptText = script.readAll();
287 (void)frame->evaluateJavaScript(scriptText);
289 else if (key == "usevolumekeys") {
290 bool grab = s->value(key, false).toBool();
291 qDebug() << "BookView::onSettingsChanged: usevolumekeys" << grab;
292 grabVolumeKeys(grab);
296 void BookView::paintEvent(QPaintEvent *e)
298 QWebView::paintEvent(e);
299 if (!mBook || !loaded) {
304 QWebFrame *frame = page()->mainFrame();
305 int contentsHeight = frame->contentsSize().height();
306 QPoint scrollPos = frame->scrollPosition();
307 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
308 QPainter painter(this);
309 foreach (Book::Bookmark b, mBook->bookmarks()) {
310 if (b.part != contentIndex) {
313 int height = contentsHeight;
314 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
315 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
319 pen.setStyle(Qt::DotLine);
322 if (contentIndex > 0) {
323 painter.drawLine(0, -scrollPos.y(), width(), -scrollPos.y());
325 if (contentIndex < (mBook->parts.size() - 1)) {
326 int h = contentsHeight - scrollPos.y() - 1;
327 painter.drawLine(0, h, width(), h);
332 void BookView::mousePressEvent(QMouseEvent *e)
334 QWebView::mousePressEvent(e);
335 #if defined(Q_WS_MAEMO_5)
336 // Start monitoring kinetic scroll
337 if (scrollerMonitor) {
338 killTimer(scrollerMonitor);
342 scrollerMonitor = startTimer(500);
344 #elif defined(Q_OS_SYMBIAN)
347 // Handle mouse press on the scroll bar
348 QWebFrame *frame = page()->mainFrame();
349 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
357 void BookView::wheelEvent(QWheelEvent *e)
359 QWebView::wheelEvent(e);
363 void BookView::addBookmark(const QString ¬e)
369 int y = page()->mainFrame()->scrollPosition().y();
370 int height = page()->mainFrame()->contentsSize().height();
371 qDebug() << ((qreal)y / (qreal)height);
372 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height, note);
376 QString BookView::tmpPath()
378 return QDir::tempPath() + "/dorian";
381 bool BookView::eventFilter(QObject *o, QEvent *e)
384 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
385 if (e->type() == QEvent::Resize) {
386 qDebug() << "BookView::eventFilter QEvent::Resize to"
387 << page()->mainFrame()->contentsSize().height();
388 } else if (e->type() == QEvent::Timer) {
389 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
390 << ((QTimerEvent *)e)->timerId();
392 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
397 // Work around Qt bug that sometimes selects web view contents during swipe
399 case QEvent::MouseButtonPress:
402 case QEvent::MouseButtonRelease:
403 #ifndef Q_OS_SYMBIAN // Too heavy on Symbian
406 mousePressed = false;
408 case QEvent::MouseMove:
413 case QEvent::MouseButtonDblClick:
419 return QObject::eventFilter(o, e);
422 void BookView::addJavaScriptObjects()
424 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
427 void BookView::goToPosition(qreal position)
429 int contentsHeight = page()->mainFrame()->contentsSize().height();
430 int scrollPos = (int)((qreal)contentsHeight * position);
431 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
433 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
434 << (position * 100) << "%, height" << contentsHeight << ")";
437 void BookView::showProgress()
441 int contentsHeight = page()->mainFrame()->contentsSize().height();
442 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
443 (qreal)contentsHeight;
444 emit progress(mBook->getProgress(contentIndex, pos));
448 void BookView::timerEvent(QTimerEvent *e)
450 #if defined(Q_WS_MAEMO_5)
451 if (e->timerId() == scrollerMonitor) {
453 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
454 (scroller->state() == QAbstractKineticScroller::Pushing))) {
457 killTimer(scrollerMonitor);
458 scrollerMonitor = -1;
461 #endif // Q_WS_MAEMO_5
463 QWebView::timerEvent(e);
466 void BookView::goPreviousPage()
468 QWebFrame *frame = page()->mainFrame();
469 int pos = frame->scrollPosition().y();
470 frame->scroll(0, -(height() - 19));
471 if (pos == frame->scrollPosition().y()) {
472 if (contentIndex > 0) {
473 Book::Bookmark bookmark(contentIndex - 1, 1.0);
474 mBook->setLastBookmark(contentIndex - 1, 1.0);
475 goToBookmark(bookmark);
482 void BookView::goNextPage()
485 QWebFrame *frame = page()->mainFrame();
486 int pos = frame->scrollPosition().y();
487 frame->scroll(0, height() - 19);
488 if (pos == frame->scrollPosition().y()) {
495 void BookView::grabVolumeKeys(bool grab)
498 grabbingVolumeKeys = grab;
503 void BookView::onMediaKeysPressed(MediaKeysObserver::MediaKeys key)
506 qDebug() << "Key" << (int)key;
507 if (grabbingVolumeKeys) {
508 if (key == MediaKeysObserver::EVolIncKey) {
509 qDebug() << "Volume up";
511 } else if (key == MediaKeysObserver::EVolDecKey){
512 qDebug() << "Volume down";
518 #endif // Q_OS_SYMBIAN
522 void BookView::adjustPosition()
526 // QWebView widget doesn't maintain reading positon during orientation
527 // switch. This heuristics tries to work around it.
529 const qreal portraitMagic = 1.66;
530 const qreal landscapeMagic = 0.655;
532 QSize desktop = QApplication::desktop()->size();
533 int screenHeight = desktop.height();
534 int screenWidth = desktop.width();
535 qreal ratio = (screenWidth<screenHeight)? portraitMagic: landscapeMagic;
536 QWebFrame *frame = page()->mainFrame();
537 int current = frame->scrollPosition().y();
538 frame->scroll(0, (int)(current * ratio) - current);
542 #endif // Q_OS_SYMBIAN