Manga rendering mode
This commit is contained in:
15 files changed
+935
-253
No files matched your search
+207
-17
@@ -3,6 +3,7 @@
|
||||
#include "IChunkMeasurer.h"
|
||||
#include "IImageLoader.h"
|
||||
#include "ChunkMeasurer.h"
|
||||
#include <QRegularExpression>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <QtConcurrent>
|
||||
@@ -34,6 +35,7 @@ QVariant ReadingModel::data(const QModelIndex &index, int role) const {
|
||||
case ContentRole: return c.html;
|
||||
case PixelHeightRole: return c.pixelHeight;
|
||||
case ChapterRole: return c.chapterIndex;
|
||||
case NaturalSizeRole: return c.naturalSize;
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
@@ -43,6 +45,7 @@ QHash<int, QByteArray> ReadingModel::roleNames() const {
|
||||
{ContentRole, "content"},
|
||||
{PixelHeightRole, "pixelHeight"},
|
||||
{ChapterRole, "chapter"},
|
||||
{NaturalSizeRole, "naturalSize"},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -58,6 +61,10 @@ int ReadingModel::totalChapters() const { return m_totalChapters; }
|
||||
bool ReadingModel::hasChapters() const { return m_totalChapters > 1; }
|
||||
bool ReadingModel::measuring() const { return m_measuring; }
|
||||
qreal ReadingModel::totalPixelHeight() const { return m_totalPixelHeight; }
|
||||
int ReadingModel::totalPagePairs() const { return m_pagePairs.size(); }
|
||||
bool ReadingModel::isMangaMode() const { return m_isMangaMode; }
|
||||
bool ReadingModel::pageShift() const { return m_pageShift; }
|
||||
bool ReadingModel::mangaRTL() const { return m_mangaRTL; }
|
||||
|
||||
double ReadingModel::progress() const
|
||||
{
|
||||
@@ -85,6 +92,16 @@ 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;
|
||||
}
|
||||
|
||||
m_totalPixelHeight = 0;
|
||||
|
||||
QStringList htmls;
|
||||
@@ -95,6 +112,13 @@ void ReadingModel::measure(const bool isNew)
|
||||
runMeasurement(htmls, 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::runMeasurement(const QStringList &htmls, const bool isNew)
|
||||
{
|
||||
if (m_measureFuture.isRunning())
|
||||
@@ -127,10 +151,7 @@ void ReadingModel::runMeasurement(const QStringList &htmls, const bool isNew)
|
||||
createIndex(m_chunks.size() - 1, 0),
|
||||
{PixelHeightRole});
|
||||
|
||||
if (isNew) {
|
||||
const auto pos = m_positions.load(m_reader->contentHash());
|
||||
emit restoreReady(pos.chunkIndex, pos.intraOffset);
|
||||
}
|
||||
emitRestoreReady(isNew);
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
}
|
||||
@@ -139,7 +160,7 @@ void ReadingModel::setContentWidth(int width)
|
||||
{
|
||||
if (width == m_contentWidth) return;
|
||||
m_contentWidth = width;
|
||||
if (m_reader) measure(false);
|
||||
if (m_reader && !m_isMangaMode) measure(false);
|
||||
}
|
||||
|
||||
void ReadingModel::setFontParams(int fontSize, qreal lineSpacing)
|
||||
@@ -147,7 +168,7 @@ 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) measure(false);
|
||||
if (m_reader && !m_isMangaMode) measure(false);
|
||||
}
|
||||
|
||||
void ReadingModel::setImageLoader(std::unique_ptr<IImageLoader> loader)
|
||||
@@ -157,6 +178,9 @@ void ReadingModel::setImageLoader(std::unique_ptr<IImageLoader> 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;
|
||||
@@ -167,6 +191,148 @@ void ReadingModel::recalculateCurrentPage()
|
||||
emit currentPageChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::rebuildPagePairs()
|
||||
{
|
||||
const int oldPairs = m_pagePairs.size();
|
||||
m_pagePairs.clear();
|
||||
|
||||
if (!m_isMangaMode || m_chunks.isEmpty()) {
|
||||
if (m_pagePairs.size() != oldPairs) emit totalPagePairsChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_mangaLandscape) {
|
||||
// Portrait: every chunk is its own solo pair.
|
||||
m_pagePairs.reserve(m_chunks.size());
|
||||
for (int i = 0; i < m_chunks.size(); ++i)
|
||||
m_pagePairs.append({i, -1});
|
||||
} else {
|
||||
// Landscape: cover alone, optional shift page alone, then portrait-pair the rest.
|
||||
m_pagePairs.append({0, -1}); // cover always solo
|
||||
int i = 1;
|
||||
if (m_pageShift && i < m_chunks.size()) {
|
||||
m_pagePairs.append({i, -1}); // page 1 solo when shift is ON
|
||||
++i;
|
||||
}
|
||||
while (i < m_chunks.size()) {
|
||||
const QSize sz = m_chunks[i].naturalSize;
|
||||
const bool isLandscape = sz.isValid() && sz.width() > sz.height();
|
||||
if (isLandscape) {
|
||||
m_pagePairs.append({i, -1});
|
||||
++i;
|
||||
} else {
|
||||
// Try to pair with next portrait page.
|
||||
const int next = i + 1;
|
||||
if (next < m_chunks.size()) {
|
||||
const QSize nextSz = m_chunks[next].naturalSize;
|
||||
const bool nextLandscape = nextSz.isValid() && nextSz.width() > nextSz.height();
|
||||
if (!nextLandscape) {
|
||||
m_pagePairs.append({i, next});
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
m_pagePairs.append({i, -1});
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m_pagePairs.size() != oldPairs) emit totalPagePairsChanged();
|
||||
|
||||
// Sync totalPages/currentPage to pair count in manga mode.
|
||||
if (const int pairs = m_pagePairs.size(); m_totalPages != pairs) {
|
||||
m_totalPages = pairs;
|
||||
emit totalPagesChanged();
|
||||
}
|
||||
if (m_currentPage == 0 && !m_pagePairs.isEmpty()) {
|
||||
m_currentPage = 1;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QVariantList ReadingModel::pagePair(int pairIndex) const
|
||||
{
|
||||
if (pairIndex < 0 || pairIndex >= m_pagePairs.size()) return {};
|
||||
const auto &p = m_pagePairs.at(pairIndex);
|
||||
QVariantList list;
|
||||
list.append(p.first);
|
||||
if (p.second >= 0)
|
||||
list.append(p.second);
|
||||
return list;
|
||||
}
|
||||
|
||||
int ReadingModel::pairIndexForChunk(int chunkIndex) const
|
||||
{
|
||||
for (int i = 0; i < m_pagePairs.size(); ++i) {
|
||||
if (m_pagePairs[i].first == chunkIndex || m_pagePairs[i].second == chunkIndex)
|
||||
return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
QString ReadingModel::imageUrlForChunk(int chunkIndex) const
|
||||
{
|
||||
if (chunkIndex < 0 || chunkIndex >= m_chunks.size()) return {};
|
||||
const QString &html = m_chunks.at(chunkIndex).html;
|
||||
// Extract src="..." from the img tag.
|
||||
static const QRegularExpression re(QStringLiteral("<img\\s[^>]*src=\"([^\"]+)\""));
|
||||
const auto match = re.match(html);
|
||||
if (match.hasMatch())
|
||||
return match.captured(1);
|
||||
return {};
|
||||
}
|
||||
|
||||
void ReadingModel::setMangaLandscape(bool landscape)
|
||||
{
|
||||
if (m_mangaLandscape == landscape) return;
|
||||
m_mangaLandscape = landscape;
|
||||
if (m_isMangaMode) rebuildPagePairs();
|
||||
}
|
||||
|
||||
void ReadingModel::setPageShift(bool shift, int currentPairIndex)
|
||||
{
|
||||
if (m_pageShift == shift) return;
|
||||
|
||||
// Pick an anchor chunk that keeps an image on screen across the rebuild AND keeps
|
||||
// back-and-forth toggling stable (no cumulative drift):
|
||||
// - turning shift ON: anchor on the higher-index chunk (.second).
|
||||
// - turning shift OFF: anchor on the lower-index chunk (.first).
|
||||
// For solo pairs (cover, wide spread) fall back to .first.
|
||||
int currentChunk = 0;
|
||||
if (currentPairIndex >= 0 && currentPairIndex < m_pagePairs.size()) {
|
||||
const auto &pair = m_pagePairs.at(currentPairIndex);
|
||||
currentChunk = (shift && pair.second >= 0) ? pair.second : pair.first;
|
||||
}
|
||||
|
||||
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, 0, m_mangaRTL, m_pageShift);
|
||||
|
||||
emit pageShiftChanged();
|
||||
// Tell MangaPageView the exact pair to jump to so it re-evaluates its display binding.
|
||||
emit pagePairsRebuilt(newPair);
|
||||
}
|
||||
|
||||
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);
|
||||
emit mangaRTLChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::loadAllChapters()
|
||||
{
|
||||
beginResetModel();
|
||||
@@ -181,34 +347,43 @@ void ReadingModel::loadAllChapters()
|
||||
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
|
||||
|
||||
if (m_reader) {
|
||||
if (!m_coverImageUrl.isEmpty()) {
|
||||
// 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}); // chunkInChapter=-1 = cover sentinel
|
||||
m_chunks.push_back({coverHtml, 0, -1, -1, QSize()}); // chunkInChapter=-1 = cover sentinel
|
||||
m_chapterStartIndices.append(1); // chapter 0 starts after cover
|
||||
} else {
|
||||
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)
|
||||
m_chunks.push_back({html, i, j++, -1});
|
||||
for (const auto &html : chapter.contentChunks) {
|
||||
const QSize sz = (j < sizes.size()) ? sizes[j] : QSize();
|
||||
m_chunks.push_back({html, i, j, -1, sz});
|
||||
++j;
|
||||
}
|
||||
}
|
||||
}
|
||||
endResetModel();
|
||||
|
||||
if (const int pages = m_chunks.size() * page_ratio; m_totalPages != pages) {
|
||||
m_totalPages = pages;
|
||||
emit totalPagesChanged();
|
||||
}
|
||||
if (const int page = m_chunks.isEmpty() ? 0 : 1; m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
if (m_isMangaMode) {
|
||||
rebuildPagePairs();
|
||||
} else {
|
||||
if (const int pages = m_chunks.size() * page_ratio; m_totalPages != pages) {
|
||||
m_totalPages = pages;
|
||||
emit totalPagesChanged();
|
||||
}
|
||||
if (const int page = m_chunks.isEmpty() ? 0 : 1; m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,6 +393,21 @@ void ReadingModel::setReader(FormatReader *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.
|
||||
if (m_isMangaMode && reader) {
|
||||
const auto pos = m_positions.load(reader->contentHash());
|
||||
m_pageShift = pos.pageShift;
|
||||
m_mangaRTL = pos.mangaRTL;
|
||||
} else {
|
||||
m_pageShift = true;
|
||||
m_mangaRTL = true;
|
||||
}
|
||||
|
||||
loadAllChapters();
|
||||
|
||||
emit totalChaptersChanged();
|
||||
|
||||
Reference in new issue
Block a user