271370eb4779139c47735a8e38c3cd534af390b8
[dorian] / bookview.cpp
1 #include <QDebug>
2 #include <QWebFrame>
3 #include <QMouseEvent>
4 #include <QFile>
5 #include <QDir>
6
7 #include "book.h"
8 #include "bookview.h"
9 #include "library.h"
10 #include "selectionsuppressor.h"
11 #include "settings.h"
12
13 #ifdef Q_WS_MAC
14 #   define ICON_PREFIX ":/icons/mac/"
15 #else
16 #   define ICON_PREFIX ":/icons/"
17 #endif
18
19 BookView::BookView(QWidget *parent):
20         QWebView(parent), contentIndex(0), mBook(0), restore(true), restorePos(0),
21         loadFinished(false)
22 {
23     settings()->setAttribute(QWebSettings::AutoLoadImages, true);
24     settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
25     settings()->setAttribute(QWebSettings::PluginsEnabled, false);
26     settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
27     settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, false);
28     page()->setContentEditable(false);
29
30 #if defined(Q_WS_MAEMO_5)
31     (void)new SelectionSuppressor(this);
32 #endif
33     QWebFrame *frame = page()->mainFrame();
34 #if defined(Q_WS_MAEMO_5)
35     frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
36 #endif
37     frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
38
39     bookmarkImage = QImage(":/icons/bookmark.png");
40
41     connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
42     connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
43             this, SLOT(onSettingsChanged(const QString &)));
44     Settings *s = Settings::instance();
45     s->setValue("zoom", s->value("zoom", 160));
46     s->setValue("font", s->value("font",
47 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
48                                  "Serif"
49 #elif defined(Q_WS_MAC)
50                                  "Hoefler Text"
51 #else
52                                  "Times New Roman"
53 #endif
54                                  ));
55     s->setValue("scheme", s->value("scheme", "default"));
56     setBook(0);
57
58     extractIcons();
59 }
60
61 BookView::~BookView()
62 {
63     removeIcons();
64 }
65
66 void BookView::loadContent(int index)
67 {
68     if (!mBook) {
69         return;
70     }
71     if ((index < 0) || (index >= mBook->toc.size())) {
72         return;
73     }
74
75     QString contentFile(mBook->content[mBook->toc[index]].href);
76     if (mBook->toc[index] == "error") {
77         setHtml(contentFile);
78     }
79     else {
80         loadFinished = false;
81         load(QUrl(contentFile));
82     }
83     contentIndex = index;
84 }
85
86 void BookView::setBook(Book *book)
87 {
88     qDebug() << "Book::setBook" << (book? book->path(): "");
89
90     setLastBookmark();
91     if (book != mBook) {
92         mBook = book;
93         if (book) {
94             book->open();
95             goToBookmark(book->lastBookmark());
96         }
97         else {
98             setHtml(tr("No book"));
99         }
100     }
101 }
102
103 Book *BookView::book()
104 {
105     return mBook;
106 }
107
108 void BookView::goPrevious()
109 {
110     loadContent(contentIndex - 1);
111 }
112
113 void BookView::goNext()
114 {
115     loadContent(contentIndex + 1);
116 }
117
118 void BookView::setLastBookmark()
119 {
120     qDebug() << "BookView::setLastBookmark";
121     if (mBook) {
122         int height = page()->mainFrame()->contentsSize().height();
123         int pos = page()->mainFrame()->scrollPosition().y();
124         mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
125     }
126 }
127
128 void BookView::goToBookmark(const Book::Bookmark &bookmark)
129 {
130     if (mBook) {
131         restore = true;
132         restorePos = bookmark.pos;
133         loadContent(bookmark.chapter);
134     }
135 }
136
137 void BookView::onLoadFinished(bool ok)
138 {
139     qDebug() << "BookView::onLoadFinished" << ok;
140     loadFinished = true;
141     addNavigationBar();
142     onSettingsChanged("scheme");
143     if (restore) {
144         restore = false;
145         if (ok && mBook) {
146             int height = page()->mainFrame()->contentsSize().height();
147             int scrollPos = (qreal)height * restorePos;
148             page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
149         }
150     }
151 }
152
153 void BookView::onSettingsChanged(const QString &key)
154 {
155     qDebug() << "BookView::onSettingsChanged" << key;
156     if (key == "zoom") {
157         setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
158     }
159     else if (key == "font") {
160         QString face = Settings::instance()->value("font").toString();
161         qDebug() << "" << face;
162         settings()->setFontFamily(QWebSettings::StandardFont, face);
163     }
164     else if (key == "scheme") {
165         QWebFrame *frame = page()->mainFrame();
166         QString scheme = Settings::instance()->value("scheme").toString();
167         qDebug() << "" << scheme;
168         if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
169             (scheme != "default")) {
170             scheme = "default";
171         }
172         QFile script(":/styles/" + scheme + ".js");
173         script.open(QFile::ReadOnly);
174         QString scriptText = script.readAll();
175         script.close();
176         QVariant ret = frame->evaluateJavaScript(scriptText);
177         qDebug() << "" << script.fileName() << ":" << scriptText;
178         qDebug() << "" << ret;
179     }
180 }
181
182 void BookView::paintEvent(QPaintEvent *e)
183 {
184     QWebView::paintEvent(e);
185     if (!mBook) {
186         return;
187     }
188
189     // Paint bookmarks
190     if (!loadFinished) {
191         return;
192     }
193     QPoint scrollPos = page()->mainFrame()->scrollPosition();
194     QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
195     QPainter painter(this);
196     foreach (Book::Bookmark b, mBook->bookmarks()) {
197         if (b.chapter != contentIndex) {
198             continue;
199         }
200         int height = page()->mainFrame()->contentsSize().height();
201         int bookmarkPos = (qreal)height * (qreal)b.pos;
202         painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
203     }
204 }
205
206 void BookView::mousePressEvent(QMouseEvent *e)
207 {
208     QWebView::mousePressEvent(e);
209 #ifndef Q_WS_MAEMO_5
210     QWebFrame *frame = page()->mainFrame();
211     if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
212         e->accept();
213         return;
214     }
215 #endif
216     e->ignore();
217 }
218
219 void BookView::addBookmark()
220 {
221     int y = page()->mainFrame()->scrollPosition().y();
222     int height = page()->mainFrame()->contentsSize().height();
223     qDebug() << "BookView::addBookMark" << ((qreal)y / (qreal)height);
224     mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
225     repaint();
226 }
227
228 void BookView::addNavigationBar()
229 {
230     QWebFrame *frame = page()->currentFrame();
231     frame->addToJavaScriptWindowObject("bv", this);
232
233     QString naviPrev =
234             "<a href=\"javascript:bv.goPrevious();\">"
235             "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
236             "src=\"file://"
237             + tmpPath() +
238             "/previous.png\" />"
239             "</a>";
240     QString naviNext =
241             "<a href=\"javascript:bv.goNext();\">"
242             "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
243             "src=\"file://"
244             + tmpPath() +
245             "/next.png\" />"
246             "</a>";
247
248     if (contentIndex == 0) {
249         naviPrev = "";
250     }
251     if (contentIndex >= mBook->toc.size() - 1) {
252         naviNext = "";
253     }
254
255     QString headerScript = "document.body.innerHTML = '" +
256         naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
257     QString trailerScript = "document.body.innerHTML += '<br /><br />" +
258         naviPrev + naviNext + "';";
259
260     frame->evaluateJavaScript(headerScript);
261     frame->evaluateJavaScript(trailerScript);
262
263     // qDebug() << page()->currentFrame()->toHtml();
264 }
265
266 QString BookView::tmpPath()
267 {
268     return QDir::tempPath() + "/dorian";
269 }
270
271 void BookView::extractIcons()
272 {
273     qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
274
275     QFile next(ICON_PREFIX + QString("/next.png"));
276     QFile prev(ICON_PREFIX + QString("/previous.png"));
277
278     QDir().mkpath(tmpPath());
279     next.copy(tmpPath() + "/next.png");
280     prev.copy(tmpPath() + "/previous.png");
281 }
282
283 void BookView::removeIcons()
284 {
285     QFile(ICON_PREFIX + QString("/next.png")).remove();
286     QFile(ICON_PREFIX + QString("/previous.png")).remove();
287     QDir().rmpath(tmpPath());
288 }