Skip to content

Commit 9a55f38

Browse files
Fixed errors with page being out of bounds
1 parent 9878ffe commit 9a55f38

File tree

5 files changed

+68
-33
lines changed

5 files changed

+68
-33
lines changed

src/presentation/desktop/modules/CppElements/document_view.cpp

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ DocumentView::DocumentView()
1919
m_contentItem->setY(0);
2020

2121
// Redraw the page when the height changes
22-
connect(this, &QQuickItem::heightChanged, this, &DocumentView::redrawPages);
22+
connect(this, &QQuickItem::heightChanged, this,
23+
[this]()
24+
{
25+
ensureInBounds();
26+
redrawPages();
27+
});
28+
29+
// Update the current page when the content Y changes
30+
connect(this, &DocumentView::contentYChanged, this,
31+
&DocumentView::updateCurrentPage);
2332

2433
// Ensure the content item has the same width as the document view
2534
connect(this, &QQuickItem::implicitWidthChanged, this,
@@ -51,6 +60,15 @@ void DocumentView::setBookController(
5160
redrawPages();
5261
}
5362

63+
void DocumentView::loadDefaultBookData()
64+
{
65+
if(m_bookController == nullptr)
66+
return;
67+
68+
auto newContentY = getContentYForPage(m_bookController->getCurrentPage());
69+
setContentY(newContentY);
70+
}
71+
5472
QSGNode* DocumentView::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*)
5573
{
5674
return oldNode;
@@ -166,8 +184,6 @@ QPair<int, int> DocumentView::getPageSpanToRender()
166184
int first = m_contentY / pageStep;
167185
int last = (m_contentY + height()) / pageStep;
168186

169-
qDebug() << "First: " << first << "\tLast: " << last;
170-
171187
// For the case that contentY perfectly aligns with the book
172188
// start (0) or end, we need to add or subtract 1
173189
if(last >= m_bookController->getPageCount())
@@ -274,16 +290,21 @@ void DocumentView::ensureInBounds()
274290
emit contentYChanged();
275291
}
276292

277-
void DocumentView::loadDefaultBookData()
293+
void DocumentView::updateCurrentPage()
278294
{
279-
if(m_bookController == nullptr)
280-
return;
295+
// The current page is the one at the middle of the screen
296+
double pageStep = (m_pageHeight + m_spacing) * m_currentZoom;
297+
int currentPage = (m_contentY + height() / 2) / pageStep;
298+
if(currentPage != m_currentPage)
299+
{
300+
m_currentPage = currentPage;
301+
emit currentPageChanged();
302+
}
303+
}
281304

282-
// Calculate content y
283-
auto x = m_bookController->getCurrentPage();
284-
auto newContentY = m_bookController->getCurrentPage() *
285-
(m_pageHeight + m_spacing) * m_currentZoom;
286-
setContentY(newContentY);
305+
long DocumentView::getContentYForPage(int page) const
306+
{
307+
return page * (m_pageHeight + m_spacing) * m_currentZoom;
287308
}
288309

289310
long DocumentView::getContentHeight() const
@@ -305,6 +326,7 @@ void DocumentView::setContentY(long newContentY)
305326
m_contentItem->setY(-m_contentY);
306327
emit contentYChanged();
307328

329+
ensureInBounds();
308330
redrawPages();
309331
}
310332

@@ -343,4 +365,23 @@ void DocumentView::setCurrentZoom(double newCurrentZoom)
343365
applyZoom(newCurrentZoom, ZoomMode::Keyboard);
344366
}
345367

368+
int DocumentView::currentPage() const
369+
{
370+
return m_currentPage;
371+
}
372+
373+
void DocumentView::setCurrentPage(int newCurrentPage)
374+
{
375+
if(m_currentPage == newCurrentPage)
376+
return;
377+
378+
if(newCurrentPage < 0 || newCurrentPage >= m_bookController->getPageCount())
379+
return;
380+
381+
m_currentPage = newCurrentPage;
382+
auto newContentY = getContentYForPage(newCurrentPage);
383+
setContentY(newContentY);
384+
emit currentPageChanged();
385+
}
386+
346387
} // namespace cpp_elements

src/presentation/desktop/modules/CppElements/document_view.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class PRESENTATION_EXPORT DocumentView : public QQuickItem
2323
long contentX READ getContentX WRITE setContentX NOTIFY contentXChanged)
2424
Q_PROPERTY(double currentZoom READ getCurrentZoom WRITE setCurrentZoom
2525
NOTIFY currentZoomChanged)
26+
Q_PROPERTY(int currentPage READ currentPage WRITE setCurrentPage NOTIFY
27+
currentPageChanged)
2628

2729
public:
2830
DocumentView();
@@ -36,12 +38,16 @@ class PRESENTATION_EXPORT DocumentView : public QQuickItem
3638
double getCurrentZoom() const;
3739
void setCurrentZoom(double newCurrentZoom);
3840

41+
int currentPage() const;
42+
void setCurrentPage(int newCurrentPage);
43+
3944
signals:
4045
void contentHeightChanged();
4146
void contentYChanged();
4247
void contentXChanged();
4348
void contentWidthChanged();
4449
void currentZoomChanged();
50+
void currentPageChanged();
4551

4652
protected:
4753
QSGNode* updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*) override;
@@ -68,6 +74,8 @@ class PRESENTATION_EXPORT DocumentView : public QQuickItem
6874
double contentYForMouseZoom(double scale);
6975
void ensureInBounds();
7076
void loadDefaultBookData();
77+
void updateCurrentPage();
78+
long getContentYForPage(int page) const;
7179

7280
adapters::IBookController* m_bookController = nullptr;
7381
QHash<int, PageView*> m_activePages;
@@ -78,6 +86,7 @@ class PRESENTATION_EXPORT DocumentView : public QQuickItem
7886
double m_currentZoom = 1;
7987
double m_zoomFactor = 0.13;
8088
int m_spacing = 5;
89+
int m_currentPage = 0;
8190
};
8291

8392
} // namespace cpp_elements

src/presentation/desktop/readingPage/MDocumentView.qml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ Pane {
5353
clip: true
5454

5555
Component.onCompleted: documentView.forceActiveFocus()
56-
57-
function testX() {
58-
console.log("Link Works!")
59-
}
6056
}
6157

6258
ScrollBar {

src/presentation/desktop/readingPage/MReadingPage.qml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ Page {
106106
MReadingToolBar {
107107
id: toolbar
108108
Layout.fillWidth: true
109-
currentPage: BookController.currentPage
110109
pageCount: BookController.pageCount
111110
bookTitle: Globals.selectedBook.title
112111

@@ -322,8 +321,7 @@ Page {
322321
property bool fullScreen: false
323322
property int prevCurrentPage: -1
324323

325-
// Just assign it once (no binding)
326-
Component.onCompleted: prevCurrentPage = BookController.currentPage
324+
Component.onCompleted: prevCurrentPage = documentView.documentView.currentPage
327325

328326
function startFullScreenMode() {
329327
if (internal.fullScreen)
@@ -350,7 +348,7 @@ Page {
350348
}
351349

352350
function saveCurrentPage() {
353-
let currentPage = BookController.currentPage
351+
let currentPage = documentView.documentView.currentPage
354352
if (currentPage === internal.prevCurrentPage)
355353
return
356354

src/presentation/desktop/readingPage/readingToolbar/MReadingToolBar.qml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Pane {
1212
id: root
1313
property bool fullScreenMode: false
1414
property string bookTitle: qsTr("Unknown name")
15-
property int currentPage: 0
1615
property int lastPage: 0
1716
property int pageCount: 0
1817
property alias chapterButton: chapterButton
@@ -143,10 +142,10 @@ Pane {
143142
horizontalAlignment: TextInput.AlignHCenter
144143
verticalAlignment: TextInput.AlignVCenter
145144
selectByMouse: true
146-
text: root.currentPage + 1
147145
color: Style.colorBaseInputText
148146
font.pointSize: Fonts.size12
149147
font.weight: Font.Normal
148+
text: documentView.documentView.currentPage + 1
150149
validator: IntValidator {
151150
bottom: 0
152151
top: 99999
@@ -165,18 +164,10 @@ Pane {
165164
// but we present them as 1 to pageCount to the user.
166165
onEditingFinished: {
167166
let newPage = Number(inputField.text)
168-
if (root.currentPage == newPage - 1)
169-
return
170-
171-
if (newPage < 1 || newPage > root.pageCount) {
172-
inputField.text = Qt.binding(
173-
() => root.currentPage + 1)
174-
return
175-
}
176-
177-
documentView.setPage(newPage - 1)
178-
documentView.forceActiveFocus(
179-
) // Discard focus when finished
167+
documentView.documentView.currentPage = newPage - 1
168+
169+
// Discard focus when finished
170+
documentView.documentView.forceActiveFocus()
180171
}
181172
}
182173
}

0 commit comments

Comments
 (0)