522 lines
17 KiB
C++
522 lines
17 KiB
C++
#include "ReadingModel.h"
|
|
#include <algorithm>
|
|
#include <numeric>
|
|
#include <QPointer>
|
|
#include <QtConcurrent>
|
|
|
|
ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
|
|
: QAbstractListModel(parent)
|
|
, m_positions(positions)
|
|
{
|
|
m_remainingLayoutTimer = new QTimer(this);
|
|
m_remainingLayoutTimer->setSingleShot(true);
|
|
m_remainingLayoutTimer->setInterval(100);
|
|
connect(m_remainingLayoutTimer, &QTimer::timeout, this, &ReadingModel::runRemainingLayout);
|
|
}
|
|
|
|
ReadingModel::~ReadingModel()
|
|
{
|
|
if (m_remainingLayoutTimer) {
|
|
m_remainingLayoutTimer->stop();
|
|
}
|
|
++m_layoutGeneration;
|
|
if (m_layoutFuture.isRunning()) {
|
|
m_layoutFuture.waitForFinished();
|
|
}
|
|
}
|
|
|
|
int ReadingModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid() || !m_isTextMode)
|
|
return 0;
|
|
return m_chunks.size();
|
|
}
|
|
|
|
QVariant ReadingModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid() || !m_isTextMode || index.row() < 0 || index.row() >= m_chunks.size())
|
|
return {};
|
|
if (role == ContentRole)
|
|
return m_chunks.at(index.row());
|
|
if (role == LayoutRole && index.row() < m_chunks.size()) {
|
|
ensureChunkLayout(index.row());
|
|
return QVariant::fromValue(m_chunkLayouts.at(index.row()));
|
|
}
|
|
if (role == PixelHeightRole && index.row() < m_chunks.size()) {
|
|
if (index.row() < m_chunkLayouts.size() && m_chunkLayouts.at(index.row()).totalHeight > 0 && !m_chunkLayouts.at(index.row()).lines.isEmpty())
|
|
return m_chunkLayouts.at(index.row()).totalHeight;
|
|
ensureChunkLayout(index.row());
|
|
return m_chunkLayouts.at(index.row()).totalHeight;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
void ReadingModel::ensureChunkLayout(int row) const
|
|
{
|
|
if (row < 0 || row >= m_chunks.size())
|
|
return;
|
|
if (row < m_chunkLayouts.size() && m_chunkLayouts[row].totalHeight > 0 && !m_chunkLayouts[row].lines.isEmpty())
|
|
return;
|
|
if (row >= m_preparedChunks.size())
|
|
m_preparedChunks.resize(m_chunks.size());
|
|
if (row >= m_chunkLayouts.size())
|
|
m_chunkLayouts.resize(m_chunks.size());
|
|
|
|
LayoutParams params;
|
|
params.contentWidth = m_layoutContentWidth;
|
|
params.fontSize = m_layoutFontSize;
|
|
params.lineSpacing = m_layoutLineSpacing;
|
|
|
|
if (m_preparedChunks[row].blocks.isEmpty() && !m_chunks[row].isEmpty()) {
|
|
m_preparedChunks[row] = ChunkLayoutEngine::prepareChunk(m_chunks[row], params.fontSize, m_imageResolver);
|
|
}
|
|
m_chunkLayouts[row] = ChunkLayoutEngine::layoutPreparedChunk(m_preparedChunks[row], params);
|
|
}
|
|
|
|
QHash<int, QByteArray> ReadingModel::roleNames() const
|
|
{
|
|
return {
|
|
{ContentRole, "content"},
|
|
{LayoutRole, "layout"},
|
|
{PixelHeightRole, "pixelHeight"}
|
|
};
|
|
}
|
|
|
|
bool ReadingModel::isTextMode() const { return m_isTextMode; }
|
|
bool ReadingModel::isMangaMode() const { return !m_isTextMode; }
|
|
bool ReadingModel::measuring() const { return m_measuring; }
|
|
int ReadingModel::totalPagePairs() const { return m_pagination.count(); }
|
|
bool ReadingModel::pageShift() const { return m_pageShift; }
|
|
bool ReadingModel::mangaRTL() const { return m_mangaRTL; }
|
|
bool ReadingModel::useBookColours() const { return m_useBookColours; }
|
|
|
|
double ReadingModel::progress() const
|
|
{
|
|
return qBound(0.0, m_progress, 1.0);
|
|
}
|
|
|
|
int ReadingModel::currentPage() const { return m_currentPage; }
|
|
int ReadingModel::totalPages() const { return m_totalPages; }
|
|
|
|
void ReadingModel::setImageResolver(IBookImageResolver *resolver)
|
|
{
|
|
m_imageResolver = resolver;
|
|
}
|
|
|
|
void ReadingModel::updateLayoutMetrics(int contentWidth, int fontSize, qreal lineSpacing, int anchorChunk)
|
|
{
|
|
if (contentWidth <= 0 || fontSize <= 0 || lineSpacing <= 0)
|
|
return;
|
|
|
|
if (m_layoutContentWidth == contentWidth && m_layoutFontSize == fontSize
|
|
&& qFuzzyCompare(m_layoutLineSpacing, lineSpacing))
|
|
return;
|
|
|
|
const bool fontSizeChanged = (m_layoutFontSize != fontSize);
|
|
m_layoutContentWidth = contentWidth;
|
|
m_layoutFontSize = fontSize;
|
|
m_layoutLineSpacing = lineSpacing;
|
|
|
|
if (!m_isTextMode || m_chunks.isEmpty())
|
|
return;
|
|
|
|
const int targetChunk = (anchorChunk >= 0) ? anchorChunk : (m_currentPage > 0 ? (m_currentPage - 1) : 0);
|
|
if (fontSizeChanged || m_preparedChunks.size() != m_chunks.size()) {
|
|
if (m_remainingLayoutTimer)
|
|
m_remainingLayoutTimer->stop();
|
|
startLayoutMeasurement(targetChunk, m_intraOffset);
|
|
return;
|
|
}
|
|
|
|
// FAST PATH: Font size unchanged, only width or line spacing changed.
|
|
// 1. Immediately layout visible chunk window synchronously (<0.2ms) so display reflows in real time without lag.
|
|
LayoutParams params;
|
|
params.contentWidth = m_layoutContentWidth;
|
|
params.fontSize = m_layoutFontSize;
|
|
params.lineSpacing = m_layoutLineSpacing;
|
|
|
|
const int startIdx = std::max(0, targetChunk - 5);
|
|
const int endIdx = std::min(int(m_preparedChunks.size() - 1), targetChunk + 25);
|
|
|
|
m_lastSyncStart = startIdx;
|
|
m_lastSyncEnd = endIdx;
|
|
|
|
if (m_chunkLayouts.size() != m_preparedChunks.size()) {
|
|
m_chunkLayouts.resize(m_preparedChunks.size());
|
|
}
|
|
|
|
for (int i = startIdx; i <= endIdx; ++i) {
|
|
m_chunkLayouts[i] = ChunkLayoutEngine::layoutPreparedChunk(m_preparedChunks[i], params);
|
|
}
|
|
|
|
if (startIdx <= endIdx) {
|
|
emit dataChanged(index(startIdx, 0), index(endIdx, 0), {LayoutRole, PixelHeightRole});
|
|
}
|
|
|
|
// 2. Debounce background layout for off-screen chunks so mouse drag stays at 60fps without thread pool saturation.
|
|
if (m_remainingLayoutTimer) {
|
|
m_remainingLayoutTimer->start();
|
|
} else {
|
|
runRemainingLayout();
|
|
}
|
|
}
|
|
|
|
void ReadingModel::runRemainingLayout()
|
|
{
|
|
if (!m_isTextMode || m_preparedChunks.isEmpty())
|
|
return;
|
|
|
|
const int generation = ++m_layoutGeneration;
|
|
const auto prepared = m_preparedChunks;
|
|
LayoutParams params;
|
|
params.contentWidth = m_layoutContentWidth;
|
|
params.fontSize = m_layoutFontSize;
|
|
params.lineSpacing = m_layoutLineSpacing;
|
|
const int syncStart = m_lastSyncStart;
|
|
const int syncEnd = m_lastSyncEnd;
|
|
|
|
QPointer<ReadingModel> self(this);
|
|
m_layoutFuture = QtConcurrent::run([self, generation, prepared, params, syncStart, syncEnd]() {
|
|
QVector<ChunkLayout> newLayouts = QtConcurrent::blockingMapped(
|
|
prepared,
|
|
[params](const ChunkLayoutEngine::PreparedChunk &prep) {
|
|
return ChunkLayoutEngine::layoutPreparedChunk(prep, params);
|
|
});
|
|
|
|
if (!self || self->m_layoutGeneration != generation)
|
|
return;
|
|
|
|
QMetaObject::invokeMethod(self.data(), [self, generation, newLayouts = std::move(newLayouts), syncStart, syncEnd]() mutable {
|
|
if (!self || generation != self->m_layoutGeneration)
|
|
return;
|
|
self->m_chunkLayouts = std::move(newLayouts);
|
|
if (!self->m_chunkLayouts.isEmpty()) {
|
|
if (syncStart > 0) {
|
|
emit self->dataChanged(self->index(0, 0), self->index(syncStart - 1, 0), {LayoutRole, PixelHeightRole});
|
|
}
|
|
if (syncEnd >= 0 && syncEnd + 1 < self->m_chunkLayouts.size()) {
|
|
emit self->dataChanged(self->index(syncEnd + 1, 0), self->index(self->m_chunkLayouts.size() - 1, 0), {LayoutRole, PixelHeightRole});
|
|
}
|
|
if (syncStart < 0 && syncEnd < 0) {
|
|
emit self->dataChanged(self->index(0, 0), self->index(self->m_chunkLayouts.size() - 1, 0), {LayoutRole, PixelHeightRole});
|
|
}
|
|
}
|
|
}, Qt::QueuedConnection);
|
|
});
|
|
}
|
|
|
|
void ReadingModel::setScrollInfo(const int topChunkIndex, const qreal proportion)
|
|
{
|
|
setScrollPosition(topChunkIndex, 0.0, proportion);
|
|
}
|
|
|
|
void ReadingModel::setScrollPosition(int topChunkIndex, qreal intraOffset, qreal proportion)
|
|
{
|
|
m_intraOffset = intraOffset;
|
|
const qreal clamped = qBound(0.0, proportion, 1.0);
|
|
if (!qFuzzyCompare(m_progress, clamped)) {
|
|
m_progress = clamped;
|
|
emit progressChanged();
|
|
}
|
|
|
|
if (m_isTextMode) {
|
|
const int page = topChunkIndex + 1;
|
|
if (m_currentPage != page) {
|
|
m_currentPage = page;
|
|
emit currentPageChanged();
|
|
}
|
|
} else {
|
|
const int page = pairIndexForChunk(topChunkIndex) + 1;
|
|
if (m_currentPage != page) {
|
|
m_currentPage = page;
|
|
emit currentPageChanged();
|
|
}
|
|
}
|
|
|
|
saveCurrentPosition(topChunkIndex, intraOffset);
|
|
}
|
|
|
|
void ReadingModel::saveCurrentPosition(int chunkIndex, qreal intraOffset)
|
|
{
|
|
if (!m_reader) return;
|
|
BookPosition pos;
|
|
pos.chunkIndex = chunkIndex;
|
|
pos.intraOffset = intraOffset;
|
|
pos.progress = m_progress;
|
|
pos.pageShift = m_pageShift;
|
|
pos.mangaRTL = m_mangaRTL;
|
|
pos.useBookColours = m_useBookColours;
|
|
m_positions.save(m_reader->contentHash(), pos);
|
|
emit positionSaved();
|
|
}
|
|
|
|
void ReadingModel::startLayoutMeasurement(int restoreChunk, qreal restoreIntraOffset)
|
|
{
|
|
if (m_remainingLayoutTimer)
|
|
m_remainingLayoutTimer->stop();
|
|
|
|
if (!m_isTextMode || m_chunks.isEmpty()) {
|
|
m_preparedChunks.clear();
|
|
m_chunkLayouts.clear();
|
|
if (m_measuring) {
|
|
m_measuring = false;
|
|
emit measuringChanged();
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_measuring = true;
|
|
emit measuringChanged();
|
|
|
|
const int generation = ++m_layoutGeneration;
|
|
const auto chunks = m_chunks;
|
|
LayoutParams params;
|
|
params.contentWidth = m_layoutContentWidth;
|
|
params.fontSize = m_layoutFontSize;
|
|
params.lineSpacing = m_layoutLineSpacing;
|
|
auto *resolver = m_imageResolver;
|
|
|
|
// Phase 1: Immediately prepare and layout the visible window synchronously (<1ms)
|
|
const int startIdx = std::max(0, restoreChunk - 5);
|
|
const int endIdx = std::min(int(chunks.size() - 1), restoreChunk + 25);
|
|
|
|
m_preparedChunks.resize(chunks.size());
|
|
m_chunkLayouts.resize(chunks.size());
|
|
|
|
for (int i = startIdx; i <= endIdx; ++i) {
|
|
m_preparedChunks[i] = ChunkLayoutEngine::prepareChunk(chunks[i], params.fontSize, resolver);
|
|
m_chunkLayouts[i] = ChunkLayoutEngine::layoutPreparedChunk(m_preparedChunks[i], params);
|
|
}
|
|
for (int i = 0; i < chunks.size(); ++i) {
|
|
if (i < startIdx || i > endIdx) {
|
|
m_chunkLayouts[i].totalHeight = 60.0;
|
|
}
|
|
}
|
|
|
|
m_lastSyncStart = startIdx;
|
|
m_lastSyncEnd = endIdx;
|
|
|
|
if (startIdx <= endIdx) {
|
|
emit dataChanged(index(startIdx, 0), index(endIdx, 0), {LayoutRole, PixelHeightRole});
|
|
}
|
|
emit restoreReady(restoreChunk, restoreIntraOffset);
|
|
|
|
if (startIdx == 0 && endIdx == chunks.size() - 1) {
|
|
m_measuring = false;
|
|
emit measuringChanged();
|
|
return;
|
|
}
|
|
|
|
// Phase 2: Compute remaining chunks in the background
|
|
QPointer<ReadingModel> self(this);
|
|
m_layoutFuture = QtConcurrent::run([self, generation, chunks, params, resolver, startIdx, endIdx]() {
|
|
auto prepared = QtConcurrent::blockingMapped(chunks, [params, resolver](const QString &chunkHtml) {
|
|
return ChunkLayoutEngine::prepareChunk(chunkHtml, params.fontSize, resolver);
|
|
});
|
|
|
|
if (!self || self->m_layoutGeneration != generation)
|
|
return;
|
|
|
|
auto layouts = QtConcurrent::blockingMapped(prepared, [params](const ChunkLayoutEngine::PreparedChunk &prep) {
|
|
return ChunkLayoutEngine::layoutPreparedChunk(prep, params);
|
|
});
|
|
|
|
if (!self || self->m_layoutGeneration != generation)
|
|
return;
|
|
|
|
QMetaObject::invokeMethod(self.data(), [self, generation, prepared = std::move(prepared), layouts = std::move(layouts), startIdx, endIdx]() mutable {
|
|
if (!self || generation != self->m_layoutGeneration)
|
|
return;
|
|
self->m_preparedChunks = std::move(prepared);
|
|
self->m_chunkLayouts = std::move(layouts);
|
|
self->m_measuring = false;
|
|
emit self->measuringChanged();
|
|
if (startIdx > 0) {
|
|
emit self->dataChanged(self->index(0, 0), self->index(startIdx - 1, 0), {LayoutRole, PixelHeightRole});
|
|
}
|
|
if (endIdx + 1 < self->m_chunkLayouts.size()) {
|
|
emit self->dataChanged(self->index(endIdx + 1, 0), self->index(self->m_chunkLayouts.size() - 1, 0), {LayoutRole, PixelHeightRole});
|
|
}
|
|
}, Qt::QueuedConnection);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
void ReadingModel::rebuildPagePairs()
|
|
{
|
|
const int oldPairs = m_pagination.count();
|
|
|
|
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_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();
|
|
|
|
if (const int pairs = m_pagination.count(); m_totalPages != pairs) {
|
|
m_totalPages = pairs;
|
|
emit totalPagesChanged();
|
|
}
|
|
if (m_currentPage == 0 && m_pagination.count() > 0) {
|
|
m_currentPage = 1;
|
|
emit currentPageChanged();
|
|
}
|
|
}
|
|
|
|
QVariantList ReadingModel::pagePair(int pairIndex) const
|
|
{
|
|
if (pairIndex < 0 || pairIndex >= m_pagination.count()) return {};
|
|
const auto p = m_pagination.at(pairIndex);
|
|
QVariantList list;
|
|
list.append(p.first);
|
|
if (p.second >= 0)
|
|
list.append(p.second);
|
|
return list;
|
|
}
|
|
|
|
int ReadingModel::pairIndexForChunk(int chunkIndex) const
|
|
{
|
|
return m_pagination.pairIndexForChunk(chunkIndex);
|
|
}
|
|
|
|
QString ReadingModel::imageUrlForChunk(int chunkIndex) const
|
|
{
|
|
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;
|
|
rebuildPagePairs();
|
|
}
|
|
|
|
void ReadingModel::setPageShift(bool shift, int currentPairIndex)
|
|
{
|
|
if (m_pageShift == shift) return;
|
|
|
|
const int currentChunk = m_pagination.anchorChunkForShiftToggle(currentPairIndex, shift);
|
|
|
|
m_pageShift = shift;
|
|
rebuildPagePairs();
|
|
|
|
const int newPair = pairIndexForChunk(currentChunk);
|
|
if (m_currentPage != newPair + 1) {
|
|
m_currentPage = newPair + 1;
|
|
emit currentPageChanged();
|
|
}
|
|
|
|
saveCurrentPosition(currentChunk);
|
|
|
|
emit pageShiftChanged();
|
|
emit pagePairsRebuilt(newPair);
|
|
}
|
|
|
|
void ReadingModel::setMangaRTL(bool rtl)
|
|
{
|
|
if (m_mangaRTL == rtl) return;
|
|
m_mangaRTL = rtl;
|
|
if (m_reader) {
|
|
const int currentChunk = (m_currentPage > 0) ? m_pagination.at(m_currentPage - 1).first : 0;
|
|
saveCurrentPosition(currentChunk >= 0 ? currentChunk : 0);
|
|
}
|
|
emit mangaRTLChanged();
|
|
}
|
|
|
|
void ReadingModel::setUseBookColours(bool enabled)
|
|
{
|
|
if (m_useBookColours == enabled) return;
|
|
m_useBookColours = enabled;
|
|
if (m_reader) {
|
|
m_positions.setUseBookColours(m_reader->contentHash(), enabled);
|
|
}
|
|
emit useBookColoursChanged();
|
|
}
|
|
|
|
void ReadingModel::loadPages()
|
|
{
|
|
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_isTextMode) {
|
|
m_pages = m_reader->pages();
|
|
}
|
|
|
|
rebuildPagePairs();
|
|
}
|
|
|
|
void ReadingModel::setReader(FormatReader *reader)
|
|
{
|
|
m_reader = reader;
|
|
|
|
beginResetModel();
|
|
m_chunks.clear();
|
|
m_preparedChunks.clear();
|
|
m_chunkLayouts.clear();
|
|
endResetModel();
|
|
|
|
const bool oldTextMode = m_isTextMode;
|
|
m_isTextMode = (reader && reader->mode() == ReadingMode::Text);
|
|
if (oldTextMode != m_isTextMode)
|
|
emit modeChanged();
|
|
|
|
int restoreChunk = 0;
|
|
qreal restoreIntraOffset = 0.0;
|
|
|
|
if (reader) {
|
|
const auto pos = m_positions.load(reader->contentHash());
|
|
restoreChunk = pos.chunkIndex;
|
|
restoreIntraOffset = pos.intraOffset;
|
|
m_intraOffset = pos.intraOffset;
|
|
m_progress = pos.progress;
|
|
m_pageShift = pos.pageShift;
|
|
m_mangaRTL = pos.mangaRTL;
|
|
m_useBookColours = pos.useBookColours;
|
|
emit progressChanged();
|
|
emit useBookColoursChanged();
|
|
} else {
|
|
m_progress = 0.0;
|
|
m_intraOffset = 0.0;
|
|
m_pageShift = true;
|
|
m_mangaRTL = true;
|
|
m_useBookColours = false;
|
|
emit progressChanged();
|
|
emit useBookColoursChanged();
|
|
}
|
|
|
|
if (m_isTextMode && m_reader) {
|
|
beginResetModel();
|
|
m_chunks = m_reader->chunks();
|
|
m_totalPages = m_chunks.size();
|
|
m_currentPage = m_totalPages > 0 ? qBound(1, restoreChunk + 1, m_totalPages) : 0;
|
|
endResetModel();
|
|
|
|
emit totalPagesChanged();
|
|
emit currentPageChanged();
|
|
|
|
startLayoutMeasurement(restoreChunk, restoreIntraOffset);
|
|
} else {
|
|
loadPages();
|
|
}
|
|
|
|
if (m_reader && !m_isTextMode) {
|
|
QMetaObject::invokeMethod(this, [this, restoreChunk] {
|
|
emit restoreReady(restoreChunk, 0.0);
|
|
}, Qt::QueuedConnection);
|
|
}
|
|
}
|