an unsuccessful attemnt to calculate current/total pages
This commit is contained in:
1 parent
353c82f6d6
commit
1a4726b95a
6 files changed
+288
-3
No files matched your search
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user