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