fix page counter
This commit is contained in:
13 files changed
+310
-122
No files matched your search
@@ -97,6 +97,26 @@ double ReadingModel::progress() const
|
||||
|
||||
int ReadingModel::currentPage() const { return m_currentPage; }
|
||||
int ReadingModel::totalPages() const { return m_totalPages; }
|
||||
int ReadingModel::currentChunk() const { return m_currentChunk; }
|
||||
|
||||
int ReadingModel::calculatePageFromPosition(int chunkIndex, qreal intraOffset) const
|
||||
{
|
||||
if (!m_isTextMode) {
|
||||
return pairIndexForChunk(chunkIndex) + 1;
|
||||
}
|
||||
if (m_chunks.isEmpty() || m_totalPages <= 0) {
|
||||
return 0;
|
||||
}
|
||||
const int safeChunk = qBound(0, chunkIndex, m_chunks.size() - 1);
|
||||
qreal intraFraction = 0.0;
|
||||
if (safeChunk < m_chunkLayouts.size() && m_chunkLayouts[safeChunk].totalHeight > 0.0) {
|
||||
intraFraction = qBound(0.0, intraOffset / m_chunkLayouts[safeChunk].totalHeight, 1.0);
|
||||
}
|
||||
const qreal charsScrolled = (safeChunk < m_chunkCumulativeChars.size() ? m_chunkCumulativeChars[safeChunk] : 0)
|
||||
+ intraFraction * m_chunks[safeChunk].charCount;
|
||||
const int page = static_cast<int>(charsScrolled / kNormalizedCharsPerPage) + 1;
|
||||
return qBound(1, page, m_totalPages);
|
||||
}
|
||||
|
||||
void ReadingModel::setImageResolver(IBookImageResolver *resolver)
|
||||
{
|
||||
@@ -120,7 +140,7 @@ void ReadingModel::updateLayoutMetrics(int contentWidth, int fontSize, qreal lin
|
||||
if (!m_isTextMode || m_chunks.isEmpty())
|
||||
return;
|
||||
|
||||
const int targetChunk = (anchorChunk >= 0) ? anchorChunk : (m_currentPage > 0 ? (m_currentPage - 1) : 0);
|
||||
const int targetChunk = (anchorChunk >= 0) ? anchorChunk : (m_currentChunk >= 0 ? m_currentChunk : 0);
|
||||
if (fontSizeChanged || m_preparedChunks.size() != m_chunks.size()) {
|
||||
if (m_remainingLayoutTimer)
|
||||
m_remainingLayoutTimer->stop();
|
||||
@@ -220,24 +240,21 @@ void ReadingModel::setScrollPosition(int topChunkIndex, qreal intraOffset, qreal
|
||||
return;
|
||||
|
||||
m_intraOffset = intraOffset;
|
||||
if (m_currentChunk != topChunkIndex) {
|
||||
m_currentChunk = topChunkIndex;
|
||||
emit currentChunkChanged();
|
||||
}
|
||||
|
||||
const qreal clamped = qBound(0.0, proportion, 1.0);
|
||||
if (!qFuzzyCompare(m_progress, clamped)) {
|
||||
m_progress = clamped;
|
||||
emit progressChanged();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
const int page = calculatePageFromPosition(topChunkIndex, intraOffset);
|
||||
if (m_currentPage != page) {
|
||||
m_currentPage = page;
|
||||
emit currentPageChanged();
|
||||
}
|
||||
|
||||
saveCurrentPosition(topChunkIndex, intraOffset);
|
||||
@@ -457,6 +474,8 @@ void ReadingModel::setUseBookColours(bool enabled)
|
||||
void ReadingModel::loadPages()
|
||||
{
|
||||
m_pages.clear();
|
||||
m_chunkCumulativeChars.clear();
|
||||
if (m_currentChunk != 0) { m_currentChunk = 0; emit currentChunkChanged(); }
|
||||
if (m_currentPage != 0) { m_currentPage = 0; emit currentPageChanged(); }
|
||||
if (m_totalPages != 0) { m_totalPages = 0; emit totalPagesChanged(); }
|
||||
|
||||
@@ -475,6 +494,7 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
m_chunks.clear();
|
||||
m_preparedChunks.clear();
|
||||
m_chunkLayouts.clear();
|
||||
m_chunkCumulativeChars.clear();
|
||||
endResetModel();
|
||||
|
||||
const bool oldTextMode = m_isTextMode;
|
||||
@@ -509,17 +529,32 @@ void ReadingModel::setReader(FormatReader *reader)
|
||||
if (m_isTextMode && m_reader) {
|
||||
beginResetModel();
|
||||
m_chunks = m_reader->chunks();
|
||||
m_totalPages = m_chunks.size();
|
||||
if (m_totalPages > 0) {
|
||||
restoreChunk = qBound(0, restoreChunk, m_totalPages - 1);
|
||||
m_chunkCumulativeChars.resize(m_chunks.size() + 1);
|
||||
m_chunkCumulativeChars[0] = 0;
|
||||
for (int i = 0; i < m_chunks.size(); ++i) {
|
||||
m_chunkCumulativeChars[i + 1] = m_chunkCumulativeChars[i] + m_chunks[i].charCount;
|
||||
}
|
||||
const qint64 totalChars = m_chunkCumulativeChars.isEmpty() ? 0 : m_chunkCumulativeChars.last();
|
||||
if (m_chunks.isEmpty()) {
|
||||
m_totalPages = 0;
|
||||
} else if (totalChars <= 0) {
|
||||
m_totalPages = 1;
|
||||
} else {
|
||||
m_totalPages = std::max(1, static_cast<int>((totalChars + kNormalizedCharsPerPage - 1) / kNormalizedCharsPerPage));
|
||||
}
|
||||
|
||||
if (!m_chunks.isEmpty()) {
|
||||
restoreChunk = qBound(0, restoreChunk, m_chunks.size() - 1);
|
||||
} else {
|
||||
restoreChunk = 0;
|
||||
}
|
||||
m_currentPage = m_totalPages > 0 ? (restoreChunk + 1) : 0;
|
||||
m_currentChunk = restoreChunk;
|
||||
m_currentPage = calculatePageFromPosition(restoreChunk, restoreIntraOffset);
|
||||
endResetModel();
|
||||
|
||||
emit totalPagesChanged();
|
||||
emit currentPageChanged();
|
||||
emit currentChunkChanged();
|
||||
|
||||
startLayoutMeasurement(restoreChunk, restoreIntraOffset);
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user