old render cleanup part 3
This commit is contained in:
45 files changed
+1057
-1135
No files matched your search
+127
-15
@@ -2,20 +2,45 @@
|
||||
#include <algorithm>
|
||||
|
||||
ReadingModel::ReadingModel(BookPositionStore &positions, QObject *parent)
|
||||
: QObject(parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_positions(positions)
|
||||
{
|
||||
}
|
||||
|
||||
ReadingModel::~ReadingModel() = default;
|
||||
|
||||
int ReadingModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid() || !m_isTextMode)
|
||||
return 0;
|
||||
return m_currentChapterData.chunks.size();
|
||||
}
|
||||
|
||||
QVariant ReadingModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || !m_isTextMode || index.row() < 0 || index.row() >= m_currentChapterData.chunks.size())
|
||||
return {};
|
||||
if (role == ContentRole)
|
||||
return m_currentChapterData.chunks.at(index.row());
|
||||
return {};
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ReadingModel::roleNames() const
|
||||
{
|
||||
return {{ContentRole, "content"}};
|
||||
}
|
||||
|
||||
bool ReadingModel::isTextMode() const { return m_isTextMode; }
|
||||
int ReadingModel::totalPagePairs() const { return m_pagination.count(); }
|
||||
bool ReadingModel::pageShift() const { return m_pageShift; }
|
||||
bool ReadingModel::mangaRTL() const { return m_mangaRTL; }
|
||||
int ReadingModel::currentChapter() const { return m_currentChapter; }
|
||||
int ReadingModel::chapterCount() const { return m_chapterCount; }
|
||||
QString ReadingModel::chapterTitle() const { return m_currentChapterData.title; }
|
||||
|
||||
double ReadingModel::progress() const
|
||||
{
|
||||
return qBound(0.0, m_yOffset, 1.0);
|
||||
return qBound(0.0, m_progress, 1.0);
|
||||
}
|
||||
|
||||
int ReadingModel::currentPage() const { return m_currentPage; }
|
||||
@@ -23,12 +48,74 @@ int ReadingModel::totalPages() const { return m_totalPages; }
|
||||
|
||||
void ReadingModel::setScrollInfo(const int topChunkIndex, const qreal proportion)
|
||||
{
|
||||
m_yOffset = proportion;
|
||||
const qreal clamped = qBound(0.0, proportion, 1.0);
|
||||
if (!qFuzzyCompare(m_progress, clamped)) {
|
||||
m_progress = clamped;
|
||||
emit progressChanged();
|
||||
}
|
||||
|
||||
const int page = pairIndexForChunk(topChunkIndex) + 1;
|
||||
if (m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
if (m_isTextMode) {
|
||||
const int page = topChunkIndex + 1;
|
||||
if (m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
} else {
|
||||
const int page = pairIndexForChunk(topChunkIndex) + 1;
|
||||
if (m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
}
|
||||
|
||||
saveCurrentPosition(topChunkIndex);
|
||||
}
|
||||
|
||||
void ReadingModel::saveCurrentPosition(int chunkIndex)
|
||||
{
|
||||
if (!m_reader) return;
|
||||
BookPosition pos;
|
||||
pos.chapterIndex = m_currentChapter;
|
||||
pos.chunkIndex = chunkIndex;
|
||||
pos.progress = m_progress;
|
||||
pos.pageShift = m_pageShift;
|
||||
pos.mangaRTL = m_mangaRTL;
|
||||
m_positions.save(m_reader->contentHash(), pos);
|
||||
emit positionSaved();
|
||||
}
|
||||
|
||||
void ReadingModel::loadChapter(int index)
|
||||
{
|
||||
if (!m_reader || !m_isTextMode)
|
||||
return;
|
||||
if (index < 0 || index >= m_chapterCount)
|
||||
return;
|
||||
|
||||
beginResetModel();
|
||||
m_currentChapter = index;
|
||||
m_currentChapterData = m_reader->loadChapter(index);
|
||||
m_totalPages = m_currentChapterData.chunks.size();
|
||||
m_currentPage = qMin(1, m_totalPages);
|
||||
endResetModel();
|
||||
|
||||
emit currentChapterChanged();
|
||||
emit totalPagesChanged();
|
||||
emit currentPageChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::nextChapter()
|
||||
{
|
||||
if (m_isTextMode && m_currentChapter + 1 < m_chapterCount) {
|
||||
loadChapter(m_currentChapter + 1);
|
||||
saveCurrentPosition(0);
|
||||
}
|
||||
}
|
||||
|
||||
void ReadingModel::previousChapter()
|
||||
{
|
||||
if (m_isTextMode && m_currentChapter > 0) {
|
||||
loadChapter(m_currentChapter - 1);
|
||||
saveCurrentPosition(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,8 +195,7 @@ void ReadingModel::setPageShift(bool shift, int currentPairIndex)
|
||||
emit currentPageChanged();
|
||||
}
|
||||
|
||||
if (m_reader)
|
||||
m_positions.save(m_reader->contentHash(), currentChunk, m_mangaRTL, m_pageShift);
|
||||
saveCurrentPosition(currentChunk);
|
||||
|
||||
emit pageShiftChanged();
|
||||
emit pagePairsRebuilt(newPair);
|
||||
@@ -121,7 +207,7 @@ void ReadingModel::setMangaRTL(bool rtl)
|
||||
m_mangaRTL = rtl;
|
||||
if (m_reader) {
|
||||
const int currentChunk = (m_currentPage > 0) ? m_pagination.at(m_currentPage - 1).first : 0;
|
||||
m_positions.save(m_reader->contentHash(), currentChunk >= 0 ? currentChunk : 0, m_mangaRTL, m_pageShift);
|
||||
saveCurrentPosition(currentChunk >= 0 ? currentChunk : 0);
|
||||
}
|
||||
emit mangaRTLChanged();
|
||||
}
|
||||
@@ -132,7 +218,7 @@ void ReadingModel::loadPages()
|
||||
if (m_currentPage != 0) { m_currentPage = 0; emit currentPageChanged(); }
|
||||
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
|
||||
|
||||
if (m_reader) {
|
||||
if (m_reader && !m_isTextMode) {
|
||||
m_pages = m_reader->pages();
|
||||
}
|
||||
|
||||
@@ -143,18 +229,44 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
{
|
||||
m_reader = reader;
|
||||
|
||||
beginResetModel();
|
||||
m_currentChapterData = {};
|
||||
endResetModel();
|
||||
|
||||
const bool oldTextMode = m_isTextMode;
|
||||
m_isTextMode = (reader && reader->mode() == ReadingMode::Text);
|
||||
if (oldTextMode != m_isTextMode)
|
||||
emit modeChanged();
|
||||
|
||||
int restoreChunk = 0;
|
||||
int restoreChapter = 0;
|
||||
|
||||
if (reader) {
|
||||
const auto pos = m_positions.load(reader->contentHash());
|
||||
restoreChunk = pos.chunkIndex;
|
||||
m_pageShift = pos.pageShift;
|
||||
m_mangaRTL = pos.mangaRTL;
|
||||
restoreChunk = pos.chunkIndex;
|
||||
restoreChapter = pos.chapterIndex;
|
||||
m_progress = pos.progress;
|
||||
m_pageShift = pos.pageShift;
|
||||
m_mangaRTL = pos.mangaRTL;
|
||||
emit progressChanged();
|
||||
} else {
|
||||
m_progress = 0.0;
|
||||
m_pageShift = true;
|
||||
m_mangaRTL = true;
|
||||
emit progressChanged();
|
||||
}
|
||||
|
||||
loadPages();
|
||||
if (m_isTextMode && m_reader) {
|
||||
m_chapterCount = m_reader->chapterCount();
|
||||
emit chapterCountChanged();
|
||||
loadChapter(qBound(0, restoreChapter, qMax(0, m_chapterCount - 1)));
|
||||
} else {
|
||||
m_chapterCount = 0;
|
||||
m_currentChapter = 0;
|
||||
emit chapterCountChanged();
|
||||
emit currentChapterChanged();
|
||||
loadPages();
|
||||
}
|
||||
|
||||
if (m_reader) {
|
||||
QMetaObject::invokeMethod(this, [this, restoreChunk] {
|
||||
|
||||
Reference in new issue
Block a user