225 lines
5.4 KiB
C++
225 lines
5.4 KiB
C++
#include "ReadingModel.h"
|
|
#include "FormatReader.h"
|
|
#include <cmath>
|
|
#include <QTextDocument>
|
|
#include <QTextCursor>
|
|
#include <QTextBlockFormat>
|
|
#include <QFont>
|
|
|
|
ReadingModel::ReadingModel(QObject *parent)
|
|
: QAbstractListModel(parent)
|
|
{
|
|
}
|
|
|
|
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())
|
|
return {};
|
|
|
|
if (role == ContentRole)
|
|
return m_chunks.at(index.row());
|
|
|
|
return {};
|
|
}
|
|
|
|
QHash<int, QByteArray> ReadingModel::roleNames() const
|
|
{
|
|
return {{ContentRole, "content"}};
|
|
}
|
|
|
|
QString ReadingModel::chapterTitle() const
|
|
{
|
|
return m_chapterTitle;
|
|
}
|
|
|
|
int ReadingModel::currentChapter() const
|
|
{
|
|
return m_currentChapter;
|
|
}
|
|
|
|
int ReadingModel::totalChapters() const
|
|
{
|
|
return m_totalChapters;
|
|
}
|
|
|
|
double ReadingModel::progress() const
|
|
{
|
|
if (m_totalChapters == 0)
|
|
return 0.0;
|
|
return static_cast<double>(m_currentChapter + 1) / m_totalChapters;
|
|
}
|
|
|
|
int ReadingModel::currentPage() const
|
|
{
|
|
return m_currentPage;
|
|
}
|
|
|
|
int ReadingModel::totalPages() const
|
|
{
|
|
return m_totalPages;
|
|
}
|
|
|
|
void ReadingModel::setViewportSize(qreal width, qreal height)
|
|
{
|
|
bool widthChanged = (width != m_viewportWidth);
|
|
m_viewportWidth = width;
|
|
m_viewportHeight = height;
|
|
|
|
if (widthChanged && !m_chunks.isEmpty())
|
|
computeChunkHeights();
|
|
|
|
recalculateTotalPages();
|
|
recalculateCurrentPage();
|
|
}
|
|
|
|
void ReadingModel::setScrollInfo(int topChunkIndex, qreal offsetInChunk)
|
|
{
|
|
if (topChunkIndex < 0 || topChunkIndex >= m_chunkOffsets.size())
|
|
return;
|
|
|
|
m_absoluteScrollY = m_chunkOffsets.at(topChunkIndex) + offsetInChunk;
|
|
recalculateCurrentPage();
|
|
}
|
|
|
|
void ReadingModel::computeChunkHeights()
|
|
{
|
|
// Text width matches the QML delegate:
|
|
// ListView left margin: 12, delegate rightPadding: 2
|
|
// delegate width = listView.width - 18 (hardcoded in QML)
|
|
// text layout width = delegate.width - rightPadding = listView.width - 20
|
|
// m_viewportWidth = chunkListView.width = parent.width - leftMargin(12)
|
|
// so delegate.width = m_viewportWidth - 18
|
|
// text width = delegate.width - rightPadding(2) = m_viewportWidth - 20
|
|
qreal textWidth = m_viewportWidth - 20.0;
|
|
if (textWidth <= 0)
|
|
textWidth = 1;
|
|
|
|
QFont font;
|
|
font.setPixelSize(16);
|
|
|
|
QTextDocument doc;
|
|
doc.setDefaultFont(font);
|
|
|
|
m_chunkHeights.clear();
|
|
m_chunkHeights.reserve(m_chunks.size());
|
|
|
|
for (const QString &chunk : m_chunks) {
|
|
doc.setHtml(chunk);
|
|
doc.setTextWidth(textWidth);
|
|
|
|
// Apply lineHeight: 1.6 (matching QML Text lineHeight property)
|
|
QTextBlockFormat fmt;
|
|
fmt.setLineHeight(160, QTextBlockFormat::ProportionalHeight);
|
|
QTextCursor cursor(&doc);
|
|
cursor.select(QTextCursor::Document);
|
|
cursor.mergeBlockFormat(fmt);
|
|
|
|
m_chunkHeights.append(doc.size().height());
|
|
}
|
|
|
|
// Build prefix sums
|
|
m_chunkOffsets.clear();
|
|
m_chunkOffsets.reserve(m_chunks.size());
|
|
qreal offset = 0;
|
|
for (qreal h : m_chunkHeights) {
|
|
m_chunkOffsets.append(offset);
|
|
offset += h;
|
|
}
|
|
m_totalContentHeight = offset;
|
|
}
|
|
|
|
void ReadingModel::recalculateTotalPages()
|
|
{
|
|
int pages = 0;
|
|
if (m_viewportHeight > 0 && m_totalContentHeight > 0)
|
|
pages = static_cast<int>(std::ceil(m_totalContentHeight / m_viewportHeight));
|
|
if (m_totalPages == pages)
|
|
return;
|
|
m_totalPages = pages;
|
|
emit totalPagesChanged();
|
|
}
|
|
|
|
void ReadingModel::recalculateCurrentPage()
|
|
{
|
|
int page = 0;
|
|
if (m_viewportHeight > 0 && m_totalPages > 0) {
|
|
page = static_cast<int>(std::floor(m_absoluteScrollY / m_viewportHeight)) + 1;
|
|
page = std::max(1, std::min(page, m_totalPages));
|
|
}
|
|
if (m_currentPage == page)
|
|
return;
|
|
m_currentPage = page;
|
|
emit currentPageChanged();
|
|
}
|
|
|
|
void ReadingModel::loadChapterData(int index)
|
|
{
|
|
beginResetModel();
|
|
m_chunks.clear();
|
|
m_chapterTitle.clear();
|
|
m_chunkHeights.clear();
|
|
m_chunkOffsets.clear();
|
|
m_totalContentHeight = 0;
|
|
m_absoluteScrollY = 0;
|
|
|
|
if (m_currentPage != 0) { m_currentPage = 0; emit currentPageChanged(); }
|
|
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
|
|
|
|
if (m_reader && index >= 0 && index < m_totalChapters) {
|
|
auto chapter = m_reader->loadChapter(index);
|
|
m_chapterTitle = chapter.title;
|
|
m_chunks = chapter.contentChunks;
|
|
}
|
|
|
|
endResetModel();
|
|
}
|
|
|
|
void ReadingModel::setReader(FormatReader *reader)
|
|
{
|
|
m_reader = reader;
|
|
m_currentChapter = 0;
|
|
m_totalChapters = reader ? reader->chapterCount() : 0;
|
|
|
|
loadChapterData(0);
|
|
|
|
emit totalChaptersChanged();
|
|
emit chapterChanged();
|
|
}
|
|
|
|
void ReadingModel::nextChapter()
|
|
{
|
|
if (!m_reader || m_currentChapter >= m_totalChapters - 1)
|
|
return;
|
|
|
|
m_currentChapter++;
|
|
loadChapterData(m_currentChapter);
|
|
emit chapterChanged();
|
|
}
|
|
|
|
void ReadingModel::previousChapter()
|
|
{
|
|
if (!m_reader || m_currentChapter <= 0)
|
|
return;
|
|
|
|
m_currentChapter--;
|
|
loadChapterData(m_currentChapter);
|
|
emit chapterChanged();
|
|
}
|
|
|
|
void ReadingModel::goToChapter(int index)
|
|
{
|
|
if (!m_reader || index < 0 || index >= m_totalChapters)
|
|
return;
|
|
|
|
m_currentChapter = index;
|
|
loadChapterData(m_currentChapter);
|
|
emit chapterChanged();
|
|
}
|