Display all chapters
This commit is contained in:
4 files changed
+170
-159
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#include "ReadingModel.h"
|
||||
#include "FormatReader.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
ReadingModel::ReadingModel(QObject *parent)
|
||||
@@ -32,7 +33,9 @@ QHash<int, QByteArray> ReadingModel::roleNames() const
|
||||
|
||||
QString ReadingModel::chapterTitle() const
|
||||
{
|
||||
return m_chapterTitle;
|
||||
if (m_currentChapter < 0 || m_currentChapter >= m_chapterTitles.size())
|
||||
return {};
|
||||
return m_chapterTitles.at(m_currentChapter);
|
||||
}
|
||||
|
||||
int ReadingModel::currentChapter() const
|
||||
@@ -52,14 +55,11 @@ bool ReadingModel::hasChapters() const
|
||||
|
||||
double ReadingModel::progress() const
|
||||
{
|
||||
if (m_totalChapters == 0)
|
||||
if (m_totalBookBytes == 0)
|
||||
return 0.0;
|
||||
double byteFraction = 0.0;
|
||||
if (m_totalChapterBytes > 0)
|
||||
byteFraction = qBound(0.0,
|
||||
static_cast<double>(m_chunkByteOffsets.value(m_topChunkIndex)) / m_totalChapterBytes,
|
||||
1.0);
|
||||
return (m_currentChapter + byteFraction) / m_totalChapters;
|
||||
return qBound(0.0,
|
||||
static_cast<double>(m_chunkByteOffsets.value(m_topChunkIndex)) / m_totalBookBytes,
|
||||
1.0);
|
||||
}
|
||||
|
||||
int ReadingModel::currentPage() const
|
||||
@@ -79,6 +79,15 @@ void ReadingModel::setScrollInfo(int topChunkIndex, qreal /*offsetInChunk*/, boo
|
||||
|
||||
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) {
|
||||
m_currentChapter = chapter;
|
||||
emit chapterChanged();
|
||||
}
|
||||
|
||||
recalculateCurrentPage();
|
||||
}
|
||||
|
||||
@@ -93,7 +102,7 @@ void ReadingModel::computeChunkBytes()
|
||||
m_chunkBytes[i] = sz;
|
||||
offset += sz;
|
||||
}
|
||||
m_totalChapterBytes = offset;
|
||||
m_totalBookBytes = offset;
|
||||
}
|
||||
|
||||
void ReadingModel::recalculateCurrentPage()
|
||||
@@ -109,24 +118,30 @@ void ReadingModel::recalculateCurrentPage()
|
||||
emit currentPageChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::loadChapterData(int index)
|
||||
void ReadingModel::loadAllChapters()
|
||||
{
|
||||
beginResetModel();
|
||||
|
||||
m_chunks.clear();
|
||||
m_chapterTitle.clear();
|
||||
m_chapterStartIndices.clear();
|
||||
m_chapterTitles.clear();
|
||||
m_chunkBytes.clear();
|
||||
m_chunkByteOffsets.clear();
|
||||
m_totalChapterBytes = 0;
|
||||
m_totalBookBytes = 0;
|
||||
m_topChunkIndex = 0;
|
||||
m_atYEnd = false;
|
||||
m_currentChapter = 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;
|
||||
if (m_reader) {
|
||||
for (int i = 0; i < m_totalChapters; ++i) {
|
||||
auto chapter = m_reader->loadChapter(i);
|
||||
m_chapterStartIndices.append(m_chunks.size());
|
||||
m_chapterTitles.append(chapter.title);
|
||||
m_chunks.append(chapter.contentChunks);
|
||||
}
|
||||
}
|
||||
|
||||
endResetModel();
|
||||
@@ -145,7 +160,7 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
m_currentChapter = 0;
|
||||
m_totalChapters = reader ? reader->chapterCount() : 0;
|
||||
|
||||
loadChapterData(0);
|
||||
loadAllChapters();
|
||||
|
||||
emit totalChaptersChanged();
|
||||
emit chapterChanged();
|
||||
@@ -155,28 +170,19 @@ void ReadingModel::nextChapter()
|
||||
{
|
||||
if (!m_reader || m_currentChapter >= m_totalChapters - 1)
|
||||
return;
|
||||
|
||||
m_currentChapter++;
|
||||
loadChapterData(m_currentChapter);
|
||||
emit chapterChanged();
|
||||
emit scrollToChunkRequested(m_chapterStartIndices.at(m_currentChapter + 1));
|
||||
}
|
||||
|
||||
void ReadingModel::previousChapter()
|
||||
{
|
||||
if (!m_reader || m_currentChapter <= 0)
|
||||
return;
|
||||
|
||||
m_currentChapter--;
|
||||
loadChapterData(m_currentChapter);
|
||||
emit chapterChanged();
|
||||
emit scrollToChunkRequested(m_chapterStartIndices.at(m_currentChapter - 1));
|
||||
}
|
||||
|
||||
void ReadingModel::goToChapter(int index)
|
||||
{
|
||||
if (!m_reader || index < 0 || index >= m_totalChapters)
|
||||
return;
|
||||
|
||||
m_currentChapter = index;
|
||||
loadChapterData(m_currentChapter);
|
||||
emit chapterChanged();
|
||||
emit scrollToChunkRequested(m_chapterStartIndices.at(index));
|
||||
}
|
||||
@@ -13,7 +13,7 @@ class ReadingModel : public QAbstractListModel {
|
||||
Q_PROPERTY(QString chapterTitle READ chapterTitle NOTIFY chapterChanged)
|
||||
Q_PROPERTY(int currentChapter READ currentChapter NOTIFY chapterChanged)
|
||||
Q_PROPERTY(int totalChapters READ totalChapters NOTIFY totalChaptersChanged)
|
||||
Q_PROPERTY(double progress READ progress NOTIFY chapterChanged)
|
||||
Q_PROPERTY(double progress READ progress NOTIFY currentPageChanged)
|
||||
Q_PROPERTY(int currentPage READ currentPage NOTIFY currentPageChanged)
|
||||
Q_PROPERTY(int totalPages READ totalPages NOTIFY totalPagesChanged)
|
||||
Q_PROPERTY(bool hasChapters READ hasChapters NOTIFY totalChaptersChanged)
|
||||
@@ -51,9 +51,10 @@ signals:
|
||||
void totalChaptersChanged();
|
||||
void currentPageChanged();
|
||||
void totalPagesChanged();
|
||||
void scrollToChunkRequested(int chunkIndex);
|
||||
|
||||
private:
|
||||
void loadChapterData(int index);
|
||||
void loadAllChapters();
|
||||
void computeChunkBytes();
|
||||
void recalculateCurrentPage();
|
||||
|
||||
@@ -66,11 +67,12 @@ private:
|
||||
bool m_atYEnd = false;
|
||||
|
||||
QStringList m_chunks;
|
||||
QVector<qsizetype> m_chunkBytes; // UTF-8 byte size of each chunk
|
||||
QVector<qsizetype> m_chunkByteOffsets; // prefix sums: offset[i] = sum of bytes[0..i-1]
|
||||
qsizetype m_totalChapterBytes = 0;
|
||||
QVector<int> m_chapterStartIndices; // m_chapterStartIndices[i] = first global chunk index of chapter i
|
||||
QVector<QString> m_chapterTitles; // title of each chapter
|
||||
|
||||
QString m_chapterTitle;
|
||||
QVector<qsizetype> m_chunkBytes; // UTF-8 byte size of each chunk (all chapters)
|
||||
QVector<qsizetype> m_chunkByteOffsets; // prefix sums: offset[i] = sum of bytes[0..i-1]
|
||||
qsizetype m_totalBookBytes = 0;
|
||||
};
|
||||
|
||||
#endif // READINGMODEL_H
|
||||
@@ -104,13 +104,9 @@ Rectangle {
|
||||
function onBookReady() {
|
||||
_restoring = true
|
||||
var pos = appState.loadBookPosition(bookController.currentFilePath)
|
||||
var savedChapter = pos["chapter"]
|
||||
var savedScrollIndex = pos["scrollIndex"]
|
||||
var savedScrollOffset = pos["scrollOffset"]
|
||||
|
||||
if (savedChapter > 0)
|
||||
readingModel.goToChapter(savedChapter)
|
||||
|
||||
Qt.callLater(function() {
|
||||
if (savedScrollIndex > 0) {
|
||||
chunkListView.positionViewAtIndex(savedScrollIndex, ListView.Beginning)
|
||||
@@ -127,4 +123,11 @@ Rectangle {
|
||||
}
|
||||
}
|
||||
|
||||
Connections {
|
||||
target: readingModel
|
||||
function onScrollToChunkRequested(chunkIndex) {
|
||||
chunkListView.positionViewAtIndex(chunkIndex, ListView.Beginning)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user