From 399cd6cba2b7a0ad3d2de29593c3143ece63aad3 Mon Sep 17 00:00:00 2001 From: Akos Polster Date: Sat, 5 Feb 2011 18:23:26 +0100 Subject: [PATCH] Fix loading book and restoring reading position on Maemo. --- adopterwindow.cpp | 2 +- bookview.cpp | 81 ++++++++++++++++++++++++++++++++++++++++------------- bookview.h | 42 +++++++++++++++++---------- mainwindow.cpp | 10 +++---- 4 files changed, 95 insertions(+), 40 deletions(-) diff --git a/adopterwindow.cpp b/adopterwindow.cpp index 5be356d..23bbf57 100644 --- a/adopterwindow.cpp +++ b/adopterwindow.cpp @@ -146,7 +146,7 @@ void AdopterWindow::resizeEvent(QResizeEvent *event) #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN) // Restore previous reading position if (bookView) { - QTimer::singleShot(230, bookView, SLOT(restoreLastBookmark())); + bookView->scheduleRestoreLastBookmark(); } #endif // defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN) } diff --git a/bookview.cpp b/bookview.cpp index b2e69c6..f4606b7 100644 --- a/bookview.cpp +++ b/bookview.cpp @@ -22,6 +22,9 @@ BookView::BookView(QWidget *parent): QWebView(parent), contentIndex(-1), { TRACE; + // Create timer for scheduling restores + restoreTimer = new QTimer(this); + // Set up web view defaults settings()->setAttribute(QWebSettings::AutoLoadImages, true); settings()->setAttribute(QWebSettings::JavascriptEnabled, true); @@ -111,26 +114,34 @@ void BookView::setBook(Book *book) { TRACE; - // Save position in current book + // Bail out if new book is the same as current book + if (book == mBook) { + return; + } + + // Save reading position of current book setLastBookmark(); - // Open new book, restore last position - if (book != mBook) { - mBook = book; - if (book) { - contentIndex = -1; - if (book->open()) { - restoreLastBookmark(); - } else { - mBook = 0; - contentIndex = 0; - setHtml(tr("Failed to open book")); - } - } - else { - contentIndex = 0; - setHtml(tr("No book")); - } + // Set new book as the current book + mBook = book; + + // Bail out if new book is null + if (!book) { + contentIndex = 0; + setHtml(tr("No book")); + return; + } + + // Open new book + if (book->open()) { + // Restore last reading position - this will force + // a reload as well + contentIndex = -1; + restoreLastBookmark(); + } else { + mBook = 0; + contentIndex = 0; + setHtml(tr("Failed to open book")); } } @@ -167,6 +178,8 @@ void BookView::setLastBookmark(bool fast) qDebug() << QString("At %1 (%2%, height %3)"). arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height); mBook->setLastBookmark(contentIndex, (qreal)pos / (qreal)height, fast); + } else { + qDebug() << "(no book)"; } } @@ -236,8 +249,36 @@ void BookView::onLoadFinished(bool ok) onSettingsChanged("scheme"); onSettingsChanged("zoom"); onSettingsChanged("font"); + scheduleRestoreAfterLoad(); +} - QTimer::singleShot(210, this, SLOT(restoreAfterLoad())); +void BookView::scheduleRestoreAfterLoad() +{ + TRACE; + if (restoreTimer->isActive()) { + // Ignore request if a restore is already in progress + return; + } + + disconnect(restoreTimer, SIGNAL(timeout()), this, 0); + connect(restoreTimer, SIGNAL(timeout()), this, SLOT(restoreAfterLoad())); + restoreTimer->setSingleShot(true); + restoreTimer->start(210); +} + +void BookView::scheduleRestoreLastBookmark() +{ + TRACE; + if (restoreTimer->isActive()) { + // Ignore request if a restore is already in progress + return; + } + + disconnect(restoreTimer, SIGNAL(timeout()), this, 0); + connect(restoreTimer, SIGNAL(timeout()), this, + SLOT(restoreLastBookmark())); + restoreTimer->setSingleShot(true); + restoreTimer->start(210); } void BookView::restoreAfterLoad() @@ -421,11 +462,13 @@ bool BookView::eventFilter(QObject *o, QEvent *e) void BookView::addJavaScriptObjects() { + TRACE; page()->mainFrame()->addToJavaScriptWindowObject("bv", this); } void BookView::goToPosition(qreal position) { + TRACE; int contentsHeight = page()->mainFrame()->contentsSize().height(); int scrollPos = (int)((qreal)contentsHeight * position); page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos)); diff --git a/bookview.h b/bookview.h index 74ef200..9a177e6 100644 --- a/bookview.h +++ b/bookview.h @@ -19,6 +19,7 @@ class Progress; class QAbstractKineticScroller; class ProgressDialog; class FlickCharm; +class QTimer; /** Visual representation of a book. */ class BookView: public QWebView @@ -52,6 +53,12 @@ public: /** If grab is true, volume keys will generate act as page up/down. */ void grabVolumeKeys(bool grab); + /** Schedule restoring last reading position after loading part. */ + void scheduleRestoreAfterLoad(); + + /** Schedule restoring last reading position. */ + void scheduleRestoreLastBookmark(); + signals: /** Part loading started. */ void partLoadStart(int index); @@ -84,13 +91,13 @@ public slots: /** Go to next page. */ void goNextPage(); - /** Restore saved position after URL loading finished. */ +protected slots: + /** Restore last reading position after part loading finished. */ void restoreAfterLoad(); - /** Restore book's last reading position. */ + /** Restore book's last reading position, load new part if needed. */ void restoreLastBookmark(); -protected slots: #ifdef Q_OS_SYMBIAN /** Observe media keys. */ void onMediaKeysPressed(MediaKeysObserver::MediaKeys key); @@ -117,22 +124,27 @@ protected: void showProgress(); private: - int contentIndex; /**< Current part in book. */ - Book *mBook; /**< Book to show. */ + int contentIndex; /**< Current part in book. */ + Book *mBook; /**< Book to show. */ bool restorePositionAfterLoad; - /**< If true, restore current position after load. */ - qreal positionAfterLoad;/**< Position to be restored after load. */ + /**< If true, restore reading position after load. */ + qreal positionAfterLoad; + /**< Position to be restored after load. */ bool restoreFragmentAfterLoad; - /**< If true, restore fragment location after load. */ + /**< If true, restore fragment location after load. */ QString fragmentAfterLoad; - /**< Fragment location to be restored after load. */ - QImage bookmarkImage; /**< Bookmark icon pre-loaded. */ - bool loaded; /**< True, if content has been loaded. */ - bool mousePressed; /**< Event filter's mouse button state. */ - bool grabbingVolumeKeys;/**< True, if volume keys should be grabbed. */ + /**< Fragment location to be restored after load. */ + QImage bookmarkImage; + /**< Bookmark icon pre-loaded. */ + bool loaded; /**< True, if content has been loaded. */ + bool mousePressed; /**< Event filter's mouse button state. */ + bool grabbingVolumeKeys; + /**< True, if volume keys should be grabbed. */ + QTimer *restoreTimer; + /**< Single timer for scheduling all restore ops. */ #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN) - int scrollerMonitor; /**< ID of timer monitoring kinetic scroll. */ + int scrollerMonitor;/**< ID of timer monitoring kinetic scroll. */ #endif #if defined(Q_WS_MAEMO_5) @@ -140,7 +152,7 @@ private: #endif #if defined(Q_OS_SYMBIAN) - FlickCharm *charm; /**< Kinetic scroller. */ + FlickCharm *charm; /**< Kinetic scroller. */ #endif }; diff --git a/mainwindow.cpp b/mainwindow.cpp index f4ebc2a..4b778ad 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -143,6 +143,9 @@ void MainWindow::initialize() TRACE; Library *library = Library::instance(); + // Show in regular (non full-screen) mode + showRegular(); + // Upgrade library if needed, then load it library->upgrade(); library->load(); @@ -167,9 +170,6 @@ void MainWindow::initialize() library->setNowReading(sorted.mapToSource(sorted.index(0, 0))); } } - - // Show in regular (non full-screen) mode - showRegular(); } void MainWindow::onCurrentBookChanged() @@ -188,7 +188,7 @@ void MainWindow::showRegular() fullScreenWindow->hide(); show(); - view->restoreLastBookmark(); + view->scheduleRestoreLastBookmark(); } void MainWindow::showBig() @@ -205,7 +205,7 @@ void MainWindow::showBig() hide(); #endif fullScreenWindow->showFullScreen(); - view->restoreLastBookmark(); + view->scheduleRestoreLastBookmark(); } void MainWindow::setCurrentBook(const QModelIndex ¤t) -- 1.7.9.5