Refactor using Fable

This commit is contained in:
Agel committed 2026-07-31 17:31:02 +02:00
1 parent 1a60a6b93c
commit 83968646c6
90 files changed
+2080 -2267

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
#include "MangaPagination.h"
void MangaPagination::clear()
{
m_pairs.clear();
}
void MangaPagination::rebuild(const QVector<QSize> &naturalSizes, bool landscape, bool pageShift)
{
m_pairs.clear();
if (naturalSizes.isEmpty())
return;
const int count = naturalSizes.size();
const auto isLandscapePage = [&naturalSizes](int i) {
const QSize sz = naturalSizes.at(i);
return sz.isValid() && sz.width() > sz.height();
};
if (!landscape) {
// Portrait: every chunk is its own solo pair.
m_pairs.reserve(count);
for (int i = 0; i < count; ++i)
m_pairs.append({i, -1});
return;
}
// Landscape: cover alone, optional shift page alone, then portrait-pair the rest.
m_pairs.append({0, -1}); // cover always solo
int i = 1;
if (pageShift && i < count) {
m_pairs.append({i, -1}); // page 1 solo when shift is ON
++i;
}
while (i < count) {
if (isLandscapePage(i)) {
m_pairs.append({i, -1});
++i;
} else {
// Try to pair with next portrait page.
const int next = i + 1;
if (next < count && !isLandscapePage(next)) {
m_pairs.append({i, next});
i += 2;
continue;
}
m_pairs.append({i, -1});
++i;
}
}
}
int MangaPagination::count() const
{
return m_pairs.size();
}
MangaPagination::Pair MangaPagination::at(int pairIndex) const
{
if (pairIndex < 0 || pairIndex >= m_pairs.size())
return {};
return m_pairs.at(pairIndex);
}
int MangaPagination::pairIndexForChunk(int chunkIndex) const
{
for (int i = 0; i < m_pairs.size(); ++i) {
if (m_pairs[i].first == chunkIndex || m_pairs[i].second == chunkIndex)
return i;
}
return 0;
}
int MangaPagination::anchorChunkForShiftToggle(int currentPairIndex, bool shiftTurningOn) const
{
if (currentPairIndex < 0 || currentPairIndex >= m_pairs.size())
return 0;
const Pair &pair = m_pairs.at(currentPairIndex);
return (shiftTurningOn && pair.second >= 0) ? pair.second : pair.first;
}
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include <QSize>
#include <QVector>
// Pure page-pairing logic for manga mode. Given the natural sizes of the
// page images, decides which pages display alone and which form two-page
// spreads. Holds no Qt object state and emits nothing — the owning model
// drives signals and persistence.
class MangaPagination {
public:
struct Pair {
int first = -1;
int second = -1; // -1 = solo page
};
void clear();
// Portrait viewport: every page solo. Landscape: cover solo, optional
// shift page solo, consecutive portrait pages paired, wide pages solo.
void rebuild(const QVector<QSize> &naturalSizes, bool landscape, bool pageShift);
int count() const;
Pair at(int pairIndex) const;
int pairIndexForChunk(int chunkIndex) const;
// Chunk to keep on screen across a shift toggle. Anchoring on the
// higher-index chunk when shift turns on (and lower when it turns off)
// keeps repeated back-and-forth toggling stable with no drift.
int anchorChunkForShiftToggle(int currentPairIndex, bool shiftTurningOn) const;
private:
QVector<Pair> m_pairs;
};
@@ -0,0 +1,50 @@
#include "MeasurementController.h"
#include "backend/ChunkMeasurer.h"
#include <QtConcurrent>
MeasurementController::MeasurementController(QObject *parent)
: QObject(parent)
{
}
MeasurementController::~MeasurementController()
{
if (m_future.isRunning()) {
m_future.cancel();
m_future.waitForFinished();
}
}
void MeasurementController::setImageLoader(std::unique_ptr<IImageLoader> loader)
{
m_imageLoader = std::move(loader);
}
bool MeasurementController::measuring() const
{
return m_measuring;
}
void MeasurementController::start(const QStringList &htmls, int contentWidth,
int fontSize, qreal lineSpacing, bool isNew)
{
if (m_future.isRunning())
m_future.cancel();
m_measuring = true;
emit measuringChanged();
std::unique_ptr<IImageLoader> loaderClone = m_imageLoader ? m_imageLoader->clone() : nullptr;
auto measurer = std::make_shared<ChunkMeasurer>(std::move(loaderClone), fontSize, lineSpacing);
m_future = QtConcurrent::run([this, htmls, measurer, contentWidth, isNew]() {
QVector<int> heights = measurer->measure(htmls, contentWidth);
QMetaObject::invokeMethod(this, [this, heights, isNew] {
m_measuring = false;
emit measuringChanged();
emit heightsReady(heights, isNew);
}, Qt::QueuedConnection);
});
}
@@ -0,0 +1,37 @@
#pragma once
#include <QFuture>
#include <QObject>
#include <QStringList>
#include <QVector>
#include <memory>
#include "backend/IImageLoader.h"
// Runs chunk height measurement off the GUI thread and reports the results
// back on it. Owns the image loader used to resolve embedded images and the
// lifetime of the in-flight measurement.
class MeasurementController : public QObject {
Q_OBJECT
public:
explicit MeasurementController(QObject *parent = nullptr);
~MeasurementController() override;
void setImageLoader(std::unique_ptr<IImageLoader> loader);
// isNew is passed through untouched to heightsReady so the owner knows
// whether this measurement should trigger a position restore.
void start(const QStringList &htmls, int contentWidth,
int fontSize, qreal lineSpacing, bool isNew);
bool measuring() const;
signals:
void measuringChanged();
void heightsReady(QVector<int> heights, bool isNew);
private:
std::unique_ptr<IImageLoader> m_imageLoader;
bool m_measuring = false;
QFuture<void> m_future;
};