9 # include <QAbstractKineticScroller>
20 # define ICON_PREFIX ":/icons/mac/"
22 # define ICON_PREFIX ":/icons/"
25 BookView::BookView(QWidget *parent):
26 QWebView(parent), contentIndex(-1), mBook(0),
27 restorePositionAfterLoad(false), positionAfterLoad(0), loaded(false),
28 contentsHeight(0), decorated(false), scrollerMonitor(-1)
30 Trace t("BookView::BookView");
31 settings()->setAttribute(QWebSettings::AutoLoadImages, true);
32 settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
33 settings()->setAttribute(QWebSettings::JavaEnabled, false);
34 settings()->setAttribute(QWebSettings::PluginsEnabled, false);
35 settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
36 settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
37 settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, false);
38 settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, false);
39 settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled,
41 settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
42 settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
43 settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
45 settings()->setDefaultTextEncoding("utf-8");
46 page()->setContentEditable(false);
48 #if defined(Q_WS_MAEMO_5)
49 // Suppress unwanted text selections on Maemo
50 installEventFilter(this);
52 QWebFrame *frame = page()->mainFrame();
53 #if defined(Q_WS_MAEMO_5)
54 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
56 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
58 bookmarkImage = QImage(":/icons/bookmark.png");
60 connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
61 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
62 this, SLOT(addJavaScriptObjects()));
63 connect(frame, SIGNAL(contentsSizeChanged(const QSize &)),
64 this, SLOT(onContentsSizeChanged(const QSize &)));
65 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
66 this, SLOT(onSettingsChanged(const QString &)));
67 Settings *s = Settings::instance();
68 s->setValue("zoom", s->value("zoom", 160));
69 s->setValue("font", s->value("font",
70 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
72 #elif defined(Q_WS_MAC)
78 s->setValue("scheme", s->value("scheme", "default"));
83 scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
89 Trace t("BookView::~BookView");
93 void BookView::loadContent(int index)
95 Trace t("BookView::loadContent");
99 if ((index < 0) || (index >= mBook->parts.size())) {
103 QString contentFile(mBook->content[mBook->parts[index]].href);
104 if (mBook->parts[index] == "error") {
105 setHtml(contentFile);
110 emit partLoadStart(index);
111 load(QUrl(contentFile));
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 t.trace(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 t.trace(QString("Loading new part %1").arg(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");
211 onSettingsChanged("scheme");
212 emit partLoadEnd(contentIndex);
216 void BookView::onSettingsChanged(const QString &key)
218 Trace t("BookView::onSettingsChanged " + key);
220 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
222 else if (key == "font") {
223 QString face = Settings::instance()->value("font").toString();
224 settings()->setFontFamily(QWebSettings::StandardFont, face);
226 else if (key == "scheme") {
227 QWebFrame *frame = page()->mainFrame();
228 QString scheme = Settings::instance()->value("scheme").toString();
229 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
230 (scheme != "default")) {
233 QFile script(":/styles/" + scheme + ".js");
234 script.open(QFile::ReadOnly);
235 QString scriptText = script.readAll();
237 QVariant ret = frame->evaluateJavaScript(scriptText);
241 void BookView::paintEvent(QPaintEvent *e)
243 QWebView::paintEvent(e);
244 if (!mBook || !loaded) {
249 QPoint scrollPos = page()->mainFrame()->scrollPosition();
250 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
251 QPainter painter(this);
252 foreach (Book::Bookmark b, mBook->bookmarks()) {
253 if (b.part != contentIndex) {
256 int height = contentsHeight;
257 int bookmarkPos = (qreal)height * (qreal)b.pos;
258 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
262 void BookView::mousePressEvent(QMouseEvent *e)
264 QWebView::mousePressEvent(e);
266 // Start monitoring kinetic scroll
268 scrollerMonitor = startTimer(250);
271 // Handle mouse presses on the scroll bar
272 QWebFrame *frame = page()->mainFrame();
273 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
277 #endif // Q_WS_MAEMO_5
281 void BookView::wheelEvent(QWheelEvent *e)
283 QWebView::wheelEvent(e);
287 void BookView::addBookmark()
289 Trace t("BookView::addBookmark");
293 int y = page()->mainFrame()->scrollPosition().y();
294 int height = page()->mainFrame()->contentsSize().height();
295 t.trace(QString().setNum((qreal)y / (qreal)height));
296 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
300 void BookView::addNavigationBar()
302 Trace t("BookView::addNavigationBar");
308 "<a href=\"javascript:bv.goPrevious();\">"
309 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
315 "<a href=\"javascript:bv.goNext();\">"
316 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
321 if (contentIndex == 0) {
324 if (contentIndex >= mBook->parts.size() - 1) {
328 QWebFrame *frame = page()->currentFrame();
329 QString headerScript = "document.body.innerHTML = '" +
330 naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
331 QString trailerScript = "document.body.innerHTML += '<br /><br />" +
332 naviPrev + naviNext + "';";
334 frame->evaluateJavaScript(headerScript);
335 frame->evaluateJavaScript(trailerScript);
339 QString BookView::tmpPath()
341 return QDir::tempPath() + "/dorian";
344 void BookView::extractIcons()
346 QFile next(ICON_PREFIX + QString("/next.png"));
347 QFile prev(ICON_PREFIX + QString("/previous.png"));
349 QDir().mkpath(tmpPath());
350 next.copy(tmpPath() + "/next.png");
351 prev.copy(tmpPath() + "/previous.png");
354 void BookView::removeIcons()
356 QFile(ICON_PREFIX + QString("/next.png")).remove();
357 QFile(ICON_PREFIX + QString("/previous.png")).remove();
358 QDir().rmpath(tmpPath());
361 bool BookView::eventFilter(QObject *o, QEvent *e)
363 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
364 if (e->type() == QEvent::Resize) {
365 Trace::trace(QString("BookView::eventFilter QEvent::Resize to %1").
366 arg(page()->mainFrame()->contentsSize().height()));
368 Trace::trace(QString("BookView::eventFilter %1").
369 arg(Trace::event(e->type())));
373 // Work around Qt bug that sometimes selects web view contents during swipe
375 case QEvent::MouseButtonPress:
376 emit suppressedMouseButtonPress();
379 case QEvent::MouseButtonRelease:
381 mousePressed = false;
383 case QEvent::MouseMove:
388 case QEvent::MouseButtonDblClick:
394 return QObject::eventFilter(o, e);
397 void BookView::addJavaScriptObjects()
399 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
402 void BookView::onContentsSizeChanged(const QSize &size)
404 contentsHeight = size.height();
406 if (restorePositionAfterLoad) {
407 Trace::trace("BookView::onContentSizeChanged: Time to restore");
408 restorePositionAfterLoad = false;
409 goToPosition(positionAfterLoad);
414 void BookView::leaveEvent(QEvent *e)
416 Trace t("BookView::leaveEvent");
417 // Save current position, to be restored later
419 QWebView::leaveEvent(e);
422 void BookView::enterEvent(QEvent *e)
424 Trace t("BookView::enterEvent");
425 // Restore position saved at Leave event. This seems to be required,
426 // after temporarily switching from portrait to landscape and back
427 restoreLastBookmark();
428 QWebView::enterEvent(e);
431 void BookView::goToPosition(qreal position)
433 int scrollPos = (qreal)contentsHeight * position;
434 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
436 Trace::trace(QString("BookView::goToPosition: To %1 (%2%, height %3)").
437 arg(scrollPos).arg(position * 100).arg(contentsHeight));
440 void BookView::showProgress()
443 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
444 (qreal)contentsHeight;
445 emit progress(mBook->getProgress(contentIndex, pos));
449 void BookView::timerEvent(QTimerEvent *e)
451 if (e->timerId() == scrollerMonitor) {
454 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
455 (scroller->state() == QAbstractKineticScroller::Pushing))) {
458 killTimer(scrollerMonitor);
460 #endif // Q_WS_MAEMO_5
464 void BookView::keyPressEvent(QKeyEvent* event)
466 switch (event->key()) {
478 QWebView::keyPressEvent(event);
481 void BookView::goPreviousPage()
483 QWebFrame *frame = page()->mainFrame();
484 int pos = frame->scrollPosition().y();
485 frame->scroll(0, -height());
486 if (pos == frame->scrollPosition().y()) {
487 if (contentIndex > 0) {
488 goToBookmark(Book::Bookmark(contentIndex - 1, 1.0));
495 void BookView::goNextPage()
497 Trace t("BookView::goNextPage");
498 QWebFrame *frame = page()->mainFrame();
499 int pos = frame->scrollPosition().y();
500 frame->scroll(0, height());
501 if (pos == frame->scrollPosition().y()) {