add library with reading history, progress tracking, and view persistence

This commit is contained in:
Agel committed 2026-03-20 11:20:16 +01:00
1 parent c62733bc26
commit c4046d8533
15 files changed
+685 -40

No files matched your search

+123
View File
@@ -1,5 +1,13 @@
#include "AppState.h"
#include <QSettings>
#include <QCryptographicHash>
#include <QDateTime>
static QString bookSettingsKey(const QString &filePath)
{
QByteArray hash = QCryptographicHash::hash(filePath.toUtf8(), QCryptographicHash::Md5).toHex();
return QStringLiteral("bookmarks/") + QString::fromLatin1(hash);
}
AppState::AppState(QObject *parent)
: QObject(parent)
@@ -111,6 +119,117 @@ QUrl AppState::lastFileUrl() const
return QUrl::fromLocalFile(m_lastFilePath);
}
void AppState::saveBookPosition(const QString &filePath, int chapter, int scrollIndex, qreal scrollOffset)
{
QSettings s;
QString key = bookSettingsKey(filePath);
s.setValue(key + QStringLiteral("/chapter"), chapter);
s.setValue(key + QStringLiteral("/scrollIndex"), scrollIndex);
s.setValue(key + QStringLiteral("/scrollOffset"), scrollOffset);
}
QVariantMap AppState::loadBookPosition(const QString &filePath) const
{
QSettings s;
QString key = bookSettingsKey(filePath);
QVariantMap result;
result[QStringLiteral("chapter")] = s.value(key + QStringLiteral("/chapter"), 0).toInt();
result[QStringLiteral("scrollIndex")] = s.value(key + QStringLiteral("/scrollIndex"), 0).toInt();
result[QStringLiteral("scrollOffset")] = s.value(key + QStringLiteral("/scrollOffset"), 0.0).toDouble();
return result;
}
QString AppState::lastPage() const { return m_lastPage; }
void AppState::setLastPage(const QString &page)
{
if (m_lastPage == page)
return;
m_lastPage = page;
emit lastPageChanged();
}
void AppState::addOrUpdateLibraryBook(const QString &filePath, const QString &title, const QString &author, double progress)
{
QString lastOpened = QDateTime::currentDateTime().toString(Qt::ISODate);
for (int i = 0; i < m_libraryBooks.size(); i++) {
QVariantMap book = m_libraryBooks[i].toMap();
if (book[QStringLiteral("filePath")].toString() == filePath) {
book[QStringLiteral("title")] = title;
book[QStringLiteral("author")] = author;
book[QStringLiteral("lastOpened")] = lastOpened;
if (progress >= 0.0)
book[QStringLiteral("progress")] = progress;
m_libraryBooks[i] = book;
// Move to front (most recently opened)
m_libraryBooks.move(i, 0);
saveLibraryBooks();
emit libraryBooksChanged();
return;
}
}
QVariantMap newBook;
newBook[QStringLiteral("filePath")] = filePath;
newBook[QStringLiteral("title")] = title;
newBook[QStringLiteral("author")] = author;
newBook[QStringLiteral("progress")] = qMax(progress, 0.0);
newBook[QStringLiteral("lastOpened")] = lastOpened;
m_libraryBooks.prepend(newBook);
saveLibraryBooks();
emit libraryBooksChanged();
}
void AppState::removeLibraryBook(const QString &filePath)
{
for (int i = 0; i < m_libraryBooks.size(); i++) {
if (m_libraryBooks[i].toMap()[QStringLiteral("filePath")].toString() == filePath) {
m_libraryBooks.removeAt(i);
saveLibraryBooks();
emit libraryBooksChanged();
return;
}
}
}
QVariantList AppState::libraryBooks() const
{
return m_libraryBooks;
}
void AppState::loadLibraryBooks()
{
QSettings s;
int size = s.beginReadArray(QStringLiteral("library"));
m_libraryBooks.clear();
for (int i = 0; i < size; i++) {
s.setArrayIndex(i);
QVariantMap book;
book[QStringLiteral("filePath")] = s.value(QStringLiteral("filePath")).toString();
book[QStringLiteral("title")] = s.value(QStringLiteral("title")).toString();
book[QStringLiteral("author")] = s.value(QStringLiteral("author")).toString();
book[QStringLiteral("progress")] = s.value(QStringLiteral("progress"), 0.0).toDouble();
book[QStringLiteral("lastOpened")] = s.value(QStringLiteral("lastOpened")).toString();
m_libraryBooks.append(book);
}
s.endArray();
}
void AppState::saveLibraryBooks()
{
QSettings s;
s.beginWriteArray(QStringLiteral("library"), m_libraryBooks.size());
for (int i = 0; i < m_libraryBooks.size(); i++) {
s.setArrayIndex(i);
QVariantMap book = m_libraryBooks[i].toMap();
s.setValue(QStringLiteral("filePath"), book[QStringLiteral("filePath")]);
s.setValue(QStringLiteral("title"), book[QStringLiteral("title")]);
s.setValue(QStringLiteral("author"), book[QStringLiteral("author")]);
s.setValue(QStringLiteral("progress"), book[QStringLiteral("progress")]);
s.setValue(QStringLiteral("lastOpened"), book[QStringLiteral("lastOpened")]);
}
s.endArray();
}
void AppState::save()
{
QSettings s;
@@ -119,11 +238,13 @@ void AppState::save()
s.setValue(QStringLiteral("session/lastScrollPosition"), m_lastScrollPosition);
s.setValue(QStringLiteral("session/lastScrollIndex"), m_lastScrollIndex);
s.setValue(QStringLiteral("session/lastScrollOffset"), m_lastScrollOffset);
s.setValue(QStringLiteral("session/lastPage"), m_lastPage);
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);
saveLibraryBooks();
}
void AppState::restore()
@@ -134,11 +255,13 @@ void AppState::restore()
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_lastPage = s.value(QStringLiteral("session/lastPage"), QStringLiteral("starter")).toString();
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();
loadLibraryBooks();
emit stateRestored();
}
+20
View File
@@ -4,6 +4,8 @@
#include <QObject>
#include <QString>
#include <QUrl>
#include <QVariantMap>
#include <QVariantList>
class AppState : public QObject {
Q_OBJECT
@@ -17,6 +19,8 @@ class AppState : public QObject {
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)
Q_PROPERTY(QVariantList libraryBooks READ libraryBooks NOTIFY libraryBooksChanged)
Q_PROPERTY(QString lastPage READ lastPage WRITE setLastPage NOTIFY lastPageChanged)
public:
explicit AppState(QObject *parent = nullptr);
@@ -53,6 +57,16 @@ public:
Q_INVOKABLE QUrl lastFileUrl() const;
Q_INVOKABLE void saveBookPosition(const QString &filePath, int chapter, int scrollIndex, qreal scrollOffset);
Q_INVOKABLE QVariantMap loadBookPosition(const QString &filePath) const;
QString lastPage() const;
void setLastPage(const QString &page);
Q_INVOKABLE void addOrUpdateLibraryBook(const QString &filePath, const QString &title, const QString &author, double progress);
Q_INVOKABLE void removeLibraryBook(const QString &filePath);
Q_INVOKABLE QVariantList libraryBooks() const;
Q_INVOKABLE void save();
Q_INVOKABLE void restore();
@@ -68,8 +82,12 @@ signals:
void windowHeightChanged();
void windowXChanged();
void windowYChanged();
void libraryBooksChanged();
void lastPageChanged();
private:
void loadLibraryBooks();
void saveLibraryBooks();
QString m_lastFilePath;
int m_lastChapter = 0;
qreal m_lastScrollPosition = 0.0;
@@ -80,6 +98,8 @@ private:
int m_windowHeight = 768;
int m_windowX = -1;
int m_windowY = -1;
QVariantList m_libraryBooks;
QString m_lastPage = QStringLiteral("starter");
};
#endif // APPSTATE_H
+41
View File
@@ -1,4 +1,5 @@
#include "BookController.h"
#include "AppState.h"
#include "ReaderFactory.h"
#include "ReadingModel.h"
#include <QFileDialog>
@@ -114,10 +115,31 @@ void BookController::setReadingModel(ReadingModel *model)
m_readingModel = model;
}
void BookController::setAppState(AppState *state)
{
m_appState = state;
}
void BookController::openBookFromPath(const QString &filePath)
{
openBook(QUrl::fromLocalFile(filePath));
}
void BookController::openBook(const QUrl &fileUrl)
{
QString filePath = fileUrl.toLocalFile();
if (m_reader && m_appState && m_readingModel) {
m_appState->saveBookPosition(m_currentFilePath,
m_readingModel->currentChapter(),
m_appState->lastScrollIndex(),
m_appState->lastScrollOffset());
m_appState->addOrUpdateLibraryBook(m_currentFilePath, m_bookTitle, m_bookAuthor,
m_readingModel->progress());
}
if (m_reader)
closeBook();
m_loading = true;
emit loadingChanged();
@@ -151,6 +173,9 @@ void BookController::openBook(const QUrl &fileUrl)
if (m_readingModel)
m_readingModel->setReader(m_reader.get());
if (m_appState)
m_appState->addOrUpdateLibraryBook(m_currentFilePath, m_bookTitle, m_bookAuthor, -1.0);
m_loading = false;
emit loadingChanged();
emit bookChanged();
@@ -278,6 +303,14 @@ void BookController::browseAndOpenFallback()
void BookController::closeBook()
{
if (m_reader && m_appState && m_readingModel && !m_currentFilePath.isEmpty()) {
m_appState->saveBookPosition(m_currentFilePath,
m_readingModel->currentChapter(),
m_appState->lastScrollIndex(),
m_appState->lastScrollOffset());
m_appState->addOrUpdateLibraryBook(m_currentFilePath, m_bookTitle, m_bookAuthor,
m_readingModel->progress());
}
m_reader.reset();
m_bookTitle.clear();
m_bookAuthor.clear();
@@ -288,3 +321,11 @@ void BookController::closeBook()
emit bookChanged();
}
void BookController::clearError()
{
if (m_errorMessage.isEmpty())
return;
m_errorMessage.clear();
emit errorMessageChanged();
}
+5
View File
@@ -9,6 +9,7 @@
#include "FormatReader.h"
class AppState;
class ReadingModel;
class BookController : public QObject {
@@ -33,10 +34,13 @@ public:
QString currentFilePath() const;
void setReadingModel(ReadingModel *model);
void setAppState(AppState *state);
Q_INVOKABLE void openBook(const QUrl &fileUrl);
Q_INVOKABLE void openBookFromPath(const QString &filePath);
Q_INVOKABLE void browseAndOpen();
Q_INVOKABLE void closeBook();
Q_INVOKABLE void clearError();
signals:
void bookReady();
@@ -55,6 +59,7 @@ private:
std::unique_ptr<FormatReader> m_reader;
ReadingModel *m_readingModel = nullptr;
AppState *m_appState = nullptr;
QString m_bookTitle;
QString m_bookAuthor;
QString m_currentFilePath;
+4 -1
View File
@@ -58,7 +58,10 @@ double ReadingModel::progress() const
{
if (m_totalChapters == 0)
return 0.0;
return static_cast<double>(m_currentChapter + 1) / m_totalChapters;
double chapterFraction = 0.0;
if (m_totalContentHeight > 0)
chapterFraction = qBound(0.0, m_absoluteScrollY / m_totalContentHeight, 1.0);
return (m_currentChapter + chapterFraction) / m_totalChapters;
}
int ReadingModel::currentPage() const