fix page counter

This commit is contained in:
Agel committed 2026-10-30 15:47:53 +01:00
1 parent 511472bc73
commit da8abf0332
13 files changed
+310 -122

No files matched your search

+52 -17
View File
@@ -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 {
+6
View File
@@ -22,6 +22,7 @@ class ReadingModel : public QAbstractListModel {
Q_PROPERTY(double progress READ progress NOTIFY progressChanged)
Q_PROPERTY(int currentPage READ currentPage NOTIFY currentPageChanged)
Q_PROPERTY(int totalPages READ totalPages NOTIFY totalPagesChanged)
Q_PROPERTY(int currentChunk READ currentChunk NOTIFY currentChunkChanged)
Q_PROPERTY(int totalPagePairs READ totalPagePairs NOTIFY totalPagePairsChanged)
Q_PROPERTY(bool pageShift READ pageShift NOTIFY pageShiftChanged)
Q_PROPERTY(bool mangaRTL READ mangaRTL NOTIFY mangaRTLChanged)
@@ -48,6 +49,7 @@ public:
double progress() const;
int currentPage() const;
int totalPages() const;
int currentChunk() const;
int totalPagePairs() const;
bool pageShift() const;
bool mangaRTL() const;
@@ -84,6 +86,7 @@ signals:
void progressChanged();
void currentPageChanged();
void totalPagesChanged();
void currentChunkChanged();
void totalPagePairsChanged();
void pageShiftChanged();
void mangaRTLChanged();
@@ -99,6 +102,7 @@ private:
void runRemainingLayout();
void saveCurrentPosition(int chunkIndex, qreal intraOffset = 0.0);
void ensureChunkLayout(int row) const;
int calculatePageFromPosition(int chunkIndex, qreal intraOffset) const;
FormatReader *m_reader = nullptr;
IBookImageResolver *m_imageResolver = nullptr;
@@ -106,7 +110,9 @@ private:
bool m_measuring = false;
int m_currentPage = 0;
int m_totalPages = 0;
int m_currentChunk = 0;
QVector<ContentChunk> m_chunks;
QVector<qint64> m_chunkCumulativeChars;
mutable QVector<ChunkLayoutEngine::PreparedChunk> m_preparedChunks;
mutable QVector<ChunkLayout> m_chunkLayouts;
int m_lastSyncStart = -1;
-3
View File
@@ -70,7 +70,6 @@ struct BinaryResource {
struct ContentChunkValue {
QString section_href;
int blocks_count = 0;
int word_count = 0;
int char_count = 0;
int unnested_containers = 0;
@@ -79,7 +78,6 @@ struct ContentChunkValue {
return ContentChunkValue{
c.sectionHref,
static_cast<int>(c.blocks.size()),
c.wordCount,
c.charCount,
c.unnestedContainers
};
@@ -89,7 +87,6 @@ struct ContentChunkValue {
{
return section_href == o.section_href &&
blocks_count == o.blocks_count &&
word_count == o.word_count &&
char_count == o.char_count &&
unnested_containers == o.unnested_containers;
}
@@ -4,19 +4,18 @@
namespace {
void countInlines(const QVector<InlineItem> &inlines, int &pChars, int &pWords)
int countInlines(const QVector<InlineItem> &inlines)
{
pChars = 0;
pWords = 0;
int pChars = 0;
for (const auto &inl : inlines) {
if (inl.kind == InlineItem::Kind::Text) {
pChars += inl.text.size();
pWords += inl.text.split(QLatin1Char(' '), Qt::SkipEmptyParts).size();
} else if (inl.kind == InlineItem::Kind::Image) {
pChars += 1;
pWords += 1;
const bool isSmallIcon = (inl.cssWidth > 0 && inl.cssWidth <= 48 && inl.cssHeight > 0 && inl.cssHeight <= 48);
pChars += isSmallIcon ? 1 : kNormalizedCharsPerPage;
}
}
return pChars;
}
} // namespace
@@ -233,13 +232,10 @@ void DocumentChunkBuilder::finishParagraph()
return;
}
int pChars = 0;
int pWords = 0;
countInlines(inlines, pChars, pWords);
const int pChars = countInlines(inlines);
m_currentChunk.blocks.append(m_currentParagraph);
m_currentChunk.charCount += pChars;
m_currentChunk.wordCount += pWords;
m_hasActiveParagraph = false;
m_currentParagraph = BlockItem();
@@ -277,12 +273,9 @@ void DocumentChunkBuilder::appendBlock(const BlockItem &block)
} else if (block.kind == BlockItem::Kind::Rule) {
appendRule(block.ruleColor);
} else if (block.kind == BlockItem::Kind::Paragraph) {
int pChars = 0;
int pWords = 0;
countInlines(block.inlines, pChars, pWords);
const int pChars = countInlines(block.inlines);
m_currentChunk.blocks.append(block);
m_currentChunk.charCount += pChars;
m_currentChunk.wordCount += pWords;
if (m_currentChunk.charCount >= m_targetChars) {
m_pendingFlush = true;
+4 -2
View File
@@ -141,10 +141,13 @@ struct ParseParams {
qreal contentWidth = 600; // px, resolves percentage lengths
};
// Standard normalized page size based on publishing / typography Normseite:
// 1,800 characters with spaces (30 lines x 60 characters, ~250-300 words).
static constexpr int kNormalizedCharsPerPage = 1800;
struct ContentChunk {
QString sectionHref;
QVector<BlockItem> blocks;
int wordCount = 0;
int charCount = 0;
int unnestedContainers = 0;
@@ -154,7 +157,6 @@ struct ContentChunk {
{
return sectionHref == o.sectionHref
&& blocks == o.blocks
&& wordCount == o.wordCount
&& charCount == o.charCount
&& unnestedContainers == o.unnestedContainers;
}
@@ -707,7 +707,6 @@ QVector<ContentChunk> parseChunks(const QString &html,
b.inlines.append(textItem);
chunk.blocks.append(b);
chunk.charCount = html.size();
chunk.wordCount = html.split(QLatin1Char(' '), Qt::SkipEmptyParts).size();
return {chunk};
}
+36 -28
View File
@@ -12,6 +12,20 @@ Rectangle {
property bool restoreReady: false
property int initialIndex: 0
property real initialOffset: 0
property real lastScrollSyncTime: 0
function syncScrollPosition() {
var probeX = chunkListView.width / 2;
var probeY = chunkListView.contentY + 10;
var topIdx = chunkListView.indexAt(probeX, probeY);
if (topIdx < 0)
return;
var topItem = chunkListView.itemAtIndex(topIdx);
var intraOffset = topItem ? (chunkListView.contentY - topItem.y) : 0;
var maxScroll = Math.max(1, chunkListView.contentHeight - chunkListView.height);
var prop = Math.min(1.0, Math.max(0.0, chunkListView.contentY / maxScroll));
readingModel.setScrollPosition(topIdx, intraOffset, prop);
}
gradient: Gradient {
GradientStop { position: 0.0; color: ThemeManager.currentTheme.gradientTop }
@@ -57,6 +71,10 @@ Rectangle {
property: "contentY"
duration: 180
easing.type: Easing.OutQuad
onFinished: {
lastScrollSyncTime = Date.now();
syncScrollPosition();
}
}
function updateContentMetrics() {
@@ -68,12 +86,11 @@ Rectangle {
var probeX = chunkListView.width / 2;
var probeY = chunkListView.contentY + 10;
topIdx = chunkListView.indexAt(probeX, probeY);
if (topIdx < 0) {
topIdx = (readingModel.currentPage > 0) ? (readingModel.currentPage - 1) : 0;
}
var topItem = chunkListView.itemAtIndex(topIdx);
if (topItem && topItem.height > 0) {
intraRatio = Math.max(0.0, Math.min(1.0, (chunkListView.contentY - topItem.y) / topItem.height));
if (topIdx >= 0) {
var topItem = chunkListView.itemAtIndex(topIdx);
if (topItem && topItem.height > 0) {
intraRatio = Math.max(0.0, Math.min(1.0, (chunkListView.contentY - topItem.y) / topItem.height));
}
}
}
@@ -143,9 +160,21 @@ Rectangle {
onContentYChanged: {
if (contentReady && restoreReady) {
scrollTrackTimer.restart();
var now = Date.now();
if (now - lastScrollSyncTime >= 250) {
lastScrollSyncTime = now;
syncScrollPosition();
}
}
}
onMovementEnded: {
lastScrollSyncTime = Date.now();
syncScrollPosition();
}
onFlickEnded: {
lastScrollSyncTime = Date.now();
syncScrollPosition();
}
ScrollBar.vertical: ScrollBar {
id: vScrollBar
@@ -180,27 +209,6 @@ Rectangle {
}
}
Timer {
id: scrollTrackTimer
interval: 150
repeat: false
onTriggered: {
if (contentReady && restoreReady && readingModel.isTextMode) {
var probeX = chunkListView.width / 2;
var probeY = chunkListView.contentY + 10;
var topIdx = chunkListView.indexAt(probeX, probeY);
if (topIdx < 0) {
topIdx = (readingModel.currentPage > 0) ? (readingModel.currentPage - 1) : 0;
}
var topItem = chunkListView.itemAtIndex(topIdx);
var intraOffset = topItem ? (chunkListView.contentY - topItem.y) : 0;
var maxScroll = Math.max(1, chunkListView.contentHeight - chunkListView.height);
var prop = Math.min(1.0, Math.max(0.0, chunkListView.contentY / maxScroll));
readingModel.setScrollPosition(topIdx, intraOffset, prop);
}
}
}
Connections {
target: readingModel
function onRestoreReady(chunkIndex, intraOffset) {