9 # include <QAbstractKineticScroller>
19 BookView::BookView(QWidget *parent):
20 QWebView(parent), contentIndex(-1), mBook(0),
21 restorePositionAfterLoad(false), positionAfterLoad(0), loaded(false),
24 Trace t("BookView::BookView");
25 settings()->setAttribute(QWebSettings::AutoLoadImages, true);
26 settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
27 settings()->setAttribute(QWebSettings::JavaEnabled, false);
28 settings()->setAttribute(QWebSettings::PluginsEnabled, false);
29 settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
30 settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
31 settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, false);
32 settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, false);
33 settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled,
35 settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
36 settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
37 settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
39 settings()->setDefaultTextEncoding("utf-8");
40 page()->setContentEditable(false);
42 #if defined(Q_WS_MAEMO_5)
43 // Suppress unwanted text selections on Maemo
44 installEventFilter(this);
46 QWebFrame *frame = page()->mainFrame();
47 #if defined(Q_WS_MAEMO_5)
48 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
50 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
52 bookmarkImage = QImage(":/icons/bookmark.png");
54 connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
55 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
56 this, SLOT(addJavaScriptObjects()));
57 connect(frame, SIGNAL(contentsSizeChanged(const QSize &)),
58 this, SLOT(onContentsSizeChanged(const QSize &)));
59 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
60 this, SLOT(onSettingsChanged(const QString &)));
61 Settings *s = Settings::instance();
62 s->setValue("zoom", s->value("zoom", 160));
63 s->setValue("font", s->value("font",
64 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
66 #elif defined(Q_WS_MAC)
72 s->setValue("scheme", s->value("scheme", "default"));
77 scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
83 Trace t("BookView::~BookView");
86 void BookView::loadContent(int index)
88 Trace t("BookView::loadContent");
92 if ((index < 0) || (index >= mBook->parts.size())) {
96 QString contentFile(mBook->content[mBook->parts[index]].href);
97 if (mBook->parts[index] == "error") {
102 emit partLoadStart(index);
103 QUrl u = QUrl::fromLocalFile(QDir(mBook->rootPath()).
104 absoluteFilePath(contentFile));
105 qDebug() << "Loading" << u;
108 contentIndex = index;
111 void BookView::setBook(Book *book)
113 Trace t("BookView::setBook");
115 // Save position in current book
118 // Open new book, restore last position
124 restoreLastBookmark();
128 setHtml(tr("Failed to open book"));
133 setHtml(tr("No book"));
138 Book *BookView::book()
143 void BookView::goPrevious()
145 Trace t("BookView::goPrevious");
146 if (mBook && (contentIndex > 0)) {
147 mBook->setLastBookmark(contentIndex - 1, 0);
148 loadContent(contentIndex - 1);
152 void BookView::goNext()
154 Trace t("BookView::goNext");
155 if (mBook && (contentIndex < (mBook->parts.size() - 1))) {
156 mBook->setLastBookmark(contentIndex + 1, 0);
157 loadContent(contentIndex + 1);
161 void BookView::setLastBookmark()
163 Trace t("BookView::setLastBookmark");
165 int height = contentsHeight;
166 int pos = page()->mainFrame()->scrollPosition().y();
167 qDebug() << QString("At %1 (%2%, height %3)").
168 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
169 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
173 void BookView::restoreLastBookmark()
175 Trace t("BookView::restoreLastBookmark");
177 goToBookmark(mBook->lastBookmark());
181 void BookView::goToBookmark(const Book::Bookmark &bookmark)
183 Trace t("BookView::goToBookmark");
185 if (bookmark.part != contentIndex) {
186 qDebug () << "Loading new part" << bookmark.part;
187 mBook->setLastBookmark(bookmark.part, bookmark.pos);
188 restorePositionAfterLoad = true;
189 positionAfterLoad = bookmark.pos;
190 loadContent(bookmark.part);
192 goToPosition(bookmark.pos);
197 void BookView::onLoadFinished(bool ok)
199 Trace t("BookView::onLoadFinished");
201 qDebug() << "Not OK";
205 onSettingsChanged("scheme");
206 emit partLoadEnd(contentIndex);
210 void BookView::onSettingsChanged(const QString &key)
212 Trace t("BookView::onSettingsChanged " + key);
214 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
216 else if (key == "font") {
217 QString face = Settings::instance()->value("font").toString();
218 settings()->setFontFamily(QWebSettings::StandardFont, face);
220 else if (key == "scheme") {
221 QWebFrame *frame = page()->mainFrame();
222 QString scheme = Settings::instance()->value("scheme").toString();
223 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
224 (scheme != "default")) {
227 QFile script(":/styles/" + scheme + ".js");
228 script.open(QFile::ReadOnly);
229 QString scriptText = script.readAll();
231 QVariant ret = frame->evaluateJavaScript(scriptText);
235 void BookView::paintEvent(QPaintEvent *e)
237 QWebView::paintEvent(e);
238 if (!mBook || !loaded) {
243 QPoint scrollPos = page()->mainFrame()->scrollPosition();
244 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
245 QPainter painter(this);
246 foreach (Book::Bookmark b, mBook->bookmarks()) {
247 if (b.part != contentIndex) {
250 int height = contentsHeight;
251 int bookmarkPos = (int)((qreal)height * (qreal)b.pos);
252 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
256 void BookView::mousePressEvent(QMouseEvent *e)
258 QWebView::mousePressEvent(e);
260 // Start monitoring kinetic scroll
261 if (scrollerMonitor) {
262 killTimer(scrollerMonitor);
266 scrollerMonitor = startTimer(500);
269 // Handle mouse presses on the scroll bar
270 QWebFrame *frame = page()->mainFrame();
271 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
275 #endif // Q_WS_MAEMO_5
279 void BookView::wheelEvent(QWheelEvent *e)
281 QWebView::wheelEvent(e);
285 void BookView::addBookmark()
287 Trace t("BookView::addBookmark");
291 int y = page()->mainFrame()->scrollPosition().y();
292 int height = page()->mainFrame()->contentsSize().height();
293 qDebug() << ((qreal)y / (qreal)height);
294 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
298 QString BookView::tmpPath()
300 return QDir::tempPath() + "/dorian";
303 bool BookView::eventFilter(QObject *o, QEvent *e)
305 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
306 if (e->type() == QEvent::Resize) {
307 qDebug() << "BookView::eventFilter QEvent::Resize to"
308 << page()->mainFrame()->contentsSize().height();
309 } else if (e->type() == QEvent::Timer) {
310 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
311 << ((QTimerEvent *)e)->timerId();
313 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
317 // Work around Qt bug that sometimes selects web view contents during swipe
319 case QEvent::MouseButtonPress:
320 emit suppressedMouseButtonPress();
323 case QEvent::MouseButtonRelease:
325 mousePressed = false;
327 case QEvent::MouseMove:
332 case QEvent::MouseButtonDblClick:
338 return QObject::eventFilter(o, e);
341 void BookView::addJavaScriptObjects()
343 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
346 void BookView::onContentsSizeChanged(const QSize &size)
348 contentsHeight = size.height();
349 if (restorePositionAfterLoad) {
350 qDebug() << "BookView::onContentSizeChanged: Time to restore";
351 restorePositionAfterLoad = false;
352 goToPosition(positionAfterLoad);
356 void BookView::leaveEvent(QEvent *e)
358 Trace t("BookView::leaveEvent");
359 // Save current position, to be restored later
361 QWebView::leaveEvent(e);
364 void BookView::enterEvent(QEvent *e)
366 Trace t("BookView::enterEvent");
367 // Restore position saved at Leave event. This seems to be required,
368 // after temporarily switching from portrait to landscape and back
369 restoreLastBookmark();
370 QWebView::enterEvent(e);
373 void BookView::goToPosition(qreal position)
375 int scrollPos = (int)((qreal)contentsHeight * position);
376 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
378 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
379 << (position * 100) << "%, height" << contentsHeight << ")";
382 void BookView::showProgress()
385 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
386 (qreal)contentsHeight;
387 emit progress(mBook->getProgress(contentIndex, pos));
391 void BookView::timerEvent(QTimerEvent *e)
394 if (e->timerId() == scrollerMonitor) {
396 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
397 (scroller->state() == QAbstractKineticScroller::Pushing))) {
400 killTimer(scrollerMonitor);
405 #endif // Q_WS_MAEMO_5
408 void BookView::keyPressEvent(QKeyEvent* event)
410 switch (event->key()) {
422 QWebView::keyPressEvent(event);
425 void BookView::goPreviousPage()
427 QWebFrame *frame = page()->mainFrame();
428 int pos = frame->scrollPosition().y();
429 frame->scroll(0, -height());
430 if (pos == frame->scrollPosition().y()) {
431 if (contentIndex > 0) {
432 Book::Bookmark bookmark(contentIndex - 1, 1.0);
433 mBook->setLastBookmark(contentIndex - 1, 1.0);
434 goToBookmark(bookmark);
441 void BookView::goNextPage()
443 Trace t("BookView::goNextPage");
444 QWebFrame *frame = page()->mainFrame();
445 int pos = frame->scrollPosition().y();
446 frame->scroll(0, height());
447 if (pos == frame->scrollPosition().y()) {