fix page counter
This commit is contained in:
13 files changed
+310
-122
No files matched your search
@@ -78,6 +78,39 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class VariableCharsTextReader : public FormatReader {
|
||||
public:
|
||||
explicit VariableCharsTextReader(const QVector<int> &charsPerChunk)
|
||||
{
|
||||
m_chunks.reserve(charsPerChunk.size());
|
||||
for (int i = 0; i < charsPerChunk.size(); ++i) {
|
||||
ContentChunk chunk;
|
||||
chunk.sectionHref = QStringLiteral("sec_%1.xhtml").arg(i);
|
||||
chunk.charCount = charsPerChunk[i];
|
||||
BlockItem p;
|
||||
p.kind = BlockItem::Kind::Paragraph;
|
||||
InlineItem t;
|
||||
t.kind = InlineItem::Kind::Text;
|
||||
t.text = QString(charsPerChunk[i], QLatin1Char('A'));
|
||||
p.inlines.append(t);
|
||||
chunk.blocks.append(p);
|
||||
m_chunks.append(chunk);
|
||||
}
|
||||
}
|
||||
ReadingMode mode() const override { return ReadingMode::Text; }
|
||||
bool open(const QString &) override { return true; }
|
||||
QString lastError() const override { return {}; }
|
||||
QString title() const override { return QStringLiteral("Norm Reader"); }
|
||||
QString author() const override { return QStringLiteral("Author"); }
|
||||
QString filePath() const override { return QStringLiteral("/fake/norm.epub"); }
|
||||
QString coverImageUrl() const override { return {}; }
|
||||
QByteArray contentHash() const override { return QByteArray("normhash"); }
|
||||
bool loadMetadata(const QString &) override { return true; }
|
||||
QVector<ContentChunk> chunks() const override { return m_chunks; }
|
||||
private:
|
||||
QVector<ContentChunk> m_chunks;
|
||||
};
|
||||
|
||||
class ReadingModelTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
@@ -293,6 +326,129 @@ private slots:
|
||||
const auto layout55 = model.data(model.index(55, 0), ReadingModel::LayoutRole).value<ChunkLayout>();
|
||||
QVERIFY(!layout55.lines.isEmpty());
|
||||
}
|
||||
|
||||
void normalizedPageCountCalculations()
|
||||
{
|
||||
Ctx ctx;
|
||||
ReadingModel model(*ctx.store);
|
||||
|
||||
// 1. Empty book
|
||||
QCOMPARE(model.totalPages(), 0);
|
||||
QCOMPARE(model.currentPage(), 0);
|
||||
QCOMPARE(model.currentChunk(), 0);
|
||||
|
||||
// 2. Short book: 500 characters (< 1800) -> 1 page
|
||||
VariableCharsTextReader shortReader({500});
|
||||
model.setReader(&shortReader);
|
||||
QCOMPARE(model.totalPages(), 1);
|
||||
QCOMPARE(model.currentPage(), 1);
|
||||
QCOMPARE(model.currentChunk(), 0);
|
||||
|
||||
// 3. Exactly 1800 characters -> 1 page
|
||||
VariableCharsTextReader exactReader({1800});
|
||||
model.setReader(&exactReader);
|
||||
QCOMPARE(model.totalPages(), 1);
|
||||
QCOMPARE(model.currentPage(), 1);
|
||||
|
||||
// 4. 1801 characters -> 2 pages
|
||||
VariableCharsTextReader splitReader({1801});
|
||||
model.setReader(&splitReader);
|
||||
QCOMPARE(model.totalPages(), 2);
|
||||
QCOMPARE(model.currentPage(), 1);
|
||||
|
||||
// 5. Multi-chunk book: 5 chunks with 720 chars each (3600 chars total) -> 2 pages
|
||||
VariableCharsTextReader multiChunk({720, 720, 720, 720, 720});
|
||||
model.setReader(&multiChunk);
|
||||
QCOMPARE(model.rowCount(), 5);
|
||||
QCOMPARE(model.totalPages(), 2);
|
||||
QCOMPARE(model.currentPage(), 1);
|
||||
}
|
||||
|
||||
void normalizedPageCurrentPageProgression()
|
||||
{
|
||||
Ctx ctx;
|
||||
ReadingModel model(*ctx.store);
|
||||
|
||||
// 6 chunks of 900 chars each = 5400 chars = 3 normalized pages (1800 chars/page)
|
||||
// Chunk 0: 0..900 (Page 1)
|
||||
// Chunk 1: 900..1800 (Page 1)
|
||||
// Chunk 2: 1800..2700 (Page 2)
|
||||
// Chunk 3: 2700..3600 (Page 2)
|
||||
// Chunk 4: 3600..4500 (Page 3)
|
||||
// Chunk 5: 4500..5400 (Page 3)
|
||||
VariableCharsTextReader reader({900, 900, 900, 900, 900, 900});
|
||||
model.setReader(&reader);
|
||||
QCOMPARE(model.totalPages(), 3);
|
||||
QCOMPARE(model.currentPage(), 1);
|
||||
QCOMPARE(model.currentChunk(), 0);
|
||||
|
||||
QSignalSpy pageSpy(&model, &ReadingModel::currentPageChanged);
|
||||
QSignalSpy chunkSpy(&model, &ReadingModel::currentChunkChanged);
|
||||
|
||||
// Scroll to chunk 1: 900 chars scrolled -> still Page 1
|
||||
model.setScrollPosition(1, 0.0, 0.16);
|
||||
QCOMPARE(model.currentChunk(), 1);
|
||||
QCOMPARE(model.currentPage(), 1);
|
||||
QCOMPARE(chunkSpy.count(), 1);
|
||||
QCOMPARE(pageSpy.count(), 0);
|
||||
|
||||
// Scroll to chunk 2: 1800 chars scrolled -> Page 2
|
||||
model.setScrollPosition(2, 0.0, 0.33);
|
||||
QCOMPARE(model.currentChunk(), 2);
|
||||
QCOMPARE(model.currentPage(), 2);
|
||||
QCOMPARE(pageSpy.count(), 1);
|
||||
|
||||
// Scroll to chunk 3: 2700 chars scrolled -> Page 2
|
||||
model.setScrollPosition(3, 0.0, 0.50);
|
||||
QCOMPARE(model.currentChunk(), 3);
|
||||
QCOMPARE(model.currentPage(), 2);
|
||||
|
||||
// Scroll to chunk 4: 3600 chars scrolled -> Page 3
|
||||
model.setScrollPosition(4, 0.0, 0.67);
|
||||
QCOMPARE(model.currentChunk(), 4);
|
||||
QCOMPARE(model.currentPage(), 3);
|
||||
QCOMPARE(pageSpy.count(), 2);
|
||||
|
||||
// Scroll to end of chunk 5: Page 3 (clamped to totalPages)
|
||||
model.setScrollPosition(5, 100.0, 1.0);
|
||||
QCOMPARE(model.currentChunk(), 5);
|
||||
QCOMPARE(model.currentPage(), 3);
|
||||
}
|
||||
|
||||
void viewportGeometryInvariance()
|
||||
{
|
||||
Ctx ctx;
|
||||
ReadingModel model(*ctx.store);
|
||||
|
||||
// 10 chunks of 1800 chars each = 18000 chars = 10 normalized pages
|
||||
QVector<int> chunkSizes(10, 1800);
|
||||
VariableCharsTextReader reader(chunkSizes);
|
||||
model.setReader(&reader);
|
||||
|
||||
QCOMPARE(model.totalPages(), 10);
|
||||
QCOMPARE(model.currentPage(), 1);
|
||||
|
||||
// Scroll to chunk 3 (Page 4)
|
||||
model.setScrollPosition(3, 0.0, 0.3);
|
||||
QCOMPARE(model.currentPage(), 4);
|
||||
QCOMPARE(model.currentChunk(), 3);
|
||||
|
||||
// Simulate resizing viewport width and changing font preferences
|
||||
model.updateLayoutMetrics(300, 12, 1.0, 3);
|
||||
QCOMPARE(model.totalPages(), 10);
|
||||
QCOMPARE(model.currentPage(), 4);
|
||||
QCOMPARE(model.currentChunk(), 3);
|
||||
|
||||
model.updateLayoutMetrics(1400, 28, 1.8, 3);
|
||||
QCOMPARE(model.totalPages(), 10);
|
||||
QCOMPARE(model.currentPage(), 4);
|
||||
QCOMPARE(model.currentChunk(), 3);
|
||||
|
||||
model.updateLayoutMetrics(800, 16, 1.2, 3);
|
||||
QCOMPARE(model.totalPages(), 10);
|
||||
QCOMPARE(model.currentPage(), 4);
|
||||
QCOMPARE(model.currentChunk(), 3);
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(ReadingModelTest)
|
||||
|
||||
Reference in new issue
Block a user