10 #include "selectionsuppressor.h"
14 # define ICON_PREFIX ":/icons/mac/"
16 # define ICON_PREFIX ":/icons/"
19 BookView::BookView(QWidget *parent):
20 QWebView(parent), contentIndex(0), 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(): "");
95 goToBookmark(book->lastBookmark());
98 setHtml(tr("No book"));
103 Book *BookView::book()
108 void BookView::goPrevious()
110 loadContent(contentIndex - 1);
113 void BookView::goNext()
115 loadContent(contentIndex + 1);
118 void BookView::setLastBookmark()
120 qDebug() << "BookView::setLastBookmark";
122 int height = page()->mainFrame()->contentsSize().height();
123 int pos = page()->mainFrame()->scrollPosition().y();
124 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
128 void BookView::goToBookmark(const Book::Bookmark &bookmark)
132 restorePos = bookmark.pos;
133 loadContent(bookmark.chapter);
137 void BookView::onLoadFinished(bool ok)
139 qDebug() << "BookView::onLoadFinished" << ok;
142 onSettingsChanged("scheme");
143 emit chapterLoaded(contentIndex);
147 int height = page()->mainFrame()->contentsSize().height();
148 int scrollPos = (qreal)height * restorePos;
149 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
154 void BookView::onSettingsChanged(const QString &key)
156 qDebug() << "BookView::onSettingsChanged" << key;
158 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
160 else if (key == "font") {
161 QString face = Settings::instance()->value("font").toString();
162 qDebug() << "" << face;
163 settings()->setFontFamily(QWebSettings::StandardFont, face);
165 else if (key == "scheme") {
166 QWebFrame *frame = page()->mainFrame();
167 QString scheme = Settings::instance()->value("scheme").toString();
168 qDebug() << "" << scheme;
169 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
170 (scheme != "default")) {
173 QFile script(":/styles/" + scheme + ".js");
174 script.open(QFile::ReadOnly);
175 QString scriptText = script.readAll();
177 QVariant ret = frame->evaluateJavaScript(scriptText);
178 qDebug() << "" << script.fileName() << ":" << scriptText;
179 qDebug() << "" << ret;
183 void BookView::paintEvent(QPaintEvent *e)
185 QWebView::paintEvent(e);
194 QPoint scrollPos = page()->mainFrame()->scrollPosition();
195 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
196 QPainter painter(this);
197 foreach (Book::Bookmark b, mBook->bookmarks()) {
198 if (b.chapter != contentIndex) {
201 int height = page()->mainFrame()->contentsSize().height();
202 int bookmarkPos = (qreal)height * (qreal)b.pos;
203 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
207 void BookView::mousePressEvent(QMouseEvent *e)
209 QWebView::mousePressEvent(e);
211 QWebFrame *frame = page()->mainFrame();
212 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
220 void BookView::addBookmark()
222 int y = page()->mainFrame()->scrollPosition().y();
223 int height = page()->mainFrame()->contentsSize().height();
224 qDebug() << "BookView::addBookMark" << ((qreal)y / (qreal)height);
225 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
229 void BookView::addNavigationBar()
236 "<a href=\"javascript:bv.goPrevious();\">"
237 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
243 "<a href=\"javascript:bv.goNext();\">"
244 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
249 if (contentIndex == 0) {
252 if (contentIndex >= mBook->toc.size() - 1) {
256 QWebFrame *frame = page()->currentFrame();
257 frame->addToJavaScriptWindowObject("bv", this);
258 QString headerScript = "document.body.innerHTML = '" +
259 naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
260 QString trailerScript = "document.body.innerHTML += '<br /><br />" +
261 naviPrev + naviNext + "';";
263 frame->evaluateJavaScript(headerScript);
264 frame->evaluateJavaScript(trailerScript);
267 QString BookView::tmpPath()
269 return QDir::tempPath() + "/dorian";
272 void BookView::extractIcons()
274 qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
276 QFile next(ICON_PREFIX + QString("/next.png"));
277 QFile prev(ICON_PREFIX + QString("/previous.png"));
279 QDir().mkpath(tmpPath());
280 next.copy(tmpPath() + "/next.png");
281 prev.copy(tmpPath() + "/previous.png");
284 void BookView::removeIcons()
286 QFile(ICON_PREFIX + QString("/next.png")).remove();
287 QFile(ICON_PREFIX + QString("/previous.png")).remove();
288 QDir().rmpath(tmpPath());