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 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";
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 qDebug() << ((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 qDebug() << "BookView::eventFilter QEvent::Resize to"
366 << page()->mainFrame()->contentsSize().height();
368 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
372 // Work around Qt bug that sometimes selects web view contents during swipe
374 case QEvent::MouseButtonPress:
375 emit suppressedMouseButtonPress();
378 case QEvent::MouseButtonRelease:
380 mousePressed = false;
382 case QEvent::MouseMove:
387 case QEvent::MouseButtonDblClick:
393 return QObject::eventFilter(o, e);
396 void BookView::addJavaScriptObjects()
398 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
401 void BookView::onContentsSizeChanged(const QSize &size)
403 contentsHeight = size.height();
405 if (restorePositionAfterLoad) {
406 qDebug() << "BookView::onContentSizeChanged: Time to restore";
407 restorePositionAfterLoad = false;
408 goToPosition(positionAfterLoad);
413 void BookView::leaveEvent(QEvent *e)
415 Trace t("BookView::leaveEvent");
416 // Save current position, to be restored later
418 QWebView::leaveEvent(e);
421 void BookView::enterEvent(QEvent *e)
423 Trace t("BookView::enterEvent");
424 // Restore position saved at Leave event. This seems to be required,
425 // after temporarily switching from portrait to landscape and back
426 restoreLastBookmark();
427 QWebView::enterEvent(e);
430 void BookView::goToPosition(qreal position)
432 int scrollPos = (qreal)contentsHeight * position;
433 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
435 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
436 << (position * 100) << "%, height" << contentsHeight << ")";
439 void BookView::showProgress()
442 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
443 (qreal)contentsHeight;
444 emit progress(mBook->getProgress(contentIndex, pos));
448 void BookView::timerEvent(QTimerEvent *e)
450 if (e->timerId() == scrollerMonitor) {
453 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
454 (scroller->state() == QAbstractKineticScroller::Pushing))) {
457 killTimer(scrollerMonitor);
459 #endif // Q_WS_MAEMO_5
463 void BookView::keyPressEvent(QKeyEvent* event)
465 switch (event->key()) {
477 QWebView::keyPressEvent(event);
480 void BookView::goPreviousPage()
482 QWebFrame *frame = page()->mainFrame();
483 int pos = frame->scrollPosition().y();
484 frame->scroll(0, -height());
485 if (pos == frame->scrollPosition().y()) {
486 if (contentIndex > 0) {
487 goToBookmark(Book::Bookmark(contentIndex - 1, 1.0));
494 void BookView::goNextPage()
496 Trace t("BookView::goNextPage");
497 QWebFrame *frame = page()->mainFrame();
498 int pos = frame->scrollPosition().y();
499 frame->scroll(0, height());
500 if (pos == frame->scrollPosition().y()) {