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 emit chapterLoadStart(index);
83 load(QUrl(contentFile));
88 void BookView::setBook(Book *book)
90 qDebug() << "Book::setBook" << (book? book->path(): "");
98 goToBookmark(book->lastBookmark());
102 setHtml(tr("No book"));
107 Book *BookView::book()
112 void BookView::goPrevious()
114 loadContent(contentIndex - 1);
117 void BookView::goNext()
119 loadContent(contentIndex + 1);
122 void BookView::setLastBookmark()
124 qDebug() << "BookView::setLastBookmark";
126 int height = page()->mainFrame()->contentsSize().height();
127 int pos = page()->mainFrame()->scrollPosition().y();
128 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
132 void BookView::goToBookmark(const Book::Bookmark &bookmark)
136 restorePos = bookmark.pos;
137 if (bookmark.chapter != contentIndex) {
138 loadContent(bookmark.chapter);
140 onLoadFinished(true);
145 void BookView::onLoadFinished(bool ok)
147 qDebug() << "BookView::onLoadFinished" << ok;
150 onSettingsChanged("scheme");
151 emit chapterLoadEnd(contentIndex);
155 int height = page()->mainFrame()->contentsSize().height();
156 int scrollPos = (qreal)height * restorePos;
157 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
162 void BookView::onSettingsChanged(const QString &key)
164 qDebug() << "BookView::onSettingsChanged" << key;
166 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
168 else if (key == "font") {
169 QString face = Settings::instance()->value("font").toString();
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 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);
187 void BookView::paintEvent(QPaintEvent *e)
189 QWebView::paintEvent(e);
198 QPoint scrollPos = page()->mainFrame()->scrollPosition();
199 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
200 QPainter painter(this);
201 foreach (Book::Bookmark b, mBook->bookmarks()) {
202 if (b.chapter != contentIndex) {
205 int height = page()->mainFrame()->contentsSize().height();
206 int bookmarkPos = (qreal)height * (qreal)b.pos;
207 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
211 void BookView::mousePressEvent(QMouseEvent *e)
213 QWebView::mousePressEvent(e);
215 QWebFrame *frame = page()->mainFrame();
216 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
224 void BookView::addBookmark()
226 int y = page()->mainFrame()->scrollPosition().y();
227 int height = page()->mainFrame()->contentsSize().height();
228 qDebug() << "BookView::addBookMark" << ((qreal)y / (qreal)height);
229 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
233 void BookView::addNavigationBar()
240 "<a href=\"javascript:bv.goPrevious();\">"
241 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
247 "<a href=\"javascript:bv.goNext();\">"
248 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
253 if (contentIndex == 0) {
256 if (contentIndex >= mBook->toc.size() - 1) {
260 QWebFrame *frame = page()->currentFrame();
261 frame->addToJavaScriptWindowObject("bv", this);
262 QString headerScript = "document.body.innerHTML = '" +
263 naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
264 QString trailerScript = "document.body.innerHTML += '<br /><br />" +
265 naviPrev + naviNext + "';";
267 frame->evaluateJavaScript(headerScript);
268 frame->evaluateJavaScript(trailerScript);
271 QString BookView::tmpPath()
273 return QDir::tempPath() + "/dorian";
276 void BookView::extractIcons()
278 qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
280 QFile next(ICON_PREFIX + QString("/next.png"));
281 QFile prev(ICON_PREFIX + QString("/previous.png"));
283 QDir().mkpath(tmpPath());
284 next.copy(tmpPath() + "/next.png");
285 prev.copy(tmpPath() + "/previous.png");
288 void BookView::removeIcons()
290 QFile(ICON_PREFIX + QString("/next.png")).remove();
291 QFile(ICON_PREFIX + QString("/previous.png")).remove();
292 QDir().rmpath(tmpPath());