Upgrade to Qt 6.10.3, refactor AppState for LibraryModel integration, add ChunkMeasurer tests, and update CI/docker configurations.
This commit is contained in:
53 files changed
+2095
-880
No files matched your search
+149
-84
@@ -1,34 +1,47 @@
|
||||
#include "ReadingModel.h"
|
||||
#include "FormatReader.h"
|
||||
#include "IChunkMeasurer.h"
|
||||
#include "IImageLoader.h"
|
||||
#include "ChunkMeasurer.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <QtConcurrent>
|
||||
|
||||
ReadingModel::ReadingModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
#include "AppState.h"
|
||||
|
||||
ReadingModel::ReadingModel(QObject *parent) : QAbstractListModel(parent) {}
|
||||
|
||||
ReadingModel::~ReadingModel()
|
||||
{
|
||||
if (m_measureFuture.isRunning()) {
|
||||
m_measureFuture.cancel();
|
||||
m_measureFuture.waitForFinished();
|
||||
}
|
||||
}
|
||||
|
||||
int ReadingModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
return 0;
|
||||
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() >= 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 {};
|
||||
|
||||
if (role == ContentRole)
|
||||
return m_chunks.at(index.row());
|
||||
|
||||
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;
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ReadingModel::roleNames() const
|
||||
{
|
||||
return {{ContentRole, "content"}};
|
||||
QHash<int, QByteArray> ReadingModel::roleNames() const {
|
||||
return {
|
||||
{ContentRole, "content"},
|
||||
{PixelHeightRole, "pixelHeight"},
|
||||
{ChapterRole, "chapter"},
|
||||
};
|
||||
}
|
||||
|
||||
QString ReadingModel::chapterTitle() const
|
||||
@@ -38,52 +51,27 @@ QString ReadingModel::chapterTitle() const
|
||||
return m_chapterTitles.at(m_currentChapter);
|
||||
}
|
||||
|
||||
int ReadingModel::currentChapter() const
|
||||
{
|
||||
return m_currentChapter;
|
||||
}
|
||||
|
||||
int ReadingModel::totalChapters() const
|
||||
{
|
||||
return m_totalChapters;
|
||||
}
|
||||
|
||||
bool ReadingModel::hasChapters() const
|
||||
{
|
||||
return m_totalChapters > 1;
|
||||
}
|
||||
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; }
|
||||
qreal ReadingModel::totalPixelHeight() const { return m_totalPixelHeight; }
|
||||
|
||||
double ReadingModel::progress() const
|
||||
{
|
||||
if (m_totalBookBytes == 0)
|
||||
return 0.0;
|
||||
return qBound(0.0,
|
||||
static_cast<double>(m_chunkByteOffsets.value(m_topChunkIndex)) / m_totalBookBytes,
|
||||
1.0);
|
||||
return qBound(0, static_cast<int>(m_yOffset * 100), 100);
|
||||
}
|
||||
|
||||
int ReadingModel::currentPage() const
|
||||
int ReadingModel::currentPage() const { return m_currentPage; }
|
||||
int ReadingModel::totalPages() const { return m_totalPages; }
|
||||
|
||||
void ReadingModel::setScrollInfo(const int topChunkIndex, const qreal proportion)
|
||||
{
|
||||
return m_currentPage;
|
||||
}
|
||||
m_yOffset = proportion;
|
||||
|
||||
int ReadingModel::totalPages() const
|
||||
{
|
||||
return m_totalPages;
|
||||
}
|
||||
|
||||
void ReadingModel::setScrollInfo(int topChunkIndex, qreal /*offsetInChunk*/, bool atYEnd)
|
||||
{
|
||||
if (topChunkIndex < 0 || topChunkIndex >= m_chunks.size())
|
||||
return;
|
||||
|
||||
m_topChunkIndex = topChunkIndex;
|
||||
m_atYEnd = atYEnd;
|
||||
|
||||
// Derive current chapter from global chunk index
|
||||
auto it = std::upper_bound(m_chapterStartIndices.begin(), m_chapterStartIndices.end(), topChunkIndex);
|
||||
int chapter = std::max(0, static_cast<int>(std::distance(m_chapterStartIndices.begin(), it)) - 1);
|
||||
if (chapter != m_currentChapter) {
|
||||
// Derive current chapter
|
||||
const auto it = std::upper_bound(m_chapterStartIndices.begin(), m_chapterStartIndices.end(), topChunkIndex);
|
||||
if (const int chapter = std::max(0, static_cast<int>(std::distance(m_chapterStartIndices.begin(), it)) - 1); chapter != m_currentChapter) {
|
||||
m_currentChapter = chapter;
|
||||
emit chapterChanged();
|
||||
}
|
||||
@@ -91,26 +79,87 @@ void ReadingModel::setScrollInfo(int topChunkIndex, qreal /*offsetInChunk*/, boo
|
||||
recalculateCurrentPage();
|
||||
}
|
||||
|
||||
void ReadingModel::computeChunkBytes()
|
||||
void ReadingModel::measure(const bool isNew)
|
||||
{
|
||||
m_chunkBytes.resize(m_chunks.size());
|
||||
m_chunkByteOffsets.resize(m_chunks.size());
|
||||
qsizetype offset = 0;
|
||||
for (int i = 0; i < m_chunks.size(); ++i) {
|
||||
m_chunkByteOffsets[i] = offset;
|
||||
qsizetype sz = m_chunks.at(i).toUtf8().size();
|
||||
m_chunkBytes[i] = sz;
|
||||
offset += sz;
|
||||
}
|
||||
m_totalBookBytes = offset;
|
||||
if (!m_reader) return;
|
||||
|
||||
m_totalPixelHeight = 0;
|
||||
|
||||
QStringList htmls;
|
||||
htmls.reserve(m_chunks.size());
|
||||
for (const auto &c : m_chunks)
|
||||
htmls.append(c.html);
|
||||
|
||||
runMeasurement(htmls, isNew);
|
||||
}
|
||||
|
||||
void ReadingModel::runMeasurement(const QStringList &htmls, const bool isNew)
|
||||
{
|
||||
if (m_measureFuture.isRunning())
|
||||
m_measureFuture.cancel();
|
||||
|
||||
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});
|
||||
|
||||
if (isNew) {
|
||||
QVariantMap map = AppState::loadBookPosition(m_reader->filePath());
|
||||
int savedChunk = map.value(QStringLiteral("chunkIndex"), 0).toInt();
|
||||
qreal savedIntra = map.value(QStringLiteral("intraChunkOffset"), 0).toInt();
|
||||
restoreReady(savedChunk, savedIntra);
|
||||
}
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
}
|
||||
|
||||
void ReadingModel::setContentWidth(int width)
|
||||
{
|
||||
if (width == m_contentWidth) return;
|
||||
m_contentWidth = width;
|
||||
if (m_reader) 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) measure(false);
|
||||
}
|
||||
|
||||
void ReadingModel::setImageLoader(std::unique_ptr<IImageLoader> loader)
|
||||
{
|
||||
m_imageLoader = std::move(loader);
|
||||
}
|
||||
|
||||
void ReadingModel::recalculateCurrentPage()
|
||||
{
|
||||
int page = 0;
|
||||
if (!m_chunks.isEmpty()) {
|
||||
page = m_atYEnd ? m_totalPages : m_topChunkIndex + 1;
|
||||
page = std::max(1, std::min(page, m_totalPages));
|
||||
page = qBound(0.0, m_yOffset / m_totalPixelHeight, 1.0) * m_chunks.size() * page_ratio;
|
||||
}
|
||||
if (m_currentPage == page)
|
||||
return;
|
||||
@@ -121,37 +170,46 @@ void ReadingModel::recalculateCurrentPage()
|
||||
void ReadingModel::loadAllChapters()
|
||||
{
|
||||
beginResetModel();
|
||||
|
||||
m_chunks.clear();
|
||||
m_chapterStartIndices.clear();
|
||||
m_chapterTitles.clear();
|
||||
m_chunkBytes.clear();
|
||||
m_chunkByteOffsets.clear();
|
||||
m_totalBookBytes = 0;
|
||||
m_topChunkIndex = 0;
|
||||
m_atYEnd = false;
|
||||
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) {
|
||||
if (!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_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);
|
||||
m_chapterStartIndices.append(m_chunks.size());
|
||||
if (i > 0)
|
||||
m_chapterStartIndices.append(m_chunks.size());
|
||||
m_chapterTitles.append(chapter.title);
|
||||
m_chunks.append(chapter.contentChunks);
|
||||
int j = 0;
|
||||
for (const auto &html : chapter.contentChunks)
|
||||
m_chunks.push_back({html, i, j++, -1});
|
||||
}
|
||||
}
|
||||
|
||||
endResetModel();
|
||||
|
||||
computeChunkBytes();
|
||||
|
||||
int pages = m_chunks.size();
|
||||
if (m_totalPages != pages) { m_totalPages = pages; emit totalPagesChanged(); }
|
||||
int page = m_chunks.isEmpty() ? 0 : 1;
|
||||
if (m_currentPage != page) { m_currentPage = page; emit currentPageChanged(); }
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void ReadingModel::setReader(FormatReader *reader)
|
||||
@@ -166,6 +224,13 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
emit chapterChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::setCoverImageUrl(const QString &coverImageUrl)
|
||||
{
|
||||
if (m_coverImageUrl != coverImageUrl) {
|
||||
m_coverImageUrl = coverImageUrl;
|
||||
}
|
||||
}
|
||||
|
||||
void ReadingModel::nextChapter()
|
||||
{
|
||||
if (!m_reader || m_currentChapter >= m_totalChapters - 1)
|
||||
|
||||
Reference in new issue
Block a user