5 #if defined(Q_WS_MAEMO_5)
6 # include <QAbstractKineticScroller>
7 #elif defined(Q_OS_SYMBIAN)
8 # include "flickcharm.h"
17 #include "progressdialog.h"
20 BookView::BookView(QWidget *parent):
21 QWebView(parent), contentIndex(-1), mBook(0),
22 restorePositionAfterLoad(false), positionAfterLoad(0), loaded(false),
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);
44 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
45 // Suppress unwanted text selections on Maemo and Symbian
46 installEventFilter(this);
48 QWebFrame *frame = page()->mainFrame();
49 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
50 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
52 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
54 bookmarkImage = QImage(":/icons/bookmark.png");
56 connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
57 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
58 this, SLOT(addJavaScriptObjects()));
59 connect(frame, SIGNAL(contentsSizeChanged(const QSize &)),
60 this, SLOT(onContentsSizeChanged(const QSize &)));
61 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
62 this, SLOT(onSettingsChanged(const QString &)));
63 Settings *s = Settings::instance();
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"));
69 #if defined(Q_WS_MAEMO_5)
71 scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
72 #elif defined(Q_OS_SYMBIAN)
74 charm = new FlickCharm(this);
75 charm->activateOn(this);
84 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 int height = contentsHeight;
163 int pos = page()->mainFrame()->scrollPosition().y();
164 qDebug() << QString("At %1 (%2%, height %3)").
165 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
166 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
170 void BookView::restoreLastBookmark()
174 goToBookmark(mBook->lastBookmark());
178 void BookView::goToBookmark(const Book::Bookmark &bookmark)
182 if (bookmark.part != contentIndex) {
183 qDebug () << "Loading new part" << bookmark.part;
184 mBook->setLastBookmark(bookmark.part, bookmark.pos);
185 restorePositionAfterLoad = true;
186 positionAfterLoad = bookmark.pos;
187 loadContent(bookmark.part);
189 goToPosition(bookmark.pos);
194 void BookView::goToPart(int part, const QString &fragment)
198 if (part != contentIndex) {
199 qDebug() << "Loading new part" << part;
200 restoreFragmentAfterLoad = true;
201 fragmentAfterLoad = fragment;
204 goToFragment(fragment);
210 void BookView::goToFragment(const QString &fragment)
213 if (!fragment.isEmpty()) {
214 QVariant ret = page()->mainFrame()->evaluateJavaScript(
215 QString("window.location='") + fragment + "'");
221 void BookView::onLoadFinished(bool ok)
225 qDebug() << "Not OK";
229 onSettingsChanged("scheme");
230 onSettingsChanged("zoom");
231 onSettingsChanged("font");
232 emit partLoadEnd(contentIndex);
236 void BookView::onSettingsChanged(const QString &key)
239 qDebug() << key << Settings::instance()->value(key);
242 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
244 else if (key == "font") {
245 QString face = Settings::instance()->value(key).toString();
246 settings()->setFontFamily(QWebSettings::StandardFont, face);
248 else if (key == "scheme") {
249 QWebFrame *frame = page()->mainFrame();
250 QString scheme = Settings::instance()->value("scheme").toString();
251 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
252 (scheme != "default")) {
255 QFile script(":/styles/" + scheme + ".js");
256 script.open(QFile::ReadOnly);
257 QString scriptText = script.readAll();
259 QVariant ret = frame->evaluateJavaScript(scriptText);
263 void BookView::paintEvent(QPaintEvent *e)
265 QWebView::paintEvent(e);
266 if (!mBook || !loaded) {
271 QPoint scrollPos = page()->mainFrame()->scrollPosition();
272 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
273 QPainter painter(this);
274 foreach (Book::Bookmark b, mBook->bookmarks()) {
275 if (b.part != contentIndex) {
278 int height = contentsHeight;
279 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
280 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
284 void BookView::mousePressEvent(QMouseEvent *e)
286 QWebView::mousePressEvent(e);
287 #if defined(Q_WS_MAEMO_5)
288 // Start monitoring kinetic scroll
289 if (scrollerMonitor) {
290 killTimer(scrollerMonitor);
294 scrollerMonitor = startTimer(500);
297 // Handle mouse presses on the scroll bar
298 QWebFrame *frame = page()->mainFrame();
299 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
303 #endif // Q_WS_MAEMO_5
307 void BookView::wheelEvent(QWheelEvent *e)
309 QWebView::wheelEvent(e);
313 void BookView::addBookmark(const QString ¬e)
319 int y = page()->mainFrame()->scrollPosition().y();
320 int height = page()->mainFrame()->contentsSize().height();
321 qDebug() << ((qreal)y / (qreal)height);
322 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height, note);
326 QString BookView::tmpPath()
328 return QDir::tempPath() + "/dorian";
331 bool BookView::eventFilter(QObject *o, QEvent *e)
334 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
335 if (e->type() == QEvent::Resize) {
336 qDebug() << "BookView::eventFilter QEvent::Resize to"
337 << page()->mainFrame()->contentsSize().height();
338 } else if (e->type() == QEvent::Timer) {
339 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
340 << ((QTimerEvent *)e)->timerId();
342 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
347 // Work around Qt bug that sometimes selects web view contents during swipe
349 case QEvent::MouseButtonPress:
350 emit suppressedMouseButtonPress();
353 case QEvent::MouseButtonRelease:
355 mousePressed = false;
357 case QEvent::MouseMove:
362 case QEvent::MouseButtonDblClick:
368 return QObject::eventFilter(o, e);
371 void BookView::addJavaScriptObjects()
373 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
376 void BookView::onContentsSizeChanged(const QSize &size)
379 contentsHeight = size.height();
380 if (restoreFragmentAfterLoad) {
381 qDebug() << "Restorint to fragment" << fragmentAfterLoad;
382 goToFragment(fragmentAfterLoad);
383 } else if (restorePositionAfterLoad) {
384 qDebug() << "Restoring to position";
385 goToPosition(positionAfterLoad);
387 restorePositionAfterLoad = false;
388 restoreFragmentAfterLoad = false;
393 void BookView::leaveEvent(QEvent *e)
396 // Save current position, to be restored later
398 QWebView::leaveEvent(e);
401 void BookView::enterEvent(QEvent *e)
404 // Restore position saved at Leave event. This seems to be required,
405 // after temporarily switching from portrait to landscape and back
406 restoreLastBookmark();
407 QWebView::enterEvent(e);
410 #endif // Q_WS_MAEMO_5
412 void BookView::goToPosition(qreal position)
414 int scrollPos = (int)((qreal)contentsHeight * position);
415 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
417 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
418 << (position * 100) << "%, height" << contentsHeight << ")";
421 void BookView::showProgress()
424 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
425 (qreal)contentsHeight;
426 emit progress(mBook->getProgress(contentIndex, pos));
430 void BookView::timerEvent(QTimerEvent *e)
432 #if defined(Q_WS_MAEMO_5)
433 if (e->timerId() == scrollerMonitor) {
435 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
436 (scroller->state() == QAbstractKineticScroller::Pushing))) {
439 killTimer(scrollerMonitor);
440 scrollerMonitor = -1;
444 QWebView::timerEvent(e);
447 void BookView::keyPressEvent(QKeyEvent *event)
449 switch (event->key()) {
461 QWebView::keyPressEvent(event);
464 void BookView::goPreviousPage()
466 QWebFrame *frame = page()->mainFrame();
467 int pos = frame->scrollPosition().y();
468 frame->scroll(0, -height());
469 if (pos == frame->scrollPosition().y()) {
470 if (contentIndex > 0) {
471 Book::Bookmark bookmark(contentIndex - 1, 1.0);
472 mBook->setLastBookmark(contentIndex - 1, 1.0);
473 goToBookmark(bookmark);
480 void BookView::goNextPage()
483 QWebFrame *frame = page()->mainFrame();
484 int pos = frame->scrollPosition().y();
485 frame->scroll(0, height());
486 if (pos == frame->scrollPosition().y()) {