Display all chapters

This commit is contained in:
Agel committed 2026-03-23 23:28:23 +01:00
1 parent d808f45346
commit 808ec3f787
4 files changed
+170 -159

No files matched your search

+35 -29
View File
@@ -1,5 +1,6 @@
#include "ReadingModel.h"
#include "FormatReader.h"
#include <algorithm>
#include <cmath>
ReadingModel::ReadingModel(QObject *parent)
@@ -32,7 +33,9 @@ QHash<int, QByteArray> ReadingModel::roleNames() const
QString ReadingModel::chapterTitle() const
{
return m_chapterTitle;
if (m_currentChapter < 0 || m_currentChapter >= m_chapterTitles.size())
return {};
return m_chapterTitles.at(m_currentChapter);
}
int ReadingModel::currentChapter() const
@@ -52,14 +55,11 @@ bool ReadingModel::hasChapters() const
double ReadingModel::progress() const
{
if (m_totalChapters == 0)
if (m_totalBookBytes == 0)
return 0.0;
double byteFraction = 0.0;
if (m_totalChapterBytes > 0)
byteFraction = qBound(0.0,
static_cast<double>(m_chunkByteOffsets.value(m_topChunkIndex)) / m_totalChapterBytes,
1.0);
return (m_currentChapter + byteFraction) / m_totalChapters;
return qBound(0.0,
static_cast<double>(m_chunkByteOffsets.value(m_topChunkIndex)) / m_totalBookBytes,
1.0);
}
int ReadingModel::currentPage() const
@@ -79,6 +79,15 @@ void ReadingModel::setScrollInfo(int topChunkIndex, qreal /*offsetInChunk*/, boo
m_topChunkIndex = topChunkIndex;
m_atYEnd = atYEnd;
// Derive current chapter from global chunk index
auto it = std::upper_bound(m_chapterStartIndices.begin(), m_chapterStartIndices.end(), topChunkIndex);
int chapter = std::max(0, static_cast<int>(std::distance(m_chapterStartIndices.begin(), it)) - 1);
if (chapter != m_currentChapter) {
m_currentChapter = chapter;
emit chapterChanged();
}
recalculateCurrentPage();
}
@@ -93,7 +102,7 @@ void ReadingModel::computeChunkBytes()
m_chunkBytes[i] = sz;
offset += sz;
}
m_totalChapterBytes = offset;
m_totalBookBytes = offset;
}
void ReadingModel::recalculateCurrentPage()
@@ -109,24 +118,30 @@ void ReadingModel::recalculateCurrentPage()
emit currentPageChanged();
}
void ReadingModel::loadChapterData(int index)
void ReadingModel::loadAllChapters()
{
beginResetModel();
m_chunks.clear();
m_chapterTitle.clear();
m_chapterStartIndices.clear();
m_chapterTitles.clear();
m_chunkBytes.clear();
m_chunkByteOffsets.clear();
m_totalChapterBytes = 0;
m_totalBookBytes = 0;
m_topChunkIndex = 0;
m_atYEnd = false;
m_currentChapter = 0;
if (m_currentPage != 0) { m_currentPage = 0; emit currentPageChanged(); }
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
if (m_reader && index >= 0 && index < m_totalChapters) {
auto chapter = m_reader->loadChapter(index);
m_chapterTitle = chapter.title;
m_chunks = chapter.contentChunks;
if (m_reader) {
for (int i = 0; i < m_totalChapters; ++i) {
auto chapter = m_reader->loadChapter(i);
m_chapterStartIndices.append(m_chunks.size());
m_chapterTitles.append(chapter.title);
m_chunks.append(chapter.contentChunks);
}
}
endResetModel();
@@ -145,7 +160,7 @@ void ReadingModel::setReader(FormatReader *reader)
m_currentChapter = 0;
m_totalChapters = reader ? reader->chapterCount() : 0;
loadChapterData(0);
loadAllChapters();
emit totalChaptersChanged();
emit chapterChanged();
@@ -155,28 +170,19 @@ void ReadingModel::nextChapter()
{
if (!m_reader || m_currentChapter >= m_totalChapters - 1)
return;
m_currentChapter++;
loadChapterData(m_currentChapter);
emit chapterChanged();
emit scrollToChunkRequested(m_chapterStartIndices.at(m_currentChapter + 1));
}
void ReadingModel::previousChapter()
{
if (!m_reader || m_currentChapter <= 0)
return;
m_currentChapter--;
loadChapterData(m_currentChapter);
emit chapterChanged();
emit scrollToChunkRequested(m_chapterStartIndices.at(m_currentChapter - 1));
}
void ReadingModel::goToChapter(int index)
{
if (!m_reader || index < 0 || index >= m_totalChapters)
return;
m_currentChapter = index;
loadChapterData(m_currentChapter);
emit chapterChanged();
emit scrollToChunkRequested(m_chapterStartIndices.at(index));
}
+8 -6
View File
@@ -13,7 +13,7 @@ class ReadingModel : public QAbstractListModel {
Q_PROPERTY(QString chapterTitle READ chapterTitle NOTIFY chapterChanged)
Q_PROPERTY(int currentChapter READ currentChapter NOTIFY chapterChanged)
Q_PROPERTY(int totalChapters READ totalChapters NOTIFY totalChaptersChanged)
Q_PROPERTY(double progress READ progress NOTIFY chapterChanged)
Q_PROPERTY(double progress READ progress NOTIFY currentPageChanged)
Q_PROPERTY(int currentPage READ currentPage NOTIFY currentPageChanged)
Q_PROPERTY(int totalPages READ totalPages NOTIFY totalPagesChanged)
Q_PROPERTY(bool hasChapters READ hasChapters NOTIFY totalChaptersChanged)
@@ -51,9 +51,10 @@ signals:
void totalChaptersChanged();
void currentPageChanged();
void totalPagesChanged();
void scrollToChunkRequested(int chunkIndex);
private:
void loadChapterData(int index);
void loadAllChapters();
void computeChunkBytes();
void recalculateCurrentPage();
@@ -66,11 +67,12 @@ private:
bool m_atYEnd = false;
QStringList m_chunks;
QVector<qsizetype> m_chunkBytes; // UTF-8 byte size of each chunk
QVector<qsizetype> m_chunkByteOffsets; // prefix sums: offset[i] = sum of bytes[0..i-1]
qsizetype m_totalChapterBytes = 0;
QVector<int> m_chapterStartIndices; // m_chapterStartIndices[i] = first global chunk index of chapter i
QVector<QString> m_chapterTitles; // title of each chapter
QString m_chapterTitle;
QVector<qsizetype> m_chunkBytes; // UTF-8 byte size of each chunk (all chapters)
QVector<qsizetype> m_chunkByteOffsets; // prefix sums: offset[i] = sum of bytes[0..i-1]
qsizetype m_totalBookBytes = 0;
};
#endif // READINGMODEL_H
+7 -4
View File
@@ -104,13 +104,9 @@ Rectangle {
function onBookReady() {
_restoring = true
var pos = appState.loadBookPosition(bookController.currentFilePath)
var savedChapter = pos["chapter"]
var savedScrollIndex = pos["scrollIndex"]
var savedScrollOffset = pos["scrollOffset"]
if (savedChapter > 0)
readingModel.goToChapter(savedChapter)
Qt.callLater(function() {
if (savedScrollIndex > 0) {
chunkListView.positionViewAtIndex(savedScrollIndex, ListView.Beginning)
@@ -127,4 +123,11 @@ Rectangle {
}
}
Connections {
target: readingModel
function onScrollToChunkRequested(chunkIndex) {
chunkListView.positionViewAtIndex(chunkIndex, ListView.Beginning)
}
}
}
+120 -120
View File
@@ -14,6 +14,7 @@ public:
QString author() override { return {}; }
};
// 3 chapters, 2 chunks each → 6 total chunks
class FakeReader : public FormatReader {
public:
bool open(const QString &) override { return true; }
@@ -48,38 +49,40 @@ private slots:
QCOMPARE(model.rowCount(QModelIndex()), 0);
}
void setReaderLoadsFirstChapter()
void setReaderLoadsAllChapters()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QCOMPARE(model.currentChapter(), 0);
// All 3 chapters × 2 chunks = 6 total rows
QCOMPARE(model.rowCount(QModelIndex()), 6);
QCOMPARE(model.totalChapters(), 3);
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0"));
}
void rowCountMatchesChunks()
void chunkContentAcrossChapters()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QCOMPARE(model.rowCount(QModelIndex()), 2);
}
void dataReturnsChunkContent()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QModelIndex idx0 = model.index(0, 0);
QModelIndex idx1 = model.index(1, 0);
QCOMPARE(model.data(idx0, ReadingModel::ContentRole).toString(),
// Chapter 0: chunks 0,1
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 0</p>"));
QCOMPARE(model.data(idx1, ReadingModel::ContentRole).toString(),
QCOMPARE(model.data(model.index(1, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk B of 0</p>"));
// Chapter 1: chunks 2,3
QCOMPARE(model.data(model.index(2, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 1</p>"));
QCOMPARE(model.data(model.index(3, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk B of 1</p>"));
// Chapter 2: chunks 4,5
QCOMPARE(model.data(model.index(4, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 2</p>"));
QCOMPARE(model.data(model.index(5, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk B of 2</p>"));
}
void dataInvalidForOutOfRange()
@@ -92,123 +95,146 @@ private slots:
QVERIFY(!model.data(badIdx, ReadingModel::ContentRole).isValid());
}
void nextChapter()
void currentChapterDerivedFromScroll()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.nextChapter();
QCOMPARE(model.currentChapter(), 0);
// Chunk 2 = first chunk of chapter 1
model.setScrollInfo(2, 0, false);
QCOMPARE(model.currentChapter(), 1);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 1"));
QCOMPARE(spy.count(), 1);
QCOMPARE(model.rowCount(QModelIndex()), 2);
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 1</p>"));
// Chunk 4 = first chunk of chapter 2
model.setScrollInfo(4, 0, false);
QCOMPARE(model.currentChapter(), 2);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 2"));
// Back to chapter 0
model.setScrollInfo(0, 0, false);
QCOMPARE(model.currentChapter(), 0);
}
void nextChapterAtEnd()
void chapterChangedSignalOnScroll()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.goToChapter(2);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.nextChapter();
QCOMPARE(model.currentChapter(), 2);
model.setScrollInfo(0, 0, false); // still chapter 0 — no signal
QCOMPARE(spy.count(), 0);
model.setScrollInfo(2, 0, false); // chapter 1 → signal
QCOMPARE(spy.count(), 1);
model.setScrollInfo(3, 0, false); // still chapter 1 — no signal
QCOMPARE(spy.count(), 1);
model.setScrollInfo(4, 0, false); // chapter 2 → signal
QCOMPARE(spy.count(), 2);
}
void previousChapter()
void goToChapterEmitsScrollSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.goToChapter(1);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.previousChapter();
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0"));
QCOMPARE(spy.count(), 1);
}
void previousChapterAtStart()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.previousChapter();
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(spy.count(), 0);
}
void goToChapter()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QCOMPARE(spy.at(0).at(0).toInt(), 2); // chapter 1 starts at chunk 2
model.goToChapter(2);
QCOMPARE(model.currentChapter(), 2);
QCOMPARE(model.rowCount(QModelIndex()), 2);
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 2</p>"));
QCOMPARE(spy.count(), 2);
QCOMPARE(spy.at(1).at(0).toInt(), 4); // chapter 2 starts at chunk 4
}
void goToChapterOutOfRange()
void goToChapterOutOfRangeNoSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.goToChapter(-1);
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(spy.count(), 0);
model.goToChapter(99);
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(spy.count(), 0);
}
void progressCalculation()
void nextChapterEmitsScrollSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
// Chapter 0 of 3, at chunk 0 → byte offset 0 → fraction 0 → 0/3 = 0.0
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.nextChapter(); // chapter 0 → 1
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).at(0).toInt(), 2);
}
void nextChapterAtEndNoSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(4, 0, false); // chapter 2
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.nextChapter();
QCOMPARE(spy.count(), 0);
}
void previousChapterEmitsScrollSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(4, 0, false); // chapter 2
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.previousChapter();
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).at(0).toInt(), 2); // chapter 1 starts at chunk 2
}
void previousChapterAtStartNoSignal()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::scrollToChunkRequested);
model.previousChapter();
QCOMPARE(spy.count(), 0);
}
void progressIsBookWide()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
// At chunk 0: progress = 0
QCOMPARE(model.progress(), 0.0);
model.goToChapter(2);
// Chapter 2 of 3, at chunk 0 → fraction 0 → 2/3
QCOMPARE(model.progress(), 2.0 / 3.0);
}
// At chunk 3 (half-way): progress ~0.5
model.setScrollInfo(3, 0, false);
double p = model.progress();
QVERIFY(p > 0.0);
QVERIFY(p < 1.0);
void progressUsesBytes()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader); // chapter 0: 2 equal-ish chunks
// At chunk 0: fraction = 0 (byteOffset[0] = 0)
double p0 = model.progress();
QCOMPARE(p0, 0.0);
// Scroll to chunk 1: fraction = bytes[0] / totalBytes > 0
model.setScrollInfo(1, 0, false);
double p1 = model.progress();
QVERIFY(p1 > 0.0);
QVERIFY(p1 < 1.0 / 3.0); // still in chapter 0 of 3
// At chunk 0 again: progress = 0
model.setScrollInfo(0, 0, false);
QCOMPARE(model.progress(), 0.0);
}
void setReaderNull()
@@ -224,20 +250,6 @@ private slots:
QCOMPARE(model.rowCount(QModelIndex()), 0);
}
void navigationResetsModel()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QCOMPARE(model.rowCount(QModelIndex()), 2);
model.nextChapter();
QCOMPARE(model.rowCount(QModelIndex()), 2);
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
QStringLiteral("<p>Chunk A of 1</p>"));
}
void roleNamesContainsContent()
{
ReadingModel model;
@@ -253,28 +265,13 @@ private slots:
QCOMPARE(model.totalPages(), 0);
}
void chapterLoadSetsPages()
{
// Pages are set immediately on chapter load (totalPages = chunk count)
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QCOMPARE(model.totalPages(), 2); // FakeReader returns 2 chunks
QCOMPARE(model.currentPage(), 1);
}
void chapterLoadResetsPages()
void totalPagesEqualsAllChunks()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setScrollInfo(1, 0, false);
QCOMPARE(model.currentPage(), 2);
model.goToChapter(1);
// After chapter change, pages reset to new chapter's chunk count and position 1
QCOMPARE(model.totalPages(), 2);
QCOMPARE(model.totalPages(), 6); // 3 chapters × 2 chunks
QCOMPARE(model.currentPage(), 1);
}
@@ -282,13 +279,16 @@ private slots:
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader); // 2 chunks
model.setReader(&reader);
model.setScrollInfo(0, 0, false);
QCOMPARE(model.currentPage(), 1);
model.setScrollInfo(1, 0, false);
QCOMPARE(model.currentPage(), 2);
model.setScrollInfo(3, 0, false);
QCOMPARE(model.currentPage(), 4);
model.setScrollInfo(5, 0, false);
QCOMPARE(model.currentPage(), 6);
}
void atYEndGivesLastPage()
@@ -313,7 +313,7 @@ private slots:
void hasChaptersTrueForMultipleChapters()
{
ReadingModel model;
FakeReader reader; // 3 chapters
FakeReader reader;
model.setReader(&reader);
QVERIFY(model.hasChapters());