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 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
62 this, SLOT(onSettingsChanged(const QString &)));
65 // Enable kinetic scrolling
66 #if defined(Q_WS_MAEMO_5)
68 scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
69 #elif defined(Q_OS_SYMBIAN)
71 charm = new FlickCharm(this);
72 charm->activateOn(this);
75 // Observe media keys on Symbian
77 MediaKeysObserver *observer = MediaKeysObserver::instance();
78 connect(observer, SIGNAL(mediaKeyPressed(MediaKeysObserver::MediaKeys)),
79 this, SLOT(onMediaKeysPressed(MediaKeysObserver::MediaKeys)));
83 void BookView::loadContent(int index)
90 if ((index < 0) || (index >= mBook->parts.size())) {
94 QString contentFile(mBook->content[mBook->parts[index]].href);
95 if (mBook->parts[index] == "error") {
99 emit partLoadStart(index);
100 QUrl u = QUrl::fromLocalFile(QDir(mBook->rootPath()).
101 absoluteFilePath(contentFile));
102 qDebug() << "Loading" << u;
105 contentIndex = index;
108 void BookView::setBook(Book *book)
112 // Save position in current book
115 // Open new book, restore last position
121 restoreLastBookmark();
125 setHtml(tr("Failed to open book"));
130 setHtml(tr("No book"));
135 Book *BookView::book()
140 void BookView::goPrevious()
143 if (mBook && (contentIndex > 0)) {
144 mBook->setLastBookmark(contentIndex - 1, 0);
145 loadContent(contentIndex - 1);
149 void BookView::goNext()
152 if (mBook && (contentIndex < (mBook->parts.size() - 1))) {
153 mBook->setLastBookmark(contentIndex + 1, 0);
154 loadContent(contentIndex + 1);
158 void BookView::setLastBookmark()
162 QWebFrame *frame = page()->mainFrame();
163 int height = frame->contentsSize().height();
164 int pos = frame->scrollPosition().y();
165 qDebug() << QString("At %1 (%2%, height %3)").
166 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
167 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
171 void BookView::restoreLastBookmark()
175 goToBookmark(mBook->lastBookmark());
179 void BookView::goToBookmark(const Book::Bookmark &bookmark)
183 if (bookmark.part != contentIndex) {
184 qDebug () << "Loading new part" << bookmark.part;
185 mBook->setLastBookmark(bookmark.part, bookmark.pos);
186 restorePositionAfterLoad = true;
187 positionAfterLoad = bookmark.pos;
188 loadContent(bookmark.part);
190 goToPosition(bookmark.pos);
195 void BookView::goToPart(int part, const QString &fragment)
199 if (fragment.isEmpty()) {
200 goToBookmark(Book::Bookmark(part, 0));
202 if (part != contentIndex) {
203 qDebug() << "Loading new part" << part;
204 restoreFragmentAfterLoad = true;
205 fragmentAfterLoad = fragment;
208 goToFragment(fragment);
215 void BookView::goToFragment(const QString &fragment)
218 if (!fragment.isEmpty()) {
219 QVariant ret = page()->mainFrame()->evaluateJavaScript(
220 QString("window.location='") + fragment + "'");
222 // FIXME: setLastBookmark();
226 void BookView::onLoadFinished(bool ok)
230 qDebug() << "Not OK";
234 onSettingsChanged("scheme");
235 onSettingsChanged("zoom");
236 onSettingsChanged("font");
238 QTimer::singleShot(210, this, SLOT(restoreAfterLoad()));
241 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)
260 Settings *s = Settings::instance();
261 Platform *p = Platform::instance();
264 int value = s->value(key, p->defaultZoom()).toInt();
265 qDebug() << "BookView::onSettingsChanged: zoom" << value;
266 setZoomFactor(value / 100.);
268 else if (key == "font") {
269 QString face = s->value(key, p->defaultFont()).toString();
270 qDebug() << "BookView::onSettingsChanged: font" << face;
271 settings()->setFontFamily(QWebSettings::StandardFont, face);
273 else if (key == "scheme") {
274 QWebFrame *frame = page()->mainFrame();
275 QString scheme = Settings::instance()->value("scheme").toString();
276 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
277 (scheme != "default")) {
280 qDebug() << "BookView::onSettingsChanged: scheme" << scheme;
281 QFile script(":/styles/" + scheme + ".js");
282 script.open(QFile::ReadOnly);
283 QString scriptText = script.readAll();
285 (void)frame->evaluateJavaScript(scriptText);
287 else if (key == "usevolumekeys") {
288 bool grab = s->value(key, false).toBool();
289 qDebug() << "BookView::onSettingsChanged: usevolumekeys" << grab;
290 grabVolumeKeys(grab);
294 void BookView::paintEvent(QPaintEvent *e)
296 QWebView::paintEvent(e);
297 if (!mBook || !loaded) {
302 QWebFrame *frame = page()->mainFrame();
303 int contentsHeight = frame->contentsSize().height();
304 QPoint scrollPos = frame->scrollPosition();
305 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
306 QPainter painter(this);
307 foreach (Book::Bookmark b, mBook->bookmarks()) {
308 if (b.part != contentIndex) {
311 int height = contentsHeight;
312 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
313 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
317 void BookView::mousePressEvent(QMouseEvent *e)
319 QWebView::mousePressEvent(e);
320 #if defined(Q_WS_MAEMO_5)
321 // Start monitoring kinetic scroll
322 if (scrollerMonitor) {
323 killTimer(scrollerMonitor);
327 scrollerMonitor = startTimer(500);
330 // Handle mouse presses on the scroll bar
331 QWebFrame *frame = page()->mainFrame();
332 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
336 #endif // Q_WS_MAEMO_5
340 void BookView::wheelEvent(QWheelEvent *e)
342 QWebView::wheelEvent(e);
346 void BookView::addBookmark(const QString ¬e)
352 int y = page()->mainFrame()->scrollPosition().y();
353 int height = page()->mainFrame()->contentsSize().height();
354 qDebug() << ((qreal)y / (qreal)height);
355 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height, note);
359 QString BookView::tmpPath()
361 return QDir::tempPath() + "/dorian";
364 bool BookView::eventFilter(QObject *o, QEvent *e)
367 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
368 if (e->type() == QEvent::Resize) {
369 qDebug() << "BookView::eventFilter QEvent::Resize to"
370 << page()->mainFrame()->contentsSize().height();
371 } else if (e->type() == QEvent::Timer) {
372 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
373 << ((QTimerEvent *)e)->timerId();
375 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
380 // Work around Qt bug that sometimes selects web view contents during swipe
382 case QEvent::MouseButtonPress:
383 emit suppressedMouseButtonPress();
386 case QEvent::MouseButtonRelease:
388 mousePressed = false;
390 case QEvent::MouseMove:
395 case QEvent::MouseButtonDblClick:
401 return QObject::eventFilter(o, e);
404 void BookView::addJavaScriptObjects()
406 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
409 void BookView::goToPosition(qreal position)
411 int contentsHeight = page()->mainFrame()->contentsSize().height();
412 int scrollPos = (int)((qreal)contentsHeight * position);
413 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
415 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
416 << (position * 100) << "%, height" << contentsHeight << ")";
419 void BookView::showProgress()
422 int contentsHeight = page()->mainFrame()->contentsSize().height();
423 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
424 (qreal)contentsHeight;
425 emit progress(mBook->getProgress(contentIndex, pos));
429 void BookView::timerEvent(QTimerEvent *e)
431 #if defined(Q_WS_MAEMO_5)
432 if (e->timerId() == scrollerMonitor) {
434 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
435 (scroller->state() == QAbstractKineticScroller::Pushing))) {
438 killTimer(scrollerMonitor);
439 scrollerMonitor = -1;
443 QWebView::timerEvent(e);
446 void BookView::goPreviousPage()
448 QWebFrame *frame = page()->mainFrame();
449 int pos = frame->scrollPosition().y();
450 frame->scroll(0, -height());
451 if (pos == frame->scrollPosition().y()) {
452 if (contentIndex > 0) {
453 Book::Bookmark bookmark(contentIndex - 1, 1.0);
454 mBook->setLastBookmark(contentIndex - 1, 1.0);
455 goToBookmark(bookmark);
462 void BookView::goNextPage()
465 QWebFrame *frame = page()->mainFrame();
466 int pos = frame->scrollPosition().y();
467 frame->scroll(0, height());
468 if (pos == frame->scrollPosition().y()) {
471 // setLastBookmark();
476 void BookView::grabVolumeKeys(bool grab)
479 grabbingVolumeKeys = grab;
484 void BookView::onMediaKeysPressed(MediaKeysObserver::MediaKeys key)
487 qDebug() << "Key" << (int)key;
488 if (grabbingVolumeKeys) {
489 if (key == MediaKeysObserver::EVolIncKey) {
490 qDebug() << "Volume up";
492 } else if (key == MediaKeysObserver::EVolDecKey){
493 qDebug() << "Volume down";
499 #endif // Q_OS_SYMBIAN