reintroduced epub support

This commit is contained in:
Agel committed 2026-10-25 19:59:23 +01:00
1 parent 49a959a89d
commit d14170a68f
49 files changed
+5048 -205

No files matched your search

+213 -59
View File
@@ -1,42 +1,65 @@
#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() = default;
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_currentChapterData.chunks.size();
return m_chunks.size();
}
QVariant ReadingModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !m_isTextMode || index.row() < 0 || index.row() >= m_currentChapterData.chunks.size())
if (!index.isValid() || !m_isTextMode || index.row() < 0 || index.row() >= m_chunks.size())
return {};
if (role == ContentRole)
return m_currentChapterData.chunks.at(index.row());
return m_chunks.at(index.row());
if (role == LayoutRole && index.row() < m_chunkLayouts.size())
return QVariant::fromValue(m_chunkLayouts.at(index.row()));
if (role == PixelHeightRole && index.row() < m_chunkLayouts.size())
return m_chunkLayouts.at(index.row()).totalHeight;
return {};
}
QHash<int, QByteArray> ReadingModel::roleNames() const
{
return {{ContentRole, "content"}};
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; }
int ReadingModel::currentChapter() const { return m_currentChapter; }
int ReadingModel::chapterCount() const { return m_chapterCount; }
QString ReadingModel::chapterTitle() const { return m_currentChapterData.title; }
double ReadingModel::progress() const
{
@@ -46,8 +69,108 @@ double ReadingModel::progress() const
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 - 2);
const int endIdx = std::min(int(m_preparedChunks.size() - 1), targetChunk + 4);
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;
QPointer<ReadingModel> self(this);
m_layoutFuture = QtConcurrent::run([self, generation, prepared, params]() {
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)]() mutable {
if (!self || generation != self->m_layoutGeneration)
return;
self->m_chunkLayouts = std::move(newLayouts);
if (!self->m_chunkLayouts.isEmpty()) {
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;
@@ -68,15 +191,15 @@ void ReadingModel::setScrollInfo(const int topChunkIndex, const qreal proportion
}
}
saveCurrentPosition(topChunkIndex);
saveCurrentPosition(topChunkIndex, intraOffset);
}
void ReadingModel::saveCurrentPosition(int chunkIndex)
void ReadingModel::saveCurrentPosition(int chunkIndex, qreal intraOffset)
{
if (!m_reader) return;
BookPosition pos;
pos.chapterIndex = m_currentChapter;
pos.chunkIndex = chunkIndex;
pos.intraOffset = intraOffset;
pos.progress = m_progress;
pos.pageShift = m_pageShift;
pos.mangaRTL = m_mangaRTL;
@@ -84,40 +207,64 @@ void ReadingModel::saveCurrentPosition(int chunkIndex)
emit positionSaved();
}
void ReadingModel::loadChapter(int index)
void ReadingModel::startLayoutMeasurement(int restoreChunk, qreal restoreIntraOffset)
{
if (!m_reader || !m_isTextMode)
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;
if (index < 0 || index >= m_chapterCount)
return;
beginResetModel();
m_currentChapter = index;
m_currentChapterData = m_reader->loadChapter(index);
m_totalPages = m_currentChapterData.chunks.size();
m_currentPage = qMin(1, m_totalPages);
endResetModel();
emit currentChapterChanged();
emit totalPagesChanged();
emit currentPageChanged();
}
void ReadingModel::nextChapter()
{
if (m_isTextMode && m_currentChapter + 1 < m_chapterCount) {
loadChapter(m_currentChapter + 1);
saveCurrentPosition(0);
}
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;
QPointer<ReadingModel> self(this);
m_layoutFuture = QtConcurrent::run([self, generation, chunks, params, resolver, restoreChunk, restoreIntraOffset]() {
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), restoreChunk, restoreIntraOffset]() 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 (!self->m_chunkLayouts.isEmpty()) {
emit self->dataChanged(self->index(0, 0), self->index(self->m_chunkLayouts.size() - 1, 0), {LayoutRole, PixelHeightRole});
}
emit self->restoreReady(restoreChunk, restoreIntraOffset);
}, Qt::QueuedConnection);
});
}
void ReadingModel::previousChapter()
{
if (m_isTextMode && m_currentChapter > 0) {
loadChapter(m_currentChapter - 1);
saveCurrentPosition(0);
}
}
void ReadingModel::rebuildPagePairs()
{
@@ -230,7 +377,9 @@ void ReadingModel::setReader(FormatReader *reader)
m_reader = reader;
beginResetModel();
m_currentChapterData = {};
m_chunks.clear();
m_preparedChunks.clear();
m_chunkLayouts.clear();
endResetModel();
const bool oldTextMode = m_isTextMode;
@@ -239,38 +388,43 @@ void ReadingModel::setReader(FormatReader *reader)
emit modeChanged();
int restoreChunk = 0;
int restoreChapter = 0;
qreal restoreIntraOffset = 0.0;
if (reader) {
const auto pos = m_positions.load(reader->contentHash());
restoreChunk = pos.chunkIndex;
restoreChapter = pos.chapterIndex;
m_progress = pos.progress;
m_pageShift = pos.pageShift;
m_mangaRTL = pos.mangaRTL;
restoreChunk = pos.chunkIndex;
restoreIntraOffset = pos.intraOffset;
m_intraOffset = pos.intraOffset;
m_progress = pos.progress;
m_pageShift = pos.pageShift;
m_mangaRTL = pos.mangaRTL;
emit progressChanged();
} else {
m_progress = 0.0;
m_pageShift = true;
m_mangaRTL = true;
m_progress = 0.0;
m_intraOffset = 0.0;
m_pageShift = true;
m_mangaRTL = true;
emit progressChanged();
}
if (m_isTextMode && m_reader) {
m_chapterCount = m_reader->chapterCount();
emit chapterCountChanged();
loadChapter(qBound(0, restoreChapter, qMax(0, m_chapterCount - 1)));
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 {
m_chapterCount = 0;
m_currentChapter = 0;
emit chapterCountChanged();
emit currentChapterChanged();
loadPages();
}
if (m_reader) {
if (m_reader && !m_isTextMode) {
QMetaObject::invokeMethod(this, [this, restoreChunk] {
emit restoreReady(restoreChunk);
emit restoreReady(restoreChunk, 0.0);
}, Qt::QueuedConnection);
}
}