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),
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");
92 void BookView::loadContent(int index)
94 Trace t("BookView::loadContent");
98 if ((index < 0) || (index >= mBook->parts.size())) {
102 QString contentFile(mBook->content[mBook->parts[index]].href);
103 if (mBook->parts[index] == "error") {
104 setHtml(contentFile);
108 emit partLoadStart(index);
109 QUrl u = QUrl::fromLocalFile(QDir(mBook->rootPath()).
110 absoluteFilePath(contentFile));
111 qDebug() << "Loading" << u;
114 contentIndex = index;
117 void BookView::setBook(Book *book)
119 Trace t("BookView::setBook");
121 // Save position in current book
124 // Open new book, restore last position
130 restoreLastBookmark();
134 setHtml(tr("Failed to open book"));
139 setHtml(tr("No book"));
144 Book *BookView::book()
149 void BookView::goPrevious()
151 Trace t("BookView::goPrevious");
152 if (mBook && (contentIndex > 0)) {
153 mBook->setLastBookmark(contentIndex - 1, 0);
154 loadContent(contentIndex - 1);
158 void BookView::goNext()
160 Trace t("BookView::goNext");
161 if (mBook && (contentIndex < (mBook->parts.size() - 1))) {
162 mBook->setLastBookmark(contentIndex + 1, 0);
163 loadContent(contentIndex + 1);
167 void BookView::setLastBookmark()
169 Trace t("BookView::setLastBookmark");
171 int height = contentsHeight;
172 int pos = page()->mainFrame()->scrollPosition().y();
173 qDebug() << QString("At %1 (%2%, height %3)").
174 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height);
175 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
179 void BookView::restoreLastBookmark()
181 Trace t("BookView::restoreLastBookmark");
183 goToBookmark(mBook->lastBookmark());
187 void BookView::goToBookmark(const Book::Bookmark &bookmark)
189 Trace t("BookView::goToBookmark");
191 if (bookmark.part != contentIndex) {
192 qDebug () << "Loading new part" << bookmark.part;
193 mBook->setLastBookmark(bookmark.part, bookmark.pos);
194 restorePositionAfterLoad = true;
195 positionAfterLoad = bookmark.pos;
196 loadContent(bookmark.part);
198 goToPosition(bookmark.pos);
203 void BookView::onLoadFinished(bool ok)
205 Trace t("BookView::onLoadFinished");
207 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 = (int)((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
267 if (scrollerMonitor) {
268 killTimer(scrollerMonitor);
272 scrollerMonitor = startTimer(500);
275 // Handle mouse presses on the scroll bar
276 QWebFrame *frame = page()->mainFrame();
277 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
281 #endif // Q_WS_MAEMO_5
285 void BookView::wheelEvent(QWheelEvent *e)
287 QWebView::wheelEvent(e);
291 void BookView::addBookmark()
293 Trace t("BookView::addBookmark");
297 int y = page()->mainFrame()->scrollPosition().y();
298 int height = page()->mainFrame()->contentsSize().height();
299 qDebug() << ((qreal)y / (qreal)height);
300 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
304 QString BookView::tmpPath()
306 return QDir::tempPath() + "/dorian";
309 bool BookView::eventFilter(QObject *o, QEvent *e)
311 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
312 if (e->type() == QEvent::Resize) {
313 qDebug() << "BookView::eventFilter QEvent::Resize to"
314 << page()->mainFrame()->contentsSize().height();
315 } else if (e->type() == QEvent::Timer) {
316 qDebug() << "BookView::eventFilter" << "QEvent::Timer"
317 << ((QTimerEvent *)e)->timerId();
319 qDebug() << "BookView::eventFilter" << Trace::event(e->type());
323 // Work around Qt bug that sometimes selects web view contents during swipe
325 case QEvent::MouseButtonPress:
326 emit suppressedMouseButtonPress();
329 case QEvent::MouseButtonRelease:
331 mousePressed = false;
333 case QEvent::MouseMove:
338 case QEvent::MouseButtonDblClick:
344 return QObject::eventFilter(o, e);
347 void BookView::addJavaScriptObjects()
349 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
352 void BookView::onContentsSizeChanged(const QSize &size)
354 contentsHeight = size.height();
355 if (restorePositionAfterLoad) {
356 qDebug() << "BookView::onContentSizeChanged: Time to restore";
357 restorePositionAfterLoad = false;
358 goToPosition(positionAfterLoad);
362 void BookView::leaveEvent(QEvent *e)
364 Trace t("BookView::leaveEvent");
365 // Save current position, to be restored later
367 QWebView::leaveEvent(e);
370 void BookView::enterEvent(QEvent *e)
372 Trace t("BookView::enterEvent");
373 // Restore position saved at Leave event. This seems to be required,
374 // after temporarily switching from portrait to landscape and back
375 restoreLastBookmark();
376 QWebView::enterEvent(e);
379 void BookView::goToPosition(qreal position)
381 int scrollPos = (int)((qreal)contentsHeight * position);
382 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
384 qDebug() << "BookView::goToPosition: To" << scrollPos << "("
385 << (position * 100) << "%, height" << contentsHeight << ")";
388 void BookView::showProgress()
391 qreal pos = (qreal)(page()->mainFrame()->scrollPosition().y()) /
392 (qreal)contentsHeight;
393 emit progress(mBook->getProgress(contentIndex, pos));
397 void BookView::timerEvent(QTimerEvent *e)
400 if (e->timerId() == scrollerMonitor) {
402 ((scroller->state() == QAbstractKineticScroller::AutoScrolling) ||
403 (scroller->state() == QAbstractKineticScroller::Pushing))) {
406 killTimer(scrollerMonitor);
411 #endif // Q_WS_MAEMO_5
414 void BookView::keyPressEvent(QKeyEvent* event)
416 switch (event->key()) {
428 QWebView::keyPressEvent(event);
431 void BookView::goPreviousPage()
433 QWebFrame *frame = page()->mainFrame();
434 int pos = frame->scrollPosition().y();
435 frame->scroll(0, -height());
436 if (pos == frame->scrollPosition().y()) {
437 if (contentIndex > 0) {
438 Book::Bookmark bookmark(contentIndex - 1, 1.0);
439 mBook->setLastBookmark(contentIndex - 1, 1.0);
440 goToBookmark(bookmark);
447 void BookView::goNextPage()
449 Trace t("BookView::goNextPage");
450 QWebFrame *frame = page()->mainFrame();
451 int pos = frame->scrollPosition().y();
452 frame->scroll(0, height());
453 if (pos == frame->scrollPosition().y()) {