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");
146 int height = page()->mainFrame()->contentsSize().height();
147 int scrollPos = (qreal)height * restorePos;
148 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
153 void BookView::onSettingsChanged(const QString &key)
155 qDebug() << "BookView::onSettingsChanged" << key;
157 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
159 else if (key == "font") {
160 QString face = Settings::instance()->value("font").toString();
161 qDebug() << "" << face;
162 settings()->setFontFamily(QWebSettings::StandardFont, face);
164 else if (key == "scheme") {
165 QWebFrame *frame = page()->mainFrame();
166 QString scheme = Settings::instance()->value("scheme").toString();
167 qDebug() << "" << scheme;
168 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
169 (scheme != "default")) {
172 QFile script(":/styles/" + scheme + ".js");
173 script.open(QFile::ReadOnly);
174 QString scriptText = script.readAll();
176 QVariant ret = frame->evaluateJavaScript(scriptText);
177 qDebug() << "" << script.fileName() << ":" << scriptText;
178 qDebug() << "" << ret;
182 void BookView::paintEvent(QPaintEvent *e)
184 QWebView::paintEvent(e);
193 QPoint scrollPos = page()->mainFrame()->scrollPosition();
194 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
195 QPainter painter(this);
196 foreach (Book::Bookmark b, mBook->bookmarks()) {
197 if (b.chapter != contentIndex) {
200 int height = page()->mainFrame()->contentsSize().height();
201 int bookmarkPos = (qreal)height * (qreal)b.pos;
202 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
206 void BookView::mousePressEvent(QMouseEvent *e)
208 QWebView::mousePressEvent(e);
210 QWebFrame *frame = page()->mainFrame();
211 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
219 void BookView::addBookmark()
221 int y = page()->mainFrame()->scrollPosition().y();
222 int height = page()->mainFrame()->contentsSize().height();
223 qDebug() << "BookView::addBookMark" << ((qreal)y / (qreal)height);
224 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
228 void BookView::addNavigationBar()
230 QWebFrame *frame = page()->currentFrame();
231 frame->addToJavaScriptWindowObject("bv", this);
234 "<a href=\"javascript:bv.goPrevious();\">"
235 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
241 "<a href=\"javascript:bv.goNext();\">"
242 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
248 if (contentIndex == 0) {
251 if (contentIndex >= mBook->toc.size() - 1) {
255 QString headerScript = "document.body.innerHTML = '" +
256 naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
257 QString trailerScript = "document.body.innerHTML += '<br /><br />" +
258 naviPrev + naviNext + "';";
260 frame->evaluateJavaScript(headerScript);
261 frame->evaluateJavaScript(trailerScript);
263 // qDebug() << page()->currentFrame()->toHtml();
266 QString BookView::tmpPath()
268 return QDir::tempPath() + "/dorian";
271 void BookView::extractIcons()
273 qDebug() << "BookView::extractIcons: Extracting to" << tmpPath();
275 QFile next(ICON_PREFIX + QString("/next.png"));
276 QFile prev(ICON_PREFIX + QString("/previous.png"));
278 QDir().mkpath(tmpPath());
279 next.copy(tmpPath() + "/next.png");
280 prev.copy(tmpPath() + "/previous.png");
283 void BookView::removeIcons()
285 QFile(ICON_PREFIX + QString("/next.png")).remove();
286 QFile(ICON_PREFIX + QString("/previous.png")).remove();
287 QDir().rmpath(tmpPath());