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

+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