an unsuccessful attemnt to calculate current/total pages

This commit is contained in:
Agel committed 2026-03-19 22:39:40 +01:00
1 parent 353c82f6d6
commit 1a4726b95a
6 files changed
+288 -3

No files matched your search

+2
View File
@@ -159,6 +159,8 @@ add_asteria_test(ReaderFactoryTest tests/ReaderFactoryTest.cpp
add_asteria_test(ReadingModelTest tests/ReadingModelTest.cpp
src/backend/ReadingModel.cpp)
target_link_libraries(ReadingModelTest PRIVATE Qt6::Gui)
set_tests_properties(ReadingModelTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
add_asteria_test(BookControllerTest tests/BookControllerTest.cpp
src/backend/BookController.cpp
+115
View File
@@ -1,5 +1,10 @@
#include "ReadingModel.h"
#include "FormatReader.h"
#include <cmath>
#include <QTextDocument>
#include <QTextCursor>
#include <QTextBlockFormat>
#include <QFont>
ReadingModel::ReadingModel(QObject *parent)
: QAbstractListModel(parent)
@@ -51,11 +56,121 @@ double ReadingModel::progress() const
return static_cast<double>(m_currentChapter + 1) / m_totalChapters;
}
int ReadingModel::currentPage() const
{
return m_currentPage;
}
int ReadingModel::totalPages() const
{
return m_totalPages;
}
void ReadingModel::setViewportSize(qreal width, qreal height)
{
bool widthChanged = (width != m_viewportWidth);
m_viewportWidth = width;
m_viewportHeight = height;
if (widthChanged && !m_chunks.isEmpty())
computeChunkHeights();
recalculateTotalPages();
recalculateCurrentPage();
}
void ReadingModel::setScrollInfo(int topChunkIndex, qreal offsetInChunk)
{
if (topChunkIndex < 0 || topChunkIndex >= m_chunkOffsets.size())
return;
m_absoluteScrollY = m_chunkOffsets.at(topChunkIndex) + offsetInChunk;
recalculateCurrentPage();
}
void ReadingModel::computeChunkHeights()
{
// Text width matches the QML delegate:
// ListView left margin: 12, delegate rightPadding: 2
// delegate width = listView.width - 18 (hardcoded in QML)
// text layout width = delegate.width - rightPadding = listView.width - 20
// m_viewportWidth = chunkListView.width = parent.width - leftMargin(12)
// so delegate.width = m_viewportWidth - 18
// text width = delegate.width - rightPadding(2) = m_viewportWidth - 20
qreal textWidth = m_viewportWidth - 20.0;
if (textWidth <= 0)
textWidth = 1;
QFont font;
font.setPixelSize(16);
QTextDocument doc;
doc.setDefaultFont(font);
m_chunkHeights.clear();
m_chunkHeights.reserve(m_chunks.size());
for (const QString &chunk : m_chunks) {
doc.setHtml(chunk);
doc.setTextWidth(textWidth);
// Apply lineHeight: 1.6 (matching QML Text lineHeight property)
QTextBlockFormat fmt;
fmt.setLineHeight(160, QTextBlockFormat::ProportionalHeight);
QTextCursor cursor(&doc);
cursor.select(QTextCursor::Document);
cursor.mergeBlockFormat(fmt);
m_chunkHeights.append(doc.size().height());
}
// Build prefix sums
m_chunkOffsets.clear();
m_chunkOffsets.reserve(m_chunks.size());
qreal offset = 0;
for (qreal h : m_chunkHeights) {
m_chunkOffsets.append(offset);
offset += h;
}
m_totalContentHeight = offset;
}
void ReadingModel::recalculateTotalPages()
{
int pages = 0;
if (m_viewportHeight > 0 && m_totalContentHeight > 0)
pages = static_cast<int>(std::ceil(m_totalContentHeight / m_viewportHeight));
if (m_totalPages == pages)
return;
m_totalPages = pages;
emit totalPagesChanged();
}
void ReadingModel::recalculateCurrentPage()
{
int page = 0;
if (m_viewportHeight > 0 && m_totalPages > 0) {
page = static_cast<int>(std::floor(m_absoluteScrollY / m_viewportHeight)) + 1;
page = std::max(1, std::min(page, m_totalPages));
}
if (m_currentPage == page)
return;
m_currentPage = page;
emit currentPageChanged();
}
void ReadingModel::loadChapterData(int index)
{
beginResetModel();
m_chunks.clear();
m_chapterTitle.clear();
m_chunkHeights.clear();
m_chunkOffsets.clear();
m_totalContentHeight = 0;
m_absoluteScrollY = 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);
+27 -1
View File
@@ -4,6 +4,7 @@
#include <QAbstractListModel>
#include <QString>
#include <QStringList>
#include <QVector>
class FormatReader;
@@ -13,6 +14,8 @@ class ReadingModel : public QAbstractListModel {
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(int currentPage READ currentPage NOTIFY currentPageChanged)
Q_PROPERTY(int totalPages READ totalPages NOTIFY totalPagesChanged)
public:
enum Roles {
@@ -30,8 +33,15 @@ public:
int currentChapter() const;
int totalChapters() const;
double progress() const;
int currentPage() const;
int totalPages() const;
void setReader(FormatReader *reader);
// Called from QML on resize
Q_INVOKABLE void setViewportSize(qreal width, qreal height);
// Called from QML on every scroll (real-time)
Q_INVOKABLE void setScrollInfo(int topChunkIndex, qreal offsetInChunk);
Q_INVOKABLE void nextChapter();
Q_INVOKABLE void previousChapter();
Q_INVOKABLE void goToChapter(int index);
@@ -39,14 +49,30 @@ public:
signals:
void chapterChanged();
void totalChaptersChanged();
void currentPageChanged();
void totalPagesChanged();
private:
void loadChapterData(int index);
void computeChunkHeights();
void recalculateTotalPages();
void recalculateCurrentPage();
FormatReader *m_reader = nullptr;
int m_currentChapter = 0;
int m_totalChapters = 0;
QString m_chapterTitle;
int m_currentPage = 0;
int m_totalPages = 0;
qreal m_viewportWidth = 0;
qreal m_viewportHeight = 0;
qreal m_absoluteScrollY = 0;
QStringList m_chunks;
QVector<qreal> m_chunkHeights; // rendered height of each chunk
QVector<qreal> m_chunkOffsets; // prefix sums: offset[i] = sum of heights[0..i-1]
qreal m_totalContentHeight = 0;
QString m_chapterTitle;
};
#endif // READINGMODEL_H
+1 -1
View File
@@ -29,7 +29,7 @@ Rectangle {
anchors.verticalCenter: parent.verticalCenter
visible: bookController.hasBook
text: bookController.hasBook
? (readingModel.currentChapter + 1) + "/" + readingModel.totalChapters
? readingModel.currentPage + "/" + readingModel.totalPages
: ""
font.pixelSize: 11
font.bold: true
+24 -1
View File
@@ -27,7 +27,18 @@ Rectangle {
text: model.content
}
onContentYChanged: scrollTrackTimer.restart()
onContentYChanged: {
scrollTrackTimer.restart()
var idx = chunkListView.indexAt(0, chunkListView.contentY)
if (idx >= 0) {
var item = chunkListView.itemAtIndex(idx)
var offset = item ? chunkListView.contentY - item.y : 0
readingModel.setScrollInfo(idx, offset)
}
}
onWidthChanged: readingModel.setViewportSize(chunkListView.width, chunkListView.height)
onHeightChanged: readingModel.setViewportSize(chunkListView.width, chunkListView.height)
ScrollBar.vertical: ScrollBar {
policy: ScrollBar.AsNeeded
@@ -78,6 +89,18 @@ Rectangle {
chunkListView.contentY += appState.lastScrollOffset
})
}
Qt.callLater(function() {
readingModel.setViewportSize(chunkListView.width, chunkListView.height)
})
}
}
Connections {
target: readingModel
function onChapterChanged() {
Qt.callLater(function() {
readingModel.setViewportSize(chunkListView.width, chunkListView.height)
})
}
}
}
+119
View File
@@ -218,6 +218,125 @@ private slots:
QVERIFY(roles.contains(ReadingModel::ContentRole));
QCOMPARE(roles.value(ReadingModel::ContentRole), QByteArray("content"));
}
// Page calculation tests
void initialPageState()
{
ReadingModel model;
QCOMPARE(model.currentPage(), 0);
QCOMPARE(model.totalPages(), 0);
}
void zeroViewportSafe()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setViewportSize(0, 0);
QCOMPARE(model.totalPages(), 0);
QCOMPARE(model.currentPage(), 0);
}
void computeChunkHeightsBasic()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setViewportSize(800, 600);
QVERIFY(model.totalPages() > 0);
QVERIFY(model.currentPage() > 0);
}
void setScrollInfoPage1()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setViewportSize(800, 600);
model.setScrollInfo(0, 0);
QCOMPARE(model.currentPage(), 1);
}
void currentPageNeverBelowOne()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setViewportSize(800, 600);
model.setScrollInfo(0, 0);
QVERIFY(model.currentPage() >= 1);
}
void currentPageClampsToTotalPages()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setViewportSize(800, 600);
// Scroll to last chunk with huge offset
model.setScrollInfo(model.rowCount(QModelIndex()) - 1, 99999);
QCOMPARE(model.currentPage(), model.totalPages());
}
void scrollInfoDoesNotChangeTotalPages()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setViewportSize(800, 600);
int pages = model.totalPages();
model.setScrollInfo(0, 0);
model.setScrollInfo(0, 100);
model.setScrollInfo(1, 0);
QCOMPARE(model.totalPages(), pages);
}
void chapterLoadResetsPages()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setViewportSize(800, 600);
model.setScrollInfo(0, 50);
QVERIFY(model.totalPages() > 0);
QVERIFY(model.currentPage() > 0);
model.goToChapter(1);
QCOMPARE(model.currentPage(), 0);
QCOMPARE(model.totalPages(), 0);
}
void viewportResizeRecalculates()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setViewportSize(800, 600);
int pages800 = model.totalPages();
// Narrower viewport → chunks taller → more pages
model.setViewportSize(400, 600);
QVERIFY(model.totalPages() >= pages800);
// Shorter viewport (same width) → more pages
model.setViewportSize(400, 300);
QVERIFY(model.totalPages() > 0);
}
void setScrollInfoAdvancesMonotonically()
{
// Build a FakeReader with many chunks so we can scroll across pages
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setViewportSize(800, 5); // tiny viewport to guarantee multiple pages
QVERIFY(model.totalPages() > 1);
// chunk 0 at offset 0 → page 1
model.setScrollInfo(0, 0);
int firstPage = model.currentPage();
// chunk 1 at offset 0 → page ≥ firstPage
model.setScrollInfo(1, 0);
int secondPage = model.currentPage();
QVERIFY(secondPage >= firstPage);
}
};
QTEST_MAIN(ReadingModelTest)