From 2ecf9f4178cc6ca39f62d19b74e96a419f20cd67 Mon Sep 17 00:00:00 2001 From: Agel Date: Mon, 16 Mar 2026 15:34:15 +0100 Subject: [PATCH] restore opened file and position --- CMakeLists.txt | 4 + src/backend/AppState.cpp | 131 ++++++++++++++++++++- src/backend/AppState.h | 61 +++++++++- src/backend/BookController.cpp | 7 ++ src/backend/BookController.h | 3 + src/backend/ReadingModel.h | 2 - src/main.cpp | 9 ++ src/qml/Main.qml | 30 ++++- src/qml/components/PageView.qml | 31 +++++ src/qml/pages/SettingsPage.qml | 6 +- tests/AppStateTest.cpp | 197 ++++++++++++++++++++++++++++++++ tests/BookControllerTest.cpp | 21 ++++ 12 files changed, 491 insertions(+), 11 deletions(-) create mode 100644 tests/AppStateTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 44b22a5..c75cda5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,7 +143,11 @@ add_asteria_test(BookControllerTest tests/BookControllerTest.cpp src/backend/ReaderFactory.cpp src/backend/ReadingModel.cpp src/backend/formats/TxtReader.cpp) +add_asteria_test(AppStateTest tests/AppStateTest.cpp + src/backend/AppState.cpp) + target_link_libraries(BookControllerTest PRIVATE Qt6::Widgets) + if(Qt6DBus_FOUND) target_link_libraries(BookControllerTest PRIVATE Qt6::DBus) target_compile_definitions(BookControllerTest PRIVATE HAS_QT_DBUS) diff --git a/src/backend/AppState.cpp b/src/backend/AppState.cpp index 90b0b71..219a63e 100644 --- a/src/backend/AppState.cpp +++ b/src/backend/AppState.cpp @@ -1,21 +1,144 @@ #include "AppState.h" +#include AppState::AppState(QObject *parent) : QObject(parent) { } -QString AppState::lastFilePath() const +QString AppState::lastFilePath() const { return m_lastFilePath; } + +void AppState::setLastFilePath(const QString &path) { - return m_lastFilePath; + if (m_lastFilePath == path) + return; + m_lastFilePath = path; + emit lastFilePathChanged(); +} + +int AppState::lastChapter() const { return m_lastChapter; } + +void AppState::setLastChapter(int chapter) +{ + if (m_lastChapter == chapter) + return; + m_lastChapter = chapter; + emit lastChapterChanged(); +} + +qreal AppState::lastScrollPosition() const { return m_lastScrollPosition; } + +void AppState::setLastScrollPosition(qreal position) +{ + if (qFuzzyCompare(m_lastScrollPosition, position)) + return; + m_lastScrollPosition = position; + emit lastScrollPositionChanged(); +} + +int AppState::lastScrollIndex() const { return m_lastScrollIndex; } + +void AppState::setLastScrollIndex(int index) +{ + if (m_lastScrollIndex == index) + return; + m_lastScrollIndex = index; + emit lastScrollIndexChanged(); +} + +qreal AppState::lastScrollOffset() const { return m_lastScrollOffset; } + +void AppState::setLastScrollOffset(qreal offset) +{ + if (qFuzzyCompare(m_lastScrollOffset, offset)) + return; + m_lastScrollOffset = offset; + emit lastScrollOffsetChanged(); +} + +QString AppState::pageFlipMode() const { return m_pageFlipMode; } + +void AppState::setPageFlipMode(const QString &mode) +{ + if (m_pageFlipMode == mode) + return; + m_pageFlipMode = mode; + emit pageFlipModeChanged(); +} + +int AppState::windowWidth() const { return m_windowWidth; } + +void AppState::setWindowWidth(int width) +{ + if (m_windowWidth == width) + return; + m_windowWidth = width; + emit windowWidthChanged(); +} + +int AppState::windowHeight() const { return m_windowHeight; } + +void AppState::setWindowHeight(int height) +{ + if (m_windowHeight == height) + return; + m_windowHeight = height; + emit windowHeightChanged(); +} + +int AppState::windowX() const { return m_windowX; } + +void AppState::setWindowX(int x) +{ + if (m_windowX == x) + return; + m_windowX = x; + emit windowXChanged(); +} + +int AppState::windowY() const { return m_windowY; } + +void AppState::setWindowY(int y) +{ + if (m_windowY == y) + return; + m_windowY = y; + emit windowYChanged(); +} + +QUrl AppState::lastFileUrl() const +{ + return QUrl::fromLocalFile(m_lastFilePath); } void AppState::save() { - // Will use QSettings via TDD + QSettings s; + s.setValue(QStringLiteral("session/lastFilePath"), m_lastFilePath); + s.setValue(QStringLiteral("session/lastChapter"), m_lastChapter); + s.setValue(QStringLiteral("session/lastScrollPosition"), m_lastScrollPosition); + s.setValue(QStringLiteral("session/lastScrollIndex"), m_lastScrollIndex); + s.setValue(QStringLiteral("session/lastScrollOffset"), m_lastScrollOffset); + s.setValue(QStringLiteral("preferences/pageFlipMode"), m_pageFlipMode); + s.setValue(QStringLiteral("window/width"), m_windowWidth); + s.setValue(QStringLiteral("window/height"), m_windowHeight); + s.setValue(QStringLiteral("window/x"), m_windowX); + s.setValue(QStringLiteral("window/y"), m_windowY); } void AppState::restore() { - // Will use QSettings via TDD + QSettings s; + m_lastFilePath = s.value(QStringLiteral("session/lastFilePath"), QString()).toString(); + m_lastChapter = s.value(QStringLiteral("session/lastChapter"), 0).toInt(); + m_lastScrollPosition = s.value(QStringLiteral("session/lastScrollPosition"), 0.0).toDouble(); + m_lastScrollIndex = s.value(QStringLiteral("session/lastScrollIndex"), 0).toInt(); + m_lastScrollOffset = s.value(QStringLiteral("session/lastScrollOffset"), 0.0).toDouble(); + m_pageFlipMode = s.value(QStringLiteral("preferences/pageFlipMode"), QStringLiteral("scroll")).toString(); + m_windowWidth = s.value(QStringLiteral("window/width"), 1024).toInt(); + m_windowHeight = s.value(QStringLiteral("window/height"), 768).toInt(); + m_windowX = s.value(QStringLiteral("window/x"), -1).toInt(); + m_windowY = s.value(QStringLiteral("window/y"), -1).toInt(); + + emit stateRestored(); } diff --git a/src/backend/AppState.h b/src/backend/AppState.h index 68d6898..02b2d2b 100644 --- a/src/backend/AppState.h +++ b/src/backend/AppState.h @@ -3,24 +3,83 @@ #include #include +#include class AppState : public QObject { Q_OBJECT - Q_PROPERTY(QString lastFilePath READ lastFilePath NOTIFY lastFilePathChanged) + Q_PROPERTY(QString lastFilePath READ lastFilePath WRITE setLastFilePath NOTIFY lastFilePathChanged) + Q_PROPERTY(int lastChapter READ lastChapter WRITE setLastChapter NOTIFY lastChapterChanged) + Q_PROPERTY(qreal lastScrollPosition READ lastScrollPosition WRITE setLastScrollPosition NOTIFY lastScrollPositionChanged) + Q_PROPERTY(int lastScrollIndex READ lastScrollIndex WRITE setLastScrollIndex NOTIFY lastScrollIndexChanged) + Q_PROPERTY(qreal lastScrollOffset READ lastScrollOffset WRITE setLastScrollOffset NOTIFY lastScrollOffsetChanged) + Q_PROPERTY(QString pageFlipMode READ pageFlipMode WRITE setPageFlipMode NOTIFY pageFlipModeChanged) + Q_PROPERTY(int windowWidth READ windowWidth WRITE setWindowWidth NOTIFY windowWidthChanged) + Q_PROPERTY(int windowHeight READ windowHeight WRITE setWindowHeight NOTIFY windowHeightChanged) + Q_PROPERTY(int windowX READ windowX WRITE setWindowX NOTIFY windowXChanged) + Q_PROPERTY(int windowY READ windowY WRITE setWindowY NOTIFY windowYChanged) public: explicit AppState(QObject *parent = nullptr); QString lastFilePath() const; + void setLastFilePath(const QString &path); + + int lastChapter() const; + void setLastChapter(int chapter); + + qreal lastScrollPosition() const; + void setLastScrollPosition(qreal position); + + int lastScrollIndex() const; + void setLastScrollIndex(int index); + + qreal lastScrollOffset() const; + void setLastScrollOffset(qreal offset); + + QString pageFlipMode() const; + void setPageFlipMode(const QString &mode); + + int windowWidth() const; + void setWindowWidth(int width); + + int windowHeight() const; + void setWindowHeight(int height); + + int windowX() const; + void setWindowX(int x); + + int windowY() const; + void setWindowY(int y); + + Q_INVOKABLE QUrl lastFileUrl() const; Q_INVOKABLE void save(); Q_INVOKABLE void restore(); signals: void lastFilePathChanged(); + void lastChapterChanged(); + void lastScrollPositionChanged(); + void lastScrollIndexChanged(); + void lastScrollOffsetChanged(); + void pageFlipModeChanged(); + void stateRestored(); + void windowWidthChanged(); + void windowHeightChanged(); + void windowXChanged(); + void windowYChanged(); private: QString m_lastFilePath; + int m_lastChapter = 0; + qreal m_lastScrollPosition = 0.0; + int m_lastScrollIndex = 0; + qreal m_lastScrollOffset = 0.0; + QString m_pageFlipMode = QStringLiteral("scroll"); + int m_windowWidth = 1024; + int m_windowHeight = 768; + int m_windowX = -1; + int m_windowY = -1; }; #endif // APPSTATE_H diff --git a/src/backend/BookController.cpp b/src/backend/BookController.cpp index d23c915..1c67b66 100644 --- a/src/backend/BookController.cpp +++ b/src/backend/BookController.cpp @@ -97,6 +97,11 @@ bool BookController::hasBook() const return m_reader != nullptr; } +QString BookController::currentFilePath() const +{ + return m_currentFilePath; +} + QList BookController::toc() const { if (m_reader) @@ -139,6 +144,7 @@ void BookController::openBook(const QUrl &fileUrl) } m_reader = std::move(reader); + m_currentFilePath = filePath; m_bookTitle = m_reader->title(); m_bookAuthor = m_reader->author(); @@ -275,6 +281,7 @@ void BookController::closeBook() m_reader.reset(); m_bookTitle.clear(); m_bookAuthor.clear(); + m_currentFilePath.clear(); if (m_readingModel) m_readingModel->setReader(nullptr); diff --git a/src/backend/BookController.h b/src/backend/BookController.h index d7edd87..75c0679 100644 --- a/src/backend/BookController.h +++ b/src/backend/BookController.h @@ -19,6 +19,7 @@ class BookController : public QObject { Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged) Q_PROPERTY(bool hasBook READ hasBook NOTIFY bookChanged) Q_PROPERTY(QList toc READ toc NOTIFY bookChanged) + Q_PROPERTY(QString currentFilePath READ currentFilePath NOTIFY bookChanged) public: explicit BookController(QObject *parent = nullptr); @@ -29,6 +30,7 @@ public: QString errorMessage() const; bool hasBook() const; QList toc() const; + QString currentFilePath() const; void setReadingModel(ReadingModel *model); @@ -55,6 +57,7 @@ private: ReadingModel *m_readingModel = nullptr; QString m_bookTitle; QString m_bookAuthor; + QString m_currentFilePath; bool m_loading = false; QString m_errorMessage; #ifdef HAS_QT_DBUS diff --git a/src/backend/ReadingModel.h b/src/backend/ReadingModel.h index 1a78f01..69a34a3 100644 --- a/src/backend/ReadingModel.h +++ b/src/backend/ReadingModel.h @@ -30,7 +30,6 @@ public: int currentChapter() const; int totalChapters() const; double progress() const; - void setReader(FormatReader *reader); Q_INVOKABLE void nextChapter(); @@ -40,7 +39,6 @@ public: signals: void chapterChanged(); void totalChaptersChanged(); - private: void loadChapterData(int index); diff --git a/src/main.cpp b/src/main.cpp index 09f2b6a..0ec0ef3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,7 @@ int main(int argc, char *argv[]) bookController.setReadingModel(&readingModel); AppState appState; + appState.restore(); QQmlApplicationEngine engine; @@ -34,5 +35,13 @@ int main(int argc, char *argv[]) Qt::QueuedConnection); engine.load(url); + QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]() { + if (bookController.hasBook()) { + appState.setLastFilePath(bookController.currentFilePath()); + appState.setLastChapter(readingModel.currentChapter()); + } + appState.save(); + }); + return app.exec(); } diff --git a/src/qml/Main.qml b/src/qml/Main.qml index 48c1905..2487bfb 100644 --- a/src/qml/Main.qml +++ b/src/qml/Main.qml @@ -17,7 +17,35 @@ ApplicationWindow { property bool sidebarOpen: false property string currentPage: "starter" - property string pageFlipMode: "scroll" // "scroll" | "page" + property string pageFlipMode: appState.pageFlipMode + + Component.onCompleted: { + width = appState.windowWidth + height = appState.windowHeight + if (appState.windowX >= 0 && appState.windowY >= 0) { + x = appState.windowX + y = appState.windowY + } + if (appState.lastFilePath !== "") + bookController.openBook(appState.lastFileUrl()) + } + + Timer { + id: geometrySaveTimer + interval: 500 + repeat: false + onTriggered: { + appState.setWindowWidth(window.width) + appState.setWindowHeight(window.height) + appState.setWindowX(window.x) + appState.setWindowY(window.y) + } + } + + onWidthChanged: geometrySaveTimer.restart() + onHeightChanged: geometrySaveTimer.restart() + onXChanged: geometrySaveTimer.restart() + onYChanged: geometrySaveTimer.restart() Row { anchors.fill: parent diff --git a/src/qml/components/PageView.qml b/src/qml/components/PageView.qml index fce49ff..2ead8a9 100644 --- a/src/qml/components/PageView.qml +++ b/src/qml/components/PageView.qml @@ -9,6 +9,7 @@ Rectangle { } ListView { + id: chunkListView anchors.fill: parent anchors.leftMargin: 12 model: bookController.hasBook ? readingModel : null @@ -26,6 +27,8 @@ Rectangle { text: model.content } + onContentYChanged: scrollTrackTimer.restart() + ScrollBar.vertical: ScrollBar { policy: ScrollBar.AsNeeded padding: 0 @@ -49,4 +52,32 @@ Rectangle { color: Theme.readingText } } + + Timer { + id: scrollTrackTimer + interval: 300 + repeat: false + onTriggered: { + var idx = chunkListView.indexAt(0, chunkListView.contentY) + appState.lastScrollIndex = idx + var item = chunkListView.itemAtIndex(idx) + appState.lastScrollOffset = item ? chunkListView.contentY - item.y : 0 + appState.lastScrollPosition = chunkListView.contentY + } + } + + Connections { + target: bookController + function onBookReady() { + if (bookController.currentFilePath === appState.lastFilePath) { + readingModel.goToChapter(appState.lastChapter) + if (appState.lastScrollIndex > 0) + Qt.callLater(function() { + chunkListView.positionViewAtIndex( + appState.lastScrollIndex, ListView.Beginning) + chunkListView.contentY += appState.lastScrollOffset + }) + } + } + } } diff --git a/src/qml/pages/SettingsPage.qml b/src/qml/pages/SettingsPage.qml index 89d8828..dbf2e00 100644 --- a/src/qml/pages/SettingsPage.qml +++ b/src/qml/pages/SettingsPage.qml @@ -52,7 +52,7 @@ Rectangle { width: 100 height: 40 radius: 6 - color: window.pageFlipMode === modelData.value + color: appState.pageFlipMode === modelData.value ? Theme.primaryAccent : Theme.surface @@ -60,7 +60,7 @@ Rectangle { anchors.centerIn: parent text: modelData.label font.pixelSize: 14 - color: window.pageFlipMode === modelData.value + color: appState.pageFlipMode === modelData.value ? Theme.readingText : Theme.uiText } @@ -68,7 +68,7 @@ Rectangle { MouseArea { anchors.fill: parent cursorShape: Qt.PointingHandCursor - onClicked: window.pageFlipMode = modelData.value + onClicked: appState.pageFlipMode = modelData.value } } } diff --git a/tests/AppStateTest.cpp b/tests/AppStateTest.cpp new file mode 100644 index 0000000..15533a1 --- /dev/null +++ b/tests/AppStateTest.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include "backend/AppState.h" + +class AppStateTest : public QObject { + Q_OBJECT + +private slots: + void initTestCase() + { + QCoreApplication::setOrganizationName(QStringLiteral("AsteriaTest")); + QCoreApplication::setApplicationName(QStringLiteral("AppStateTest")); + } + + void cleanup() + { + QSettings().clear(); + } + + void saveAndRestoreLastFilePath() + { + { + AppState state; + state.setLastFilePath(QStringLiteral("/books/test.epub")); + state.save(); + } + AppState state2; + state2.restore(); + QCOMPARE(state2.lastFilePath(), QStringLiteral("/books/test.epub")); + } + + void saveAndRestoreLastChapter() + { + { + AppState state; + state.setLastChapter(5); + state.save(); + } + AppState state2; + state2.restore(); + QCOMPARE(state2.lastChapter(), 5); + } + + void saveAndRestoreLastScrollPosition() + { + { + AppState state; + state.setLastScrollPosition(542.5); + state.save(); + } + AppState state2; + state2.restore(); + QCOMPARE(state2.lastScrollPosition(), 542.5); + } + + void saveAndRestoreLastScrollIndex() + { + { + AppState state; + state.setLastScrollIndex(42); + state.save(); + } + AppState state2; + state2.restore(); + QCOMPARE(state2.lastScrollIndex(), 42); + } + + void saveAndRestoreLastScrollOffset() + { + { + AppState state; + state.setLastScrollOffset(123.75); + state.save(); + } + AppState state2; + state2.restore(); + QCOMPARE(state2.lastScrollOffset(), 123.75); + } + + void saveAndRestorePageFlipMode() + { + { + AppState state; + state.setPageFlipMode(QStringLiteral("page")); + state.save(); + } + AppState state2; + state2.restore(); + QCOMPARE(state2.pageFlipMode(), QStringLiteral("page")); + } + + void saveAndRestoreWindowGeometry() + { + { + AppState state; + state.setWindowWidth(1280); + state.setWindowHeight(800); + state.setWindowX(100); + state.setWindowY(50); + state.save(); + } + AppState state2; + state2.restore(); + QCOMPARE(state2.windowWidth(), 1280); + QCOMPARE(state2.windowHeight(), 800); + QCOMPARE(state2.windowX(), 100); + QCOMPARE(state2.windowY(), 50); + } + + void restoreWithNoSavedData() + { + AppState state; + state.restore(); + QVERIFY(state.lastFilePath().isEmpty()); + QCOMPARE(state.lastChapter(), 0); + QCOMPARE(state.lastScrollPosition(), 0.0); + QCOMPARE(state.lastScrollIndex(), 0); + QCOMPARE(state.lastScrollOffset(), 0.0); + QCOMPARE(state.pageFlipMode(), QStringLiteral("scroll")); + QCOMPARE(state.windowWidth(), 1024); + QCOMPARE(state.windowHeight(), 768); + QCOMPARE(state.windowX(), -1); + QCOMPARE(state.windowY(), -1); + } + + void settersEmitSignals() + { + AppState state; + QSignalSpy spyFilePath(&state, &AppState::lastFilePathChanged); + QSignalSpy spyChapter(&state, &AppState::lastChapterChanged); + QSignalSpy spyScroll(&state, &AppState::lastScrollPositionChanged); + QSignalSpy spyScrollIndex(&state, &AppState::lastScrollIndexChanged); + QSignalSpy spyScrollOffset(&state, &AppState::lastScrollOffsetChanged); + QSignalSpy spyFlip(&state, &AppState::pageFlipModeChanged); + QSignalSpy spyWidth(&state, &AppState::windowWidthChanged); + QSignalSpy spyHeight(&state, &AppState::windowHeightChanged); + QSignalSpy spyX(&state, &AppState::windowXChanged); + QSignalSpy spyY(&state, &AppState::windowYChanged); + + state.setLastFilePath(QStringLiteral("/foo.txt")); + state.setLastChapter(3); + state.setLastScrollPosition(10.5); + state.setLastScrollIndex(7); + state.setLastScrollOffset(55.25); + state.setPageFlipMode(QStringLiteral("page")); + state.setWindowWidth(800); + state.setWindowHeight(600); + state.setWindowX(200); + state.setWindowY(150); + + QCOMPARE(spyFilePath.count(), 1); + QCOMPARE(spyChapter.count(), 1); + QCOMPARE(spyScroll.count(), 1); + QCOMPARE(spyScrollIndex.count(), 1); + QCOMPARE(spyScrollOffset.count(), 1); + QCOMPARE(spyFlip.count(), 1); + QCOMPARE(spyWidth.count(), 1); + QCOMPARE(spyHeight.count(), 1); + QCOMPARE(spyX.count(), 1); + QCOMPARE(spyY.count(), 1); + } + + void restoreEmitsSingleSignal() + { + AppState state; + + QSignalSpy spyRestored(&state, &AppState::stateRestored); + QSignalSpy spyFilePath(&state, &AppState::lastFilePathChanged); + QSignalSpy spyChapter(&state, &AppState::lastChapterChanged); + QSignalSpy spyScroll(&state, &AppState::lastScrollPositionChanged); + QSignalSpy spyScrollIndex(&state, &AppState::lastScrollIndexChanged); + QSignalSpy spyScrollOffset(&state, &AppState::lastScrollOffsetChanged); + QSignalSpy spyFlip(&state, &AppState::pageFlipModeChanged); + QSignalSpy spyWidth(&state, &AppState::windowWidthChanged); + QSignalSpy spyHeight(&state, &AppState::windowHeightChanged); + QSignalSpy spyX(&state, &AppState::windowXChanged); + QSignalSpy spyY(&state, &AppState::windowYChanged); + + state.restore(); + + QCOMPARE(spyRestored.count(), 1); + QCOMPARE(spyFilePath.count(), 0); + QCOMPARE(spyChapter.count(), 0); + QCOMPARE(spyScroll.count(), 0); + QCOMPARE(spyScrollIndex.count(), 0); + QCOMPARE(spyScrollOffset.count(), 0); + QCOMPARE(spyFlip.count(), 0); + QCOMPARE(spyWidth.count(), 0); + QCOMPARE(spyHeight.count(), 0); + QCOMPARE(spyX.count(), 0); + QCOMPARE(spyY.count(), 0); + } +}; + +QTEST_MAIN(AppStateTest) +#include "AppStateTest.moc" diff --git a/tests/BookControllerTest.cpp b/tests/BookControllerTest.cpp index b46a1a9..b4a0aaf 100644 --- a/tests/BookControllerTest.cpp +++ b/tests/BookControllerTest.cpp @@ -88,6 +88,27 @@ private slots: // After completion, loading should be false QVERIFY(!ctrl.loading()); } + + void currentFilePathInitiallyEmpty() + { + BookController ctrl; + QVERIFY(ctrl.currentFilePath().isEmpty()); + } + + void currentFilePathSetOnOpen() + { + BookController ctrl; + ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt"))); + QCOMPARE(ctrl.currentFilePath(), fixturePath("sample.txt")); + } + + void currentFilePathClearedOnClose() + { + BookController ctrl; + ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt"))); + ctrl.closeBook(); + QVERIFY(ctrl.currentFilePath().isEmpty()); + } }; QTEST_MAIN(BookControllerTest)