15 # define ICON_PREFIX ":/icons/mac/"
17 # define ICON_PREFIX ":/icons/"
20 BookView::BookView(QWidget *parent):
21 QWebView(parent), contentIndex(-1), mBook(0), restorePositionAfterLoad(false),
22 positionAfterLoad(0), loaded(false), contentsHeight(0), decorated(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, false);
34 settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
35 settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
36 settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
38 settings()->setDefaultTextEncoding("utf-8");
39 page()->setContentEditable(false);
41 #if defined(Q_WS_MAEMO_5)
42 // Suppress unwanted text selections on Maemo
43 installEventFilter(this);
45 QWebFrame *frame = page()->mainFrame();
46 #if defined(Q_WS_MAEMO_5)
47 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
49 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
51 bookmarkImage = QImage(":/icons/bookmark.png");
53 connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
54 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
55 this, SLOT(addJavaScriptObjects()));
56 connect(frame, SIGNAL(contentsSizeChanged(const QSize &)),
57 this, SLOT(onContentsSizeChanged(const QSize &)));
58 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
59 this, SLOT(onSettingsChanged(const QString &)));
60 Settings *s = Settings::instance();
61 s->setValue("zoom", s->value("zoom", 160));
62 s->setValue("font", s->value("font",
63 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
65 #elif defined(Q_WS_MAC)
71 s->setValue("scheme", s->value("scheme", "default"));
79 Trace t("BookView::~BookView");
83 void BookView::loadContent(int index)
85 Trace t("BookView::loadContent");
89 if ((index < 0) || (index >= mBook->toc.size())) {
93 QString contentFile(mBook->content[mBook->toc[index]].href);
94 if (mBook->toc[index] == "error") {
100 emit chapterLoadStart(index);
101 load(QUrl(contentFile));
103 contentIndex = index;
106 void BookView::setBook(Book *book)
108 Trace t("BookView::setBook");
110 // Save position in current book
113 // Open new book, restore last position
119 restoreLastBookmark();
123 setHtml(tr("No book"));
128 Book *BookView::book()
133 void BookView::goPrevious()
135 Trace t("BookView::goPrevious");
136 loadContent(contentIndex - 1);
139 void BookView::goNext()
141 Trace t("BookView::goNext");
142 loadContent(contentIndex + 1);
145 void BookView::setLastBookmark()
147 Trace t("BookView::setLastBookmark");
149 int height = contentsHeight;
150 int pos = page()->mainFrame()->scrollPosition().y();
151 t.trace(QString("At %1 (%2%, height %3)").
152 arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height));
153 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
157 void BookView::restoreLastBookmark()
159 Trace t("BookView::restoreLastBookmark");
161 goToBookmark(mBook->lastBookmark());
165 void BookView::goToBookmark(const Book::Bookmark &bookmark)
167 Trace t("BookView::goToBookmark");
169 if (bookmark.chapter != contentIndex) {
170 t.trace(QString("Loading new chapter %1").arg(bookmark.chapter));
171 restorePositionAfterLoad = true;
172 positionAfterLoad = bookmark.pos;
173 loadContent(bookmark.chapter);
175 goToPosition(bookmark.pos);
180 void BookView::onLoadFinished(bool ok)
182 Trace t("BookView::onLoadFinished");
189 onSettingsChanged("scheme");
190 emit chapterLoadEnd(contentIndex);
193 void BookView::onSettingsChanged(const QString &key)
195 Trace t("BookView::onSettingsChanged " + key);
197 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
199 else if (key == "font") {
200 QString face = Settings::instance()->value("font").toString();
201 settings()->setFontFamily(QWebSettings::StandardFont, face);
203 else if (key == "scheme") {
204 QWebFrame *frame = page()->mainFrame();
205 QString scheme = Settings::instance()->value("scheme").toString();
206 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
207 (scheme != "default")) {
210 QFile script(":/styles/" + scheme + ".js");
211 script.open(QFile::ReadOnly);
212 QString scriptText = script.readAll();
214 QVariant ret = frame->evaluateJavaScript(scriptText);
218 void BookView::paintEvent(QPaintEvent *e)
220 QWebView::paintEvent(e);
221 if (!mBook || !loaded) {
226 QPoint scrollPos = page()->mainFrame()->scrollPosition();
227 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
228 QPainter painter(this);
229 foreach (Book::Bookmark b, mBook->bookmarks()) {
230 if (b.chapter != contentIndex) {
233 int height = contentsHeight;
234 int bookmarkPos = (qreal)height * (qreal)b.pos;
235 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
239 void BookView::mousePressEvent(QMouseEvent *e)
241 QWebView::mousePressEvent(e);
243 QWebFrame *frame = page()->mainFrame();
244 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
248 #endif // Q_WS_MAEMO_5
252 void BookView::addBookmark()
254 Trace t("BookView::addBookmark");
255 int y = page()->mainFrame()->scrollPosition().y();
256 int height = page()->mainFrame()->contentsSize().height();
257 t.trace(QString().setNum((qreal)y / (qreal)height));
258 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
262 void BookView::addNavigationBar()
264 Trace t("BookView::addNavigationBar");
270 "<a href=\"javascript:bv.goPrevious();\">"
271 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
277 "<a href=\"javascript:bv.goNext();\">"
278 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
283 if (contentIndex == 0) {
286 if (contentIndex >= mBook->toc.size() - 1) {
290 QWebFrame *frame = page()->currentFrame();
291 QString headerScript = "document.body.innerHTML = '" +
292 naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
293 QString trailerScript = "document.body.innerHTML += '<br /><br />" +
294 naviPrev + naviNext + "';";
296 frame->evaluateJavaScript(headerScript);
297 frame->evaluateJavaScript(trailerScript);
301 QString BookView::tmpPath()
303 return QDir::tempPath() + "/dorian";
306 void BookView::extractIcons()
308 QFile next(ICON_PREFIX + QString("/next.png"));
309 QFile prev(ICON_PREFIX + QString("/previous.png"));
311 QDir().mkpath(tmpPath());
312 next.copy(tmpPath() + "/next.png");
313 prev.copy(tmpPath() + "/previous.png");
316 void BookView::removeIcons()
318 QFile(ICON_PREFIX + QString("/next.png")).remove();
319 QFile(ICON_PREFIX + QString("/previous.png")).remove();
320 QDir().rmpath(tmpPath());
323 bool BookView::eventFilter(QObject *o, QEvent *e)
326 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
327 if (e->type() == QEvent::Resize) {
328 Trace::debug(QString("BookView::eventFilter QEvent::Resize to %1").
329 arg(page()->mainFrame()->contentsSize().height()));
331 Trace::debug(QString("BookView::eventFilter %1").
332 arg(Trace::event(e->type())));
338 case QEvent::MouseButtonPress:
339 emit suppressedMouseButtonPress();
342 case QEvent::MouseButtonRelease:
343 mousePressed = false;
345 case QEvent::MouseMove:
350 case QEvent::MouseButtonDblClick:
356 return QObject::eventFilter(o, e);
359 void BookView::addJavaScriptObjects()
361 page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
364 void BookView::onContentsSizeChanged(const QSize &size)
366 contentsHeight = size.height();
368 if (restorePositionAfterLoad) {
369 Trace::debug("BookView::onContentSizeChanged: Time to restore");
370 restorePositionAfterLoad = false;
371 goToPosition(positionAfterLoad);
376 void BookView::leaveEvent(QEvent *e)
378 Trace t("BookView::leaveEvent");
379 // Save current position, to be restored later
381 QWebView::leaveEvent(e);
384 void BookView::enterEvent(QEvent *e)
386 Trace t("BookView::enterEvent");
387 // Restore position saved at Leave event. This seems to be required,
388 // after temporarily switching from portrait to landscape and back
389 restoreLastBookmark();
390 QWebView::enterEvent(e);
393 void BookView::goToPosition(qreal position)
395 int scrollPos = (qreal)contentsHeight * position;
396 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
398 Trace::debug(QString("BookView::goToPosition: To %1 (%2%, height %3)").
399 arg(scrollPos).arg(position * 100).arg(contentsHeight));