10 #include "selectionsuppressor.h"
14 # define ICON_PREFIX ":/icons/mac/"
16 # define ICON_PREFIX ":/icons/"
19 BookView::BookView(QWidget *parent):
20 QWebView(parent), contentIndex(-1), mBook(0), restore(true), restorePos(0),
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);
30 #if defined(Q_WS_MAEMO_5)
31 (void)new SelectionSuppressor(this);
33 QWebFrame *frame = page()->mainFrame();
34 #if defined(Q_WS_MAEMO_5)
35 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
37 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
39 bookmarkImage = QImage(":/icons/bookmark.png");
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)
49 #elif defined(Q_WS_MAC)
55 s->setValue("scheme", s->value("scheme", "default"));
66 void BookView::loadContent(int index)
71 if ((index < 0) || (index >= mBook->toc.size())) {
75 QString contentFile(mBook->content[mBook->toc[index]].href);
76 if (mBook->toc[index] == "error") {
81 load(QUrl(contentFile));
86 void BookView::setBook(Book *book)
88 qDebug() << "Book::setBook" << (book? book->path(): "");
96 goToBookmark(book->lastBookmark());
100 setHtml(tr("No book"));
105 Book *BookView::book()
110 void BookView::goPrevious()
112 loadContent(contentIndex - 1);
115 void BookView::goNext()
117 loadContent(contentIndex + 1);
120 void BookView::setLastBookmark()
122 qDebug() << "BookView::setLastBookmark";
124 int height = page()->mainFrame()->contentsSize().height();
125 int pos = page()->mainFrame()->scrollPosition().y();
126 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
130 void BookView::goToBookmark(const Book::Bookmark &bookmark)
134 restorePos = bookmark.pos;
135 if (bookmark.chapter != contentIndex) {
136 loadContent(bookmark.chapter);
138 onLoadFinished(true);
143 void BookView::onLoadFinished(bool ok)
145 qDebug() << "BookView::onLoadFinished" << ok;
148 onSettingsChanged("scheme");
149 emit chapterLoaded(contentIndex);
153 int height = page()->mainFrame()->contentsSize().height();
154 int scrollPos = (qreal)height * restorePos;
155 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
160 void BookView::onSettingsChanged(const QString &key)
162 qDebug() << "BookView::onSettingsChanged" << key;
164 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
166 else if (key == "font") {
167 QString face = Settings::instance()->value("font").toString();
168 qDebug() << "" << face;
169 settings()->setFontFamily(QWebSettings::StandardFont, face);
171 else if (key == "scheme") {
172 QWebFrame *frame = page()->mainFrame();
173 QString scheme = Settings::instance()->value("scheme").toString();
174 qDebug() << "" << scheme;
175 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
176 (scheme != "default")) {
179 QFile script(":/styles/" + scheme + ".js");
180 script.open(QFile::ReadOnly);
181 QString scriptText = script.readAll();
183 QVariant ret = frame->evaluateJavaScript(scriptText);
184 qDebug() << "" << script.fileName() << ":" << scriptText;
185 qDebug() << "" << ret;
189 void BookView::paintEvent(QPaintEvent *e)
191 QWebView::paintEvent(e);
200 QPoint scrollPos = page()->mainFrame()->scrollPosition();
201 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
202 QPainter painter(this);
203 foreach (Book::Bookmark b, mBook->bookmarks()) {
204 if (b.chapter != contentIndex) {
207 int height = page()->mainFrame()->contentsSize().height();
208 int bookmarkPos = (qreal)height * (qreal)b.pos;
209 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
213 void BookView::mousePressEvent(QMouseEvent *e)
215 QWebView::mousePressEvent(e);
217 QWebFrame *frame = page()->mainFrame();
218 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
226 void BookView::addBookmark()
228 int y = page()->mainFrame()->scrollPosition().y();
229 int height = page()->mainFrame()->contentsSize().height();
230 qDebug() << "BookView::addBookMark" << ((qreal)y / (qreal)height);
231 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
235 void BookView::addNavigationBar()
242 "<a href=\"javascript:bv.goPrevious();\">"
243 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
249 "<a href=\"javascript:bv.goNext();\">"
250 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
255 if (contentIndex == 0) {
258 if (contentIndex >= mBook->toc.size() - 1) {
262 QWebFrame *frame = page()->currentFrame();
263 frame->addToJavaScriptWindowObject("bv", this);
264 QString headerScript = "document.body.innerHTML = '" +
265 naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
266 QString trailerScript = "document.body.innerHTML += '<br /><br />" +
267 naviPrev + naviNext + "';";
269 frame->evaluateJavaScript(headerScript);
270 frame->evaluateJavaScript(trailerScript);
273 QString BookView::tmpPath()
275 return QDir::tempPath() + "/dorian";
278 void BookView::extractIcons()
280 qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
282 QFile next(ICON_PREFIX + QString("/next.png"));
283 QFile prev(ICON_PREFIX + QString("/previous.png"));
285 QDir().mkpath(tmpPath());
286 next.copy(tmpPath() + "/next.png");
287 prev.copy(tmpPath() + "/previous.png");
290 void BookView::removeIcons()
292 QFile(ICON_PREFIX + QString("/next.png")).remove();
293 QFile(ICON_PREFIX + QString("/previous.png")).remove();
294 QDir().rmpath(tmpPath());