15 # define ICON_PREFIX ":/icons/mac/"
17 # define ICON_PREFIX ":/icons/"
20 BookView::BookView(QWidget *parent):
21 QWebView(parent), contentIndex(-1), mBook(0),
22 restorePositionAfterLoad(false), positionAfterLoad(0), loaded(false),
23 contentsHeight(0), decorated(false)
25 Trace t("BookView::BookView");
26 settings()->setAttribute(QWebSettings::AutoLoadImages, true);
27 settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
28 settings()->setAttribute(QWebSettings::JavaEnabled, false);
29 settings()->setAttribute(QWebSettings::PluginsEnabled, false);
30 settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
31 settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
32 settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, false);
33 settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, false);
34 settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled,
36 settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
37 settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
38 settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
40 settings()->setDefaultTextEncoding("utf-8");
41 page()->setContentEditable(false);
43 #if defined(Q_WS_MAEMO_5)
44 // Suppress unwanted text selections on Maemo
45 installEventFilter(this);
47 QWebFrame *frame = page()->mainFrame();
48 #if defined(Q_WS_MAEMO_5)
49 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
51 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
53 bookmarkImage = QImage(":/icons/bookmark.png");
55 connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
56 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
57 this, SLOT(addJavaScriptObjects()));
58 connect(frame, SIGNAL(contentsSizeChanged(const QSize &)),
59 this, SLOT(onContentsSizeChanged(const QSize &)));
60 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
61 this, SLOT(onSettingsChanged(const QString &)));
62 Settings *s = Settings::instance();
63 s->setValue("zoom", s->value("zoom", 160));
64 s->setValue("font", s->value("font",
65 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
67 #elif defined(Q_WS_MAC)
73 s->setValue("scheme", s->value("scheme", "default"));
81 Trace t("BookView::~BookView");
85 void BookView::loadContent(int index)
87 Trace t("BookView::loadContent");
91 if ((index < 0) || (index >= mBook->toc.size())) {
95 QString contentFile(mBook->content[mBook->toc[index]].href);
96 if (mBook->toc[index] == "error") {
102 emit chapterLoadStart(index);
103 load(QUrl(contentFile));
105 contentIndex = index;
108 void BookView::setBook(Book *book)
110 Trace t("BookView::setBook");
112 // Save position in current book
115 // Open new book, restore last position
121 restoreLastBookmark();
125 setHtml(tr("No book"));
130 Book *BookView::book()
135 void BookView::goPrevious()
137 Trace t("BookView::goPrevious");
138 loadContent(contentIndex - 1);
141 void BookView::goNext()
143 Trace t("BookView::goNext");
144 loadContent(contentIndex + 1);
147 void BookView::setLastBookmark()
149 Trace t("BookView::setLastBookmark");
151 int height = contentsHeight;
152 int pos = page()->mainFrame()->scrollPosition().y();
153 t.trace(QString("At %1 (%2%, height %3)").
154 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height));
155 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
159 void BookView::restoreLastBookmark()
161 Trace t("BookView::restoreLastBookmark");
163 goToBookmark(mBook->lastBookmark());
167 void BookView::goToBookmark(const Book::Bookmark &bookmark)
169 Trace t("BookView::goToBookmark");
171 if (bookmark.chapter != contentIndex) {
172 t.trace(QString("Loading new chapter %1").arg(bookmark.chapter));
173 mBook->setLastBookmark(bookmark.chapter, bookmark.pos);
174 restorePositionAfterLoad = true;
175 positionAfterLoad = bookmark.pos;
176 loadContent(bookmark.chapter);
178 goToPosition(bookmark.pos);
183 void BookView::onLoadFinished(bool ok)
185 Trace t("BookView::onLoadFinished");
192 onSettingsChanged("scheme");
193 emit chapterLoadEnd(contentIndex);
196 void BookView::onSettingsChanged(const QString &key)
198 Trace t("BookView::onSettingsChanged " + key);
200 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
202 else if (key == "font") {
203 QString face = Settings::instance()->value("font").toString();
204 settings()->setFontFamily(QWebSettings::StandardFont, face);
206 else if (key == "scheme") {
207 QWebFrame *frame = page()->mainFrame();
208 QString scheme = Settings::instance()->value("scheme").toString();
209 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
210 (scheme != "default")) {
213 QFile script(":/styles/" + scheme + ".js");
214 script.open(QFile::ReadOnly);
215 QString scriptText = script.readAll();
217 QVariant ret = frame->evaluateJavaScript(scriptText);
221 void BookView::paintEvent(QPaintEvent *e)
223 QWebView::paintEvent(e);
224 if (!mBook || !loaded) {
229 QPoint scrollPos = page()->mainFrame()->scrollPosition();
230 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
231 QPainter painter(this);
232 foreach (Book::Bookmark b, mBook->bookmarks()) {
233 if (b.chapter != contentIndex) {
236 int height = contentsHeight;
237 int bookmarkPos = (qreal)height * (qreal)b.pos;
238 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
242 void BookView::mousePressEvent(QMouseEvent *e)
244 QWebView::mousePressEvent(e);
246 QWebFrame *frame = page()->mainFrame();
247 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
251 #endif // Q_WS_MAEMO_5
255 void BookView::addBookmark()
257 Trace t("BookView::addBookmark");
258 int y = page()->mainFrame()->scrollPosition().y();
259 int height = page()->mainFrame()->contentsSize().height();
260 t.trace(QString().setNum((qreal)y / (qreal)height));
261 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
265 void BookView::addNavigationBar()
267 Trace t("BookView::addNavigationBar");
273 "<a href=\"javascript:bv.goPrevious();\">"
274 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
280 "<a href=\"javascript:bv.goNext();\">"
281 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
286 if (contentIndex == 0) {
289 if (contentIndex >= mBook->toc.size() - 1) {
293 QWebFrame *frame = page()->currentFrame();
294 QString headerScript = "document.body.innerHTML = '" +
295 naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
296 QString trailerScript = "document.body.innerHTML += '<br /><br />" +
297 naviPrev + naviNext + "';";
299 frame->evaluateJavaScript(headerScript);
300 frame->evaluateJavaScript(trailerScript);
304 QString BookView::tmpPath()
306 return QDir::tempPath() + "/dorian";
309 void BookView::extractIcons()
311 QFile next(ICON_PREFIX + QString("/next.png"));
312 QFile prev(ICON_PREFIX + QString("/previous.png"));
314 QDir().mkpath(tmpPath());
315 next.copy(tmpPath() + "/next.png");
316 prev.copy(tmpPath() + "/previous.png");
319 void BookView::removeIcons()
321 QFile(ICON_PREFIX + QString("/next.png")).remove();
322 QFile(ICON_PREFIX + QString("/previous.png")).remove();
323 QDir().rmpath(tmpPath());
326 bool BookView::eventFilter(QObject *o, QEvent *e)
329 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
330 if (e->type() == QEvent::Resize) {
331 Trace::debug(QString("BookView::eventFilter QEvent::Resize to %1").
332 arg(page()->mainFrame()->contentsSize().height()));
334 Trace::debug(QString("BookView::eventFilter %1").
335 arg(Trace::event(e->type())));
341 case QEvent::MouseButtonPress:
342 emit suppressedMouseButtonPress();
345 case QEvent::MouseButtonRelease:
346 mousePressed = false;
348 case QEvent::MouseMove:
353 case QEvent::MouseButtonDblClick:
359 return QObject::eventFilter(o, e);
362 void BookView::addJavaScriptObjects()
364 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
367 void BookView::onContentsSizeChanged(const QSize &size)
369 contentsHeight = size.height();
371 if (restorePositionAfterLoad) {
372 Trace::debug("BookView::onContentSizeChanged: Time to restore");
373 restorePositionAfterLoad = false;
374 goToPosition(positionAfterLoad);
379 void BookView::leaveEvent(QEvent *e)
381 Trace t("BookView::leaveEvent");
382 // Save current position, to be restored later
384 QWebView::leaveEvent(e);
387 void BookView::enterEvent(QEvent *e)
389 Trace t("BookView::enterEvent");
390 // Restore position saved at Leave event. This seems to be required,
391 // after temporarily switching from portrait to landscape and back
392 restoreLastBookmark();
393 QWebView::enterEvent(e);
396 void BookView::goToPosition(qreal position)
398 int scrollPos = (qreal)contentsHeight * position;
399 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
401 Trace::debug(QString("BookView::goToPosition: To %1 (%2%, height %3)").
402 arg(scrollPos).arg(position * 100).arg(contentsHeight));