restore opened file and position

This commit is contained in:
Agel committed 2026-03-16 15:34:15 +01:00
1 parent fcacd0495d
commit 2ecf9f4178
12 files changed
+491 -11

No files matched your search

+4
View File
@@ -143,7 +143,11 @@ add_asteria_test(BookControllerTest tests/BookControllerTest.cpp
src/backend/ReaderFactory.cpp src/backend/ReaderFactory.cpp
src/backend/ReadingModel.cpp src/backend/ReadingModel.cpp
src/backend/formats/TxtReader.cpp) src/backend/formats/TxtReader.cpp)
add_asteria_test(AppStateTest tests/AppStateTest.cpp
src/backend/AppState.cpp)
target_link_libraries(BookControllerTest PRIVATE Qt6::Widgets) target_link_libraries(BookControllerTest PRIVATE Qt6::Widgets)
if(Qt6DBus_FOUND) if(Qt6DBus_FOUND)
target_link_libraries(BookControllerTest PRIVATE Qt6::DBus) target_link_libraries(BookControllerTest PRIVATE Qt6::DBus)
target_compile_definitions(BookControllerTest PRIVATE HAS_QT_DBUS) target_compile_definitions(BookControllerTest PRIVATE HAS_QT_DBUS)
+127 -4
View File
@@ -1,21 +1,144 @@
#include "AppState.h" #include "AppState.h"
#include <QSettings>
AppState::AppState(QObject *parent) AppState::AppState(QObject *parent)
: 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() 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() 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();
} }
+60 -1
View File
@@ -3,24 +3,83 @@
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QUrl>
class AppState : public QObject { class AppState : public QObject {
Q_OBJECT 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: public:
explicit AppState(QObject *parent = nullptr); explicit AppState(QObject *parent = nullptr);
QString lastFilePath() const; 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 save();
Q_INVOKABLE void restore(); Q_INVOKABLE void restore();
signals: signals:
void lastFilePathChanged(); void lastFilePathChanged();
void lastChapterChanged();
void lastScrollPositionChanged();
void lastScrollIndexChanged();
void lastScrollOffsetChanged();
void pageFlipModeChanged();
void stateRestored();
void windowWidthChanged();
void windowHeightChanged();
void windowXChanged();
void windowYChanged();
private: private:
QString m_lastFilePath; 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 #endif // APPSTATE_H
+7
View File
@@ -97,6 +97,11 @@ bool BookController::hasBook() const
return m_reader != nullptr; return m_reader != nullptr;
} }
QString BookController::currentFilePath() const
{
return m_currentFilePath;
}
QList<TocEntry> BookController::toc() const QList<TocEntry> BookController::toc() const
{ {
if (m_reader) if (m_reader)
@@ -139,6 +144,7 @@ void BookController::openBook(const QUrl &fileUrl)
} }
m_reader = std::move(reader); m_reader = std::move(reader);
m_currentFilePath = filePath;
m_bookTitle = m_reader->title(); m_bookTitle = m_reader->title();
m_bookAuthor = m_reader->author(); m_bookAuthor = m_reader->author();
@@ -275,6 +281,7 @@ void BookController::closeBook()
m_reader.reset(); m_reader.reset();
m_bookTitle.clear(); m_bookTitle.clear();
m_bookAuthor.clear(); m_bookAuthor.clear();
m_currentFilePath.clear();
if (m_readingModel) if (m_readingModel)
m_readingModel->setReader(nullptr); m_readingModel->setReader(nullptr);
+3
View File
@@ -19,6 +19,7 @@ class BookController : public QObject {
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged) Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
Q_PROPERTY(bool hasBook READ hasBook NOTIFY bookChanged) Q_PROPERTY(bool hasBook READ hasBook NOTIFY bookChanged)
Q_PROPERTY(QList<TocEntry> toc READ toc NOTIFY bookChanged) Q_PROPERTY(QList<TocEntry> toc READ toc NOTIFY bookChanged)
Q_PROPERTY(QString currentFilePath READ currentFilePath NOTIFY bookChanged)
public: public:
explicit BookController(QObject *parent = nullptr); explicit BookController(QObject *parent = nullptr);
@@ -29,6 +30,7 @@ public:
QString errorMessage() const; QString errorMessage() const;
bool hasBook() const; bool hasBook() const;
QList<TocEntry> toc() const; QList<TocEntry> toc() const;
QString currentFilePath() const;
void setReadingModel(ReadingModel *model); void setReadingModel(ReadingModel *model);
@@ -55,6 +57,7 @@ private:
ReadingModel *m_readingModel = nullptr; ReadingModel *m_readingModel = nullptr;
QString m_bookTitle; QString m_bookTitle;
QString m_bookAuthor; QString m_bookAuthor;
QString m_currentFilePath;
bool m_loading = false; bool m_loading = false;
QString m_errorMessage; QString m_errorMessage;
#ifdef HAS_QT_DBUS #ifdef HAS_QT_DBUS
-2
View File
@@ -30,7 +30,6 @@ public:
int currentChapter() const; int currentChapter() const;
int totalChapters() const; int totalChapters() const;
double progress() const; double progress() const;
void setReader(FormatReader *reader); void setReader(FormatReader *reader);
Q_INVOKABLE void nextChapter(); Q_INVOKABLE void nextChapter();
@@ -40,7 +39,6 @@ public:
signals: signals:
void chapterChanged(); void chapterChanged();
void totalChaptersChanged(); void totalChaptersChanged();
private: private:
void loadChapterData(int index); void loadChapterData(int index);
+9
View File
@@ -21,6 +21,7 @@ int main(int argc, char *argv[])
bookController.setReadingModel(&readingModel); bookController.setReadingModel(&readingModel);
AppState appState; AppState appState;
appState.restore();
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
@@ -34,5 +35,13 @@ int main(int argc, char *argv[])
Qt::QueuedConnection); Qt::QueuedConnection);
engine.load(url); engine.load(url);
QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]() {
if (bookController.hasBook()) {
appState.setLastFilePath(bookController.currentFilePath());
appState.setLastChapter(readingModel.currentChapter());
}
appState.save();
});
return app.exec(); return app.exec();
} }
+29 -1
View File
@@ -17,7 +17,35 @@ ApplicationWindow {
property bool sidebarOpen: false property bool sidebarOpen: false
property string currentPage: "starter" 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 { Row {
anchors.fill: parent anchors.fill: parent
+31
View File
@@ -9,6 +9,7 @@ Rectangle {
} }
ListView { ListView {
id: chunkListView
anchors.fill: parent anchors.fill: parent
anchors.leftMargin: 12 anchors.leftMargin: 12
model: bookController.hasBook ? readingModel : null model: bookController.hasBook ? readingModel : null
@@ -26,6 +27,8 @@ Rectangle {
text: model.content text: model.content
} }
onContentYChanged: scrollTrackTimer.restart()
ScrollBar.vertical: ScrollBar { ScrollBar.vertical: ScrollBar {
policy: ScrollBar.AsNeeded policy: ScrollBar.AsNeeded
padding: 0 padding: 0
@@ -49,4 +52,32 @@ Rectangle {
color: Theme.readingText 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
})
}
}
}
} }
+3 -3
View File
@@ -52,7 +52,7 @@ Rectangle {
width: 100 width: 100
height: 40 height: 40
radius: 6 radius: 6
color: window.pageFlipMode === modelData.value color: appState.pageFlipMode === modelData.value
? Theme.primaryAccent ? Theme.primaryAccent
: Theme.surface : Theme.surface
@@ -60,7 +60,7 @@ Rectangle {
anchors.centerIn: parent anchors.centerIn: parent
text: modelData.label text: modelData.label
font.pixelSize: 14 font.pixelSize: 14
color: window.pageFlipMode === modelData.value color: appState.pageFlipMode === modelData.value
? Theme.readingText ? Theme.readingText
: Theme.uiText : Theme.uiText
} }
@@ -68,7 +68,7 @@ Rectangle {
MouseArea { MouseArea {
anchors.fill: parent anchors.fill: parent
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: window.pageFlipMode = modelData.value onClicked: appState.pageFlipMode = modelData.value
} }
} }
} }
+197
View File
@@ -0,0 +1,197 @@
#include <QtTest>
#include <QSignalSpy>
#include <QSettings>
#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"
+21
View File
@@ -88,6 +88,27 @@ private slots:
// After completion, loading should be false // After completion, loading should be false
QVERIFY(!ctrl.loading()); 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) QTEST_MAIN(BookControllerTest)