From 4d52c8cc60962930d5bfb1ede61d00417ac2dd53 Mon Sep 17 00:00:00 2001 From: Agel Date: Sun, 15 Mar 2026 17:07:18 +0100 Subject: [PATCH] Initial --- .claude/settings.local.json | 8 + .gitignore | 35 +++++ CLAUDE.md | 258 +++++++++++++++++++++++++++++++ CMakeLists.txt | 82 ++++++++++ scripts/install-deps-ubuntu.sh | 55 +++++++ scripts/install-deps-windows.ps1 | 70 +++++++++ 6 files changed, 508 insertions(+) create mode 100644 .claude/settings.local.json create mode 100644 .gitignore create mode 100644 CLAUDE.md create mode 100644 CMakeLists.txt create mode 100755 scripts/install-deps-ubuntu.sh create mode 100644 scripts/install-deps-windows.ps1 diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000..f7a7f5e --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,8 @@ +{ + "permissions": { + "allow": [ + "Bash(find:*)", + "Bash(grep -iv -E '^\\(png|jpg|JPG|gif|mp4|exe|cache|js|css|php|java|cs|properties|class|form|PAS|CPP\\)$')" + ] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aff27e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +# Build directories +build/ +build-*/ +cmake-build-*/ + +# CMake generated files +CMakeUserPresets.json + +# Qt Creator +*.user +*.user.* + +# IDE / editor +.idea/ +.vscode/ +*.swp +*.swo +*~ + +# Compiled objects +*.o +*.obj +*.moc +*.rcc +*.qmlc +*.jsc + +# Generated files +moc_*.cpp +qrc_*.cpp +ui_*.h + +# OS files +.DS_Store +Thumbs.db diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..9118c83 --- /dev/null +++ b/CLAUDE.md @@ -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 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 = `
` 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 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..20e320c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,82 @@ +cmake_minimum_required(VERSION 3.16) +project(asteria-qt VERSION 0.1.0 LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_AUTOMOC ON) + +# Qt6 components +find_package(Qt6 REQUIRED COMPONENTS + Core + Gui + Qml + Quick + Concurrent + Svg + Test +) +find_package(Qt6 OPTIONAL_COMPONENTS Pdf) + +# Third-party libraries +find_package(PkgConfig QUIET) +if(PkgConfig_FOUND) + pkg_check_modules(UCHARDET uchardet) +endif() + +# Source files +set(SOURCES + src/main.cpp +) + +# Executable +qt_add_executable(asteria-qt ${SOURCES}) + +target_include_directories(asteria-qt PRIVATE src) + +target_link_libraries(asteria-qt PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Qml + Qt6::Quick + Qt6::Concurrent + Qt6::Svg +) + +if(Qt6Pdf_FOUND) + target_link_libraries(asteria-qt PRIVATE Qt6::Pdf) + target_compile_definitions(asteria-qt PRIVATE HAS_QT_PDF) +endif() + +if(UCHARDET_FOUND) + target_include_directories(asteria-qt PRIVATE ${UCHARDET_INCLUDE_DIRS}) + target_link_libraries(asteria-qt PRIVATE ${UCHARDET_LIBRARIES}) + target_compile_definitions(asteria-qt PRIVATE HAS_UCHARDET) +endif() + +# QML module +qt_add_qml_module(asteria-qt + URI Asteria + VERSION 1.0 + QML_FILES + src/qml/Main.qml + RESOURCE_PREFIX / + NO_RESOURCE_TARGET_PATH +) + +# Testing +enable_testing() + +# Helper function to add tests +function(add_asteria_test TEST_NAME TEST_SOURCE) + qt_add_executable(${TEST_NAME} ${TEST_SOURCE}) + target_include_directories(${TEST_NAME} PRIVATE src) + target_link_libraries(${TEST_NAME} PRIVATE + Qt6::Core + Qt6::Test + ) + add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME}) +endfunction() + +# Tests will be added here as they are created, e.g.: +# add_asteria_test(TxtReaderTest tests/formats/TxtReaderTest.cpp) +# add_asteria_test(BookControllerTest tests/BookControllerTest.cpp) diff --git a/scripts/install-deps-ubuntu.sh b/scripts/install-deps-ubuntu.sh new file mode 100755 index 0000000..2f3a5e6 --- /dev/null +++ b/scripts/install-deps-ubuntu.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +# Install all development dependencies for asteria-qt on Ubuntu/Debian. +# Tested on Ubuntu 24.04. Run with sudo or as root. + +set -euo pipefail + +apt-get update + +# ── Build tools ──────────────────────────────────────────────────────────────── +apt-get install -y \ + build-essential \ + cmake \ + ninja-build \ + git + +# ── Qt 6 core development ────────────────────────────────────────────────────── +# qt6-base-dev : Core, Gui, Widgets, Network, SQL, ... +# qt6-declarative-* : QML / Qt Quick +# qt6-tools-* : CMake find_package integration, lupdate, lrelease +# qt6-shadertools : Required by Qt Quick renderer +# qt6-svg-dev : SVG icon support +apt-get install -y \ + qt6-base-dev \ + qt6-base-dev-tools \ + qt6-declarative-dev \ + qt6-declarative-dev-tools \ + qt6-tools-dev \ + qt6-tools-dev-tools \ + qt6-shadertools-dev \ + qt6-svg-dev + +# ── Qt 6 PDF (for PDF format support) ───────────────────────────────────────── +apt-get install -y qt6-pdf-dev + +# ── Encoding detection ───────────────────────────────────────────────────────── +apt-get install -y libuchardet-dev + +# ── ZIP extraction (EPUB, DOCX, ODT) ────────────────────────────────────────── +# QuaZip is the Qt-idiomatic wrapper; minizip is the underlying C lib. +apt-get install -y \ + libquazip1-qt6-dev \ + libminizip-dev + +# ── DjVu support ─────────────────────────────────────────────────────────────── +apt-get install -y libdjvulibre-dev + +# ── Legacy .doc support (libwpd) ─────────────────────────────────────────────── +apt-get install -y libwpd-dev + +echo "" +echo "All dependencies installed. Qt6 version:" +dpkg -l qt6-base-dev | awk '/^ii/{print $3}' +echo "" +echo "Verify CMake can find Qt6:" +echo " cmake -B /tmp/qt6-test -DCMAKE_PREFIX_PATH=/usr/lib/x86_64-linux-gnu/cmake" diff --git a/scripts/install-deps-windows.ps1 b/scripts/install-deps-windows.ps1 new file mode 100644 index 0000000..ab6e3ba --- /dev/null +++ b/scripts/install-deps-windows.ps1 @@ -0,0 +1,70 @@ +# Install all development dependencies for asteria-qt on Windows. +# Requires: winget (Windows 10 1709+) and an internet connection. +# Run in an elevated (Administrator) PowerShell session. +# +# After running this script you must add Qt to your PATH / CMAKE_PREFIX_PATH. +# The default Qt install path is: C:\Qt\6.x.x\msvc2022_64\ + +#Requires -RunAsAdministrator +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' + +# ── Build tools ──────────────────────────────────────────────────────────────── +Write-Host "Installing build tools..." -ForegroundColor Cyan + +# Visual Studio Build Tools 2022 (C++ compiler, MSBuild) +winget install --id Microsoft.VisualStudio.2022.BuildTools --silent --override ` + "--wait --quiet --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended" + +# CMake +winget install --id Kitware.CMake --silent + +# Ninja +winget install --id Ninja-build.Ninja --silent + +# Git +winget install --id Git.Git --silent + +# ── Qt 6 ─────────────────────────────────────────────────────────────────────── +Write-Host "" +Write-Host "Qt 6 cannot be installed via winget in a fully unattended way." -ForegroundColor Yellow +Write-Host "Install it manually using the Qt Online Installer:" -ForegroundColor Yellow +Write-Host " https://www.qt.io/download-open-source" -ForegroundColor Cyan +Write-Host "" +Write-Host "In the installer, select Qt 6.7+ and check these components:" -ForegroundColor Yellow +Write-Host " - MSVC 2022 64-bit" +Write-Host " - Qt PDF" +Write-Host " - Qt Quick / Qt Declarative (included by default)" +Write-Host " - Qt SVG" +Write-Host " - Qt Shader Tools" +Write-Host "" +Write-Host "After installing, set CMAKE_PREFIX_PATH in your build commands:" -ForegroundColor Yellow +Write-Host ' cmake -B build -DCMAKE_PREFIX_PATH="C:\Qt\6.7.0\msvc2022_64"' -ForegroundColor Cyan + +# ── vcpkg (for third-party C++ libs) ────────────────────────────────────────── +Write-Host "" +Write-Host "Installing vcpkg for third-party libraries..." -ForegroundColor Cyan + +$vcpkgDir = "C:\vcpkg" +if (-not (Test-Path $vcpkgDir)) { + git clone https://github.com/microsoft/vcpkg.git $vcpkgDir + & "$vcpkgDir\bootstrap-vcpkg.bat" -disableMetrics +} else { + Write-Host "vcpkg already present at $vcpkgDir" -ForegroundColor Green +} + +# ── Third-party libraries via vcpkg ─────────────────────────────────────────── +Write-Host "" +Write-Host "Installing third-party libraries via vcpkg (x64-windows)..." -ForegroundColor Cyan + +$vcpkg = "$vcpkgDir\vcpkg.exe" +& $vcpkg install ` + uchardet:x64-windows ` + quazip:x64-windows ` + minizip:x64-windows ` + djvulibre:x64-windows ` + libwpd:x64-windows + +Write-Host "" +Write-Host "Done. Add vcpkg toolchain to your CMake configure command:" -ForegroundColor Green +Write-Host " -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake" -ForegroundColor Cyan