old render cleanup part 1
This commit is contained in:
38 files changed
+166
-1366
No files matched your search
@@ -34,36 +34,22 @@ AppCoordinator::AppCoordinator(Book &book,
|
||||
, m_formatRegistry(formatRegistry)
|
||||
, m_engine(engine)
|
||||
{
|
||||
// Typography changes → re-push font params into ReadingModel
|
||||
connect(&m_readingPreferences, &ReadingPreferences::fontSizeChanged,
|
||||
this, &AppCoordinator::onFontPrefsChanged);
|
||||
connect(&m_readingPreferences, &ReadingPreferences::lineSpacingChanged,
|
||||
this, &AppCoordinator::onFontPrefsChanged);
|
||||
|
||||
connect(&m_book, &Book::willOpenNewBook, this, &AppCoordinator::onWillOpenNewBook);
|
||||
connect(&m_book, &Book::positionSaved, this, &AppCoordinator::onPositionSaved);
|
||||
connect(&m_book, &Book::bookChanged, this, &AppCoordinator::onBookChanged);
|
||||
connect(&m_book, &Book::contentWidthChanged, this, &AppCoordinator::onContentWidthChanged);
|
||||
|
||||
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
|
||||
this, &AppCoordinator::onAboutToQuit);
|
||||
}
|
||||
|
||||
void AppCoordinator::onFontPrefsChanged()
|
||||
{
|
||||
m_readingModel.setFontParams(m_readingPreferences.fontSize(),
|
||||
m_readingPreferences.lineSpacing());
|
||||
}
|
||||
|
||||
// Before a new book open: snapshot the outgoing book's progress into the library.
|
||||
// Scroll position is handled by PageView's scroll tracker via book.saveCurrentPosition().
|
||||
void AppCoordinator::onWillOpenNewBook()
|
||||
{
|
||||
if (m_book.hasBook())
|
||||
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
|
||||
}
|
||||
|
||||
// Real-time history update: fired by PageView's scrollTrackTimer on every scroll.
|
||||
// Real-time history update: fired when position is saved.
|
||||
void AppCoordinator::onPositionSaved()
|
||||
{
|
||||
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
|
||||
@@ -87,34 +73,18 @@ void AppCoordinator::onBookChanged()
|
||||
m_engine.removeImageProvider(provider->scheme());
|
||||
m_engine.addImageProvider(provider->scheme(), provider);
|
||||
provider->setFilePath(filePath);
|
||||
provider->setDisplayWidth(m_contentWidth);
|
||||
m_currentImageProvider = provider;
|
||||
|
||||
if (!reader->coverImagePath().isEmpty())
|
||||
m_book.setCoverImageUrl(provider->coverImageUrl(reader->coverImagePath()));
|
||||
}
|
||||
|
||||
m_readingModel.setFontParams(m_readingPreferences.fontSize(),
|
||||
m_readingPreferences.lineSpacing());
|
||||
m_readingModel.setImageLoader(m_formatRegistry.createImageLoader(filePath));
|
||||
m_readingModel.setCoverImageUrl(m_book.coverImageUrl());
|
||||
m_readingModel.setReader(reader);
|
||||
m_readingModel.measure(true);
|
||||
|
||||
m_uiState.setLastFilePath(filePath);
|
||||
m_historyModel.addOrUpdateBook(filePath, -1.0);
|
||||
}
|
||||
|
||||
// Propagate content-width changes to image provider + reading model.
|
||||
// Called from Book::setContentWidth which is triggered by QML's PageView.
|
||||
void AppCoordinator::onContentWidthChanged(int width)
|
||||
{
|
||||
m_contentWidth = width;
|
||||
if (m_currentImageProvider)
|
||||
m_currentImageProvider->setDisplayWidth(width);
|
||||
m_readingModel.setContentWidth(width);
|
||||
}
|
||||
|
||||
// Quit: flush everything to persistent storage.
|
||||
// Scroll position was last saved by PageView's scroll tracker; we save library progress here.
|
||||
void AppCoordinator::onAboutToQuit()
|
||||
|
||||
@@ -35,8 +35,6 @@ private slots:
|
||||
void onBookChanged();
|
||||
void onWillOpenNewBook();
|
||||
void onPositionSaved();
|
||||
void onContentWidthChanged(int width);
|
||||
void onFontPrefsChanged();
|
||||
void onAboutToQuit();
|
||||
|
||||
private:
|
||||
@@ -51,5 +49,4 @@ private:
|
||||
QQmlApplicationEngine &m_engine;
|
||||
|
||||
IImageProvider *m_currentImageProvider = nullptr;
|
||||
int m_contentWidth = 0;
|
||||
};
|
||||
@@ -1,96 +0,0 @@
|
||||
#include "ChunkMeasurer.h"
|
||||
#include <QFont>
|
||||
#include <QFontMetricsF>
|
||||
#include <QImage>
|
||||
#include <QTextBlockFormat>
|
||||
#include <QTextCursor>
|
||||
#include <QTextDocument>
|
||||
#include <QTextOption>
|
||||
#include <QThread>
|
||||
#include <QtConcurrent>
|
||||
#include <QAtomicInt>
|
||||
|
||||
// Private: one instance per worker thread — owns its IImageLoader clone
|
||||
class ImageLoadingDocument : public QTextDocument {
|
||||
IImageLoader *m_loader = nullptr;
|
||||
int m_maxWidth = 0;
|
||||
public:
|
||||
void setLoader(IImageLoader *loader, int maxWidth) {
|
||||
m_loader = loader;
|
||||
m_maxWidth = maxWidth;
|
||||
}
|
||||
protected:
|
||||
QVariant loadResource(int type, const QUrl &url) override {
|
||||
if (type == QTextDocument::ImageResource && m_loader) {
|
||||
QImage img = m_loader->load(url, m_maxWidth);
|
||||
if (!img.isNull()) return img;
|
||||
}
|
||||
return QTextDocument::loadResource(type, url);
|
||||
}
|
||||
};
|
||||
|
||||
ChunkMeasurer::ChunkMeasurer(std::unique_ptr<IImageLoader> imageLoader,
|
||||
int fontSize, qreal lineSpacing)
|
||||
: m_imageLoader(std::move(imageLoader))
|
||||
, m_fontSize(fontSize)
|
||||
, m_lineSpacing(lineSpacing)
|
||||
{}
|
||||
|
||||
QVector<int> ChunkMeasurer::measure(const QStringList &chunks, qreal textWidth)
|
||||
{
|
||||
const int n = chunks.size();
|
||||
if (n == 0) return {};
|
||||
if (textWidth <= 0) textWidth = 1.0;
|
||||
|
||||
QVector<int> heights(n, 0);
|
||||
QAtomicInt nextIdx(0);
|
||||
const int threadCount = 2;
|
||||
|
||||
// Capture by value what each worker needs (no shared mutable state)
|
||||
auto worker = [&]() {
|
||||
std::unique_ptr<IImageLoader> loader =
|
||||
m_imageLoader ? m_imageLoader->clone() : nullptr;
|
||||
|
||||
ImageLoadingDocument doc;
|
||||
doc.setLoader(loader.get(), qRound(textWidth));
|
||||
doc.setDocumentMargin(0);
|
||||
doc.setUndoRedoEnabled(false);
|
||||
|
||||
QTextOption textOption = doc.defaultTextOption();
|
||||
textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
|
||||
doc.setDefaultTextOption(textOption);
|
||||
|
||||
QFont font;
|
||||
font.setPixelSize(m_fontSize);
|
||||
doc.setDefaultFont(font);
|
||||
|
||||
for (;;) {
|
||||
const int i = nextIdx.fetchAndAddRelaxed(1);
|
||||
if (i >= n) break;
|
||||
|
||||
doc.setHtml(chunks[i]);
|
||||
doc.setTextWidth(textWidth);
|
||||
|
||||
QTextBlockFormat fmt;
|
||||
fmt.setLineHeight(m_lineSpacing * 100.0, QTextBlockFormat::ProportionalHeight);
|
||||
QTextCursor cursor(&doc);
|
||||
cursor.select(QTextCursor::Document);
|
||||
cursor.mergeBlockFormat(fmt);
|
||||
|
||||
// QTextDocument includes trailing line spacing after the last line,
|
||||
// but QML's Text element does not. Subtract the correction.
|
||||
QFontMetricsF fm(font);
|
||||
qreal trailingCorrection = (m_lineSpacing - 1.0) * fm.lineSpacing();
|
||||
heights[i] = qRound(doc.size().height() - trailingCorrection);
|
||||
}
|
||||
};
|
||||
|
||||
QList<QFuture<void>> futures;
|
||||
futures.reserve(threadCount);
|
||||
for (int t = 0; t < threadCount; ++t)
|
||||
futures.append(QtConcurrent::run(worker));
|
||||
for (auto &f : futures)
|
||||
f.waitForFinished();
|
||||
|
||||
return heights;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#ifndef CHUNKMEASURER_H
|
||||
#define CHUNKMEASURER_H
|
||||
#include "IChunkMeasurer.h"
|
||||
#include "IImageLoader.h"
|
||||
#include <memory>
|
||||
class ChunkMeasurer : public IChunkMeasurer {
|
||||
public:
|
||||
explicit ChunkMeasurer(std::unique_ptr<IImageLoader> imageLoader = nullptr,
|
||||
int fontSize = 16, qreal lineSpacing = 1.0);
|
||||
QVector<int> measure(const QStringList &chunks, qreal textWidth) override;
|
||||
private:
|
||||
std::unique_ptr<IImageLoader> m_imageLoader;
|
||||
int m_fontSize;
|
||||
qreal m_lineSpacing;
|
||||
};
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
#ifndef ICHUNKMEASURER_H
|
||||
#define ICHUNKMEASURER_H
|
||||
#include <QStringList>
|
||||
#include <QVector>
|
||||
#include <QtGlobal>
|
||||
class IChunkMeasurer {
|
||||
public:
|
||||
virtual ~IChunkMeasurer() = default;
|
||||
virtual QVector<int> measure(const QStringList &chunks, qreal textWidth) = 0;
|
||||
};
|
||||
#endif
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include <QImage>
|
||||
#include <QUrl>
|
||||
#include <memory>
|
||||
class IImageLoader {
|
||||
public:
|
||||
virtual ~IImageLoader() = default;
|
||||
virtual QImage load(const QUrl &url, int maxWidth) const = 0;
|
||||
virtual std::unique_ptr<IImageLoader> clone() const = 0;
|
||||
};
|
||||
@@ -6,7 +6,6 @@ class IImageProvider : public QQuickAsyncImageProvider {
|
||||
public:
|
||||
virtual ~IImageProvider() = default;
|
||||
virtual void setFilePath(const QString &filePath) = 0;
|
||||
virtual void setDisplayWidth(int width) = 0;
|
||||
virtual void clear() = 0;
|
||||
// Returns the full QML image URL for a given internal path (e.g., "image://epub/OEBPS/cover.jpg")
|
||||
virtual QString coverImageUrl(const QString &coverPath) const = 0;
|
||||
|
||||
+29
-145
@@ -2,47 +2,15 @@
|
||||
#include "FormatReader.h"
|
||||
#include <QRegularExpression>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
: QObject(parent)
|
||||
, m_positions(positions)
|
||||
{
|
||||
connect(&m_measurer, &MeasurementController::measuringChanged,
|
||||
this, &ReadingModel::measuringChanged);
|
||||
connect(&m_measurer, &MeasurementController::heightsReady,
|
||||
this, &ReadingModel::onHeightsReady);
|
||||
}
|
||||
|
||||
ReadingModel::~ReadingModel() = default;
|
||||
|
||||
int ReadingModel::rowCount(const QModelIndex &parent) const {
|
||||
if (parent.isValid()) return 0;
|
||||
return m_chunks.size();
|
||||
}
|
||||
|
||||
QVariant ReadingModel::data(const QModelIndex &index, int role) const {
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= (int)m_chunks.size())
|
||||
return {};
|
||||
const auto &c = m_chunks.at(index.row());
|
||||
switch (role) {
|
||||
case ContentRole: return c.html;
|
||||
case PixelHeightRole: return c.pixelHeight;
|
||||
case ChapterRole: return c.chapterIndex;
|
||||
case NaturalSizeRole: return c.naturalSize;
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ReadingModel::roleNames() const {
|
||||
return {
|
||||
{ContentRole, "content"},
|
||||
{PixelHeightRole, "pixelHeight"},
|
||||
{ChapterRole, "chapter"},
|
||||
{NaturalSizeRole, "naturalSize"},
|
||||
};
|
||||
}
|
||||
|
||||
QString ReadingModel::chapterTitle() const
|
||||
{
|
||||
if (m_currentChapter < 0 || m_currentChapter >= m_chapterTitles.size())
|
||||
@@ -53,8 +21,6 @@ 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_measurer.measuring(); }
|
||||
qreal ReadingModel::totalPixelHeight() const { return m_totalPixelHeight; }
|
||||
int ReadingModel::totalPagePairs() const { return m_pagination.count(); }
|
||||
bool ReadingModel::isMangaMode() const { return m_isMangaMode; }
|
||||
bool ReadingModel::pageShift() const { return m_pageShift; }
|
||||
@@ -79,89 +45,13 @@ void ReadingModel::setScrollInfo(const int topChunkIndex, const qreal proportion
|
||||
emit chapterChanged();
|
||||
}
|
||||
|
||||
recalculateCurrentPage();
|
||||
}
|
||||
|
||||
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;
|
||||
const int page = pairIndexForChunk(topChunkIndex) + 1;
|
||||
if (m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
}
|
||||
|
||||
m_totalPixelHeight = 0;
|
||||
|
||||
QStringList htmls;
|
||||
htmls.reserve(m_chunks.size());
|
||||
for (const auto &c : m_chunks)
|
||||
htmls.append(c.html);
|
||||
|
||||
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)
|
||||
{
|
||||
if (!isNew) return;
|
||||
const auto pos = m_positions.load(m_reader->contentHash());
|
||||
emit restoreReady(pos.chunkIndex, pos.intraOffset);
|
||||
}
|
||||
|
||||
void ReadingModel::onHeightsReady(const QVector<int> &heights, const bool 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);
|
||||
emit totalPixelHeightChanged();
|
||||
emit dataChanged(createIndex(0, 0),
|
||||
createIndex(m_chunks.size() - 1, 0),
|
||||
{PixelHeightRole});
|
||||
|
||||
emitRestoreReady(isNew);
|
||||
}
|
||||
|
||||
void ReadingModel::setContentWidth(int width)
|
||||
{
|
||||
if (width == m_contentWidth) return;
|
||||
m_contentWidth = width;
|
||||
if (m_reader && !m_isMangaMode) measure(false);
|
||||
}
|
||||
|
||||
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 && !m_isMangaMode) measure(false);
|
||||
}
|
||||
|
||||
void ReadingModel::setImageLoader(std::unique_ptr<IImageLoader> loader)
|
||||
{
|
||||
m_measurer.setImageLoader(std::move(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;
|
||||
}
|
||||
if (m_currentPage == page)
|
||||
return;
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::rebuildPagePairs()
|
||||
@@ -242,7 +132,7 @@ void ReadingModel::setPageShift(bool shift, int currentPairIndex)
|
||||
|
||||
// Persist immediately.
|
||||
if (m_reader)
|
||||
m_positions.save(m_reader->contentHash(), currentChunk, 0, m_mangaRTL, m_pageShift);
|
||||
m_positions.save(m_reader->contentHash(), currentChunk, m_mangaRTL, m_pageShift);
|
||||
|
||||
emit pageShiftChanged();
|
||||
// Tell MangaPageView the exact pair to jump to so it re-evaluates its display binding.
|
||||
@@ -253,8 +143,10 @@ 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);
|
||||
if (m_reader) {
|
||||
const int currentChunk = (m_currentPage > 0) ? m_pagination.at(m_currentPage - 1).first : 0;
|
||||
m_positions.save(m_reader->contentHash(), currentChunk >= 0 ? currentChunk : 0, m_mangaRTL, m_pageShift);
|
||||
}
|
||||
emit mangaRTLChanged();
|
||||
}
|
||||
|
||||
@@ -268,29 +160,16 @@ static QString extractImageUrl(const QString &html)
|
||||
|
||||
void ReadingModel::loadAllChapters()
|
||||
{
|
||||
beginResetModel();
|
||||
m_chunks.clear();
|
||||
m_chapterStartIndices.clear();
|
||||
m_chapterTitles.clear();
|
||||
m_totalPixelHeight = 0.0;
|
||||
emit totalPixelHeightChanged();
|
||||
m_currentChapter = 0;
|
||||
|
||||
if (m_currentPage != 0) { m_currentPage = 0; emit currentPageChanged(); }
|
||||
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
|
||||
|
||||
if (m_reader) {
|
||||
// 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, QSize(),
|
||||
extractImageUrl(coverHtml)}); // chunkInChapter=-1 = cover sentinel
|
||||
m_chapterStartIndices.append(1); // chapter 0 starts after cover
|
||||
} else {
|
||||
m_chapterStartIndices.append(0);
|
||||
}
|
||||
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);
|
||||
@@ -300,18 +179,17 @@ 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, extractImageUrl(html)});
|
||||
m_chunks.push_back({i, sz, extractImageUrl(html)});
|
||||
++j;
|
||||
}
|
||||
}
|
||||
}
|
||||
endResetModel();
|
||||
|
||||
if (m_isMangaMode) {
|
||||
rebuildPagePairs();
|
||||
} else {
|
||||
if (const int pages = m_chunks.size() * page_ratio; m_totalPages != pages) {
|
||||
m_totalPages = pages;
|
||||
if (m_totalPages != m_chunks.size()) {
|
||||
m_totalPages = m_chunks.size();
|
||||
emit totalPagesChanged();
|
||||
}
|
||||
if (const int page = m_chunks.isEmpty() ? 0 : 1; m_currentPage != page) {
|
||||
@@ -333,10 +211,17 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
emit isMangaModeChanged();
|
||||
|
||||
// Load per-book pageShift and mangaRTL before loadAllChapters so rebuildPagePairs uses the saved values.
|
||||
if (m_isMangaMode && reader) {
|
||||
int restoreChunk = 0;
|
||||
if (reader) {
|
||||
const auto pos = m_positions.load(reader->contentHash());
|
||||
m_pageShift = pos.pageShift;
|
||||
m_mangaRTL = pos.mangaRTL;
|
||||
restoreChunk = pos.chunkIndex;
|
||||
if (m_isMangaMode) {
|
||||
m_pageShift = pos.pageShift;
|
||||
m_mangaRTL = pos.mangaRTL;
|
||||
} else {
|
||||
m_pageShift = true;
|
||||
m_mangaRTL = true;
|
||||
}
|
||||
} else {
|
||||
m_pageShift = true;
|
||||
m_mangaRTL = true;
|
||||
@@ -346,12 +231,11 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
|
||||
emit totalChaptersChanged();
|
||||
emit chapterChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::setCoverImageUrl(const QString &coverImageUrl)
|
||||
{
|
||||
if (m_coverImageUrl != coverImageUrl) {
|
||||
m_coverImageUrl = coverImageUrl;
|
||||
if (m_reader) {
|
||||
QMetaObject::invokeMethod(this, [this, restoreChunk] {
|
||||
emit restoreReady(restoreChunk);
|
||||
}, Qt::QueuedConnection);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,28 +1,23 @@
|
||||
#ifndef READINGMODEL_H
|
||||
#define READINGMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QObject>
|
||||
#include <QSize>
|
||||
#include <QString>
|
||||
#include <QVariantList>
|
||||
#include <QVector>
|
||||
#include <memory>
|
||||
#include "IImageLoader.h"
|
||||
#include "reading/MangaPagination.h"
|
||||
#include "reading/MeasurementController.h"
|
||||
#include "state/BookPositionStore.h"
|
||||
|
||||
class FormatReader;
|
||||
|
||||
struct ContentChunk {
|
||||
QString html; // empty when unloaded (lazy streaming)
|
||||
int chapterIndex = 0; // which chapter owns this chunk
|
||||
int chunkInChapter = 0; // 0-based index within chapter (-1 for cover)
|
||||
int pixelHeight = -1; // -1 = not yet measured
|
||||
QSize naturalSize; // image pixel dimensions (null for text chunks)
|
||||
QString imageUrl; // src of the chunk's img tag (empty for text chunks)
|
||||
int chapterIndex = 0;
|
||||
QSize naturalSize;
|
||||
QString imageUrl;
|
||||
};
|
||||
|
||||
class ReadingModel : public QAbstractListModel {
|
||||
class ReadingModel : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString chapterTitle READ chapterTitle NOTIFY chapterChanged)
|
||||
Q_PROPERTY(int currentChapter READ currentChapter NOTIFY chapterChanged)
|
||||
@@ -31,28 +26,15 @@ class ReadingModel : public QAbstractListModel {
|
||||
Q_PROPERTY(int currentPage READ currentPage NOTIFY currentPageChanged)
|
||||
Q_PROPERTY(int totalPages READ totalPages NOTIFY totalPagesChanged)
|
||||
Q_PROPERTY(bool hasChapters READ hasChapters NOTIFY totalChaptersChanged)
|
||||
Q_PROPERTY(bool measuring READ measuring NOTIFY measuringChanged)
|
||||
Q_PROPERTY(qreal totalPixelHeight READ totalPixelHeight NOTIFY totalPixelHeightChanged)
|
||||
Q_PROPERTY(int totalPagePairs READ totalPagePairs NOTIFY totalPagePairsChanged)
|
||||
Q_PROPERTY(bool isMangaMode READ isMangaMode NOTIFY isMangaModeChanged)
|
||||
Q_PROPERTY(bool pageShift READ pageShift NOTIFY pageShiftChanged)
|
||||
Q_PROPERTY(bool mangaRTL READ mangaRTL NOTIFY mangaRTLChanged)
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
ContentRole = Qt::UserRole + 1,
|
||||
PixelHeightRole = Qt::UserRole + 2,
|
||||
ChapterRole = Qt::UserRole + 3,
|
||||
NaturalSizeRole = Qt::UserRole + 4,
|
||||
};
|
||||
|
||||
explicit ReadingModel(BookPositionStore &positions, QObject *parent = nullptr);
|
||||
~ReadingModel() override;
|
||||
|
||||
int rowCount(const QModelIndex &parent) const override;
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
QString chapterTitle() const;
|
||||
int currentChapter() const;
|
||||
int totalChapters() const;
|
||||
@@ -60,17 +42,14 @@ public:
|
||||
int currentPage() const;
|
||||
int totalPages() const;
|
||||
bool hasChapters() const;
|
||||
bool measuring() const;
|
||||
qreal totalPixelHeight() const;
|
||||
int totalPagePairs() const;
|
||||
bool isMangaMode() const;
|
||||
bool pageShift() const;
|
||||
bool mangaRTL() const;
|
||||
|
||||
void setReader(FormatReader *reader);
|
||||
void setCoverImageUrl(const QString &coverImageUrl);
|
||||
|
||||
// Called from QML on every scroll (real-time)
|
||||
// Called from QML on every page navigation
|
||||
Q_INVOKABLE void setScrollInfo(int topChunkIndex, qreal proportion);
|
||||
|
||||
Q_INVOKABLE void nextChapter();
|
||||
@@ -90,19 +69,9 @@ public:
|
||||
Q_INVOKABLE void setPageShift(bool shift, int currentPairIndex = 0);
|
||||
Q_INVOKABLE void setMangaRTL(bool rtl);
|
||||
|
||||
// Measurement
|
||||
Q_INVOKABLE void measure(bool isNew);
|
||||
|
||||
// Preferences & state
|
||||
void setContentWidth(int width);
|
||||
Q_INVOKABLE void setFontParams(int fontSize, qreal lineSpacing);
|
||||
void setImageLoader(std::unique_ptr<IImageLoader> loader);
|
||||
|
||||
signals:
|
||||
void chapterChanged();
|
||||
void totalChaptersChanged();
|
||||
void measuringChanged();
|
||||
void totalPixelHeightChanged();
|
||||
void currentPageChanged();
|
||||
void totalPagesChanged();
|
||||
void totalPagePairsChanged();
|
||||
@@ -113,21 +82,13 @@ signals:
|
||||
void pagePairsRebuilt(int newPairIndex);
|
||||
void scrollToChunkRequested(int chunkIndex);
|
||||
|
||||
void restoreReady(int chunkIndex, int intraChunkOffset);
|
||||
|
||||
private slots:
|
||||
void onHeightsReady(const QVector<int> &heights, bool isNew);
|
||||
void restoreReady(int chunkIndex);
|
||||
|
||||
private:
|
||||
static constexpr qreal page_ratio = 1.5;
|
||||
|
||||
void loadAllChapters();
|
||||
void recalculateCurrentPage();
|
||||
void rebuildPagePairs();
|
||||
void emitRestoreReady(bool isNew);
|
||||
|
||||
FormatReader *m_reader = nullptr;
|
||||
QString m_coverImageUrl;
|
||||
int m_currentChapter = 0;
|
||||
int m_totalChapters = 0;
|
||||
int m_currentPage = 0;
|
||||
@@ -141,17 +102,10 @@ private:
|
||||
QVector<int> m_chapterStartIndices;
|
||||
QVector<QString> m_chapterTitles;
|
||||
|
||||
// Manga page-pair layout. In RTL mode the "left" slot is the later page visually.
|
||||
MangaPagination m_pagination;
|
||||
|
||||
qreal m_totalPixelHeight = 0.0;
|
||||
qreal m_yOffset = 0.0;
|
||||
int m_contentWidth = 0;
|
||||
int m_fontSize = 0;
|
||||
qreal m_lineSpacing = 0;
|
||||
|
||||
BookPositionStore &m_positions;
|
||||
MeasurementController m_measurer;
|
||||
};
|
||||
|
||||
#endif // READINGMODEL_H
|
||||
@@ -143,16 +143,9 @@ void Book::clearError()
|
||||
emit errorMessageChanged();
|
||||
}
|
||||
|
||||
void Book::saveCurrentPosition(int chunkIndex, int intraOffset, bool pageShift)
|
||||
void Book::saveCurrentPosition(int chunkIndex, bool mangaRTL, bool pageShift)
|
||||
{
|
||||
if (!m_reader) return;
|
||||
m_positions.save(m_reader->contentHash(), chunkIndex, intraOffset, pageShift);
|
||||
m_positions.save(m_reader->contentHash(), chunkIndex, mangaRTL, pageShift);
|
||||
emit positionSaved();
|
||||
}
|
||||
|
||||
void Book::setContentWidth(int width)
|
||||
{
|
||||
if (m_contentWidth == width) return;
|
||||
m_contentWidth = width;
|
||||
emit contentWidthChanged(width);
|
||||
}
|
||||
@@ -51,12 +51,8 @@ public:
|
||||
Q_INVOKABLE void close();
|
||||
Q_INVOKABLE void clearError();
|
||||
|
||||
// Save current scroll position. Called from QML's scroll tracker; forwards to BookPositionStore.
|
||||
Q_INVOKABLE void saveCurrentPosition(int chunkIndex, int intraOffset, bool pageShift = false);
|
||||
|
||||
// Called from QML's PageView when its width changes. Propagates to image provider + ReadingModel
|
||||
// via the contentWidthChanged signal (wired in main.cpp).
|
||||
Q_INVOKABLE void setContentWidth(int width);
|
||||
// Save current position. Called from QML; forwards to BookPositionStore.
|
||||
Q_INVOKABLE void saveCurrentPosition(int chunkIndex, bool mangaRTL = true, bool pageShift = true);
|
||||
|
||||
signals:
|
||||
// Emitted just before replacing the current reader with a new one.
|
||||
@@ -67,7 +63,6 @@ signals:
|
||||
void bookChanged();
|
||||
void loadingChanged();
|
||||
void errorMessageChanged();
|
||||
void contentWidthChanged(int width);
|
||||
|
||||
private:
|
||||
void openImpl(const QString &filePath);
|
||||
@@ -80,7 +75,6 @@ private:
|
||||
QString m_errorMessage;
|
||||
bool m_loading = false;
|
||||
int m_openGeneration = 0;
|
||||
int m_contentWidth = 0;
|
||||
QFuture<void> m_openFuture;
|
||||
};
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
QImage EntryImageProvider::loadImageFromSource(IByteSource &source,
|
||||
const QString &entryName,
|
||||
int displayWidth,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
if (entryName.isEmpty())
|
||||
@@ -25,7 +24,7 @@ QImage EntryImageProvider::loadImageFromSource(IByteSource &source,
|
||||
if (!image.loadFromData(data))
|
||||
return {};
|
||||
|
||||
return scaleToFit(image, displayWidth, requestedSize);
|
||||
return scaleToFit(image, requestedSize);
|
||||
}
|
||||
|
||||
EntryImageProvider::EntryImageProvider(SourceFactory sourceFactory, QString scheme)
|
||||
@@ -38,11 +37,9 @@ QQuickImageResponse *EntryImageProvider::requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
QString sourcePath;
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
displayWidth = m_displayWidth;
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
|
||||
// id may be "entry/name.jpg?src=<encoded-path>"
|
||||
@@ -52,50 +49,16 @@ QQuickImageResponse *EntryImageProvider::requestImageResponse(const QString &id,
|
||||
sourcePath = parsed.sourcePath;
|
||||
|
||||
auto *response = new AsyncImageResponse(
|
||||
[factory = m_sourceFactory, sourcePath, entryName, displayWidth, requestedSize]() -> QImage {
|
||||
[factory = m_sourceFactory, sourcePath, entryName, requestedSize]() -> QImage {
|
||||
auto source = factory();
|
||||
if (!source->open(sourcePath))
|
||||
return {};
|
||||
return loadImageFromSource(*source, entryName, displayWidth, requestedSize);
|
||||
return loadImageFromSource(*source, entryName, requestedSize);
|
||||
});
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
QImage EntryImageProvider::load(const QUrl &url, int maxWidth) const
|
||||
{
|
||||
if (url.scheme() != QLatin1String("image"))
|
||||
return {};
|
||||
|
||||
const ImageUrl::Parsed parsed = ImageUrl::parseImageUrl(url);
|
||||
const QString entryName = parsed.entry;
|
||||
if (entryName.isEmpty())
|
||||
return {};
|
||||
|
||||
QString sourcePath = parsed.sourcePath;
|
||||
if (sourcePath.isEmpty()) {
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
|
||||
auto source = m_sourceFactory();
|
||||
if (!source->open(sourcePath))
|
||||
return {};
|
||||
return loadImageFromSource(*source, entryName, maxWidth, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> EntryImageProvider::clone() const
|
||||
{
|
||||
QString sourcePath;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
auto c = std::make_unique<EntryImageProvider>(m_sourceFactory, m_scheme);
|
||||
c->setFilePath(sourcePath);
|
||||
return c;
|
||||
}
|
||||
|
||||
void EntryImageProvider::setFilePath(const QString &filePath)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
@@ -114,15 +77,8 @@ QString EntryImageProvider::scheme() const
|
||||
return m_scheme;
|
||||
}
|
||||
|
||||
void EntryImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
void EntryImageProvider::clear()
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_sourcePath.clear();
|
||||
m_displayWidth = 0;
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "IByteSource.h"
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
|
||||
#include <functional>
|
||||
@@ -9,7 +8,7 @@
|
||||
#include <QMutex>
|
||||
#include <QString>
|
||||
|
||||
class EntryImageProvider : public IImageProvider, public IImageLoader {
|
||||
class EntryImageProvider : public IImageProvider {
|
||||
public:
|
||||
using SourceFactory = std::function<std::unique_ptr<IByteSource>()>;
|
||||
|
||||
@@ -19,17 +18,12 @@ public:
|
||||
const QSize &requestedSize) override;
|
||||
|
||||
void setFilePath(const QString &filePath) override;
|
||||
void setDisplayWidth(int width) override;
|
||||
void clear() override;
|
||||
QString coverImageUrl(const QString &coverPath) const override;
|
||||
QString scheme() const override;
|
||||
|
||||
QImage load(const QUrl &url, int maxWidth) const override;
|
||||
std::unique_ptr<IImageLoader> clone() const override;
|
||||
|
||||
static QImage loadImageFromSource(IByteSource &source,
|
||||
const QString &entryName,
|
||||
int displayWidth,
|
||||
const QSize &requestedSize);
|
||||
|
||||
private:
|
||||
@@ -37,5 +31,4 @@ private:
|
||||
QString m_scheme;
|
||||
mutable QMutex m_mutex;
|
||||
QString m_sourcePath;
|
||||
int m_displayWidth = 0;
|
||||
};
|
||||
@@ -7,36 +7,6 @@
|
||||
#include <QFileInfo>
|
||||
#include <QQmlEngine>
|
||||
|
||||
// Dispatches load() calls to the right child loader based on the image:// host (scheme name).
|
||||
class CompositeImageLoader : public IImageLoader {
|
||||
public:
|
||||
void add(const QString &host, std::unique_ptr<IImageLoader> loader)
|
||||
{
|
||||
m_loaders.emplace_back(host, std::move(loader));
|
||||
}
|
||||
|
||||
QImage load(const QUrl &url, int maxWidth) const override
|
||||
{
|
||||
const QString host = url.host();
|
||||
for (const auto &[h, loader] : m_loaders) {
|
||||
if (h == host)
|
||||
return loader->load(url, maxWidth);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> clone() const override
|
||||
{
|
||||
auto c = std::make_unique<CompositeImageLoader>();
|
||||
for (const auto &[host, loader] : m_loaders)
|
||||
c->add(host, loader->clone());
|
||||
return c;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::pair<QString, std::unique_ptr<IImageLoader>>> m_loaders;
|
||||
};
|
||||
|
||||
FormatRegistry::FormatRegistry(QVector<FormatSpec> specs)
|
||||
: m_specs(std::move(specs))
|
||||
{
|
||||
@@ -69,24 +39,6 @@ IImageProvider *FormatRegistry::createImageProvider(const QString &filePath) con
|
||||
return new EntryImageProvider(spec->makeSource, spec->scheme);
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> FormatRegistry::createImageLoader(const QString &filePath) const
|
||||
{
|
||||
const FormatSpec *spec = specFor(filePath);
|
||||
if (!spec || spec->scheme.isEmpty() || !spec->makeSource)
|
||||
return nullptr;
|
||||
|
||||
auto loader = std::make_unique<EntryImageProvider>(spec->makeSource, spec->scheme);
|
||||
loader->setFilePath(filePath);
|
||||
|
||||
if (!spec->loaderIncludesWeb)
|
||||
return loader;
|
||||
|
||||
auto composite = std::make_unique<CompositeImageLoader>();
|
||||
composite->add(spec->scheme, std::move(loader));
|
||||
composite->add(QStringLiteral("web"), std::make_unique<WebImageProvider>());
|
||||
return composite;
|
||||
}
|
||||
|
||||
QStringList FormatRegistry::supportedExtensions() const
|
||||
{
|
||||
QStringList out;
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
class FormatReader;
|
||||
class IByteSource;
|
||||
class IImageLoader;
|
||||
class IImageProvider;
|
||||
class QQmlEngine;
|
||||
|
||||
@@ -21,7 +20,6 @@ struct FormatSpec {
|
||||
QString scheme; // image:// scheme; empty = format has no images
|
||||
std::function<std::unique_ptr<FormatReader>()> makeReader;
|
||||
std::function<std::unique_ptr<IByteSource>()> makeSource; // required when scheme is set
|
||||
bool loaderIncludesWeb = false; // formats whose content may reference web images (epub)
|
||||
};
|
||||
|
||||
class FormatRegistry {
|
||||
@@ -34,9 +32,6 @@ public:
|
||||
// Caller owns the pointer (the QML engine takes ownership on registration).
|
||||
IImageProvider *createImageProvider(const QString &filePath) const;
|
||||
|
||||
// Synchronous loader for measurement, or nullptr for formats without images.
|
||||
std::unique_ptr<IImageLoader> createImageLoader(const QString &filePath) const;
|
||||
|
||||
QStringList supportedExtensions() const;
|
||||
|
||||
// Register one provider per scheme with the QML engine (engine takes ownership).
|
||||
|
||||
@@ -3,17 +3,13 @@
|
||||
#include <QImage>
|
||||
#include <QSize>
|
||||
|
||||
// Honor an explicit requested size (clamped to the display width), otherwise
|
||||
// scale down to fit the display width — never enlarge.
|
||||
inline QImage scaleToFit(QImage image, int displayWidth, const QSize &requestedSize)
|
||||
// Honor an explicit requested size, otherwise keep original image dimensions — never enlarge.
|
||||
inline QImage scaleToFit(QImage image, const QSize &requestedSize)
|
||||
{
|
||||
if (requestedSize.width() > 0 || requestedSize.height() > 0) {
|
||||
int targetW = requestedSize.width() > 0 ? requestedSize.width() : -1;
|
||||
int targetH = requestedSize.height() > 0 ? requestedSize.height() : -1;
|
||||
|
||||
if (displayWidth > 0 && targetW > 0 && targetW > displayWidth)
|
||||
targetW = displayWidth;
|
||||
|
||||
if (targetW > 0 && targetH > 0) {
|
||||
image = image.scaled(QSize(targetW, targetH), Qt::KeepAspectRatio,
|
||||
Qt::SmoothTransformation);
|
||||
@@ -22,9 +18,6 @@ inline QImage scaleToFit(QImage image, int displayWidth, const QSize &requestedS
|
||||
} else {
|
||||
image = image.scaledToHeight(targetH, Qt::SmoothTransformation);
|
||||
}
|
||||
} else {
|
||||
if (displayWidth > 0 && image.width() > displayWidth)
|
||||
image = image.scaledToWidth(displayWidth, Qt::SmoothTransformation);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
@@ -39,7 +39,7 @@ QString WebImageProvider::cacheFilePath(const QString &urlString)
|
||||
// WebImageProvider
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QImage WebImageProvider::loadWebImage(const QString &urlString, int displayWidth,
|
||||
QImage WebImageProvider::loadWebImage(const QString &urlString,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
if (urlString.isEmpty())
|
||||
@@ -50,7 +50,7 @@ QImage WebImageProvider::loadWebImage(const QString &urlString, int displayWidth
|
||||
if (QFile::exists(cachePath)) {
|
||||
QImage image;
|
||||
if (image.load(cachePath))
|
||||
return scaleToFit(image, displayWidth, requestedSize);
|
||||
return scaleToFit(image, requestedSize);
|
||||
}
|
||||
|
||||
// Download — blocking, safe to call from any thread via a local event loop
|
||||
@@ -89,7 +89,7 @@ QImage WebImageProvider::loadWebImage(const QString &urlString, int displayWidth
|
||||
if (!image.loadFromData(data))
|
||||
return {};
|
||||
|
||||
return scaleToFit(image, displayWidth, requestedSize);
|
||||
return scaleToFit(image, requestedSize);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -109,13 +109,8 @@ QQuickImageResponse *WebImageProvider::requestImageResponse(const QString &id,
|
||||
return response;
|
||||
}
|
||||
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
auto *response = new AsyncImageResponse([id, displayWidth, requestedSize] {
|
||||
return loadWebImage(id, displayWidth, requestedSize);
|
||||
auto *response = new AsyncImageResponse([id, requestedSize] {
|
||||
return loadWebImage(id, requestedSize);
|
||||
});
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
@@ -125,42 +120,3 @@ QString WebImageProvider::scheme() const
|
||||
{
|
||||
return QStringLiteral("web");
|
||||
}
|
||||
|
||||
void WebImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebImageProvider — IImageLoader
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QImage WebImageProvider::load(const QUrl &url, int maxWidth) const
|
||||
{
|
||||
// url is image://web/<original-web-url>
|
||||
// url.path() yields "/<original-web-url>"; strip leading '/'
|
||||
QString webUrl = url.path();
|
||||
if (webUrl.startsWith(QLatin1Char('/')))
|
||||
webUrl = webUrl.mid(1);
|
||||
if (webUrl.isEmpty())
|
||||
return {};
|
||||
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
const int effective = (displayWidth > 0 && maxWidth > 0)
|
||||
? qMin(displayWidth, maxWidth)
|
||||
: qMax(displayWidth, maxWidth);
|
||||
return loadWebImage(webUrl, effective, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> WebImageProvider::clone() const
|
||||
{
|
||||
auto c = std::make_unique<WebImageProvider>();
|
||||
QMutexLocker lock(&m_mutex);
|
||||
c->m_displayWidth = m_displayWidth;
|
||||
return c;
|
||||
}
|
||||
@@ -1,34 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <QMutex>
|
||||
#include <QSize>
|
||||
#include <QString>
|
||||
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
|
||||
class WebImageProvider : public IImageProvider, public IImageLoader {
|
||||
class WebImageProvider : public IImageProvider {
|
||||
public:
|
||||
QQuickImageResponse *requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize) override;
|
||||
|
||||
void setFilePath(const QString &) override {}
|
||||
void setDisplayWidth(int width) override;
|
||||
void clear() override {}
|
||||
QString coverImageUrl(const QString &) const override { return {}; }
|
||||
QString scheme() const override;
|
||||
|
||||
QImage load(const QUrl &url, int maxWidth) const override;
|
||||
std::unique_ptr<IImageLoader> clone() const override;
|
||||
|
||||
// Download (or load from disk cache) a web image. Thread-safe, may block.
|
||||
static QImage loadWebImage(const QString &urlString, int displayWidth,
|
||||
static QImage loadWebImage(const QString &urlString,
|
||||
const QSize &requestedSize);
|
||||
|
||||
static QString cacheDir();
|
||||
static QString cacheFilePath(const QString &urlString);
|
||||
|
||||
private:
|
||||
mutable QMutex m_mutex;
|
||||
int m_displayWidth = 0;
|
||||
};
|
||||
@@ -1,50 +0,0 @@
|
||||
#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);
|
||||
});
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
#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;
|
||||
};
|
||||
@@ -12,19 +12,17 @@ BookPosition BookPositionStore::load(const QByteArray &contentMd5)
|
||||
const QVariantMap d = m_state.loadSection(sectionKey(contentMd5));
|
||||
BookPosition pos;
|
||||
pos.chunkIndex = d.value(QStringLiteral("chunkIndex"), 0).toInt();
|
||||
pos.intraOffset = d.value(QStringLiteral("intraOffset"), 0).toInt();
|
||||
pos.pageShift = d.value(QStringLiteral("pageShift"), true).toBool();
|
||||
pos.mangaRTL = d.value(QStringLiteral("mangaRTL"), true).toBool();
|
||||
return pos;
|
||||
}
|
||||
|
||||
void BookPositionStore::save(const QByteArray &contentMd5, int chunkIndex, int intraOffset, bool mangaRTL, bool pageShift)
|
||||
void BookPositionStore::save(const QByteArray &contentMd5, int chunkIndex, bool mangaRTL, bool pageShift)
|
||||
{
|
||||
if (contentMd5.isEmpty())
|
||||
return;
|
||||
m_state.saveSection(sectionKey(contentMd5), {
|
||||
{QStringLiteral("chunkIndex"), chunkIndex},
|
||||
{QStringLiteral("intraOffset"), intraOffset},
|
||||
{QStringLiteral("pageShift"), pageShift},
|
||||
{QStringLiteral("mangaRTL"), mangaRTL},
|
||||
});
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
struct BookPosition {
|
||||
int chunkIndex = 0;
|
||||
int intraOffset = 0;
|
||||
bool pageShift = true; // manga page-shift; default ON
|
||||
bool mangaRTL = true; // manga reading direction; default RTL
|
||||
};
|
||||
@@ -19,7 +18,7 @@ public:
|
||||
BookPosition load(const QByteArray &contentMd5);
|
||||
|
||||
// Save the current position for a book identified by MD5 of its contents.
|
||||
void save(const QByteArray &contentMd5, int chunkIndex, int intraOffset, bool mangaRTL = true, bool pageShift = true);
|
||||
void save(const QByteArray &contentMd5, int chunkIndex, bool mangaRTL = true, bool pageShift = true);
|
||||
|
||||
private:
|
||||
AppState &m_state;
|
||||
|
||||
Reference in new issue
Block a user