From 934bb6663f5cda53d52bced9be0576665218599b Mon Sep 17 00:00:00 2001 From: Akos Polster Date: Sun, 1 Aug 2010 02:28:47 +0200 Subject: [PATCH] Restore bookmarks to the correct position. --- bookview.cpp | 74 ++++++++++++++++++++++++++++++++++++++++------------ bookview.h | 33 ++++++++++++++++++----- chaptersdialog.cpp | 2 +- pkg/changelog | 2 ++ 4 files changed, 87 insertions(+), 24 deletions(-) diff --git a/bookview.cpp b/bookview.cpp index 83af012..08a5d64 100644 --- a/bookview.cpp +++ b/bookview.cpp @@ -19,15 +19,23 @@ 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) @@ -43,6 +51,10 @@ BookView::BookView(QWidget *parent): 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(); @@ -84,6 +96,7 @@ void BookView::loadContent(int index) } else { loaded = false; + decorated = false; emit chapterLoadStart(index); load(QUrl(contentFile)); } @@ -129,8 +142,10 @@ void BookView::setLastBookmark() { 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); } } @@ -151,21 +166,15 @@ void BookView::goToBookmark(const Book::Bookmark &bookmark) 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) @@ -208,7 +217,7 @@ void BookView::paintEvent(QPaintEvent *e) 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); } @@ -234,7 +243,7 @@ void BookView::addBookmark() 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() @@ -266,7 +275,6 @@ void BookView::addNavigationBar() } QWebFrame *frame = page()->currentFrame(); - frame->addToJavaScriptWindowObject("bv", this); QString headerScript = "document.body.innerHTML = '" + naviPrev + naviNext + "
" + "' + document.body.innerHTML;"; QString trailerScript = "document.body.innerHTML += '

" + @@ -274,6 +282,7 @@ void BookView::addNavigationBar() frame->evaluateJavaScript(headerScript); frame->evaluateJavaScript(trailerScript); + decorated = true; } QString BookView::tmpPath() @@ -324,3 +333,34 @@ bool BookView::eventFilter(QObject *, QEvent *e) } 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); + } + } + } + } +} diff --git a/bookview.h b/bookview.h index f1627c4..82b7a3c 100644 --- a/bookview.h +++ b/bookview.h @@ -28,6 +28,8 @@ public: signals: void chapterLoadStart(int index); void chapterLoadEnd(int index); + + /** Signal button press when the real event has been suppressed. */ void suppressedMouseButtonPress(); public slots: @@ -36,25 +38,44 @@ 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 diff --git a/chaptersdialog.cpp b/chaptersdialog.cpp index a769421..c74983d 100644 --- a/chaptersdialog.cpp +++ b/chaptersdialog.cpp @@ -13,7 +13,7 @@ ChaptersDialog::ChaptersDialog(Book *b, QWidget *parent): QFrame *frame = new QFrame(this); setCentralWidget(frame); - QHBoxLayout *horizontalLayout = new QHBoxLayout(this); + QHBoxLayout *horizontalLayout = new QHBoxLayout(frame); frame->setLayout(horizontalLayout); list = new QListWidget(this); diff --git a/pkg/changelog b/pkg/changelog index 8ae7ea0..60e5fcc 100644 --- a/pkg/changelog +++ b/pkg/changelog @@ -1,6 +1,8 @@ dorian (0.0.14-1) unstable; urgency=low * Make returning from full screen even more obvious + * Add more restrictions to JavaScript handling + * Restore bookmarks to the correct position -- Akos Polster Sat, 31 Jul 2010 20:00:00 +0200 -- 1.7.9.5