8 #if defined(Q_WS_MAEMO_5)
9 # include <QAbstractKineticScroller>
10 #elif defined(Q_OS_SYMBIAN)
11 # include "flickcharm.h"
21 BookView::BookView(QWidget *parent):
22 QWebView(parent), contentIndex(-1), mBook(0),
23 restorePositionAfterLoad(false), positionAfterLoad(0), loaded(false),
26 Trace t("BookView::BookView");
27 settings()->setAttribute(QWebSettings::AutoLoadImages, true);
28 settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
29 settings()->setAttribute(QWebSettings::JavaEnabled, false);
30 settings()->setAttribute(QWebSettings::PluginsEnabled, false);
31 settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
32 settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
33 settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, false);
34 settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, false);
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",
66 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
68 #elif defined(Q_WS_MAC)
74 s->setValue("scheme", s->value("scheme", "default"));
77 #if defined(Q_WS_MAEMO_5)
79 scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
80 #elif defined(Q_OS_SYMBIAN)
81 FlickCharm *charm = new FlickCharm(this);
82 charm->activateOn(this);
88 Trace t("BookView::~BookView");
91 void BookView::loadContent(int index)
93 Trace t("BookView::loadContent");
97 if ((index < 0) || (index >= mBook->parts.size())) {
101 QString contentFile(mBook->content[mBook->parts[index]].href);
102 if (mBook->parts[index] == "error") {
103 setHtml(contentFile);
107 emit partLoadStart(index);
108 QUrl u = QUrl::fromLocalFile(QDir(mBook->rootPath()).
109 absoluteFilePath(contentFile));
110 qDebug() << "Loading" << u;
113 contentIndex = index;
116 void BookView::setBook(Book *book)
118 Trace t("BookView::setBook");
120 // Save position in current book
123 // Open new book, restore last position
129 restoreLastBookmark();
133 setHtml(tr("Failed to open book"));
138 setHtml(tr("No book"));
143 Book *BookView::book()
148 void BookView::goPrevious()
150 Trace t("BookView::goPrevious");
151 if (mBook && (contentIndex > 0)) {
152 mBook->setLastBookmark(contentIndex - 1, 0);
153 loadContent(contentIndex - 1);
157 void BookView::goNext()
159 Trace t("BookView::goNext");
160 if (mBook && (contentIndex < (mBook->parts.size() - 1))) {
161 mBook->setLastBookmark(contentIndex + 1, 0);
162 loadContent(contentIndex + 1);
166 void BookView::setLastBookmark()
168 Trace t("BookView::setLastBookmark");
170 int height = contentsHeight;
171 int pos = page()->mainFrame()->scrollPosition().y();
172 qDebug() << QString("At %1 (%2%, height %3)").
173 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
174 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
178 void BookView::restoreLastBookmark()
180 Trace t("BookView::restoreLastBookmark");
182 goToBookmark(mBook->lastBookmark());
186 void BookView::goToBookmark(const Book::Bookmark &bookmark)
188 Trace t("BookView::goToBookmark");
190 if (bookmark.part != contentIndex) {
191 qDebug () << "Loading new part" << bookmark.part;
192 mBook->setLastBookmark(bookmark.part, bookmark.pos);
193 restorePositionAfterLoad = true;
194 positionAfterLoad = bookmark.pos;
195 loadContent(bookmark.part);
197 goToPosition(bookmark.pos);
202 void BookView::onLoadFinished(bool ok)
204 Trace t("BookView::onLoadFinished");
206 qDebug() << "Not OK";
210 onSettingsChanged("scheme");
211 emit partLoadEnd(contentIndex);
215 void BookView::onSettingsChanged(const QString &key)
217 Trace t("BookView::onSettingsChanged " + key);
219 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
221 else if (key == "font") {
222 QString face = Settings::instance()->value("font").toString();
223 settings()->setFontFamily(QWebSettings::StandardFont, face);
225 else if (key == "scheme") {
226 QWebFrame *frame = page()->mainFrame();
227 QString scheme = Settings::instance()->value("scheme").toString();
228 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
229 (scheme != "default")) {
232 QFile script(":/styles/" + scheme + ".js");
233 script.open(QFile::ReadOnly);
234 QString scriptText = script.readAll();
236 QVariant ret = frame->evaluateJavaScript(scriptText);
240 void BookView::paintEvent(QPaintEvent *e)
242 QWebView::paintEvent(e);
243 if (!mBook || !loaded) {
248 QPoint scrollPos = page()->mainFrame()->scrollPosition();
249 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
250 QPainter painter(this);
251 foreach (Book::Bookmark b, mBook->bookmarks()) {
252 if (b.part != contentIndex) {
255 int height = contentsHeight;
256 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
257 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
261 void BookView::mousePressEvent(QMouseEvent *e)
263 QWebView::mousePressEvent(e);
265 // Start monitoring kinetic scroll
266 if (scrollerMonitor) {
267 killTimer(scrollerMonitor);
271 scrollerMonitor = startTimer(500);
274 // Handle mouse presses on the scroll bar
275 QWebFrame *frame = page()->mainFrame();
276 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
280 #endif // Q_WS_MAEMO_5
284 void BookView::wheelEvent(QWheelEvent *e)
286 QWebView::wheelEvent(e);
290 void BookView::addBookmark()
292 Trace t("BookView::addBookmark");
296 int y = page()->mainFrame()->scrollPosition().y();
297 int height = page()->mainFrame()->contentsSize().height();
298 qDebug() << ((qreal)y / (qreal)height);
299 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
303 QString BookView::tmpPath()
305 return QDir::tempPath() + "/dorian";
308 bool BookView::eventFilter(QObject *o, QEvent *e)
310 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
311 if (e->type() == QEvent::Resize) {
312 qDebug() << "BookView::eventFilter QEvent::Resize to"
313 << page()->mainFrame()->contentsSize().height();
314 } else if (e->type() == QEvent::Timer) {
315 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
316 << ((QTimerEvent *)e)->timerId();
318 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
322 // Work around Qt bug that sometimes selects web view contents during swipe
324 case QEvent::MouseButtonPress:
325 emit suppressedMouseButtonPress();
328 case QEvent::MouseButtonRelease:
330 mousePressed = false;
332 case QEvent::MouseMove:
337 case QEvent::MouseButtonDblClick:
343 return QObject::eventFilter(o, e);
346 void BookView::addJavaScriptObjects()
348 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
351 void BookView::onContentsSizeChanged(const QSize &size)
353 contentsHeight = size.height();
354 if (restorePositionAfterLoad) {
355 qDebug() << "BookView::onContentSizeChanged: Time to restore";
356 restorePositionAfterLoad = false;
357 goToPosition(positionAfterLoad);
361 void BookView::leaveEvent(QEvent *e)
363 Trace t("BookView::leaveEvent");
364 // Save current position, to be restored later
366 QWebView::leaveEvent(e);
369 void BookView::enterEvent(QEvent *e)
371 Trace t("BookView::enterEvent");
372 // Restore position saved at Leave event. This seems to be required,
373 // after temporarily switching from portrait to landscape and back
374 restoreLastBookmark();
375 QWebView::enterEvent(e);
378 void BookView::goToPosition(qreal position)
380 int scrollPos = (int)((qreal)contentsHeight * position);
381 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
383 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
384 << (position * 100) << "%, height" << contentsHeight << ")";
387 void BookView::showProgress()
390 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
391 (qreal)contentsHeight;
392 emit progress(mBook->getProgress(contentIndex, pos));
396 void BookView::timerEvent(QTimerEvent *e)
399 if (e->timerId() == scrollerMonitor) {
401 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
402 (scroller->state() == QAbstractKineticScroller::Pushing))) {
405 killTimer(scrollerMonitor);
409 QWebView::timerEvent(e);
412 void BookView::keyPressEvent(QKeyEvent* event)
414 switch (event->key()) {
426 QWebView::keyPressEvent(event);
429 void BookView::goPreviousPage()
431 QWebFrame *frame = page()->mainFrame();
432 int pos = frame->scrollPosition().y();
433 frame->scroll(0, -height());
434 if (pos == frame->scrollPosition().y()) {
435 if (contentIndex > 0) {
436 Book::Bookmark bookmark(contentIndex - 1, 1.0);
437 mBook->setLastBookmark(contentIndex - 1, 1.0);
438 goToBookmark(bookmark);
445 void BookView::goNextPage()
447 Trace t("BookView::goNextPage");
448 QWebFrame *frame = page()->mainFrame();
449 int pos = frame->scrollPosition().y();
450 frame->scroll(0, height());
451 if (pos == frame->scrollPosition().y()) {