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));
|
||||
}
|
||||
Reference in new issue
Block a user