Some refactor
This commit is contained in:
37 files changed
+1599
-981
No files matched your search
+131
-39
@@ -1,4 +1,5 @@
|
||||
#include <QApplication>
|
||||
#include <QDateTime>
|
||||
#include <QFileInfo>
|
||||
#include <QSize>
|
||||
#include <QQmlApplicationEngine>
|
||||
@@ -8,12 +9,17 @@
|
||||
#include <QSettings>
|
||||
#include <QSGRendererInterface>
|
||||
|
||||
#include "backend/BookController.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
#include "backend/ReadingModel.h"
|
||||
#include "backend/AppState.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
#include "backend/ImageLoaderFactory.h"
|
||||
#include "backend/ImageProviderFactory.h"
|
||||
#include "backend/LibraryModel.h"
|
||||
#include "backend/ReadingModel.h"
|
||||
#include "backend/NativeFileDialogService.h"
|
||||
#include "backend/book/Book.h"
|
||||
#include "backend/state/BookPositionStore.h"
|
||||
#include "backend/state/ReadingPreferences.h"
|
||||
#include "backend/state/UiState.h"
|
||||
|
||||
#ifdef HAS_QT_DBUS
|
||||
#include <QDBusConnection>
|
||||
@@ -80,50 +86,147 @@ int main(int argc, char *argv[])
|
||||
|
||||
QQuickStyle::setStyle(QStringLiteral("Basic"));
|
||||
|
||||
ReadingModel readingModel;
|
||||
// --- Object graph (construction order = dependency order) ---
|
||||
QSettings settings;
|
||||
AppState appState(settings);
|
||||
|
||||
BookController bookController;
|
||||
bookController.setReadingModel(&readingModel);
|
||||
ReadingPreferences readingPreferences(appState);
|
||||
UiState uiState(appState);
|
||||
BookPositionStore bookPositions(appState);
|
||||
|
||||
AppState appState;
|
||||
appState.restore();
|
||||
bookController.setAppState(&appState);
|
||||
LibraryModel libraryModel;
|
||||
for (const QVariantMap &entry : appState.loadArray(QStringLiteral("library"))) {
|
||||
const QString fp = entry.value(QStringLiteral("filePath")).toString();
|
||||
if (fp.isEmpty()) continue;
|
||||
double progress = entry.value(QStringLiteral("progress"), 0.0).toDouble();
|
||||
progress = qBound(0.0, progress, 1.0);
|
||||
const QString lo = entry.value(QStringLiteral("lastOpened")).toString();
|
||||
libraryModel.addOrUpdateBook(fp, progress, lo);
|
||||
}
|
||||
|
||||
// Command-line override: treat first argument as the file to open on startup
|
||||
if (const QStringList args = QCoreApplication::arguments(); args.size() > 1) {
|
||||
const QString &argPath = args.at(1);
|
||||
if (QFileInfo::exists(argPath))
|
||||
uiState.setLastFilePath(argPath);
|
||||
}
|
||||
|
||||
#if defined(HAS_QT_DBUS) && defined(Q_OS_LINUX)
|
||||
IFileDialogService *fileDialogService;
|
||||
if (QDBusConnection::sessionBus().isConnected()) {
|
||||
fileDialogService = new PortalFileDialogService(&bookController);
|
||||
fileDialogService = new PortalFileDialogService(nullptr);
|
||||
} else {
|
||||
fileDialogService = new NativeFileDialogService(&bookController);
|
||||
fileDialogService = new NativeFileDialogService(nullptr);
|
||||
}
|
||||
#else
|
||||
IFileDialogService *fileDialogService = new NativeFileDialogService(&bookController);
|
||||
IFileDialogService *fileDialogService = new NativeFileDialogService(nullptr);
|
||||
#endif
|
||||
bookController.setFileDialogService(fileDialogService);
|
||||
|
||||
if (const QStringList args = QCoreApplication::arguments(); args.size() > 1) {
|
||||
const QString& filePath = args.at(1);
|
||||
if (QFileInfo::exists(filePath))
|
||||
appState.setLastFilePath(filePath);
|
||||
}
|
||||
Book book(*fileDialogService, bookPositions);
|
||||
ReadingModel readingModel(bookPositions);
|
||||
|
||||
// --- QML engine ---
|
||||
QQmlApplicationEngine engine;
|
||||
engine.rootContext()->setContextProperty("isDebugBuild",
|
||||
#ifdef QT_DEBUG
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("isDebugBuild"),
|
||||
#ifdef QT_DEBUG
|
||||
true
|
||||
#else
|
||||
#else
|
||||
false
|
||||
#endif
|
||||
#endif
|
||||
);
|
||||
|
||||
bookController.setImageProviderRegistrar([&engine](const QString &scheme, IImageProvider *provider) {
|
||||
engine.removeImageProvider(scheme);
|
||||
engine.addImageProvider(scheme, provider);
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("book"), &book);
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("readingModel"), &readingModel);
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("readingPreferences"), &readingPreferences);
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("uiState"), &uiState);
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("libraryModel"), &libraryModel);
|
||||
|
||||
// --- Cross-class signal wiring ---
|
||||
|
||||
// Typography changes → re-push font params into ReadingModel
|
||||
QObject::connect(&readingPreferences, &ReadingPreferences::fontSizeChanged, [&]{
|
||||
readingModel.setFontParams(readingPreferences.fontSize(), readingPreferences.lineSpacing());
|
||||
});
|
||||
QObject::connect(&readingPreferences, &ReadingPreferences::lineSpacingChanged, [&]{
|
||||
readingModel.setFontParams(readingPreferences.fontSize(), readingPreferences.lineSpacing());
|
||||
});
|
||||
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("bookController"), &bookController);
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("readingModel"), &readingModel);
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("appState"), &appState);
|
||||
// Track the current image provider so setContentWidth can reach it
|
||||
IImageProvider *currentImageProvider = nullptr;
|
||||
|
||||
// Before a new book open: snapshot the outgoing book's progress into the library.
|
||||
// Scroll position is handled by PageView's scroll tracker via book.saveCurrentPosition().
|
||||
QObject::connect(&book, &Book::willOpenNewBook, [&]{
|
||||
if (book.hasBook())
|
||||
libraryModel.addOrUpdateBook(book.filePath(), readingModel.progress());
|
||||
});
|
||||
|
||||
// After the new reader is installed: wire up image provider + ReadingModel
|
||||
QObject::connect(&book, &Book::bookChanged, [&]{
|
||||
currentImageProvider = nullptr;
|
||||
|
||||
if (!book.hasBook()) {
|
||||
readingModel.setReader(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
FormatReader *reader = book.reader();
|
||||
const QString filePath = reader->filePath();
|
||||
|
||||
IImageProvider *provider = ImageProviderFactory::create(filePath);
|
||||
if (provider) {
|
||||
engine.removeImageProvider(provider->scheme());
|
||||
engine.addImageProvider(provider->scheme(), provider);
|
||||
provider->setFilePath(filePath);
|
||||
currentImageProvider = provider;
|
||||
|
||||
if (!reader->coverImagePath().isEmpty())
|
||||
book.setCoverImageUrl(provider->coverImageUrl(reader->coverImagePath()));
|
||||
}
|
||||
|
||||
readingModel.setFontParams(readingPreferences.fontSize(), readingPreferences.lineSpacing());
|
||||
readingModel.setImageLoader(ImageLoaderFactory::create(filePath));
|
||||
readingModel.setCoverImageUrl(book.coverImageUrl());
|
||||
readingModel.setReader(reader);
|
||||
readingModel.measure(true);
|
||||
|
||||
uiState.setLastFilePath(filePath);
|
||||
libraryModel.addOrUpdateBook(filePath, -1.0);
|
||||
});
|
||||
|
||||
// Propagate content-width changes to image provider + reading model.
|
||||
// Called from Book::setContentWidth which is triggered by QML's PageView.
|
||||
QObject::connect(&book, &Book::contentWidthChanged, [&](int width){
|
||||
if (currentImageProvider)
|
||||
currentImageProvider->setDisplayWidth(width);
|
||||
readingModel.setContentWidth(width);
|
||||
});
|
||||
|
||||
// Quit: flush everything to persistent storage.
|
||||
// Scroll position was last saved by PageView's scroll tracker; we save library progress here.
|
||||
QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]{
|
||||
if (book.hasBook()) {
|
||||
libraryModel.addOrUpdateBook(book.filePath(), readingModel.progress());
|
||||
uiState.setLastFilePath(book.filePath());
|
||||
} else {
|
||||
uiState.setLastFilePath(QString());
|
||||
}
|
||||
|
||||
readingPreferences.save();
|
||||
uiState.save();
|
||||
|
||||
QList<QVariantMap> items;
|
||||
for (int i = 0; i < libraryModel.rowCount(); ++i) {
|
||||
items.append({
|
||||
{QStringLiteral("filePath"), libraryModel.filePath(i)},
|
||||
{QStringLiteral("progress"), libraryModel.progress(i)},
|
||||
{QStringLiteral("lastOpened"), libraryModel.lastOpened(i)},
|
||||
});
|
||||
}
|
||||
appState.saveArray(QStringLiteral("library"), items);
|
||||
|
||||
qInfo() << "Settings saved to:" << settings.fileName();
|
||||
});
|
||||
|
||||
const QUrl url(QStringLiteral("qrc:/Asteria/Main.qml"));
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
@@ -148,16 +251,5 @@ int main(int argc, char *argv[])
|
||||
qInfo("Rendering API: %s", apiName);
|
||||
}
|
||||
|
||||
QObject::connect(&app, &QCoreApplication::aboutToQuit, [&bookController, &appState, &readingModel]() {
|
||||
if (bookController.hasBook()) {
|
||||
appState.setLastFilePath(bookController.currentFilePath());
|
||||
appState.addOrUpdateLibraryBook(bookController.currentFilePath(),
|
||||
readingModel.progress());
|
||||
} else {
|
||||
appState.setLastFilePath(QString());
|
||||
}
|
||||
appState.save();
|
||||
});
|
||||
|
||||
return QApplication::exec();
|
||||
}
|
||||
Reference in new issue
Block a user