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),
21 restore(true), restorePos(0), loadFinished(false)
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,
29 page()->setContentEditable(false);
31 #if defined(Q_WS_MAEMO_5)
32 (void)new SelectionSuppressor(this);
34 QWebFrame *frame = page()->mainFrame();
35 #if defined(Q_WS_MAEMO_5)
36 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
38 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
40 bookmarkImage = QImage(":/icons/bookmark.png");
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)
50 #elif defined(Q_WS_MAC)
56 s->setValue("scheme", s->value("scheme", "default"));
67 void BookView::loadContent(int index)
72 if ((index < 0) || (index >= mBook->toc.size())) {
76 QString contentFile(mBook->content[mBook->toc[index]].href);
77 if (mBook->toc[index] == "error") {
82 load(QUrl(contentFile));
87 void BookView::setBook(Book *book)
89 qDebug() << "Book::setBook" << (book? book->path(): "");
97 goToBookmark(book->lastBookmark());
101 setHtml(tr("No book"));
106 Book *BookView::book()
111 void BookView::goPrevious()
113 loadContent(contentIndex - 1);
116 void BookView::goNext()
118 loadContent(contentIndex + 1);
121 void BookView::setLastBookmark()
123 qDebug() << "BookView::setLastBookmark";
125 int height = page()->mainFrame()->contentsSize().height();
126 int pos = page()->mainFrame()->scrollPosition().y();
127 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
131 void BookView::goToBookmark(const Book::Bookmark &bookmark)
135 restorePos = bookmark.pos;
136 if (bookmark.chapter != contentIndex) {
137 loadContent(bookmark.chapter);
139 onLoadFinished(true);
144 void BookView::onLoadFinished(bool ok)
146 qDebug() << "BookView::onLoadFinished" << ok;
149 onSettingsChanged("scheme");
150 emit chapterLoaded(contentIndex);
154 int height = page()->mainFrame()->contentsSize().height();
155 int scrollPos = (qreal)height * restorePos;
156 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
161 void BookView::onSettingsChanged(const QString &key)
163 qDebug() << "BookView::onSettingsChanged" << key;
165 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
167 else if (key == "font") {
168 QString face = Settings::instance()->value("font").toString();
169 qDebug() << "" << face;
170 settings()->setFontFamily(QWebSettings::StandardFont, face);
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")) {
180 QFile script(":/styles/" + scheme + ".js");
181 script.open(QFile::ReadOnly);
182 QString scriptText = script.readAll();
184 QVariant ret = frame->evaluateJavaScript(scriptText);
185 qDebug() << "" << script.fileName() << ":" << scriptText;
186 qDebug() << "" << ret;
190 void BookView::paintEvent(QPaintEvent *e)
192 QWebView::paintEvent(e);
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) {
208 int height = page()->mainFrame()->contentsSize().height();
209 int bookmarkPos = (qreal)height * (qreal)b.pos;
210 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
214 void BookView::mousePressEvent(QMouseEvent *e)
216 QWebView::mousePressEvent(e);
218 QWebFrame *frame = page()->mainFrame();
219 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
227 void BookView::addBookmark()
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);
236 void BookView::addNavigationBar()
243 "<a href=\"javascript:bv.goPrevious();\">"
244 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
250 "<a href=\"javascript:bv.goNext();\">"
251 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
256 if (contentIndex == 0) {
259 if (contentIndex >= mBook->toc.size() - 1) {
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 + "';";
270 frame->evaluateJavaScript(headerScript);
271 frame->evaluateJavaScript(trailerScript);
274 QString BookView::tmpPath()
276 return QDir::tempPath() + "/dorian";
279 void BookView::extractIcons()
281 qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
283 QFile next(ICON_PREFIX + QString("/next.png"));
284 QFile prev(ICON_PREFIX + QString("/previous.png"));
286 QDir().mkpath(tmpPath());
287 next.copy(tmpPath() + "/next.png");
288 prev.copy(tmpPath() + "/previous.png");
291 void BookView::removeIcons()
293 QFile(ICON_PREFIX + QString("/next.png")).remove();
294 QFile(ICON_PREFIX + QString("/previous.png")).remove();
295 QDir().rmpath(tmpPath());