15 # define ICON_PREFIX ":/icons/mac/"
17 # define ICON_PREFIX ":/icons/"
20 BookView::BookView(QWidget *parent):
21 QWebView(parent), contentIndex(-1), mBook(0), restore(true),
22 positionAfterLoad(0), loaded(false)
24 Trace t("BookView::BookView");
25 settings()->setAttribute(QWebSettings::AutoLoadImages, true);
26 settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
27 settings()->setAttribute(QWebSettings::PluginsEnabled, false);
28 settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
29 settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
31 page()->setContentEditable(false);
33 #if defined(Q_WS_MAEMO_5)
34 // Suppress unwanted text selections on Maemo
35 installEventFilter(this);
37 QWebFrame *frame = page()->mainFrame();
38 #if defined(Q_WS_MAEMO_5)
39 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
41 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
43 bookmarkImage = QImage(":/icons/bookmark.png");
45 connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
46 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
47 this, SLOT(onSettingsChanged(const QString &)));
48 Settings *s = Settings::instance();
49 s->setValue("zoom", s->value("zoom", 160));
50 s->setValue("font", s->value("font",
51 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
53 #elif defined(Q_WS_MAC)
59 s->setValue("scheme", s->value("scheme", "default"));
67 Trace t("BookView::~BookView");
71 void BookView::loadContent(int index)
73 Trace t("BookView::loadContent");
77 if ((index < 0) || (index >= mBook->toc.size())) {
81 QString contentFile(mBook->content[mBook->toc[index]].href);
82 if (mBook->toc[index] == "error") {
87 emit chapterLoadStart(index);
88 load(QUrl(contentFile));
93 void BookView::setBook(Book *book)
95 Trace t("BookView::setBook");
102 goToBookmark(book->lastBookmark());
106 setHtml(tr("No book"));
111 Book *BookView::book()
116 void BookView::goPrevious()
118 Trace t("BookView::goPrevious");
119 loadContent(contentIndex - 1);
122 void BookView::goNext()
124 Trace t("BookView::goNext");
125 loadContent(contentIndex + 1);
128 void BookView::setLastBookmark()
130 Trace t("BookView::saveLastBookmark");
132 int height = page()->mainFrame()->contentsSize().height();
133 int pos = page()->mainFrame()->scrollPosition().y();
134 mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
138 void BookView::goToBookmark(const Book::Bookmark &bookmark)
140 Trace t("BookView::goToBookmark");
143 positionAfterLoad = bookmark.pos;
144 if (bookmark.chapter != contentIndex) {
145 loadContent(bookmark.chapter);
147 onLoadFinished(true);
152 void BookView::onLoadFinished(bool ok)
154 Trace t(QString("BookView::onLoadFinished: %1").arg(ok));
159 onSettingsChanged("scheme");
160 emit chapterLoadEnd(contentIndex);
164 int height = page()->mainFrame()->contentsSize().height();
165 int scrollPos = (qreal)height * positionAfterLoad;
166 page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
171 void BookView::onSettingsChanged(const QString &key)
173 Trace t("BookView::onSettingsChanged " + key);
175 setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
177 else if (key == "font") {
178 QString face = Settings::instance()->value("font").toString();
179 settings()->setFontFamily(QWebSettings::StandardFont, face);
181 else if (key == "scheme") {
182 QWebFrame *frame = page()->mainFrame();
183 QString scheme = Settings::instance()->value("scheme").toString();
184 if ((scheme != "day") && (scheme != "night") && (scheme != "sand") &&
185 (scheme != "default")) {
188 QFile script(":/styles/" + scheme + ".js");
189 script.open(QFile::ReadOnly);
190 QString scriptText = script.readAll();
192 QVariant ret = frame->evaluateJavaScript(scriptText);
196 void BookView::paintEvent(QPaintEvent *e)
198 QWebView::paintEvent(e);
199 if (!mBook || !loaded) {
204 QPoint scrollPos = page()->mainFrame()->scrollPosition();
205 QPixmap bookmarkPixmap = QPixmap::fromImage(bookmarkImage);
206 QPainter painter(this);
207 foreach (Book::Bookmark b, mBook->bookmarks()) {
208 if (b.chapter != contentIndex) {
211 int height = page()->mainFrame()->contentsSize().height();
212 int bookmarkPos = (qreal)height * (qreal)b.pos;
213 painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
217 void BookView::mousePressEvent(QMouseEvent *e)
219 QWebView::mousePressEvent(e);
221 QWebFrame *frame = page()->mainFrame();
222 if (frame->scrollBarGeometry(Qt::Vertical).contains(e->pos())) {
226 #endif // Q_WS_MAEMO_5
230 void BookView::addBookmark()
232 Trace t("BookView::addBookmark");
233 int y = page()->mainFrame()->scrollPosition().y();
234 int height = page()->mainFrame()->contentsSize().height();
235 t.trace(QString().setNum((qreal)y / (qreal)height));
236 mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
240 void BookView::addNavigationBar()
242 Trace t("BookView::addNavigationBar");
248 "<a href=\"javascript:bv.goPrevious();\">"
249 "<img width=\"95\" height=\"95\" style=\"float:left;clear:none;\" "
255 "<a href=\"javascript:bv.goNext();\">"
256 "<img width=\"95\" height=\"95\" style=\"float:right;clear:none;\" "
261 if (contentIndex == 0) {
264 if (contentIndex >= mBook->toc.size() - 1) {
268 QWebFrame *frame = page()->currentFrame();
269 frame->addToJavaScriptWindowObject("bv", this);
270 QString headerScript = "document.body.innerHTML = '" +
271 naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
272 QString trailerScript = "document.body.innerHTML += '<br /><br />" +
273 naviPrev + naviNext + "';";
275 frame->evaluateJavaScript(headerScript);
276 frame->evaluateJavaScript(trailerScript);
279 QString BookView::tmpPath()
281 return QDir::tempPath() + "/dorian";
284 void BookView::extractIcons()
286 QFile next(ICON_PREFIX + QString("/next.png"));
287 QFile prev(ICON_PREFIX + QString("/previous.png"));
289 QDir().mkpath(tmpPath());
290 next.copy(tmpPath() + "/next.png");
291 prev.copy(tmpPath() + "/previous.png");
294 void BookView::removeIcons()
296 QFile(ICON_PREFIX + QString("/next.png")).remove();
297 QFile(ICON_PREFIX + QString("/previous.png")).remove();
298 QDir().rmpath(tmpPath());
301 bool BookView::eventFilter(QObject *, QEvent *e)
303 if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
304 Trace::debug(QString("BookView::eventFilter %1").
305 arg(Trace::event(e->type())));
308 case QEvent::MouseButtonPress:
309 emit suppressedMouseButtonPress();
312 case QEvent::MouseButtonRelease:
313 mousePressed = false;
315 case QEvent::MouseMove:
320 case QEvent::MouseButtonDblClick: