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)
73 FlickCharm *charm = new FlickCharm(this);
74 charm->activateOn(this);
83 void BookView::loadContent(int index)
89 if ((index < 0) || (index >= mBook->parts.size())) {
93 QString contentFile(mBook->content[mBook->parts[index]].href);
94 if (mBook->parts[index] == "error") {
98 emit partLoadStart(index);
99 QUrl u = QUrl::fromLocalFile(QDir(mBook->rootPath()).
100 absoluteFilePath(contentFile));
101 qDebug() << "Loading" << u;
104 contentIndex = index;
107 void BookView::setBook(Book *book)
111 // Save position in current book
114 // Open new book, restore last position
120 restoreLastBookmark();
124 setHtml(tr("Failed to open book"));
129 setHtml(tr("No book"));
134 Book *BookView::book()
139 void BookView::goPrevious()
142 if (mBook && (contentIndex > 0)) {
143 mBook->setLastBookmark(contentIndex - 1, 0);
144 loadContent(contentIndex - 1);
148 void BookView::goNext()
151 if (mBook && (contentIndex < (mBook->parts.size() - 1))) {
152 mBook->setLastBookmark(contentIndex + 1, 0);
153 loadContent(contentIndex + 1);
157 void BookView::setLastBookmark()
161 int height = contentsHeight;
162 int pos = page()->mainFrame()->scrollPosition().y();
163 qDebug() << QString("At %1 (%2%, height %3)").
164 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
165 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
169 void BookView::restoreLastBookmark()
173 goToBookmark(mBook->lastBookmark());
177 void BookView::goToBookmark(const Book::Bookmark &bookmark)
181 if (bookmark.part != contentIndex) {
182 qDebug () << "Loading new part" << bookmark.part;
183 mBook->setLastBookmark(bookmark.part, bookmark.pos);
184 restorePositionAfterLoad = true;
185 positionAfterLoad = bookmark.pos;
186 loadContent(bookmark.part);
188 goToPosition(bookmark.pos);
193 void BookView::goToPart(int part, const QString &fragment)
197 if (part != contentIndex) {
198 qDebug() << "Loading new part" << part;
199 restoreFragmentAfterLoad = true;
200 fragmentAfterLoad = fragment;
203 goToFragment(fragment);
209 void BookView::goToFragment(const QString &fragment)
212 if (!fragment.isEmpty()) {
213 QVariant ret = page()->mainFrame()->evaluateJavaScript(
214 QString("window.location='") + fragment + "'");
220 void BookView::onLoadFinished(bool ok)
224 qDebug() << "Not OK";
228 onSettingsChanged("scheme");
229 emit partLoadEnd(contentIndex);
233 void BookView::onSettingsChanged(const QString &key)
236 qDebug() << key << Settings::instance()->value(key);
239 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
241 else if (key == "font") {
242 QString face = Settings::instance()->value(key).toString();
243 settings()->setFontFamily(QWebSettings::StandardFont, face);
245 else if (key == "scheme") {
246 QWebFrame *frame = page()->mainFrame();
247 QString scheme = Settings::instance()->value("scheme").toString();
248 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
249 (scheme != "default")) {
252 QFile script(":/styles/" + scheme + ".js");
253 script.open(QFile::ReadOnly);
254 QString scriptText = script.readAll();
256 QVariant ret = frame->evaluateJavaScript(scriptText);
260 void BookView::paintEvent(QPaintEvent *e)
262 QWebView::paintEvent(e);
263 if (!mBook || !loaded) {
268 QPoint scrollPos = page()->mainFrame()->scrollPosition();
269 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
270 QPainter painter(this);
271 foreach (Book::Bookmark b, mBook->bookmarks()) {
272 if (b.part != contentIndex) {
275 int height = contentsHeight;
276 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
277 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
281 void BookView::mousePressEvent(QMouseEvent *e)
283 QWebView::mousePressEvent(e);
285 // Start monitoring kinetic scroll
286 if (scrollerMonitor) {
287 killTimer(scrollerMonitor);
291 scrollerMonitor = startTimer(500);
294 // Handle mouse presses on the scroll bar
295 QWebFrame *frame = page()->mainFrame();
296 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
300 #endif // Q_WS_MAEMO_5
304 void BookView::wheelEvent(QWheelEvent *e)
306 QWebView::wheelEvent(e);
310 void BookView::addBookmark(const QString ¬e)
316 int y = page()->mainFrame()->scrollPosition().y();
317 int height = page()->mainFrame()->contentsSize().height();
318 qDebug() << ((qreal)y / (qreal)height);
319 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height, note);
323 QString BookView::tmpPath()
325 return QDir::tempPath() + "/dorian";
328 bool BookView::eventFilter(QObject *o, QEvent *e)
330 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
331 if (e->type() == QEvent::Resize) {
332 qDebug() << "BookView::eventFilter QEvent::Resize to"
333 << page()->mainFrame()->contentsSize().height();
334 } else if (e->type() == QEvent::Timer) {
335 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
336 << ((QTimerEvent *)e)->timerId();
338 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
342 // Work around Qt bug that sometimes selects web view contents during swipe
344 case QEvent::MouseButtonPress:
345 emit suppressedMouseButtonPress();
348 case QEvent::MouseButtonRelease:
350 mousePressed = false;
352 case QEvent::MouseMove:
357 case QEvent::MouseButtonDblClick:
363 return QObject::eventFilter(o, e);
366 void BookView::addJavaScriptObjects()
368 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
371 void BookView::onContentsSizeChanged(const QSize &size)
374 contentsHeight = size.height();
375 if (restoreFragmentAfterLoad) {
376 qDebug() << "Restorint to fragment" << fragmentAfterLoad;
377 goToFragment(fragmentAfterLoad);
378 } else if (restorePositionAfterLoad) {
379 qDebug() << "Restoring to position";
380 goToPosition(positionAfterLoad);
382 restorePositionAfterLoad = false;
383 restoreFragmentAfterLoad = false;
388 void BookView::leaveEvent(QEvent *e)
391 // Save current position, to be restored later
393 QWebView::leaveEvent(e);
396 void BookView::enterEvent(QEvent *e)
399 // Restore position saved at Leave event. This seems to be required,
400 // after temporarily switching from portrait to landscape and back
401 restoreLastBookmark();
402 QWebView::enterEvent(e);
405 #endif // Q_WS_MAEMO_5
407 void BookView::goToPosition(qreal position)
409 int scrollPos = (int)((qreal)contentsHeight * position);
410 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
412 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
413 << (position * 100) << "%, height" << contentsHeight << ")";
416 void BookView::showProgress()
419 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
420 (qreal)contentsHeight;
421 emit progress(mBook->getProgress(contentIndex, pos));
425 void BookView::timerEvent(QTimerEvent *e)
428 if (e->timerId() == scrollerMonitor) {
430 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
431 (scroller->state() == QAbstractKineticScroller::Pushing))) {
434 killTimer(scrollerMonitor);
438 QWebView::timerEvent(e);
441 void BookView::keyPressEvent(QKeyEvent* event)
443 switch (event->key()) {
455 QWebView::keyPressEvent(event);
458 void BookView::goPreviousPage()
460 QWebFrame *frame = page()->mainFrame();
461 int pos = frame->scrollPosition().y();
462 frame->scroll(0, -height());
463 if (pos == frame->scrollPosition().y()) {
464 if (contentIndex > 0) {
465 Book::Bookmark bookmark(contentIndex - 1, 1.0);
466 mBook->setLastBookmark(contentIndex - 1, 1.0);
467 goToBookmark(bookmark);
474 void BookView::goNextPage()
477 QWebFrame *frame = page()->mainFrame();
478 int pos = frame->scrollPosition().y();
479 frame->scroll(0, height());
480 if (pos == frame->scrollPosition().y()) {