More refactor

This commit is contained in:
Agel committed 2026-03-23 23:20:25 +01:00
1 parent 1d3b58ac46
commit d808f45346
6 files changed
+83 -283

No files matched your search

+33 -58
View File
@@ -54,10 +54,12 @@ double ReadingModel::progress() const
{
if (m_totalChapters == 0)
return 0.0;
double chapterFraction = 0.0;
if (m_totalContentHeight > 0)
chapterFraction = qBound(0.0, m_absoluteScrollY / m_totalContentHeight, 1.0);
return (m_currentChapter + chapterFraction) / m_totalChapters;
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;
}
int ReadingModel::currentPage() const
@@ -70,70 +72,35 @@ int ReadingModel::totalPages() const
return m_totalPages;
}
void ReadingModel::setChunkMeasurer(IChunkMeasurer *measurer)
void ReadingModel::setScrollInfo(int topChunkIndex, qreal /*offsetInChunk*/, bool atYEnd)
{
m_chunkMeasurer = measurer;
}
void ReadingModel::setViewportSize(qreal width, qreal height)
{
bool widthChanged = (width != m_viewportWidth);
m_viewportWidth = width;
m_viewportHeight = height;
if (!m_chunks.isEmpty() && (widthChanged || m_chunkHeights.isEmpty()))
computeChunkHeights();
recalculateTotalPages();
recalculateCurrentPage();
}
void ReadingModel::setScrollInfo(int topChunkIndex, qreal offsetInChunk, bool atYEnd)
{
if (topChunkIndex < 0 || topChunkIndex >= m_chunkOffsets.size())
if (topChunkIndex < 0 || topChunkIndex >= m_chunks.size())
return;
m_absoluteScrollY = m_chunkOffsets.at(topChunkIndex) + offsetInChunk;
m_topChunkIndex = topChunkIndex;
m_atYEnd = atYEnd;
recalculateCurrentPage();
}
void ReadingModel::computeChunkHeights()
void ReadingModel::computeChunkBytes()
{
if (m_chunkMeasurer) {
m_chunkHeights = m_chunkMeasurer->measure(m_chunks, m_viewportWidth);
} else {
m_chunkHeights.assign(m_chunks.size(), 0.0);
m_chunkBytes.resize(m_chunks.size());
m_chunkByteOffsets.resize(m_chunks.size());
qsizetype offset = 0;
for (int i = 0; i < m_chunks.size(); ++i) {
m_chunkByteOffsets[i] = offset;
qsizetype sz = m_chunks.at(i).toUtf8().size();
m_chunkBytes[i] = sz;
offset += sz;
}
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();
m_totalChapterBytes = offset;
}
void ReadingModel::recalculateCurrentPage()
{
int page = 0;
if (m_viewportHeight > 0 && m_totalPages > 0) {
page = static_cast<int>(std::floor(m_absoluteScrollY / m_viewportHeight)) + 1;
if (m_atYEnd)
page = m_totalPages;
if (!m_chunks.isEmpty()) {
page = m_atYEnd ? m_totalPages : m_topChunkIndex + 1;
page = std::max(1, std::min(page, m_totalPages));
}
if (m_currentPage == page)
@@ -147,10 +114,11 @@ void ReadingModel::loadChapterData(int index)
beginResetModel();
m_chunks.clear();
m_chapterTitle.clear();
m_chunkHeights.clear();
m_chunkOffsets.clear();
m_totalContentHeight = 0;
m_absoluteScrollY = 0;
m_chunkBytes.clear();
m_chunkByteOffsets.clear();
m_totalChapterBytes = 0;
m_topChunkIndex = 0;
m_atYEnd = false;
if (m_currentPage != 0) { m_currentPage = 0; emit currentPageChanged(); }
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
@@ -162,6 +130,13 @@ void ReadingModel::loadChapterData(int index)
}
endResetModel();
computeChunkBytes();
int pages = m_chunks.size();
if (m_totalPages != pages) { m_totalPages = pages; emit totalPagesChanged(); }
int page = m_chunks.isEmpty() ? 0 : 1;
if (m_currentPage != page) { m_currentPage = page; emit currentPageChanged(); }
}
void ReadingModel::setReader(FormatReader *reader)