old render cleanup part 2

This commit is contained in:
Agel committed 2026-10-25 15:10:38 +01:00
1 parent acc7e71e7b
commit cef762c3ee
40 files changed
+329 -3643

No files matched your search

+22 -119
View File
@@ -1,6 +1,4 @@
#include "ReadingModel.h"
#include "FormatReader.h"
#include <QRegularExpression>
#include <algorithm>
ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
@@ -11,18 +9,7 @@ ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
ReadingModel::~ReadingModel() = default;
QString ReadingModel::chapterTitle() const
{
if (m_currentChapter < 0 || m_currentChapter >= m_chapterTitles.size())
return {};
return m_chapterTitles.at(m_currentChapter);
}
int ReadingModel::currentChapter() const { return m_currentChapter; }
int ReadingModel::totalChapters() const { return m_totalChapters; }
bool ReadingModel::hasChapters() const { return m_totalChapters > 1; }
int ReadingModel::totalPagePairs() const { return m_pagination.count(); }
bool ReadingModel::isMangaMode() const { return m_isMangaMode; }
bool ReadingModel::pageShift() const { return m_pageShift; }
bool ReadingModel::mangaRTL() const { return m_mangaRTL; }
@@ -38,19 +25,10 @@ void ReadingModel::setScrollInfo(const int topChunkIndex, const qreal proportion
{
m_yOffset = proportion;
// Derive current chapter
const auto it = std::upper_bound(m_chapterStartIndices.begin(), m_chapterStartIndices.end(), topChunkIndex);
if (const int chapter = std::max(0, static_cast<int>(std::distance(m_chapterStartIndices.begin(), it)) - 1); chapter != m_currentChapter) {
m_currentChapter = chapter;
emit chapterChanged();
}
if (m_isMangaMode) {
const int page = pairIndexForChunk(topChunkIndex) + 1;
if (m_currentPage != page) {
m_currentPage = page;
emit currentPageChanged();
}
const int page = pairIndexForChunk(topChunkIndex) + 1;
if (m_currentPage != page) {
m_currentPage = page;
emit currentPageChanged();
}
}
@@ -58,21 +36,24 @@ void ReadingModel::rebuildPagePairs()
{
const int oldPairs = m_pagination.count();
if (!m_isMangaMode || m_chunks.isEmpty()) {
if (m_pages.isEmpty()) {
m_pagination.clear();
if (m_pagination.count() != oldPairs) emit totalPagePairsChanged();
if (m_totalPages != 0) {
m_totalPages = 0;
emit totalPagesChanged();
}
return;
}
QVector<QSize> sizes;
sizes.reserve(m_chunks.size());
for (const auto &c : m_chunks)
sizes.append(c.naturalSize);
sizes.reserve(m_pages.size());
for (const auto &p : m_pages)
sizes.append(p.naturalSize);
m_pagination.rebuild(sizes, m_mangaLandscape, m_pageShift);
if (m_pagination.count() != oldPairs) emit totalPagePairsChanged();
// Sync totalPages/currentPage to pair count in manga mode.
if (const int pairs = m_pagination.count(); m_totalPages != pairs) {
m_totalPages = pairs;
emit totalPagesChanged();
@@ -101,41 +82,36 @@ int ReadingModel::pairIndexForChunk(int chunkIndex) const
QString ReadingModel::imageUrlForChunk(int chunkIndex) const
{
if (chunkIndex < 0 || chunkIndex >= m_chunks.size()) return {};
return m_chunks.at(chunkIndex).imageUrl;
if (chunkIndex < 0 || chunkIndex >= m_pages.size()) return {};
return m_pages.at(chunkIndex).imageUrl;
}
void ReadingModel::setMangaLandscape(bool landscape)
{
if (m_mangaLandscape == landscape) return;
m_mangaLandscape = landscape;
if (m_isMangaMode) rebuildPagePairs();
rebuildPagePairs();
}
void ReadingModel::setPageShift(bool shift, int currentPairIndex)
{
if (m_pageShift == shift) return;
// Anchor on a chunk that keeps an image on screen across the rebuild and
// keeps back-and-forth toggling stable (see MangaPagination for the rule).
const int currentChunk = m_pagination.anchorChunkForShiftToggle(currentPairIndex, shift);
m_pageShift = shift;
rebuildPagePairs();
// Re-locate the pair that contains the chunk we were on.
const int newPair = pairIndexForChunk(currentChunk);
if (m_currentPage != newPair + 1) {
m_currentPage = newPair + 1;
emit currentPageChanged();
}
// Persist immediately.
if (m_reader)
m_positions.save(m_reader->contentHash(), currentChunk, m_mangaRTL, m_pageShift);
emit pageShiftChanged();
// Tell MangaPageView the exact pair to jump to so it re-evaluates its display binding.
emit pagePairsRebuilt(newPair);
}
@@ -150,87 +126,35 @@ void ReadingModel::setMangaRTL(bool rtl)
emit mangaRTLChanged();
}
// Extract src="..." from a chunk's img tag, done once at load time.
static QString extractImageUrl(const QString &html)
void ReadingModel::loadPages()
{
static const QRegularExpression re(QStringLiteral("<img\\s[^>]*src=\"([^\"]+)\""));
const auto match = re.match(html);
return match.hasMatch() ? match.captured(1) : QString();
}
void ReadingModel::loadAllChapters()
{
m_chunks.clear();
m_chapterStartIndices.clear();
m_chapterTitles.clear();
m_currentChapter = 0;
m_pages.clear();
if (m_currentPage != 0) { m_currentPage = 0; emit currentPageChanged(); }
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
if (m_reader) {
m_chapterStartIndices.append(0);
for (int i = 0; i < m_totalChapters; ++i) {
auto chapter = m_reader->loadChapter(i);
const QVector<QSize> sizes = m_reader->chapterPageSizes(i);
if (i > 0)
m_chapterStartIndices.append(m_chunks.size());
m_chapterTitles.append(chapter.title);
int j = 0;
for (const auto &html : chapter.contentChunks) {
const QSize sz = (j < sizes.size()) ? sizes[j] : QSize();
m_chunks.push_back({i, sz, extractImageUrl(html)});
++j;
}
}
m_pages = m_reader->pages();
}
if (m_isMangaMode) {
rebuildPagePairs();
} else {
if (m_totalPages != m_chunks.size()) {
m_totalPages = m_chunks.size();
emit totalPagesChanged();
}
if (const int page = m_chunks.isEmpty() ? 0 : 1; m_currentPage != page) {
m_currentPage = page;
emit currentPageChanged();
}
}
rebuildPagePairs();
}
void ReadingModel::setReader(FormatReader *reader)
{
m_reader = reader;
m_currentChapter = 0;
m_totalChapters = reader ? reader->chapterCount() : 0;
const bool wasManga = m_isMangaMode;
m_isMangaMode = reader && reader->preferredRenderingMode() == FormatReader::RenderingMode::Manga;
if (m_isMangaMode != wasManga)
emit isMangaModeChanged();
// Load per-book pageShift and mangaRTL before loadAllChapters so rebuildPagePairs uses the saved values.
int restoreChunk = 0;
if (reader) {
const auto pos = m_positions.load(reader->contentHash());
restoreChunk = pos.chunkIndex;
if (m_isMangaMode) {
m_pageShift = pos.pageShift;
m_mangaRTL = pos.mangaRTL;
} else {
m_pageShift = true;
m_mangaRTL = true;
}
m_pageShift = pos.pageShift;
m_mangaRTL = pos.mangaRTL;
} else {
m_pageShift = true;
m_mangaRTL = true;
}
loadAllChapters();
emit totalChaptersChanged();
emit chapterChanged();
loadPages();
if (m_reader) {
QMetaObject::invokeMethod(this, [this, restoreChunk] {
@@ -238,24 +162,3 @@ void ReadingModel::setReader(FormatReader *reader)
}, Qt::QueuedConnection);
}
}
void ReadingModel::nextChapter()
{
if (!m_reader || m_currentChapter >= m_totalChapters - 1)
return;
emit scrollToChunkRequested(m_chapterStartIndices.at(m_currentChapter + 1));
}
void ReadingModel::previousChapter()
{
if (!m_reader || m_currentChapter <= 0)
return;
emit scrollToChunkRequested(m_chapterStartIndices.at(m_currentChapter - 1));
}
void ReadingModel::goToChapter(int index)
{
if (!m_reader || index < 0 || index >= m_totalChapters)
return;
emit scrollToChunkRequested(m_chapterStartIndices.at(index));
}