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

+127 -4
View File
@@ -1,21 +1,144 @@
#include "AppState.h"
#include <QSettings>
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();
}
+60 -1
View File
@@ -3,24 +3,83 @@
#include <QObject>
#include <QString>
#include <QUrl>
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
+7
View File
@@ -97,6 +97,11 @@ bool BookController::hasBook() const
return m_reader != nullptr;
}
QString BookController::currentFilePath() const
{
return m_currentFilePath;
}
QList<TocEntry> 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);
+3
View File
@@ -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<TocEntry> 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<TocEntry> 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
-2
View File
@@ -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);
+9
View File
@@ -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();
}
+29 -1
View File
@@ -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
+31
View File
@@ -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
})
}
}
}
}
+3 -3
View File
@@ -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
}
}
}