Some refactor
This commit is contained in:
37 files changed
+1599
-981
No files matched your search
+35
-257
@@ -1,279 +1,57 @@
|
||||
#include "AppState.h"
|
||||
#include "LibraryBookModel.h"
|
||||
#include "LibraryModel.h"
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
|
||||
static constexpr int SETTINGS_VERSION = 3;
|
||||
|
||||
static QString bookSettingsKey(const QString &filePath)
|
||||
AppState::AppState(QSettings &settings)
|
||||
: m_settings(settings)
|
||||
{
|
||||
QByteArray hash = QCryptographicHash::hash(filePath.toUtf8(), QCryptographicHash::Md5).toHex();
|
||||
return QStringLiteral("bookmarks/") + QString::fromLatin1(hash);
|
||||
if (m_settings.value(QStringLiteral("version"), 0).toInt() != SETTINGS_VERSION) {
|
||||
m_settings.clear();
|
||||
m_settings.setValue(QStringLiteral("version"), SETTINGS_VERSION);
|
||||
}
|
||||
}
|
||||
|
||||
AppState::AppState(QObject *parent)
|
||||
: QObject(parent)
|
||||
QVariantMap AppState::loadSection(const QString §ion) const
|
||||
{
|
||||
m_libraryModel = new LibraryModel(this);
|
||||
}
|
||||
|
||||
QString AppState::lastFilePath() const { return m_lastFilePath; }
|
||||
|
||||
void AppState::setLastFilePath(const QString &path)
|
||||
{
|
||||
if (m_lastFilePath == path)
|
||||
return;
|
||||
m_lastFilePath = path;
|
||||
emit lastFilePathChanged();
|
||||
}
|
||||
|
||||
int AppState::horizontalMargin() const { return m_horizontalMargin; }
|
||||
|
||||
void AppState::setHorizontalMargin(int margin)
|
||||
{
|
||||
if (m_horizontalMargin == margin)
|
||||
return;
|
||||
m_horizontalMargin = margin;
|
||||
emit horizontalMarginChanged();
|
||||
}
|
||||
|
||||
int AppState::verticalMargin() const { return m_verticalMargin; }
|
||||
|
||||
void AppState::setVerticalMargin(int margin)
|
||||
{
|
||||
if (m_verticalMargin == margin)
|
||||
return;
|
||||
m_verticalMargin = margin;
|
||||
emit verticalMarginChanged();
|
||||
}
|
||||
|
||||
int AppState::fontSize() const { return m_fontSize; }
|
||||
|
||||
void AppState::setFontSize(int size)
|
||||
{
|
||||
if (m_fontSize == size)
|
||||
return;
|
||||
m_fontSize = size;
|
||||
emit fontSizeChanged();
|
||||
}
|
||||
|
||||
qreal AppState::lineSpacing() const { return m_lineSpacing; }
|
||||
|
||||
void AppState::setLineSpacing(qreal spacing)
|
||||
{
|
||||
if (qFuzzyCompare(m_lineSpacing, spacing))
|
||||
return;
|
||||
m_lineSpacing = spacing;
|
||||
emit lineSpacingChanged();
|
||||
}
|
||||
|
||||
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::saveBookPosition(const QString &filePath, int chunkIndex, int intraChunkOffset)
|
||||
{
|
||||
qInfo() << "Saving book position: chunk" << chunkIndex << "offset" << intraChunkOffset;
|
||||
QSettings s;
|
||||
QString key = bookSettingsKey(filePath);
|
||||
s.setValue(key + QStringLiteral("/chunkIndex"), chunkIndex);
|
||||
s.setValue(key + QStringLiteral("/intraChunkOffset"), intraChunkOffset);
|
||||
}
|
||||
|
||||
QVariantMap AppState::loadBookPosition(const QString &filePath)
|
||||
{
|
||||
QSettings s;
|
||||
QString key = bookSettingsKey(filePath);
|
||||
QVariantMap result;
|
||||
result[QStringLiteral("chunkIndex")] = s.value(key + QStringLiteral("/chunkIndex"), 0).toInt();
|
||||
result[QStringLiteral("intraChunkOffset")] = s.value(key + QStringLiteral("/intraChunkOffset"), 0.0).toInt();
|
||||
m_settings.beginGroup(section);
|
||||
const QStringList keys = m_settings.childKeys();
|
||||
for (const QString &key : keys)
|
||||
result[key] = m_settings.value(key);
|
||||
m_settings.endGroup();
|
||||
return result;
|
||||
}
|
||||
|
||||
QString AppState::lastPage() const { return m_lastPage; }
|
||||
|
||||
void AppState::setLastPage(const QString &page)
|
||||
void AppState::saveSection(const QString §ion, const QVariantMap &data) const
|
||||
{
|
||||
if (m_lastPage == page)
|
||||
return;
|
||||
m_lastPage = page;
|
||||
emit lastPageChanged();
|
||||
m_settings.beginGroup(section);
|
||||
m_settings.remove(QStringLiteral("")); // clear existing group
|
||||
for (auto it = data.cbegin(); it != data.cend(); ++it)
|
||||
m_settings.setValue(it.key(), it.value());
|
||||
m_settings.endGroup();
|
||||
}
|
||||
|
||||
bool AppState::sidebarOpen() const { return m_sidebarOpen; }
|
||||
|
||||
void AppState::setSidebarOpen(bool open)
|
||||
QList<QVariantMap> AppState::loadArray(const QString §ion) const
|
||||
{
|
||||
if (m_sidebarOpen == open)
|
||||
return;
|
||||
m_sidebarOpen = open;
|
||||
emit sidebarOpenChanged();
|
||||
}
|
||||
|
||||
QString AppState::themeName() const { return m_themeName; }
|
||||
|
||||
void AppState::setThemeName(const QString &themeName)
|
||||
{
|
||||
if (m_themeName == themeName)
|
||||
return;
|
||||
m_themeName = themeName;
|
||||
emit themeNameChanged();
|
||||
}
|
||||
|
||||
void AppState::addOrUpdateLibraryBook(const QString &filePath, double progress)
|
||||
{
|
||||
QString lastOpened = QDateTime::currentDateTime().toString(Qt::ISODate);
|
||||
|
||||
// Sentinel: progress < 0 means "preserve existing progress"
|
||||
double resolvedProgress = progress;
|
||||
if (progress < 0.0) {
|
||||
LibraryBookModel* existing = m_libraryModel->findBook(filePath);
|
||||
resolvedProgress = existing ? existing->progress() : 0.0;
|
||||
}
|
||||
|
||||
m_libraryModel->addOrUpdateBook(filePath, resolvedProgress, lastOpened);
|
||||
saveLibraryBooks();
|
||||
}
|
||||
|
||||
void AppState::removeLibraryBook(const QString &filePath)
|
||||
{
|
||||
m_libraryModel->removeBook(filePath);
|
||||
saveLibraryBooks();
|
||||
}
|
||||
|
||||
QObject* AppState::libraryModel() const
|
||||
{
|
||||
// Add null check to prevent crashes during initialization
|
||||
if (!m_libraryModel) {
|
||||
qWarning() << "LibraryModel accessed before initialization";
|
||||
return nullptr;
|
||||
}
|
||||
return m_libraryModel;
|
||||
}
|
||||
|
||||
void AppState::loadLibraryBooks()
|
||||
{
|
||||
QSettings s;
|
||||
int size = s.beginReadArray(QStringLiteral("library"));
|
||||
for (int i = 0; i < size; i++) {
|
||||
QList<QVariantMap> result;
|
||||
QSettings &s = const_cast<QSettings &>(m_settings);
|
||||
const int size = s.beginReadArray(section);
|
||||
for (int i = 0; i < size; ++i) {
|
||||
s.setArrayIndex(i);
|
||||
QString filePath = s.value(QStringLiteral("filePath")).toString();
|
||||
if (filePath.isEmpty())
|
||||
continue;
|
||||
bool progressOk;
|
||||
double progress = s.value(QStringLiteral("progress"), 0.0).toDouble(&progressOk);
|
||||
if (!progressOk)
|
||||
progress = 0.0;
|
||||
progress = qBound(0.0, progress, 1.0);
|
||||
QString lastOpened = s.value(QStringLiteral("lastOpened")).toString();
|
||||
m_libraryModel->addOrUpdateBook(filePath, progress, lastOpened);
|
||||
QVariantMap entry;
|
||||
for (const QString &key : s.childKeys())
|
||||
entry[key] = s.value(key);
|
||||
result.append(entry);
|
||||
}
|
||||
s.endArray();
|
||||
return result;
|
||||
}
|
||||
|
||||
void AppState::saveLibraryBooks()
|
||||
void AppState::saveArray(const QString §ion, const QList<QVariantMap> &items) const
|
||||
{
|
||||
QSettings s;
|
||||
QList<LibraryBookModel*> books = m_libraryModel->books();
|
||||
s.beginWriteArray(QStringLiteral("library"), books.size());
|
||||
for (int i = 0; i < books.size(); i++) {
|
||||
s.setArrayIndex(i);
|
||||
LibraryBookModel* book = books[i];
|
||||
s.setValue(QStringLiteral("filePath"), book->filePath());
|
||||
s.setValue(QStringLiteral("progress"), book->progress());
|
||||
s.setValue(QStringLiteral("lastOpened"), book->lastOpened());
|
||||
m_settings.beginWriteArray(section, items.size());
|
||||
for (int i = 0; i < items.size(); ++i) {
|
||||
m_settings.setArrayIndex(i);
|
||||
for (auto it = items[i].cbegin(); it != items[i].cend(); ++it)
|
||||
m_settings.setValue(it.key(), it.value());
|
||||
}
|
||||
s.endArray();
|
||||
}
|
||||
|
||||
void AppState::save()
|
||||
{
|
||||
QSettings s;
|
||||
s.setValue(QStringLiteral("version"), SETTINGS_VERSION);
|
||||
s.setValue(QStringLiteral("session/lastFilePath"), m_lastFilePath);
|
||||
s.setValue(QStringLiteral("session/lastPage"), m_lastPage);
|
||||
s.setValue(QStringLiteral("session/sidebarOpen"), m_sidebarOpen);
|
||||
s.setValue(QStringLiteral("preferences/horizontalMargin"), m_horizontalMargin);
|
||||
s.setValue(QStringLiteral("preferences/verticalMargin"), m_verticalMargin);
|
||||
s.setValue(QStringLiteral("preferences/fontSize"), m_fontSize);
|
||||
s.setValue(QStringLiteral("preferences/lineSpacing"), m_lineSpacing);
|
||||
s.setValue(QStringLiteral("preferences/themeName"), m_themeName);
|
||||
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();
|
||||
qInfo() << "filename:" << s.fileName();
|
||||
}
|
||||
|
||||
void AppState::restore()
|
||||
{
|
||||
QSettings s;
|
||||
|
||||
// Wipe and start fresh if settings are from a different/incompatible format version
|
||||
if (s.value(QStringLiteral("version"), 0).toInt() != SETTINGS_VERSION) {
|
||||
s.clear();
|
||||
emit stateRestored();
|
||||
return;
|
||||
}
|
||||
|
||||
m_lastFilePath = s.value(QStringLiteral("session/lastFilePath"), QString()).toString();
|
||||
m_lastPage = s.value(QStringLiteral("session/lastPage"), QStringLiteral("starter")).toString();
|
||||
m_sidebarOpen = s.value(QStringLiteral("session/sidebarOpen"), false).toBool();
|
||||
|
||||
m_horizontalMargin = qMax(0, s.value(QStringLiteral("preferences/horizontalMargin"), 32).toInt());
|
||||
m_verticalMargin = qMax(0, s.value(QStringLiteral("preferences/verticalMargin"), 8).toInt());
|
||||
m_fontSize = qBound(8, s.value(QStringLiteral("preferences/fontSize"), 16).toInt(), 72);
|
||||
m_lineSpacing = qMax(0.1, s.value(QStringLiteral("preferences/lineSpacing"), 1.6).toDouble());
|
||||
m_themeName = s.value(QStringLiteral("preferences/themeName"), QStringLiteral("dark")).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();
|
||||
m_settings.endArray();
|
||||
}
|
||||
+17
-98
@@ -1,114 +1,33 @@
|
||||
#ifndef APPSTATE_H
|
||||
#define APPSTATE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QSettings>
|
||||
#include <QVariantMap>
|
||||
|
||||
class LibraryBookModel;
|
||||
class LibraryModel;
|
||||
|
||||
class AppState : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString lastFilePath READ lastFilePath WRITE setLastFilePath NOTIFY lastFilePathChanged)
|
||||
Q_PROPERTY(int horizontalMargin READ horizontalMargin WRITE setHorizontalMargin NOTIFY horizontalMarginChanged)
|
||||
Q_PROPERTY(int verticalMargin READ verticalMargin WRITE setVerticalMargin NOTIFY verticalMarginChanged)
|
||||
Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
|
||||
Q_PROPERTY(qreal lineSpacing READ lineSpacing WRITE setLineSpacing NOTIFY lineSpacingChanged)
|
||||
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)
|
||||
|
||||
Q_PROPERTY(QString lastPage READ lastPage WRITE setLastPage NOTIFY lastPageChanged)
|
||||
Q_PROPERTY(bool sidebarOpen READ sidebarOpen WRITE setSidebarOpen NOTIFY sidebarOpenChanged)
|
||||
Q_PROPERTY(QString themeName READ themeName WRITE setThemeName NOTIFY themeNameChanged)
|
||||
Q_PROPERTY(QObject* libraryModel READ libraryModel CONSTANT)
|
||||
|
||||
// AppState is a pure persistence service. It wraps QSettings and provides an atomic
|
||||
// section-based API. It holds no domain state of its own. QML never sees this object.
|
||||
class AppState {
|
||||
public:
|
||||
explicit AppState(QObject *parent = nullptr);
|
||||
static constexpr int SETTINGS_VERSION = 4;
|
||||
|
||||
QString themeName() const;
|
||||
void setThemeName(const QString &themeName);
|
||||
// Constructs AppState. If the stored version does not match SETTINGS_VERSION,
|
||||
// all settings are cleared (clean migration).
|
||||
explicit AppState(QSettings &settings);
|
||||
|
||||
QString lastFilePath() const;
|
||||
void setLastFilePath(const QString &path);
|
||||
// Load all key/value pairs from the named QSettings group.
|
||||
QVariantMap loadSection(const QString §ion) const;
|
||||
|
||||
int horizontalMargin() const;
|
||||
void setHorizontalMargin(int margin);
|
||||
// Atomically replace the named QSettings group with the given data.
|
||||
void saveSection(const QString §ion, const QVariantMap &data) const;
|
||||
|
||||
int verticalMargin() const;
|
||||
void setVerticalMargin(int margin);
|
||||
// Load an ordered array (e.g., library books) from the named QSettings array.
|
||||
QList<QVariantMap> loadArray(const QString §ion) const;
|
||||
|
||||
int fontSize() const;
|
||||
void setFontSize(int size);
|
||||
|
||||
qreal lineSpacing() const;
|
||||
void setLineSpacing(qreal spacing);
|
||||
|
||||
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 static void saveBookPosition(const QString &filePath, int chunkIndex, int intraChunkOffset);
|
||||
Q_INVOKABLE static QVariantMap loadBookPosition(const QString &filePath);
|
||||
|
||||
QString lastPage() const;
|
||||
void setLastPage(const QString &page);
|
||||
|
||||
bool sidebarOpen() const;
|
||||
void setSidebarOpen(bool open);
|
||||
|
||||
Q_INVOKABLE void addOrUpdateLibraryBook(const QString &filePath, double progress);
|
||||
Q_INVOKABLE void removeLibraryBook(const QString &filePath);
|
||||
QObject* libraryModel() const;
|
||||
|
||||
Q_INVOKABLE void save();
|
||||
Q_INVOKABLE void restore();
|
||||
|
||||
signals:
|
||||
void lastFilePathChanged();
|
||||
void horizontalMarginChanged();
|
||||
void verticalMarginChanged();
|
||||
void fontSizeChanged();
|
||||
void lineSpacingChanged();
|
||||
void stateRestored();
|
||||
void windowWidthChanged();
|
||||
void windowHeightChanged();
|
||||
void windowXChanged();
|
||||
void windowYChanged();
|
||||
|
||||
void lastPageChanged();
|
||||
void sidebarOpenChanged();
|
||||
void themeNameChanged();
|
||||
// Atomically replace the named QSettings array.
|
||||
void saveArray(const QString §ion, const QList<QVariantMap> &items) const;
|
||||
|
||||
private:
|
||||
void loadLibraryBooks();
|
||||
void saveLibraryBooks();
|
||||
QString m_lastFilePath;
|
||||
int m_horizontalMargin = 32;
|
||||
int m_verticalMargin = 8;
|
||||
int m_fontSize = 16;
|
||||
qreal m_lineSpacing = 1.6;
|
||||
int m_windowWidth = 1024;
|
||||
int m_windowHeight = 768;
|
||||
int m_windowX = -1;
|
||||
int m_windowY = -1;
|
||||
LibraryModel* m_libraryModel;
|
||||
QString m_lastPage = QStringLiteral("starter");
|
||||
bool m_sidebarOpen = false;
|
||||
QString m_themeName = QStringLiteral("dark");
|
||||
QSettings &m_settings;
|
||||
};
|
||||
|
||||
#endif // APPSTATE_H
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef FORMATREADER_H
|
||||
#define FORMATREADER_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QList>
|
||||
|
||||
@@ -30,6 +31,9 @@ public:
|
||||
virtual QString title() = 0;
|
||||
virtual QString author() = 0;
|
||||
virtual QString coverImagePath() = 0;
|
||||
// MD5 of file contents, computed once during open(). Used as a stable key for
|
||||
// per-book state (bookmarks, etc.) that survives file renames.
|
||||
virtual QByteArray contentHash() = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "LibraryModel.h"
|
||||
#include "LibraryBookModel.h"
|
||||
#include <QDateTime>
|
||||
|
||||
LibraryModel::LibraryModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
@@ -157,6 +158,36 @@ void LibraryModel::clear()
|
||||
emit countChanged();
|
||||
}
|
||||
|
||||
void LibraryModel::addOrUpdateBook(const QString &filePath, double progress)
|
||||
{
|
||||
// Sentinel: progress < 0 means "preserve the existing progress value"
|
||||
double resolvedProgress = progress;
|
||||
if (progress < 0.0) {
|
||||
LibraryBookModel *existing = findBook(filePath);
|
||||
resolvedProgress = existing ? existing->progress() : 0.0;
|
||||
}
|
||||
addOrUpdateBook(filePath, resolvedProgress,
|
||||
QDateTime::currentDateTime().toString(Qt::ISODate));
|
||||
}
|
||||
|
||||
QString LibraryModel::filePath(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return {};
|
||||
return m_books.at(index)->filePath();
|
||||
}
|
||||
|
||||
double LibraryModel::progress(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return 0.0;
|
||||
return m_books.at(index)->progress();
|
||||
}
|
||||
|
||||
QString LibraryModel::lastOpened(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_books.size()) return {};
|
||||
return m_books.at(index)->lastOpened();
|
||||
}
|
||||
|
||||
QList<LibraryBookModel*> LibraryModel::books() const
|
||||
{
|
||||
return m_books;
|
||||
|
||||
@@ -36,10 +36,17 @@ public:
|
||||
|
||||
// Methods to manage books
|
||||
Q_INVOKABLE void addOrUpdateBook(const QString &filePath, double progress, const QString &lastOpened);
|
||||
// Convenience overload: sentinel progress < 0 preserves the existing progress value.
|
||||
Q_INVOKABLE void addOrUpdateBook(const QString &filePath, double progress);
|
||||
Q_INVOKABLE void removeBook(const QString &filePath);
|
||||
Q_INVOKABLE LibraryBookModel* findBook(const QString &filePath) const;
|
||||
Q_INVOKABLE void clear();
|
||||
|
||||
// Indexed accessors for persistence (e.g., main.cpp's aboutToQuit loop).
|
||||
QString filePath(int index) const;
|
||||
double progress(int index) const;
|
||||
QString lastOpened(int index) const;
|
||||
|
||||
// Access to books for saving/loading
|
||||
QList<LibraryBookModel*> books() const;
|
||||
|
||||
|
||||
@@ -7,9 +7,11 @@
|
||||
#include <cmath>
|
||||
#include <QtConcurrent>
|
||||
|
||||
#include "AppState.h"
|
||||
|
||||
ReadingModel::ReadingModel(QObject *parent) : QAbstractListModel(parent) {}
|
||||
ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_positions(positions)
|
||||
{
|
||||
}
|
||||
|
||||
ReadingModel::~ReadingModel()
|
||||
{
|
||||
@@ -126,10 +128,8 @@ void ReadingModel::runMeasurement(const QStringList &htmls, const bool isNew)
|
||||
{PixelHeightRole});
|
||||
|
||||
if (isNew) {
|
||||
QVariantMap map = AppState::loadBookPosition(m_reader->filePath());
|
||||
int savedChunk = map.value(QStringLiteral("chunkIndex"), 0).toInt();
|
||||
qreal savedIntra = map.value(QStringLiteral("intraChunkOffset"), 0).toInt();
|
||||
restoreReady(savedChunk, savedIntra);
|
||||
const auto pos = m_positions.load(m_reader->contentHash());
|
||||
emit restoreReady(pos.chunkIndex, pos.intraOffset);
|
||||
}
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
#include "IImageLoader.h"
|
||||
#include "state/BookPositionStore.h"
|
||||
|
||||
class FormatReader;
|
||||
|
||||
@@ -35,7 +36,7 @@ public:
|
||||
ChapterRole = Qt::UserRole + 3,
|
||||
};
|
||||
|
||||
explicit ReadingModel(QObject *parent = nullptr);
|
||||
explicit ReadingModel(BookPositionStore &positions, QObject *parent = nullptr);
|
||||
~ReadingModel() override;
|
||||
|
||||
int rowCount(const QModelIndex &parent) const override;
|
||||
@@ -106,6 +107,7 @@ private:
|
||||
int m_fontSize = 0;
|
||||
qreal m_lineSpacing = 0;
|
||||
|
||||
BookPositionStore &m_positions;
|
||||
std::unique_ptr<IImageLoader> m_imageLoader;
|
||||
QFuture<void> m_measureFuture;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
#include "Book.h"
|
||||
#include "backend/ReaderFactory.h"
|
||||
#include <QtConcurrent>
|
||||
|
||||
Book::Book(IFileDialogService &dialog,
|
||||
BookPositionStore &positions,
|
||||
QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_dialog(dialog)
|
||||
, m_positions(positions)
|
||||
{
|
||||
connect(&m_dialog, &IFileDialogService::fileSelected,
|
||||
this, &Book::openFromPath);
|
||||
}
|
||||
|
||||
Book::~Book()
|
||||
{
|
||||
if (m_openFuture.isRunning()) {
|
||||
m_openFuture.cancel();
|
||||
m_openFuture.waitForFinished();
|
||||
}
|
||||
}
|
||||
|
||||
bool Book::hasBook() const { return m_reader != nullptr; }
|
||||
bool Book::loading() const { return m_loading; }
|
||||
QString Book::errorMessage() const { return m_errorMessage; }
|
||||
QString Book::coverImageUrl() const { return m_coverImageUrl; }
|
||||
|
||||
QString Book::title() const { return m_reader ? m_reader->title() : QString(); }
|
||||
QString Book::author() const { return m_reader ? m_reader->author() : QString(); }
|
||||
QString Book::filePath() const { return m_reader ? m_reader->filePath() : QString(); }
|
||||
|
||||
QList<TocEntry> Book::toc() const
|
||||
{
|
||||
return m_reader ? m_reader->tableOfContents() : QList<TocEntry>{};
|
||||
}
|
||||
|
||||
FormatReader *Book::reader() const
|
||||
{
|
||||
return m_reader.get();
|
||||
}
|
||||
|
||||
void Book::setCoverImageUrl(const QString &url)
|
||||
{
|
||||
m_coverImageUrl = url;
|
||||
// bookChanged is already emitted after book open; no extra emit needed here.
|
||||
// If needed by QML after provider setup, a separate signal or re-emit can be added.
|
||||
}
|
||||
|
||||
void Book::openFromUrl(const QUrl &url)
|
||||
{
|
||||
openFromPath(url.toLocalFile());
|
||||
}
|
||||
|
||||
void Book::openFromPath(const QString &path)
|
||||
{
|
||||
openImpl(path);
|
||||
}
|
||||
|
||||
void Book::browseAndOpen()
|
||||
{
|
||||
m_dialog.open();
|
||||
}
|
||||
|
||||
void Book::openImpl(const QString &filePath)
|
||||
{
|
||||
if (m_reader)
|
||||
emit willOpenNewBook();
|
||||
|
||||
close();
|
||||
|
||||
m_loading = true;
|
||||
emit loadingChanged();
|
||||
|
||||
m_errorMessage.clear();
|
||||
emit errorMessageChanged();
|
||||
|
||||
const int generation = ++m_openGeneration;
|
||||
|
||||
m_openFuture = QtConcurrent::run([this, filePath, generation]() {
|
||||
auto reader = ReaderFactory::create(filePath);
|
||||
if (!reader) {
|
||||
QMetaObject::invokeMethod(this, [this, generation]() {
|
||||
if (generation != m_openGeneration) return;
|
||||
m_loading = false;
|
||||
emit loadingChanged();
|
||||
m_errorMessage = QStringLiteral("No format reader available for this file type");
|
||||
emit errorMessageChanged();
|
||||
}, Qt::QueuedConnection);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!reader->open(filePath)) {
|
||||
const QString err = reader->lastError();
|
||||
QMetaObject::invokeMethod(this, [this, generation, err]() {
|
||||
if (generation != m_openGeneration) return;
|
||||
m_loading = false;
|
||||
emit loadingChanged();
|
||||
m_errorMessage = err;
|
||||
emit errorMessageChanged();
|
||||
}, Qt::QueuedConnection);
|
||||
return;
|
||||
}
|
||||
|
||||
auto readerBox = std::make_shared<std::unique_ptr<FormatReader>>(std::move(reader));
|
||||
|
||||
QMetaObject::invokeMethod(this, [this, generation, readerBox]() {
|
||||
if (generation != m_openGeneration) return;
|
||||
|
||||
m_reader = std::move(*readerBox);
|
||||
m_coverImageUrl.clear();
|
||||
m_loading = false;
|
||||
emit loadingChanged();
|
||||
emit bookChanged();
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
}
|
||||
|
||||
void Book::close()
|
||||
{
|
||||
if (!m_reader) return;
|
||||
m_reader.reset();
|
||||
m_coverImageUrl.clear();
|
||||
emit bookChanged();
|
||||
}
|
||||
|
||||
void Book::clearError()
|
||||
{
|
||||
if (m_errorMessage.isEmpty()) return;
|
||||
m_errorMessage.clear();
|
||||
emit errorMessageChanged();
|
||||
}
|
||||
|
||||
void Book::saveCurrentPosition(int chunkIndex, int intraOffset)
|
||||
{
|
||||
if (!m_reader) return;
|
||||
m_positions.save(m_reader->contentHash(), chunkIndex, intraOffset);
|
||||
}
|
||||
|
||||
void Book::setContentWidth(int width)
|
||||
{
|
||||
if (m_contentWidth == width) return;
|
||||
m_contentWidth = width;
|
||||
emit contentWidthChanged(width);
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#ifndef BOOK_H
|
||||
#define BOOK_H
|
||||
|
||||
#include <QFuture>
|
||||
#include <QList>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <memory>
|
||||
|
||||
#include "backend/FormatReader.h"
|
||||
#include "backend/IFileDialogService.h"
|
||||
#include "backend/state/BookPositionStore.h"
|
||||
|
||||
class Book : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool hasBook READ hasBook NOTIFY bookChanged)
|
||||
Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
|
||||
Q_PROPERTY(QString title READ title NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString author READ author NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString filePath READ filePath NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString coverImageUrl READ coverImageUrl NOTIFY bookChanged)
|
||||
Q_PROPERTY(QList<TocEntry> toc READ toc NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
|
||||
public:
|
||||
Book(IFileDialogService &dialog,
|
||||
BookPositionStore &positions,
|
||||
QObject *parent = nullptr);
|
||||
~Book() override;
|
||||
|
||||
bool hasBook() const;
|
||||
bool loading() const;
|
||||
QString title() const;
|
||||
QString author() const;
|
||||
QString filePath() const;
|
||||
QString coverImageUrl() const;
|
||||
QList<TocEntry> toc() const;
|
||||
QString errorMessage() const;
|
||||
|
||||
// Called by main.cpp's bookChanged lambda after the image provider resolves the cover URL.
|
||||
void setCoverImageUrl(const QString &url);
|
||||
|
||||
// Access the underlying reader for wiring purposes (e.g., to push into ReadingModel).
|
||||
FormatReader *reader() const;
|
||||
|
||||
Q_INVOKABLE void openFromUrl(const QUrl &url);
|
||||
Q_INVOKABLE void openFromPath(const QString &path);
|
||||
Q_INVOKABLE void browseAndOpen();
|
||||
Q_INVOKABLE void close();
|
||||
Q_INVOKABLE void clearError();
|
||||
|
||||
// Save current scroll position. Called from QML's scroll tracker; forwards to BookPositionStore.
|
||||
Q_INVOKABLE void saveCurrentPosition(int chunkIndex, int intraOffset);
|
||||
|
||||
// Called from QML's PageView when its width changes. Propagates to image provider + ReadingModel
|
||||
// via the contentWidthChanged signal (wired in main.cpp).
|
||||
Q_INVOKABLE void setContentWidth(int width);
|
||||
|
||||
signals:
|
||||
// Emitted just before replacing the current reader with a new one.
|
||||
// Listeners can snapshot the outgoing book's progress before the swap.
|
||||
void willOpenNewBook();
|
||||
|
||||
void bookChanged();
|
||||
void loadingChanged();
|
||||
void errorMessageChanged();
|
||||
void contentWidthChanged(int width);
|
||||
|
||||
private:
|
||||
void openImpl(const QString &filePath);
|
||||
|
||||
IFileDialogService &m_dialog;
|
||||
BookPositionStore &m_positions;
|
||||
std::unique_ptr<FormatReader> m_reader;
|
||||
QString m_coverImageUrl;
|
||||
QString m_errorMessage;
|
||||
bool m_loading = false;
|
||||
int m_openGeneration = 0;
|
||||
int m_contentWidth = 0;
|
||||
QFuture<void> m_openFuture;
|
||||
};
|
||||
|
||||
#endif // BOOK_H
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "EpubReader.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QDir>
|
||||
#include <QDomDocument>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QTextStream>
|
||||
#include <QUrl>
|
||||
@@ -35,6 +37,14 @@ bool EpubReader::open(const QString &filePath)
|
||||
m_spineHrefs.clear();
|
||||
m_spineRawContent.clear();
|
||||
m_toc.clear();
|
||||
m_contentHash.clear();
|
||||
|
||||
// Compute content hash from raw file bytes before ZIP parsing
|
||||
{
|
||||
QFile f(filePath);
|
||||
if (f.open(QIODevice::ReadOnly))
|
||||
m_contentHash = QCryptographicHash::hash(f.readAll(), QCryptographicHash::Md5);
|
||||
}
|
||||
|
||||
unzFile zf = unzOpen(filePath.toUtf8().constData());
|
||||
if (!zf) {
|
||||
@@ -390,6 +400,11 @@ QString EpubReader::filePath()
|
||||
return m_filePath;
|
||||
}
|
||||
|
||||
QByteArray EpubReader::contentHash()
|
||||
{
|
||||
return m_contentHash;
|
||||
}
|
||||
|
||||
Chapter EpubReader::loadChapter(int index)
|
||||
{
|
||||
if (index < 0 || index >= m_spineRawContent.size())
|
||||
|
||||
@@ -18,9 +18,11 @@ public:
|
||||
QString title() override;
|
||||
QString author() override;
|
||||
QString coverImagePath() override;
|
||||
QByteArray contentHash() override;
|
||||
|
||||
private:
|
||||
QString m_filePath;
|
||||
QByteArray m_contentHash;
|
||||
QString m_title;
|
||||
QString m_author;
|
||||
QString m_error;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "TxtReader.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QFileInfo>
|
||||
#include <QRegularExpression>
|
||||
#include <QTextStream>
|
||||
@@ -110,6 +111,8 @@ bool TxtReader::open(const QString &filePath)
|
||||
QByteArray raw = file.readAll();
|
||||
file.close();
|
||||
|
||||
m_contentHash = QCryptographicHash::hash(raw, QCryptographicHash::Md5);
|
||||
|
||||
uchardet_t ud = uchardet_new();
|
||||
if (uchardet_handle_data(ud, raw.constData(), raw.size()) == 0) {
|
||||
uchardet_data_end(ud);
|
||||
@@ -206,3 +209,8 @@ QString TxtReader::coverImagePath()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
QByteArray TxtReader::contentHash()
|
||||
{
|
||||
return m_contentHash;
|
||||
}
|
||||
@@ -15,6 +15,7 @@ public:
|
||||
QString title() override;
|
||||
QString author() override;
|
||||
QString coverImagePath() override;
|
||||
QByteArray contentHash() override;
|
||||
|
||||
private:
|
||||
QString m_content;
|
||||
@@ -22,6 +23,7 @@ private:
|
||||
QString m_title;
|
||||
QString m_author;
|
||||
QString m_error;
|
||||
QByteArray m_contentHash;
|
||||
};
|
||||
|
||||
#endif // TXTREADER_H
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "BookPositionStore.h"
|
||||
|
||||
BookPositionStore::BookPositionStore(AppState &state)
|
||||
: m_state(state)
|
||||
{
|
||||
}
|
||||
|
||||
BookPosition BookPositionStore::load(const QByteArray &contentMd5)
|
||||
{
|
||||
if (contentMd5.isEmpty())
|
||||
return {};
|
||||
const QVariantMap d = m_state.loadSection(sectionKey(contentMd5));
|
||||
BookPosition pos;
|
||||
pos.chunkIndex = d.value(QStringLiteral("chunkIndex"), 0).toInt();
|
||||
pos.intraOffset = d.value(QStringLiteral("intraOffset"), 0).toInt();
|
||||
return pos;
|
||||
}
|
||||
|
||||
void BookPositionStore::save(const QByteArray &contentMd5, int chunkIndex, int intraOffset)
|
||||
{
|
||||
if (contentMd5.isEmpty())
|
||||
return;
|
||||
m_state.saveSection(sectionKey(contentMd5), {
|
||||
{QStringLiteral("chunkIndex"), chunkIndex},
|
||||
{QStringLiteral("intraOffset"), intraOffset},
|
||||
});
|
||||
}
|
||||
|
||||
QString BookPositionStore::sectionKey(const QByteArray &contentMd5)
|
||||
{
|
||||
return QStringLiteral("saved-progress/") + QString::fromLatin1(contentMd5.toHex());
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef BOOKPOSITIONSTORE_H
|
||||
#define BOOKPOSITIONSTORE_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include "backend/AppState.h"
|
||||
|
||||
struct BookPosition {
|
||||
int chunkIndex = 0;
|
||||
int intraOffset = 0;
|
||||
};
|
||||
|
||||
class BookPositionStore {
|
||||
public:
|
||||
explicit BookPositionStore(AppState &state);
|
||||
|
||||
// Load the saved position for a book identified by MD5 of its contents.
|
||||
BookPosition load(const QByteArray &contentMd5);
|
||||
|
||||
// Save the current position for a book identified by MD5 of its contents.
|
||||
void save(const QByteArray &contentMd5, int chunkIndex, int intraOffset);
|
||||
|
||||
private:
|
||||
AppState &m_state;
|
||||
|
||||
static QString sectionKey(const QByteArray &contentMd5);
|
||||
};
|
||||
|
||||
#endif // BOOKPOSITIONSTORE_H
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "ReadingPreferences.h"
|
||||
#include <QtGlobal>
|
||||
|
||||
ReadingPreferences::ReadingPreferences(AppState &state, QObject *parent)
|
||||
: QObject(parent), m_state(state)
|
||||
{
|
||||
const QVariantMap d = state.loadSection(QStringLiteral("reading"));
|
||||
m_fontSize = qBound(8, d.value(QStringLiteral("fontSize"), 16).toInt(), 72);
|
||||
m_lineSpacing = qMax(0.1, d.value(QStringLiteral("lineSpacing"), 1.6).toDouble());
|
||||
m_horizontalMargin = qMax(0, d.value(QStringLiteral("horizontalMargin"), 32).toInt());
|
||||
m_verticalMargin = qMax(0, d.value(QStringLiteral("verticalMargin"), 8).toInt());
|
||||
}
|
||||
|
||||
int ReadingPreferences::fontSize() const { return m_fontSize; }
|
||||
qreal ReadingPreferences::lineSpacing() const { return m_lineSpacing; }
|
||||
int ReadingPreferences::horizontalMargin() const { return m_horizontalMargin; }
|
||||
int ReadingPreferences::verticalMargin() const { return m_verticalMargin; }
|
||||
|
||||
void ReadingPreferences::setFontSize(int size)
|
||||
{
|
||||
if (m_fontSize == size) return;
|
||||
m_fontSize = size;
|
||||
emit fontSizeChanged();
|
||||
}
|
||||
|
||||
void ReadingPreferences::setLineSpacing(qreal spacing)
|
||||
{
|
||||
if (qFuzzyCompare(m_lineSpacing, spacing)) return;
|
||||
m_lineSpacing = spacing;
|
||||
emit lineSpacingChanged();
|
||||
}
|
||||
|
||||
void ReadingPreferences::setHorizontalMargin(int margin)
|
||||
{
|
||||
if (m_horizontalMargin == margin) return;
|
||||
m_horizontalMargin = margin;
|
||||
emit horizontalMarginChanged();
|
||||
}
|
||||
|
||||
void ReadingPreferences::setVerticalMargin(int margin)
|
||||
{
|
||||
if (m_verticalMargin == margin) return;
|
||||
m_verticalMargin = margin;
|
||||
emit verticalMarginChanged();
|
||||
}
|
||||
|
||||
void ReadingPreferences::save()
|
||||
{
|
||||
m_state.saveSection(QStringLiteral("reading"), {
|
||||
{QStringLiteral("fontSize"), m_fontSize},
|
||||
{QStringLiteral("lineSpacing"), m_lineSpacing},
|
||||
{QStringLiteral("horizontalMargin"),m_horizontalMargin},
|
||||
{QStringLiteral("verticalMargin"), m_verticalMargin},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef READINGPREFERENCES_H
|
||||
#define READINGPREFERENCES_H
|
||||
|
||||
#include <QObject>
|
||||
#include "backend/AppState.h"
|
||||
|
||||
class ReadingPreferences : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
|
||||
Q_PROPERTY(qreal lineSpacing READ lineSpacing WRITE setLineSpacing NOTIFY lineSpacingChanged)
|
||||
Q_PROPERTY(int horizontalMargin READ horizontalMargin WRITE setHorizontalMargin NOTIFY horizontalMarginChanged)
|
||||
Q_PROPERTY(int verticalMargin READ verticalMargin WRITE setVerticalMargin NOTIFY verticalMarginChanged)
|
||||
public:
|
||||
explicit ReadingPreferences(AppState &state, QObject *parent = nullptr);
|
||||
|
||||
int fontSize() const;
|
||||
qreal lineSpacing() const;
|
||||
int horizontalMargin() const;
|
||||
int verticalMargin() const;
|
||||
|
||||
void setFontSize(int size);
|
||||
void setLineSpacing(qreal spacing);
|
||||
void setHorizontalMargin(int margin);
|
||||
void setVerticalMargin(int margin);
|
||||
|
||||
void save();
|
||||
|
||||
signals:
|
||||
void fontSizeChanged();
|
||||
void lineSpacingChanged();
|
||||
void horizontalMarginChanged();
|
||||
void verticalMarginChanged();
|
||||
|
||||
private:
|
||||
AppState &m_state;
|
||||
int m_fontSize = 16;
|
||||
qreal m_lineSpacing = 1.6;
|
||||
int m_horizontalMargin = 32;
|
||||
int m_verticalMargin = 8;
|
||||
};
|
||||
|
||||
#endif // READINGPREFERENCES_H
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "UiState.h"
|
||||
|
||||
UiState::UiState(AppState &state, QObject *parent)
|
||||
: QObject(parent), m_state(state)
|
||||
{
|
||||
const QVariantMap d = state.loadSection(QStringLiteral("ui"));
|
||||
m_themeName = d.value(QStringLiteral("themeName"), QStringLiteral("dark")).toString();
|
||||
m_lastPage = d.value(QStringLiteral("lastPage"), QStringLiteral("starter")).toString();
|
||||
m_sidebarOpen = d.value(QStringLiteral("sidebarOpen"), false).toBool();
|
||||
m_lastFilePath = d.value(QStringLiteral("lastFilePath"), QString()).toString();
|
||||
|
||||
const QVariantMap wg = state.loadSection(QStringLiteral("window"));
|
||||
m_width = wg.value(QStringLiteral("width"), 1024).toInt();
|
||||
m_height = wg.value(QStringLiteral("height"), 768).toInt();
|
||||
m_x = wg.value(QStringLiteral("x"), -1).toInt();
|
||||
m_y = wg.value(QStringLiteral("y"), -1).toInt();
|
||||
}
|
||||
|
||||
QString UiState::themeName() const { return m_themeName; }
|
||||
QString UiState::lastPage() const { return m_lastPage; }
|
||||
bool UiState::sidebarOpen() const { return m_sidebarOpen; }
|
||||
QString UiState::lastFilePath() const { return m_lastFilePath; }
|
||||
int UiState::windowWidth() const { return m_width; }
|
||||
int UiState::windowHeight() const { return m_height; }
|
||||
int UiState::windowX() const { return m_x; }
|
||||
int UiState::windowY() const { return m_y; }
|
||||
|
||||
void UiState::setThemeName(const QString &name)
|
||||
{
|
||||
if (m_themeName == name) return;
|
||||
m_themeName = name;
|
||||
emit themeNameChanged();
|
||||
}
|
||||
|
||||
void UiState::setLastPage(const QString &page)
|
||||
{
|
||||
if (m_lastPage == page) return;
|
||||
m_lastPage = page;
|
||||
emit lastPageChanged();
|
||||
}
|
||||
|
||||
void UiState::setSidebarOpen(bool open)
|
||||
{
|
||||
if (m_sidebarOpen == open) return;
|
||||
m_sidebarOpen = open;
|
||||
emit sidebarOpenChanged();
|
||||
}
|
||||
|
||||
void UiState::setLastFilePath(const QString &path)
|
||||
{
|
||||
if (m_lastFilePath == path) return;
|
||||
m_lastFilePath = path;
|
||||
emit lastFilePathChanged();
|
||||
}
|
||||
|
||||
void UiState::setWindowWidth(int w)
|
||||
{
|
||||
if (m_width == w) return;
|
||||
m_width = w;
|
||||
emit windowWidthChanged();
|
||||
}
|
||||
|
||||
void UiState::setWindowHeight(int h)
|
||||
{
|
||||
if (m_height == h) return;
|
||||
m_height = h;
|
||||
emit windowHeightChanged();
|
||||
}
|
||||
|
||||
void UiState::setWindowX(int x)
|
||||
{
|
||||
if (m_x == x) return;
|
||||
m_x = x;
|
||||
emit windowXChanged();
|
||||
}
|
||||
|
||||
void UiState::setWindowY(int y)
|
||||
{
|
||||
if (m_y == y) return;
|
||||
m_y = y;
|
||||
emit windowYChanged();
|
||||
}
|
||||
|
||||
void UiState::save()
|
||||
{
|
||||
m_state.saveSection(QStringLiteral("ui"), {
|
||||
{QStringLiteral("themeName"), m_themeName},
|
||||
{QStringLiteral("lastPage"), m_lastPage},
|
||||
{QStringLiteral("sidebarOpen"), m_sidebarOpen},
|
||||
{QStringLiteral("lastFilePath"), m_lastFilePath},
|
||||
});
|
||||
|
||||
m_state.saveSection(QStringLiteral("window"), {
|
||||
{QStringLiteral("width"), m_width},
|
||||
{QStringLiteral("height"), m_height},
|
||||
{QStringLiteral("x"), m_x},
|
||||
{QStringLiteral("y"), m_y},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#ifndef UISTATE_H
|
||||
#define UISTATE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include "backend/AppState.h"
|
||||
|
||||
class UiState : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString themeName READ themeName WRITE setThemeName NOTIFY themeNameChanged)
|
||||
Q_PROPERTY(QString lastPage READ lastPage WRITE setLastPage NOTIFY lastPageChanged)
|
||||
Q_PROPERTY(bool sidebarOpen READ sidebarOpen WRITE setSidebarOpen NOTIFY sidebarOpenChanged)
|
||||
Q_PROPERTY(QString lastFilePath READ lastFilePath WRITE setLastFilePath NOTIFY lastFilePathChanged)
|
||||
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 UiState(AppState &state, QObject *parent = nullptr);
|
||||
|
||||
QString themeName() const;
|
||||
QString lastPage() const;
|
||||
bool sidebarOpen() const;
|
||||
QString lastFilePath() const;
|
||||
int windowWidth() const;
|
||||
int windowHeight() const;
|
||||
int windowX() const;
|
||||
int windowY() const;
|
||||
|
||||
void setThemeName(const QString &name);
|
||||
void setLastPage(const QString &page);
|
||||
void setSidebarOpen(bool open);
|
||||
void setLastFilePath(const QString &path);
|
||||
void setWindowWidth(int w);
|
||||
void setWindowHeight(int h);
|
||||
void setWindowX(int x);
|
||||
void setWindowY(int y);
|
||||
|
||||
void save();
|
||||
|
||||
signals:
|
||||
void themeNameChanged();
|
||||
void lastPageChanged();
|
||||
void sidebarOpenChanged();
|
||||
void lastFilePathChanged();
|
||||
void windowWidthChanged();
|
||||
void windowHeightChanged();
|
||||
void windowXChanged();
|
||||
void windowYChanged();
|
||||
|
||||
private:
|
||||
AppState &m_state;
|
||||
QString m_themeName = QStringLiteral("dark");
|
||||
QString m_lastPage = QStringLiteral("starter");
|
||||
bool m_sidebarOpen = false;
|
||||
QString m_lastFilePath;
|
||||
int m_width = 1024;
|
||||
int m_height = 768;
|
||||
int m_x = -1;
|
||||
int m_y = -1;
|
||||
};
|
||||
|
||||
#endif // UISTATE_H
|
||||
Reference in new issue
Block a user