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 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, false);
33 settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled,
35 settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled,
37 settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
38 settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
39 settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
41 settings()->setDefaultTextEncoding("utf-8");
42 page()->setContentEditable(false);
43 QWebFrame *frame = page()->mainFrame();
44 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
45 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
47 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
48 connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
49 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
50 this, SLOT(addJavaScriptObjects()));
52 // Suppress unwanted text selections on Maemo and Symbian
53 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
54 installEventFilter(this);
57 // Pre-load bookmark icon
58 bookmarkImage = QImage(":/icons/bookmark.png");
60 // Handle settings changes, force handling initial settings
61 Settings *s = Settings::instance();
62 connect(s, SIGNAL(valueChanged(const QString &)),
63 this, SLOT(onSettingsChanged(const QString &)));
64 s->setValue("zoom", s->value("zoom", 160));
65 s->setValue("font", s->value("font", Platform::defaultFont()));
66 s->setValue("scheme", s->value("scheme", "default"));
67 s->setValue("usevolumekeys", s->value("usevolumekeys", false));
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 // Save position in current book
120 // Open new book, restore last position
126 restoreLastBookmark();
130 setHtml(tr("Failed to open book"));
135 setHtml(tr("No book"));
140 Book *BookView::book()
145 void BookView::goPrevious()
148 if (mBook && (contentIndex > 0)) {
149 mBook->setLastBookmark(contentIndex - 1, 0);
150 loadContent(contentIndex - 1);
154 void BookView::goNext()
157 if (mBook && (contentIndex < (mBook->parts.size() - 1))) {
158 mBook->setLastBookmark(contentIndex + 1, 0);
159 loadContent(contentIndex + 1);
163 void BookView::setLastBookmark()
167 QWebFrame *frame = page()->mainFrame();
168 int height = frame->contentsSize().height();
169 int pos = frame->scrollPosition().y();
170 qDebug() << QString("At %1 (%2%, height %3)").
171 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
172 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
176 void BookView::restoreLastBookmark()
180 goToBookmark(mBook->lastBookmark());
184 void BookView::goToBookmark(const Book::Bookmark &bookmark)
188 if (bookmark.part != contentIndex) {
189 qDebug () << "Loading new part" << bookmark.part;
190 mBook->setLastBookmark(bookmark.part, bookmark.pos);
191 restorePositionAfterLoad = true;
192 positionAfterLoad = bookmark.pos;
193 loadContent(bookmark.part);
195 goToPosition(bookmark.pos);
200 void BookView::goToPart(int part, const QString &fragment)
204 if (part != contentIndex) {
205 qDebug() << "Loading new part" << part;
206 restoreFragmentAfterLoad = true;
207 fragmentAfterLoad = fragment;
210 goToFragment(fragment);
216 void BookView::goToFragment(const QString &fragment)
219 if (!fragment.isEmpty()) {
220 QVariant ret = page()->mainFrame()->evaluateJavaScript(
221 QString("window.location='") + fragment + "'");
227 void BookView::onLoadFinished(bool ok)
231 qDebug() << "Not OK";
235 onSettingsChanged("scheme");
236 onSettingsChanged("zoom");
237 onSettingsChanged("font");
239 QTimer::singleShot(210, this, SLOT(restoreAfterLoad()));
242 void BookView::restoreAfterLoad()
244 if (restoreFragmentAfterLoad) {
245 qDebug() << "Restorint to fragment" << fragmentAfterLoad;
246 goToFragment(fragmentAfterLoad);
247 restoreFragmentAfterLoad = false;
248 } else if (restorePositionAfterLoad) {
249 qDebug() << "Restoring to position" << positionAfterLoad;
250 goToPosition(positionAfterLoad);
251 restorePositionAfterLoad = false;
254 emit partLoadEnd(contentIndex);
258 void BookView::onSettingsChanged(const QString &key)
261 qDebug() << key << Settings::instance()->value(key);
264 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
266 else if (key == "font") {
267 QString face = Settings::instance()->value(key).toString();
268 settings()->setFontFamily(QWebSettings::StandardFont, face);
270 else if (key == "scheme") {
271 QWebFrame *frame = page()->mainFrame();
272 QString scheme = Settings::instance()->value("scheme").toString();
273 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
274 (scheme != "default")) {
277 QFile script(":/styles/" + scheme + ".js");
278 script.open(QFile::ReadOnly);
279 QString scriptText = script.readAll();
281 QVariant ret = frame->evaluateJavaScript(scriptText);
283 else if (key == "usevolumekeys") {
284 grabVolumeKeys(Settings::instance()->value(key).toBool());
288 void BookView::paintEvent(QPaintEvent *e)
290 QWebView::paintEvent(e);
291 if (!mBook || !loaded) {
296 QWebFrame *frame = page()->mainFrame();
297 int contentsHeight = frame->contentsSize().height();
298 QPoint scrollPos = frame->scrollPosition();
299 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
300 QPainter painter(this);
301 foreach (Book::Bookmark b, mBook->bookmarks()) {
302 if (b.part != contentIndex) {
305 int height = contentsHeight;
306 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
307 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
311 void BookView::mousePressEvent(QMouseEvent *e)
313 QWebView::mousePressEvent(e);
314 #if defined(Q_WS_MAEMO_5)
315 // Start monitoring kinetic scroll
316 if (scrollerMonitor) {
317 killTimer(scrollerMonitor);
321 scrollerMonitor = startTimer(500);
324 // Handle mouse presses on the scroll bar
325 QWebFrame *frame = page()->mainFrame();
326 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
330 #endif // Q_WS_MAEMO_5
334 void BookView::wheelEvent(QWheelEvent *e)
336 QWebView::wheelEvent(e);
340 void BookView::addBookmark(const QString ¬e)
346 int y = page()->mainFrame()->scrollPosition().y();
347 int height = page()->mainFrame()->contentsSize().height();
348 qDebug() << ((qreal)y / (qreal)height);
349 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height, note);
353 QString BookView::tmpPath()
355 return QDir::tempPath() + "/dorian";
358 bool BookView::eventFilter(QObject *o, QEvent *e)
361 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
362 if (e->type() == QEvent::Resize) {
363 qDebug() << "BookView::eventFilter QEvent::Resize to"
364 << page()->mainFrame()->contentsSize().height();
365 } else if (e->type() == QEvent::Timer) {
366 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
367 << ((QTimerEvent *)e)->timerId();
369 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
374 // Work around Qt bug that sometimes selects web view contents during swipe
376 case QEvent::MouseButtonPress:
377 emit suppressedMouseButtonPress();
380 case QEvent::MouseButtonRelease:
382 mousePressed = false;
384 case QEvent::MouseMove:
389 case QEvent::MouseButtonDblClick:
395 return QObject::eventFilter(o, e);
398 void BookView::addJavaScriptObjects()
400 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
405 void BookView::leaveEvent(QEvent *e)
408 // Save current position, to be restored later
410 QWebView::leaveEvent(e);
413 void BookView::enterEvent(QEvent *e)
416 // Restore position saved at Leave event. This seems to be required,
417 // after temporarily switching from portrait to landscape and back
418 restoreLastBookmark();
419 QWebView::enterEvent(e);
422 #endif // Q_WS_MAEMO_5
424 void BookView::goToPosition(qreal position)
426 int contentsHeight = page()->mainFrame()->contentsSize().height();
427 int scrollPos = (int)((qreal)contentsHeight * position);
428 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
430 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
431 << (position * 100) << "%, height" << contentsHeight << ")";
434 void BookView::showProgress()
437 int contentsHeight = page()->mainFrame()->contentsSize().height();
438 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
439 (qreal)contentsHeight;
440 emit progress(mBook->getProgress(contentIndex, pos));
444 void BookView::timerEvent(QTimerEvent *e)
446 #if defined(Q_WS_MAEMO_5)
447 if (e->timerId() == scrollerMonitor) {
449 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
450 (scroller->state() == QAbstractKineticScroller::Pushing))) {
453 killTimer(scrollerMonitor);
454 scrollerMonitor = -1;
458 QWebView::timerEvent(e);
461 void BookView::goPreviousPage()
463 QWebFrame *frame = page()->mainFrame();
464 int pos = frame->scrollPosition().y();
465 frame->scroll(0, -height());
466 if (pos == frame->scrollPosition().y()) {
467 if (contentIndex > 0) {
468 Book::Bookmark bookmark(contentIndex - 1, 1.0);
469 mBook->setLastBookmark(contentIndex - 1, 1.0);
470 goToBookmark(bookmark);
477 void BookView::goNextPage()
480 QWebFrame *frame = page()->mainFrame();
481 int pos = frame->scrollPosition().y();
482 frame->scroll(0, height());
483 if (pos == frame->scrollPosition().y()) {
491 void BookView::grabVolumeKeys(bool grab)
494 grabbingVolumeKeys = grab;
499 void BookView::onMediaKeysPressed(MediaKeysObserver::MediaKeys key)
502 qDebug() << "Key" << (int)key;
503 if (grabbingVolumeKeys) {
504 if (key == MediaKeysObserver::EVolIncKey) {
505 qDebug() << "Volume up";
507 } else if (key == MediaKeysObserver::EVolDecKey){
508 qDebug() << "Volume down";
514 #endif // Q_OS_SYMBIAN