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, 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()
245 if (restoreFragmentAfterLoad) {
246 qDebug() << "Restorint to fragment" << fragmentAfterLoad;
247 goToFragment(fragmentAfterLoad);
248 restoreFragmentAfterLoad = false;
249 } else if (restorePositionAfterLoad) {
250 qDebug() << "Restoring to position" << positionAfterLoad;
251 goToPosition(positionAfterLoad);
252 restorePositionAfterLoad = false;
255 emit partLoadEnd(contentIndex);
259 void BookView::onSettingsChanged(const QString &key)
262 qDebug() << key << Settings::instance()->value(key);
265 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
267 else if (key == "font") {
268 QString face = Settings::instance()->value(key).toString();
269 settings()->setFontFamily(QWebSettings::StandardFont, face);
271 else if (key == "scheme") {
272 QWebFrame *frame = page()->mainFrame();
273 QString scheme = Settings::instance()->value("scheme").toString();
274 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
275 (scheme != "default")) {
278 QFile script(":/styles/" + scheme + ".js");
279 script.open(QFile::ReadOnly);
280 QString scriptText = script.readAll();
282 QVariant ret = frame->evaluateJavaScript(scriptText);
284 else if (key == "usevolumekeys") {
285 grabVolumeKeys(Settings::instance()->value(key).toBool());
289 void BookView::paintEvent(QPaintEvent *e)
291 QWebView::paintEvent(e);
292 if (!mBook || !loaded) {
297 QWebFrame *frame = page()->mainFrame();
298 int contentsHeight = frame->contentsSize().height();
299 QPoint scrollPos = frame->scrollPosition();
300 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
301 QPainter painter(this);
302 foreach (Book::Bookmark b, mBook->bookmarks()) {
303 if (b.part != contentIndex) {
306 int height = contentsHeight;
307 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
308 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
312 void BookView::mousePressEvent(QMouseEvent *e)
314 QWebView::mousePressEvent(e);
315 #if defined(Q_WS_MAEMO_5)
316 // Start monitoring kinetic scroll
317 if (scrollerMonitor) {
318 killTimer(scrollerMonitor);
322 scrollerMonitor = startTimer(500);
325 // Handle mouse presses on the scroll bar
326 QWebFrame *frame = page()->mainFrame();
327 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
331 #endif // Q_WS_MAEMO_5
335 void BookView::wheelEvent(QWheelEvent *e)
337 QWebView::wheelEvent(e);
341 void BookView::addBookmark(const QString ¬e)
347 int y = page()->mainFrame()->scrollPosition().y();
348 int height = page()->mainFrame()->contentsSize().height();
349 qDebug() << ((qreal)y / (qreal)height);
350 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height, note);
354 QString BookView::tmpPath()
356 return QDir::tempPath() + "/dorian";
359 bool BookView::eventFilter(QObject *o, QEvent *e)
362 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
363 if (e->type() == QEvent::Resize) {
364 qDebug() << "BookView::eventFilter QEvent::Resize to"
365 << page()->mainFrame()->contentsSize().height();
366 } else if (e->type() == QEvent::Timer) {
367 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
368 << ((QTimerEvent *)e)->timerId();
370 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
375 // Work around Qt bug that sometimes selects web view contents during swipe
377 case QEvent::MouseButtonPress:
378 emit suppressedMouseButtonPress();
381 case QEvent::MouseButtonRelease:
383 mousePressed = false;
385 case QEvent::MouseMove:
390 case QEvent::MouseButtonDblClick:
396 return QObject::eventFilter(o, e);
399 void BookView::addJavaScriptObjects()
401 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
406 void BookView::leaveEvent(QEvent *e)
409 // Save current position, to be restored later
411 QWebView::leaveEvent(e);
414 void BookView::enterEvent(QEvent *e)
417 // Restore position saved at Leave event. This seems to be required,
418 // after temporarily switching from portrait to landscape and back
419 restoreLastBookmark();
420 QWebView::enterEvent(e);
423 #endif // Q_WS_MAEMO_5
425 void BookView::goToPosition(qreal position)
427 int contentsHeight = page()->mainFrame()->contentsSize().height();
428 int scrollPos = (int)((qreal)contentsHeight * position);
429 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
431 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
432 << (position * 100) << "%, height" << contentsHeight << ")";
435 void BookView::showProgress()
438 int contentsHeight = page()->mainFrame()->contentsSize().height();
439 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
440 (qreal)contentsHeight;
441 emit progress(mBook->getProgress(contentIndex, pos));
445 void BookView::timerEvent(QTimerEvent *e)
447 #if defined(Q_WS_MAEMO_5)
448 if (e->timerId() == scrollerMonitor) {
450 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
451 (scroller->state() == QAbstractKineticScroller::Pushing))) {
454 killTimer(scrollerMonitor);
455 scrollerMonitor = -1;
459 QWebView::timerEvent(e);
462 void BookView::goPreviousPage()
464 QWebFrame *frame = page()->mainFrame();
465 int pos = frame->scrollPosition().y();
466 frame->scroll(0, -height());
467 if (pos == frame->scrollPosition().y()) {
468 if (contentIndex > 0) {
469 Book::Bookmark bookmark(contentIndex - 1, 1.0);
470 mBook->setLastBookmark(contentIndex - 1, 1.0);
471 goToBookmark(bookmark);
478 void BookView::goNextPage()
481 QWebFrame *frame = page()->mainFrame();
482 int pos = frame->scrollPosition().y();
483 frame->scroll(0, height());
484 if (pos == frame->scrollPosition().y()) {
492 void BookView::grabVolumeKeys(bool grab)
495 grabbingVolumeKeys = grab;
500 void BookView::onMediaKeysPressed(MediaKeysObserver::MediaKeys key)
503 qDebug() << "Key" << (int)key;
504 if (grabbingVolumeKeys) {
505 if (key == MediaKeysObserver::EVolIncKey) {
506 qDebug() << "Volume up";
508 } else if (key == MediaKeysObserver::EVolDecKey){
509 qDebug() << "Volume down";
515 #endif // Q_OS_SYMBIAN