Refactor using Fable
This commit is contained in:
90 files changed
+2080
-2267
No files matched your search
+36
-117
@@ -9,25 +9,25 @@
|
||||
#include <QSettings>
|
||||
#include <QSGRendererInterface>
|
||||
|
||||
#include "backend/AppCoordinator.h"
|
||||
#include "backend/AppState.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
#include "backend/formats/ImageLoaderFactory.h"
|
||||
#include "backend/formats/ImageProviderFactory.h"
|
||||
#include "backend/formats/EpubImageProvider.h"
|
||||
#include "backend/formats/FileIconProvider.h"
|
||||
#include "backend/formats/FormatRegistry.h"
|
||||
#include "backend/formats/WebImageProvider.h"
|
||||
#include "backend/formats/sources/DirByteSource.h"
|
||||
#include "backend/formats/sources/Fb2ByteSource.h"
|
||||
#include "backend/formats/sources/RarByteSource.h"
|
||||
#include "backend/formats/sources/ZipByteSource.h"
|
||||
#include "backend/HistoryModel.h"
|
||||
#include "backend/ReadingModel.h"
|
||||
#include "backend/NativeFileDialogService.h"
|
||||
#include "backend/formats/ReaderFactory.h"
|
||||
#include "backend/book/Book.h"
|
||||
#include "backend/formats/TxtReader.h"
|
||||
#include "backend/formats/EpubReader.h"
|
||||
#include "backend/formats/Fb2Reader.h"
|
||||
#include "backend/formats/Fb2ImageProvider.h"
|
||||
#include "backend/formats/comic/Cbz.h"
|
||||
#include "backend/formats/comic/Cbr.h"
|
||||
#include "backend/formats/comic/Folder.h"
|
||||
#include "backend/formats/comic/CbzReader.h"
|
||||
#include "backend/formats/comic/CbrReader.h"
|
||||
#include "backend/formats/comic/FolderReader.h"
|
||||
#include "backend/state/BookPositionStore.h"
|
||||
#include "backend/state/ReadingPreferences.h"
|
||||
#include "backend/state/UiState.h"
|
||||
@@ -105,16 +105,29 @@ int main(int argc, char *argv[])
|
||||
UiState uiState(appState);
|
||||
BookPositionStore bookPositions(appState);
|
||||
|
||||
ReaderFactory readerFactory({
|
||||
[] { return std::make_unique<TxtReader>(); },
|
||||
[] { return std::make_unique<EpubReader>(); },
|
||||
[] { return std::make_unique<Fb2Reader>(); },
|
||||
[] { return std::make_unique<CbzReader>(); },
|
||||
[] { return std::make_unique<CbrReader>(); },
|
||||
[] { return std::make_unique<FolderReader>(); },
|
||||
// The format table: one row declares everything a format needs.
|
||||
FormatRegistry formatRegistry({
|
||||
{QStringLiteral("txt"), false, {},
|
||||
[] { return std::make_unique<TxtReader>(); }, nullptr},
|
||||
{QStringLiteral("epub"), false, QStringLiteral("epub"),
|
||||
[] { return std::make_unique<EpubReader>(); },
|
||||
[] { return std::make_unique<ZipByteSource>(); },
|
||||
/*loaderIncludesWeb=*/true},
|
||||
{QStringLiteral("fb2"), false, QStringLiteral("fb2"),
|
||||
[] { return std::make_unique<Fb2Reader>(); },
|
||||
[] { return std::make_unique<Fb2ByteSource>(); }},
|
||||
{QStringLiteral("cbz"), false, QStringLiteral("cbz"),
|
||||
[] { return std::make_unique<CbzReader>(); },
|
||||
[] { return std::make_unique<ZipByteSource>(); }},
|
||||
{QStringLiteral("cbr"), false, QStringLiteral("cbr"),
|
||||
[] { return std::make_unique<CbrReader>(); },
|
||||
[] { return std::make_unique<RarByteSource>(); }},
|
||||
{{}, /*matchesDirectory=*/true, QStringLiteral("dir"),
|
||||
[] { return std::make_unique<FolderReader>(); },
|
||||
[] { return std::make_unique<DirByteSource>(); }},
|
||||
});
|
||||
|
||||
HistoryModel historyModel(readerFactory);
|
||||
HistoryModel historyModel(formatRegistry);
|
||||
for (const QVariantMap &entry : appState.loadArray(QStringLiteral("history"))) {
|
||||
const QString fp = entry.value(QStringLiteral("filePath")).toString();
|
||||
if (fp.isEmpty()) continue;
|
||||
@@ -142,18 +155,14 @@ int main(int argc, char *argv[])
|
||||
IFileDialogService *fileDialogService = new NativeFileDialogService(nullptr);
|
||||
#endif
|
||||
|
||||
Book book(*fileDialogService, bookPositions, readerFactory);
|
||||
Book book(*fileDialogService, bookPositions, formatRegistry);
|
||||
ReadingModel readingModel(bookPositions);
|
||||
|
||||
// --- QML engine ---
|
||||
QQmlApplicationEngine engine;
|
||||
engine.addImageProvider(QStringLiteral("web"), new WebImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("epub"), new EpubImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("fb2"), new Fb2ImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("cbz"), new CbzImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("cbr"), new CbrImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("dir"), new FolderImageProvider());
|
||||
engine.addImageProvider(QStringLiteral("fileicon"), new FileIconProvider());
|
||||
formatRegistry.registerImageProviders(engine);
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("isDebugBuild"),
|
||||
#ifdef QT_DEBUG
|
||||
true
|
||||
@@ -168,100 +177,10 @@ int main(int argc, char *argv[])
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("uiState"), &uiState);
|
||||
engine.rootContext()->setContextProperty(QStringLiteral("historyModel"), &historyModel);
|
||||
|
||||
// --- 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());
|
||||
});
|
||||
|
||||
// Track the current image provider and content width so both stay in sync.
|
||||
IImageProvider *currentImageProvider = nullptr;
|
||||
int currentContentWidth = 0;
|
||||
|
||||
// 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())
|
||||
historyModel.addOrUpdateBook(book.filePath(), readingModel.progress());
|
||||
});
|
||||
|
||||
// Real-time history update: fired by PageView's scrollTrackTimer on every scroll.
|
||||
QObject::connect(&book, &Book::positionSaved, [&]{
|
||||
historyModel.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);
|
||||
provider->setDisplayWidth(currentContentWidth);
|
||||
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);
|
||||
historyModel.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){
|
||||
currentContentWidth = 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()) {
|
||||
historyModel.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 < historyModel.rowCount(); ++i) {
|
||||
items.append({
|
||||
{QStringLiteral("filePath"), historyModel.filePath(i)},
|
||||
{QStringLiteral("progress"), historyModel.progress(i)},
|
||||
{QStringLiteral("lastOpened"), historyModel.lastOpened(i)},
|
||||
});
|
||||
}
|
||||
appState.saveArray(QStringLiteral("history"), items);
|
||||
|
||||
qInfo() << "Settings saved to:" << settings.fileName();
|
||||
});
|
||||
// All cross-class signal wiring lives in the coordinator.
|
||||
AppCoordinator coordinator(book, readingModel, historyModel, uiState,
|
||||
readingPreferences, appState, settings,
|
||||
formatRegistry, engine);
|
||||
|
||||
const QUrl url(QStringLiteral("qrc:/Asteria/Main.qml"));
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
|
||||
Reference in new issue
Block a user