BookView::BookView(QWidget *parent):
QWebView(parent), contentIndex(-1), mBook(0), restore(true),
- positionAfterLoad(0), loaded(false)
+ positionAfterLoad(0), loaded(false), contentsHeight(0), decorated(false)
{
Trace t("BookView::BookView");
settings()->setAttribute(QWebSettings::AutoLoadImages, true);
settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
+ settings()->setAttribute(QWebSettings::JavaEnabled, false);
settings()->setAttribute(QWebSettings::PluginsEnabled, false);
+ settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
+ settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
+ settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, false);
+ settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, false);
+ settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, false);
+ settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
false);
+ settings()->setDefaultTextEncoding("utf-8");
page()->setContentEditable(false);
#if defined(Q_WS_MAEMO_5)
bookmarkImage = QImage(":/icons/bookmark.png");
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
+ connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
+ this, SLOT(addJavaScriptObjects()));
+ connect(frame, SIGNAL(contentsSizeChanged(const QSize &)),
+ this, SLOT(onContentsSizeChanged(const QSize &)));
connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
this, SLOT(onSettingsChanged(const QString &)));
Settings *s = Settings::instance();
}
else {
loaded = false;
+ decorated = false;
emit chapterLoadStart(index);
load(QUrl(contentFile));
}
{
Trace t("BookView::saveLastBookmark");
if (mBook) {
- int height = page()->mainFrame()->contentsSize().height();
+ int height = contentsHeight; // page()->mainFrame()->contentsSize().height();
int pos = page()->mainFrame()->scrollPosition().y();
+ t.trace(QString("At %1 (%2%, height %3)").
+ arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height));
mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height);
}
}
void BookView::onLoadFinished(bool ok)
{
- Trace t(QString("BookView::onLoadFinished: %1").arg(ok));
- loaded = true;
- if (ok) {
- addNavigationBar();
+ Trace t("BookView::onLoadFinished");
+ if (!ok) {
+ t.trace("Not OK");
+ return;
}
+ loaded = true;
+ addNavigationBar();
onSettingsChanged("scheme");
emit chapterLoadEnd(contentIndex);
- if (restore) {
- restore = false;
- if (ok && mBook) {
- int height = page()->mainFrame()->contentsSize().height();
- int scrollPos = (qreal)height * positionAfterLoad;
- page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
- }
- }
}
void BookView::onSettingsChanged(const QString &key)
if (b.chapter != contentIndex) {
continue;
}
- int height = page()->mainFrame()->contentsSize().height();
+ int height = contentsHeight; // page()->mainFrame()->contentsSize().height();
int bookmarkPos = (qreal)height * (qreal)b.pos;
painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
}
int height = page()->mainFrame()->contentsSize().height();
t.trace(QString().setNum((qreal)y / (qreal)height));
mBook->addBookmark(contentIndex, (qreal)y / (qreal)height);
- repaint();
+ update();
}
void BookView::addNavigationBar()
}
QWebFrame *frame = page()->currentFrame();
- frame->addToJavaScriptWindowObject("bv", this);
QString headerScript = "document.body.innerHTML = '" +
naviPrev + naviNext + "<br />" + "' + document.body.innerHTML;";
QString trailerScript = "document.body.innerHTML += '<br /><br />" +
frame->evaluateJavaScript(headerScript);
frame->evaluateJavaScript(trailerScript);
+ decorated = true;
}
QString BookView::tmpPath()
}
return false;
}
+
+void BookView::addJavaScriptObjects()
+{
+ page()->mainFrame()->addToJavaScriptWindowObject("bv", this);
+}
+
+void BookView::onContentsSizeChanged(const QSize &size)
+{
+ Trace t("BookView::onContentsSizeChanged");
+ t.trace(QString("To %1").arg(size.height()));
+ contentsHeight = size.height();
+ if (decorated) {
+ decorated = false;
+ if (restore) {
+ restore = false;
+ if (mBook) {
+ QWebPage *webPage = page();
+ QWebFrame *mainFrame = webPage->mainFrame();
+ int height = contentsHeight;
+ int scrollPos = (qreal)height * positionAfterLoad;
+ mainFrame->setScrollPosition(QPoint(0, scrollPos));
+ t.trace(QString("Restoring positon to %1 (%2%, height %3)").
+ arg(scrollPos).arg(positionAfterLoad * 100).arg(height));
+ foreach (QString key, mainFrame->metaData().keys()) {
+ QString value = mainFrame->metaData().value(key);
+ t.trace(key + ": " + value);
+ }
+ }
+ }
+ }
+}
signals:
void chapterLoadStart(int index);
void chapterLoadEnd(int index);
+
+ /** Signal button press when the real event has been suppressed. */
void suppressedMouseButtonPress();
public slots:
void onLoadFinished(bool ok);
void onSettingsChanged(const QString &key);
+ /** Add QObjects to the main frame. */
+ void addJavaScriptObjects();
+
+ /** Handle main frame contents size changes. */
+ void onContentsSizeChanged(const QSize &size);
+
protected:
virtual void paintEvent(QPaintEvent *e);
virtual void mousePressEvent(QMouseEvent *e);
bool eventFilter(QObject *o, QEvent *e);
private:
- void loadContent(int index);
- void addNavigationBar();
+ /** Save navigation icons from resource to the file system. */
void extractIcons();
+
+ /** Remove extracted icons. */
void removeIcons();
+
+ /** Load given chapter. */
+ void loadContent(int index);
+
+ /** Decorate web page frame with navigation icons. */
+ void addNavigationBar();
+
+ /** Get temporary directory for extracting book contents. */
QString tmpPath();
- int contentIndex;
- Book *mBook;
- bool restore;
+ int contentIndex; /**< Current chapter in book. */
+ Book *mBook; /**< Book to show. */
+ bool restore; /**< Restoring position after load is needed. */
qreal positionAfterLoad;
+ /**< Position to be restored after load. */
QImage bookmarkImage;
- bool loaded;
+ /**< Bookmark icon pre-loaded. */
+ bool loaded; /**< True if content has been loaded. */
bool mousePressed;
+ int contentsHeight; /**< Last know height of the frame. */
+ bool decorated; /**< True after adding the arrows to the frame contents. */
};
#endif // BOOKVIEW_H