This commit is contained in:
Agel committed 2026-03-15 17:07:18 +01:00
commit 4d52c8cc60
6 files changed
+508

No files matched your search

+258
View File
@@ -0,0 +1,258 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
Asteria Qt is a cross-platform desktop book/document reader built with Qt6/C++. It is a reimplementation of [asteria-bookreader](../asteria-bookreader) (Electron/React) using a native Qt stack.
**Target platforms:** Windows and Linux (both required from day one); macOS planned
**Current dev environment:** Linux
**Feature tier:** Minimal reader — open, read, bookmarks
### Format Support
Initial implementation: **TXT, FB2, EPUB**
Full target format list (derived from actual library inventory):
| Format | Count | Priority | Approach |
|---|---|---|---|
| EPUB | ~2500 | initial | Unzip (QuaZip/minizip) + parse OPF spine + render HTML |
| FB2 | ~1900 | initial | `QXmlStreamReader` (pure XML) |
| TXT | ~170 | initial | `QTextStream` + encoding detection (uchardet/ICU) |
| PDF | ~520 | high | `QtPdf` module (`QPdfDocument`, Qt 6.4+) |
| DOC | ~330 | high | `libwpd` or antiword subprocess |
| RTF | ~260 | high | `QTextDocumentReader` or RTF→HTML |
| HTML/HTM | ~350 | high | `QTextDocument` / `QWebEngineView` |
| DOCX | ~65 | medium | Unzip + parse `word/document.xml` (`QXmlStreamReader`) |
| DJVU | ~60 | medium | `DjVuLibre` |
| ODT | ~5 | medium | Unzip + parse OpenDocument XML |
| CHM | ~5 | medium | `CHMLib` |
| PDB | small | low | Palm/Mobipocket format |
| ZIP/RAR | ~210 | low | Inspect contents; open book files within archive |
> ACSM files are Adobe DRM tokens — not directly readable; skip.
> PPT/XLS and source code files (.py, .cs, etc.) are in the library from programming books; no need to render them.
## Installing Dependencies
### Linux (Ubuntu/Debian)
```bash
sudo scripts/install-deps-ubuntu.sh
```
Installs: Qt6 dev packages (base, QML/Quick, PDF, SVG, shader tools), uchardet, QuaZip, minizip, DjVuLibre, libwpd.
> **Current status:** Qt6 runtime (6.4.2) is present but dev headers are not yet installed. Run the script above before attempting to build.
### Windows
Run in an elevated PowerShell:
```powershell
scripts\install-deps-windows.ps1
```
Installs: VS Build Tools 2022, CMake, Ninja, Git, vcpkg, and third-party libs via vcpkg. **Qt 6 must be installed manually** via the Qt Online Installer (qt.io/download-open-source) — select MSVC 2022 64-bit + Qt PDF + Qt SVG.
After Qt is installed, pass its path to CMake:
```powershell
cmake -B build -DCMAKE_PREFIX_PATH="C:\Qt\6.x.x\msvc2022_64" `
-DCMAKE_TOOLCHAIN_FILE="C:\vcpkg\scripts\buildsystems\vcpkg.cmake"
```
## Build
```bash
# Configure (Linux — Qt6 found automatically after running install script)
cmake -B build -DCMAKE_BUILD_TYPE=Debug
# Build
cmake --build build --parallel
# Run
./build/asteria-qt # Linux
./build/asteria-qt.exe # Windows
# Release
cmake -B build-release -DCMAKE_BUILD_TYPE=Release
cmake --build build-release --parallel
```
Tests:
```bash
ctest --test-dir build --output-on-failure
```
## Test-Driven Development
**All new backend logic must be test-driven.** Follow the red-green-refactor cycle strictly:
1. **Red** — write a failing test that specifies the desired behavior before writing any implementation
2. **Green** — write the minimum implementation to make the test pass
3. **Refactor** — clean up with tests still passing
### Rules
- Never write implementation code without a corresponding failing test first
- Tests live in `tests/` mirroring the `src/backend/` structure (e.g., `tests/formats/TxtReaderTest.cpp`)
- Use `Qt Test` (`QTest`) framework — include `find_package(Qt6 REQUIRED COMPONENTS Test)` in CMakeLists
- Each format reader must have a test that loads a real sample file from `tests/fixtures/`
- `BookController`, `ReadingModel`, `BookmarkModel`, `BookmarkStore`, and `AppState` must each have unit tests
- QML-only UI code is exempt from TDD; backend logic extracted to C++ is not
- Run `ctest --test-dir build --output-on-failure` before considering any task complete
## Architecture
**Backend (C++)** handles all file I/O and format parsing. **QML** handles all UI. They communicate via `QObject` models exposed to the QML engine via `setContextProperty()`.
### QObjects Exposed to QML
| Object | Responsibility |
|---|---|
| `BookController` | Open/close book, owns reader (via `ReaderFactory`), exposes metadata (title, author), TOC |
| `ReadingModel` | Current chapter content, position, next/prev navigation, progress % |
| `BookmarkModel` | Bookmark CRUD for the current book (wraps `BookmarkStore`) |
| `AppState` | Window geometry, last file, preferences (`QSettings`) |
Sidebar state, header state, etc. stay as pure QML properties — no C++ needed.
### Content Model
All format readers produce normalized HTML content organized into chapters. The content model is defined in `FormatReader.h`:
```cpp
struct TocEntry {
QString title;
int chapterIndex;
int depth; // 0 = top level
};
struct Chapter {
int index;
QString title;
QString contentHtml; // normalized HTML
};
```
`FormatReader` interface methods:
- `bool open(const QString &filePath)` — returns false on failure
- `QString lastError()` — error description after failed open
- `int chapterCount()`
- `Chapter loadChapter(int index)` — loads one chapter on demand
- `QList<TocEntry> tableOfContents()` — empty list if format has no TOC (e.g., TXT)
- `QString title()`, `QString author()` — metadata
Chapter mapping by format: TXT = 1 chapter (whole file). FB2 = `<section>` elements. EPUB = spine items. PDF = outline entries or individual pages.
`PageView.qml` renders one chapter at a time via `Text { textFormat: Text.RichText }`. Pagination within a chapter (if ever needed) is a QML-only concern.
### Reader Factory
`ReaderFactory` is separate from `FormatReader`. It provides a static `create(const QString &filePath)` method with a simple extension-to-reader map. No plugin system — a switch statement is fine for ~11 formats.
### Async Book Opening
`BookController::openBook(path)` runs reader creation + metadata loading in `QtConcurrent::run()`. Emits `bookReady()` or `bookError(QString)` when done. Chapter loading starts synchronous (single chapter from open zip is fast) and can go async later if profiling warrants it.
### Error Handling
`FormatReader::open()` returns `bool`. Failed readers expose `QString lastError()`. `BookController` translates errors to a `QString errorMessage` property + `error(QString)` signal. QML shows an `ErrorBanner.qml` component. Error cases: file not found, unsupported format, corrupt archive, encoding failure, permission denied.
### File Structure
```
src/
main.cpp
backend/
BookController.h/cpp # Open/close, metadata, TOC — exposed to QML
ReadingModel.h/cpp # Chapter content, position, navigation — exposed to QML
BookmarkModel.h/cpp # Bookmark CRUD — exposed to QML
BookmarkStore.h/cpp # Persistence layer (QSettings)
AppState.h/cpp # Window/session state (QSettings) — exposed to QML
FormatReader.h # Abstract interface + Chapter/TocEntry structs
ReaderFactory.h/cpp # Extension → reader mapping
formats/
TxtReader.h/cpp
Fb2Reader.h/cpp
EpubReader.h/cpp
PdfReader.h/cpp
RtfReader.h/cpp
DocReader.h/cpp
DocxReader.h/cpp
HtmlReader.h/cpp
DjvuReader.h/cpp
OdtReader.h/cpp
ChmReader.h/cpp
qml/
Theme.qml # Singleton — all color constants
Main.qml
components/
Header.qml
SideBar.qml # Renders TOC from BookController
PageView.qml # Renders chapter HTML from ReadingModel
Footer.qml
ErrorBanner.qml # Error display popup
pages/
StarterPage.qml
ReaderPage.qml
SettingsPage.qml
tests/
formats/
TxtReaderTest.cpp
Fb2ReaderTest.cpp
EpubReaderTest.cpp
BookControllerTest.cpp
ReadingModelTest.cpp
BookmarkModelTest.cpp
BookmarkStoreTest.cpp
AppStateTest.cpp
fixtures/
sample.txt
sample.fb2
sample.epub
```
### Cross-Platform
- Use only Qt APIs for filesystem, paths, and dialogs — no platform-specific code
- Third-party libs must be available on Windows and Linux; prefer those with CMake `find_package` or vcpkg support
- Avoid `#ifdef Q_OS_WIN` / `#ifdef Q_OS_LINUX` except where genuinely unavoidable
## Design & Color Scheme
Dark theme only. Derived from asteria-bookreader:
| Role | Color |
|---|---|
| Background (deepest) | `#010620` |
| Background (default) | `#01082b` |
| Surface / paper | `#191d35` |
| Header / drawer bg | `#14172c` |
| Footer bg | `#1a1e36` |
| Scrollbar track | `#131628` |
| Scrollbar thumb | `#36486a` |
| Primary accent | `#3a5688` |
| Button accent | `#5785c1` |
| Sidebar button text | `#98aaf4` |
| Reading body text | `#c4c8d8` |
| UI text (primary) | `#a2a5b4` |
| Header typography | `#8488aa` |
| Dim / clock text | `#71738c` |
| Counter text | `#606175` |
Sidebar drawer width: `240` logical pixels.
All colors are defined in `src/qml/Theme.qml` (a QML singleton registered via `qmldir`). All QML files reference colors as `Theme.surface`, `Theme.backgroundDefault`, etc. Do not hardcode hex values in QML components. Do not use Qt Quick Styles (Material/Universal) — the singleton gives full control over the custom theme.
## Qt Conventions
- **Qt6 only** — no Qt5 compat shims
- All UI in **QML**; C++ for logic only
- Expose C++ to QML via `rootContext()->setContextProperty()` or `QML_ELEMENT`
- `QSettings` for persistent state
- Qt naming: `camelCase` properties/methods, `PascalCase` QML component names
- `Q_INVOKABLE` for QML-callable methods; signals for C++→QML notifications