254 lines
7.7 KiB
C++
254 lines
7.7 KiB
C++
#include "ReadingModel.h"
|
|
#include "FormatReader.h"
|
|
#include "IChunkMeasurer.h"
|
|
#include "IImageLoader.h"
|
|
#include "ChunkMeasurer.h"
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <QtConcurrent>
|
|
|
|
ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
|
|
: QAbstractListModel(parent)
|
|
, m_positions(positions)
|
|
{
|
|
}
|
|
|
|
ReadingModel::~ReadingModel()
|
|
{
|
|
if (m_measureFuture.isRunning()) {
|
|
m_measureFuture.cancel();
|
|
m_measureFuture.waitForFinished();
|
|
}
|
|
}
|
|
|
|
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;
|
|
default: return {};
|
|
}
|
|
}
|
|
|
|
QHash<int, QByteArray> ReadingModel::roleNames() const {
|
|
return {
|
|
{ContentRole, "content"},
|
|
{PixelHeightRole, "pixelHeight"},
|
|
{ChapterRole, "chapter"},
|
|
};
|
|
}
|
|
|
|
QString ReadingModel::chapterTitle() const
|
|
{
|
|
if (m_currentChapter < 0 || m_currentChapter >= m_chapterTitles.size())
|
|
return {};
|
|
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; }
|
|
bool ReadingModel::measuring() const { return m_measuring; }
|
|
qreal ReadingModel::totalPixelHeight() const { return m_totalPixelHeight; }
|
|
|
|
double ReadingModel::progress() const
|
|
{
|
|
return qBound(0.0, m_yOffset, 1.0);
|
|
}
|
|
|
|
int ReadingModel::currentPage() const { return m_currentPage; }
|
|
int ReadingModel::totalPages() const { return m_totalPages; }
|
|
|
|
void ReadingModel::setScrollInfo(const int topChunkIndex, const qreal proportion)
|
|
{
|
|
m_yOffset = proportion;
|
|
|
|
// 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();
|
|
}
|
|
|
|
recalculateCurrentPage();
|
|
}
|
|
|
|
void ReadingModel::measure(const bool isNew)
|
|
{
|
|
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) {
|
|
const auto pos = m_positions.load(m_reader->contentHash());
|
|
emit restoreReady(pos.chunkIndex, pos.intraOffset);
|
|
}
|
|
}, 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 = 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::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) {
|
|
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);
|
|
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});
|
|
}
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
|
|
void ReadingModel::setReader(FormatReader *reader)
|
|
{
|
|
m_reader = reader;
|
|
m_currentChapter = 0;
|
|
m_totalChapters = reader ? reader->chapterCount() : 0;
|
|
|
|
loadAllChapters();
|
|
|
|
emit totalChaptersChanged();
|
|
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)
|
|
return;
|
|
emit scrollToChunkRequested(m_chapterStartIndices.at(m_currentChapter + 1));
|
|
}
|
|
|
|
void ReadingModel::previousChapter()
|
|
{
|
|
if (!m_reader || m_currentChapter <= 0)
|
|
return;
|
|
emit scrollToChunkRequested(m_chapterStartIndices.at(m_currentChapter - 1));
|
|
}
|
|
|
|
void ReadingModel::goToChapter(int index)
|
|
{
|
|
if (!m_reader || index < 0 || index >= m_totalChapters)
|
|
return;
|
|
emit scrollToChunkRequested(m_chapterStartIndices.at(index));
|
|
}
|