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 qDebug() << "" << face;
171 settings()->setFontFamily(QWebSettings::StandardFont, face);
173 else if (key == "scheme") {
174 QWebFrame *frame = page()->mainFrame();
175 QString scheme = Settings::instance()->value("scheme").toString();
176 qDebug() << "" << scheme;
177 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
178 (scheme != "default")) {
181 QFile script(":/styles/" + scheme + ".js");
182 script.open(QFile::ReadOnly);
183 QString scriptText = script.readAll();
185 QVariant ret = frame->evaluateJavaScript(scriptText);
186 qDebug() << "" << script.fileName() << ":" << scriptText;
187 qDebug() << "" << ret;
191 void BookView::paintEvent(QPaintEvent *e)
193 QWebView::paintEvent(e);
202 QPoint scrollPos = page()->mainFrame()->scrollPosition();
203 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
204 QPainter painter(this);
205 foreach (Book::Bookmark b, mBook->bookmarks()) {
206 if (b.chapter != contentIndex) {
209 int height = page()->mainFrame()->contentsSize().height();
210 int bookmarkPos = (qreal)height * (qreal)b.pos;
211 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
215 void BookView::mousePressEvent(QMouseEvent *e)
217 QWebView::mousePressEvent(e);
219 QWebFrame *frame = page()->mainFrame();
220 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
228 void BookView::addBookmark()
230 int y = page()->mainFrame()->scrollPosition().y();
231 int height = page()->mainFrame()->contentsSize().height();
232 qDebug() << "BookView::addBookMark" << ((qreal)y / (qreal)height);
233 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
237 void BookView::addNavigationBar()
244 "<a href=\"javascript:bv.goPrevious();\">"
245 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
251 "<a href=\"javascript:bv.goNext();\">"
252 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
257 if (contentIndex == 0) {
260 if (contentIndex >= mBook->toc.size() - 1) {
264 QWebFrame *frame = page()->currentFrame();
265 frame->addToJavaScriptWindowObject("bv", this);
266 QString headerScript = "document.body.innerHTML = '" +
267 naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
268 QString trailerScript = "document.body.innerHTML += '<br /><br />" +
269 naviPrev + naviNext + "';";
271 frame->evaluateJavaScript(headerScript);
272 frame->evaluateJavaScript(trailerScript);
275 QString BookView::tmpPath()
277 return QDir::tempPath() + "/dorian";
280 void BookView::extractIcons()
282 qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
284 QFile next(ICON_PREFIX + QString("/next.png"));
285 QFile prev(ICON_PREFIX + QString("/previous.png"));
287 QDir().mkpath(tmpPath());
288 next.copy(tmpPath() + "/next.png");
289 prev.copy(tmpPath() + "/previous.png");
292 void BookView::removeIcons()
294 QFile(ICON_PREFIX + QString("/next.png")).remove();
295 QFile(ICON_PREFIX + QString("/previous.png")).remove();
296 QDir().rmpath(tmpPath());