old render cleanup part 1
This commit is contained in:
38 files changed
+166
-1366
No files matched your search
+29
-145
@@ -2,47 +2,15 @@
|
||||
#include "FormatReader.h"
|
||||
#include <QRegularExpression>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
: QObject(parent)
|
||||
, m_positions(positions)
|
||||
{
|
||||
connect(&m_measurer, &MeasurementController::measuringChanged,
|
||||
this, &ReadingModel::measuringChanged);
|
||||
connect(&m_measurer, &MeasurementController::heightsReady,
|
||||
this, &ReadingModel::onHeightsReady);
|
||||
}
|
||||
|
||||
ReadingModel::~ReadingModel() = default;
|
||||
|
||||
int ReadingModel::rowCount(const QModelIndex &parent) const {
|
||||
if (parent.isValid()) return 0;
|
||||
return m_chunks.size();
|
||||
}
|
||||
|
||||
QVariant ReadingModel::data(const QModelIndex &index, int role) const {
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= (int)m_chunks.size())
|
||||
return {};
|
||||
const auto &c = m_chunks.at(index.row());
|
||||
switch (role) {
|
||||
case ContentRole: return c.html;
|
||||
case PixelHeightRole: return c.pixelHeight;
|
||||
case ChapterRole: return c.chapterIndex;
|
||||
case NaturalSizeRole: return c.naturalSize;
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ReadingModel::roleNames() const {
|
||||
return {
|
||||
{ContentRole, "content"},
|
||||
{PixelHeightRole, "pixelHeight"},
|
||||
{ChapterRole, "chapter"},
|
||||
{NaturalSizeRole, "naturalSize"},
|
||||
};
|
||||
}
|
||||
|
||||
QString ReadingModel::chapterTitle() const
|
||||
{
|
||||
if (m_currentChapter < 0 || m_currentChapter >= m_chapterTitles.size())
|
||||
@@ -53,8 +21,6 @@ QString ReadingModel::chapterTitle() const
|
||||
int ReadingModel::currentChapter() const { return m_currentChapter; }
|
||||
int ReadingModel::totalChapters() const { return m_totalChapters; }
|
||||
bool ReadingModel::hasChapters() const { return m_totalChapters > 1; }
|
||||
bool ReadingModel::measuring() const { return m_measurer.measuring(); }
|
||||
qreal ReadingModel::totalPixelHeight() const { return m_totalPixelHeight; }
|
||||
int ReadingModel::totalPagePairs() const { return m_pagination.count(); }
|
||||
bool ReadingModel::isMangaMode() const { return m_isMangaMode; }
|
||||
bool ReadingModel::pageShift() const { return m_pageShift; }
|
||||
@@ -79,89 +45,13 @@ void ReadingModel::setScrollInfo(const int topChunkIndex, const qreal proportion
|
||||
emit chapterChanged();
|
||||
}
|
||||
|
||||
recalculateCurrentPage();
|
||||
}
|
||||
|
||||
void ReadingModel::measure(const bool isNew)
|
||||
{
|
||||
if (!m_reader) return;
|
||||
|
||||
if (m_isMangaMode) {
|
||||
// No measurement needed — images are sized by the view to fit the viewport.
|
||||
// Queue the emission so QML's Loader has time to create MangaPageView before
|
||||
// restoreReady arrives (in scroll mode the async measurement provides this gap).
|
||||
QMetaObject::invokeMethod(this, [this, isNew] {
|
||||
emitRestoreReady(isNew);
|
||||
}, Qt::QueuedConnection);
|
||||
return;
|
||||
const int page = pairIndexForChunk(topChunkIndex) + 1;
|
||||
if (m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
}
|
||||
|
||||
m_totalPixelHeight = 0;
|
||||
|
||||
QStringList htmls;
|
||||
htmls.reserve(m_chunks.size());
|
||||
for (const auto &c : m_chunks)
|
||||
htmls.append(c.html);
|
||||
|
||||
const int width = m_contentWidth > 0 ? m_contentWidth : 800;
|
||||
m_measurer.start(htmls, width, m_fontSize, m_lineSpacing, isNew);
|
||||
}
|
||||
|
||||
void ReadingModel::emitRestoreReady(const bool isNew)
|
||||
{
|
||||
if (!isNew) return;
|
||||
const auto pos = m_positions.load(m_reader->contentHash());
|
||||
emit restoreReady(pos.chunkIndex, pos.intraOffset);
|
||||
}
|
||||
|
||||
void ReadingModel::onHeightsReady(const QVector<int> &heights, const bool isNew)
|
||||
{
|
||||
for (int i = 0; i < qMin(heights.size(), static_cast<int>(m_chunks.size())); ++i) {
|
||||
m_chunks[i].pixelHeight = heights[i];
|
||||
m_totalPixelHeight += heights[i];
|
||||
}
|
||||
qInfo("Measured %lld chunks, total height: %f", heights.size(), m_totalPixelHeight);
|
||||
emit totalPixelHeightChanged();
|
||||
emit dataChanged(createIndex(0, 0),
|
||||
createIndex(m_chunks.size() - 1, 0),
|
||||
{PixelHeightRole});
|
||||
|
||||
emitRestoreReady(isNew);
|
||||
}
|
||||
|
||||
void ReadingModel::setContentWidth(int width)
|
||||
{
|
||||
if (width == m_contentWidth) return;
|
||||
m_contentWidth = width;
|
||||
if (m_reader && !m_isMangaMode) measure(false);
|
||||
}
|
||||
|
||||
void ReadingModel::setFontParams(int fontSize, qreal lineSpacing)
|
||||
{
|
||||
if (fontSize == m_fontSize && qFuzzyCompare(lineSpacing, m_lineSpacing)) return;
|
||||
m_fontSize = fontSize;
|
||||
m_lineSpacing = lineSpacing;
|
||||
if (m_reader && !m_isMangaMode) measure(false);
|
||||
}
|
||||
|
||||
void ReadingModel::setImageLoader(std::unique_ptr<IImageLoader> loader)
|
||||
{
|
||||
m_measurer.setImageLoader(std::move(loader));
|
||||
}
|
||||
|
||||
void ReadingModel::recalculateCurrentPage()
|
||||
{
|
||||
// In manga mode currentPage/totalPages track pair index/count (updated by MangaPageView).
|
||||
if (m_isMangaMode) return;
|
||||
|
||||
int page = 0;
|
||||
if (!m_chunks.isEmpty()) {
|
||||
page = qBound(0.0, m_yOffset / m_totalPixelHeight, 1.0) * m_chunks.size() * page_ratio;
|
||||
}
|
||||
if (m_currentPage == page)
|
||||
return;
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::rebuildPagePairs()
|
||||
@@ -242,7 +132,7 @@ void ReadingModel::setPageShift(bool shift, int currentPairIndex)
|
||||
|
||||
// Persist immediately.
|
||||
if (m_reader)
|
||||
m_positions.save(m_reader->contentHash(), currentChunk, 0, m_mangaRTL, m_pageShift);
|
||||
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.
|
||||
@@ -253,8 +143,10 @@ void ReadingModel::setMangaRTL(bool rtl)
|
||||
{
|
||||
if (m_mangaRTL == rtl) return;
|
||||
m_mangaRTL = rtl;
|
||||
if (m_reader)
|
||||
m_positions.save(m_reader->contentHash(), 0, 0, m_mangaRTL, m_pageShift);
|
||||
if (m_reader) {
|
||||
const int currentChunk = (m_currentPage > 0) ? m_pagination.at(m_currentPage - 1).first : 0;
|
||||
m_positions.save(m_reader->contentHash(), currentChunk >= 0 ? currentChunk : 0, m_mangaRTL, m_pageShift);
|
||||
}
|
||||
emit mangaRTLChanged();
|
||||
}
|
||||
|
||||
@@ -268,29 +160,16 @@ static QString extractImageUrl(const QString &html)
|
||||
|
||||
void ReadingModel::loadAllChapters()
|
||||
{
|
||||
beginResetModel();
|
||||
m_chunks.clear();
|
||||
m_chapterStartIndices.clear();
|
||||
m_chapterTitles.clear();
|
||||
m_totalPixelHeight = 0.0;
|
||||
emit totalPixelHeightChanged();
|
||||
m_currentChapter = 0;
|
||||
|
||||
if (m_currentPage != 0) { m_currentPage = 0; emit currentPageChanged(); }
|
||||
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
|
||||
|
||||
if (m_reader) {
|
||||
// In manga mode the format's first chunk is already the cover — no extra cover injection.
|
||||
if (!m_isMangaMode && !m_coverImageUrl.isEmpty()) {
|
||||
QString coverHtml = QStringLiteral("<div class=\"cover-image\" style=\"text-align: center; margin: 2em 0;\">")
|
||||
+ QStringLiteral("<img src=\"") + m_coverImageUrl
|
||||
+ QStringLiteral("\" style=\"max-width: 100%; max-height: 80vh;\" /></div>");
|
||||
m_chunks.push_back({coverHtml, 0, -1, -1, QSize(),
|
||||
extractImageUrl(coverHtml)}); // chunkInChapter=-1 = cover sentinel
|
||||
m_chapterStartIndices.append(1); // chapter 0 starts after cover
|
||||
} else {
|
||||
m_chapterStartIndices.append(0);
|
||||
}
|
||||
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);
|
||||
@@ -300,18 +179,17 @@ void ReadingModel::loadAllChapters()
|
||||
int j = 0;
|
||||
for (const auto &html : chapter.contentChunks) {
|
||||
const QSize sz = (j < sizes.size()) ? sizes[j] : QSize();
|
||||
m_chunks.push_back({html, i, j, -1, sz, extractImageUrl(html)});
|
||||
m_chunks.push_back({i, sz, extractImageUrl(html)});
|
||||
++j;
|
||||
}
|
||||
}
|
||||
}
|
||||
endResetModel();
|
||||
|
||||
if (m_isMangaMode) {
|
||||
rebuildPagePairs();
|
||||
} else {
|
||||
if (const int pages = m_chunks.size() * page_ratio; m_totalPages != pages) {
|
||||
m_totalPages = pages;
|
||||
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) {
|
||||
@@ -333,10 +211,17 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
emit isMangaModeChanged();
|
||||
|
||||
// Load per-book pageShift and mangaRTL before loadAllChapters so rebuildPagePairs uses the saved values.
|
||||
if (m_isMangaMode && reader) {
|
||||
int restoreChunk = 0;
|
||||
if (reader) {
|
||||
const auto pos = m_positions.load(reader->contentHash());
|
||||
m_pageShift = pos.pageShift;
|
||||
m_mangaRTL = pos.mangaRTL;
|
||||
restoreChunk = pos.chunkIndex;
|
||||
if (m_isMangaMode) {
|
||||
m_pageShift = pos.pageShift;
|
||||
m_mangaRTL = pos.mangaRTL;
|
||||
} else {
|
||||
m_pageShift = true;
|
||||
m_mangaRTL = true;
|
||||
}
|
||||
} else {
|
||||
m_pageShift = true;
|
||||
m_mangaRTL = true;
|
||||
@@ -346,12 +231,11 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
|
||||
emit totalChaptersChanged();
|
||||
emit chapterChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::setCoverImageUrl(const QString &coverImageUrl)
|
||||
{
|
||||
if (m_coverImageUrl != coverImageUrl) {
|
||||
m_coverImageUrl = coverImageUrl;
|
||||
if (m_reader) {
|
||||
QMetaObject::invokeMethod(this, [this, restoreChunk] {
|
||||
emit restoreReady(restoreChunk);
|
||||
}, Qt::QueuedConnection);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user