Refactor using Fable
This commit is contained in:
90 files changed
+2080
-2267
No files matched your search
+50
-116
@@ -1,26 +1,20 @@
|
||||
#include "ReadingModel.h"
|
||||
#include "FormatReader.h"
|
||||
#include "IChunkMeasurer.h"
|
||||
#include "IImageLoader.h"
|
||||
#include "ChunkMeasurer.h"
|
||||
#include <QRegularExpression>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <QtConcurrent>
|
||||
|
||||
ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_positions(positions)
|
||||
{
|
||||
connect(&m_measurer, &MeasurementController::measuringChanged,
|
||||
this, &ReadingModel::measuringChanged);
|
||||
connect(&m_measurer, &MeasurementController::heightsReady,
|
||||
this, &ReadingModel::onHeightsReady);
|
||||
}
|
||||
|
||||
ReadingModel::~ReadingModel()
|
||||
{
|
||||
if (m_measureFuture.isRunning()) {
|
||||
m_measureFuture.cancel();
|
||||
m_measureFuture.waitForFinished();
|
||||
}
|
||||
}
|
||||
ReadingModel::~ReadingModel() = default;
|
||||
|
||||
int ReadingModel::rowCount(const QModelIndex &parent) const {
|
||||
if (parent.isValid()) return 0;
|
||||
@@ -59,9 +53,9 @@ 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_measuring; }
|
||||
bool ReadingModel::measuring() const { return m_measurer.measuring(); }
|
||||
qreal ReadingModel::totalPixelHeight() const { return m_totalPixelHeight; }
|
||||
int ReadingModel::totalPagePairs() const { return m_pagePairs.size(); }
|
||||
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; }
|
||||
@@ -109,7 +103,8 @@ void ReadingModel::measure(const bool isNew)
|
||||
for (const auto &c : m_chunks)
|
||||
htmls.append(c.html);
|
||||
|
||||
runMeasurement(htmls, isNew);
|
||||
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)
|
||||
@@ -119,41 +114,19 @@ void ReadingModel::emitRestoreReady(const bool isNew)
|
||||
emit restoreReady(pos.chunkIndex, pos.intraOffset);
|
||||
}
|
||||
|
||||
void ReadingModel::runMeasurement(const QStringList &htmls, const bool isNew)
|
||||
void ReadingModel::onHeightsReady(const QVector<int> &heights, const bool isNew)
|
||||
{
|
||||
if (m_measureFuture.isRunning())
|
||||
m_measureFuture.cancel();
|
||||
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});
|
||||
|
||||
m_measuring = true;
|
||||
emit measuringChanged();
|
||||
|
||||
const int width = m_contentWidth > 0 ? m_contentWidth : 800;
|
||||
const int fontSize = m_fontSize;
|
||||
const qreal lineSpacing = m_lineSpacing;
|
||||
|
||||
std::unique_ptr<IImageLoader> loaderClone = m_imageLoader ? m_imageLoader->clone() : nullptr;
|
||||
auto measurer = new ChunkMeasurer(std::move(loaderClone), fontSize, lineSpacing);
|
||||
|
||||
|
||||
m_measureFuture = QtConcurrent::run(
|
||||
[this, htmls, measurer, width, isNew]() {
|
||||
QVector<int> heights = measurer->measure(htmls, width);
|
||||
QMetaObject::invokeMethod(this, [this, heights, 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);
|
||||
m_measuring = false;
|
||||
emit measuringChanged();
|
||||
emit totalPixelHeightChanged();
|
||||
emit dataChanged(createIndex(0, 0),
|
||||
createIndex(m_chunks.size() - 1, 0),
|
||||
{PixelHeightRole});
|
||||
|
||||
emitRestoreReady(isNew);
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
emitRestoreReady(isNew);
|
||||
}
|
||||
|
||||
void ReadingModel::setContentWidth(int width)
|
||||
@@ -173,7 +146,7 @@ void ReadingModel::setFontParams(int fontSize, qreal lineSpacing)
|
||||
|
||||
void ReadingModel::setImageLoader(std::unique_ptr<IImageLoader> loader)
|
||||
{
|
||||
m_imageLoader = std::move(loader);
|
||||
m_measurer.setImageLoader(std::move(loader));
|
||||
}
|
||||
|
||||
void ReadingModel::recalculateCurrentPage()
|
||||
@@ -193,59 +166,28 @@ void ReadingModel::recalculateCurrentPage()
|
||||
|
||||
void ReadingModel::rebuildPagePairs()
|
||||
{
|
||||
const int oldPairs = m_pagePairs.size();
|
||||
m_pagePairs.clear();
|
||||
const int oldPairs = m_pagination.count();
|
||||
|
||||
if (!m_isMangaMode || m_chunks.isEmpty()) {
|
||||
if (m_pagePairs.size() != oldPairs) emit totalPagePairsChanged();
|
||||
m_pagination.clear();
|
||||
if (m_pagination.count() != 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
QVector<QSize> sizes;
|
||||
sizes.reserve(m_chunks.size());
|
||||
for (const auto &c : m_chunks)
|
||||
sizes.append(c.naturalSize);
|
||||
m_pagination.rebuild(sizes, m_mangaLandscape, m_pageShift);
|
||||
|
||||
if (m_pagePairs.size() != oldPairs) emit totalPagePairsChanged();
|
||||
if (m_pagination.count() != oldPairs) emit totalPagePairsChanged();
|
||||
|
||||
// Sync totalPages/currentPage to pair count in manga mode.
|
||||
if (const int pairs = m_pagePairs.size(); m_totalPages != pairs) {
|
||||
if (const int pairs = m_pagination.count(); m_totalPages != pairs) {
|
||||
m_totalPages = pairs;
|
||||
emit totalPagesChanged();
|
||||
}
|
||||
if (m_currentPage == 0 && !m_pagePairs.isEmpty()) {
|
||||
if (m_currentPage == 0 && m_pagination.count() > 0) {
|
||||
m_currentPage = 1;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
@@ -253,8 +195,8 @@ void ReadingModel::rebuildPagePairs()
|
||||
|
||||
QVariantList ReadingModel::pagePair(int pairIndex) const
|
||||
{
|
||||
if (pairIndex < 0 || pairIndex >= m_pagePairs.size()) return {};
|
||||
const auto &p = m_pagePairs.at(pairIndex);
|
||||
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)
|
||||
@@ -264,23 +206,13 @@ QVariantList ReadingModel::pagePair(int pairIndex) const
|
||||
|
||||
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;
|
||||
return m_pagination.pairIndexForChunk(chunkIndex);
|
||||
}
|
||||
|
||||
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 {};
|
||||
return m_chunks.at(chunkIndex).imageUrl;
|
||||
}
|
||||
|
||||
void ReadingModel::setMangaLandscape(bool landscape)
|
||||
@@ -294,16 +226,9 @@ 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;
|
||||
}
|
||||
// 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();
|
||||
@@ -333,6 +258,14 @@ 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)
|
||||
{
|
||||
static const QRegularExpression re(QStringLiteral("<img\\s[^>]*src=\"([^\"]+)\""));
|
||||
const auto match = re.match(html);
|
||||
return match.hasMatch() ? match.captured(1) : QString();
|
||||
}
|
||||
|
||||
void ReadingModel::loadAllChapters()
|
||||
{
|
||||
beginResetModel();
|
||||
@@ -352,7 +285,8 @@ void ReadingModel::loadAllChapters()
|
||||
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()}); // chunkInChapter=-1 = cover sentinel
|
||||
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);
|
||||
@@ -366,7 +300,7 @@ 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});
|
||||
m_chunks.push_back({html, i, j, -1, sz, extractImageUrl(html)});
|
||||
++j;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user