summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/pdfwidgets/pdfviewer/CMakeLists.txt1
-rw-r--r--examples/pdfwidgets/pdfviewer/mainwindow.cpp43
-rw-r--r--examples/pdfwidgets/pdfviewer/mainwindow.h7
-rw-r--r--examples/pdfwidgets/pdfviewer/mainwindow.ui34
-rw-r--r--examples/pdfwidgets/pdfviewer/pageselector.cpp125
-rw-r--r--examples/pdfwidgets/pdfviewer/pageselector.h86
-rw-r--r--examples/pdfwidgets/pdfviewer/pdfviewer.pro2
-rw-r--r--src/pdf/CMakeLists.txt2
-rw-r--r--src/pdf/qpdfnavigationstack.cpp326
-rw-r--r--src/pdf/qpdfnavigationstack.h (renamed from src/pdf/qpdfpagenavigation.h)62
-rw-r--r--src/pdf/qpdfpagenavigation.cpp293
-rw-r--r--src/pdfquick/PdfMultiPageView.qml29
-rw-r--r--src/pdfquick/PdfPageView.qml4
-rw-r--r--src/pdfquick/PdfScrollablePageView.qml4
-rw-r--r--src/pdfquick/qquickpdfnavigationstack.cpp162
-rw-r--r--src/pdfquick/qquickpdfnavigationstack_p.h32
-rw-r--r--src/pdfquick/qquickpdfpageimage.cpp1
-rw-r--r--src/pdfwidgets/qpdfview.cpp14
-rw-r--r--src/pdfwidgets/qpdfview.h4
-rw-r--r--src/pdfwidgets/qpdfview_p.h2
-rw-r--r--tests/auto/pdf/CMakeLists.txt2
-rw-r--r--tests/auto/pdf/qpdfpagenavigation/CMakeLists.txt8
-rw-r--r--tests/auto/pdf/qpdfpagenavigation/pdf-sample.pagenavigation.pdfbin27523 -> 0 bytes
-rw-r--r--tests/auto/pdf/qpdfpagenavigation/tst_qpdfpagenavigation.cpp203
24 files changed, 478 insertions, 968 deletions
diff --git a/examples/pdfwidgets/pdfviewer/CMakeLists.txt b/examples/pdfwidgets/pdfviewer/CMakeLists.txt
index cae762131..e1d2fb580 100644
--- a/examples/pdfwidgets/pdfviewer/CMakeLists.txt
+++ b/examples/pdfwidgets/pdfviewer/CMakeLists.txt
@@ -15,7 +15,6 @@ find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets PdfWidgets)
qt_add_executable(pdfviewerwidgets
main.cpp
mainwindow.cpp mainwindow.h mainwindow.ui
- pageselector.cpp pageselector.h
zoomselector.cpp zoomselector.h
)
diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.cpp b/examples/pdfwidgets/pdfviewer/mainwindow.cpp
index bba1125b9..3fbb77532 100644
--- a/examples/pdfwidgets/pdfviewer/mainwindow.cpp
+++ b/examples/pdfwidgets/pdfviewer/mainwindow.cpp
@@ -51,14 +51,14 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
-#include "pageselector.h"
#include "zoomselector.h"
#include <QFileDialog>
#include <QMessageBox>
+#include <QSpinBox>
#include <QPdfBookmarkModel>
#include <QPdfDocument>
-#include <QPdfPageNavigation>
+#include <QPdfNavigationStack>
#include <QtMath>
const qreal zoomMultiplier = qSqrt(2.0);
@@ -69,7 +69,7 @@ MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_zoomSelector(new ZoomSelector(this))
- , m_pageSelector(new PageSelector(this))
+ , m_pageSelector(new QSpinBox(this))
, m_document(new QPdfDocument(this))
{
ui->setupUi(this);
@@ -77,10 +77,12 @@ MainWindow::MainWindow(QWidget *parent)
m_zoomSelector->setMaximumWidth(150);
ui->mainToolBar->insertWidget(ui->actionZoom_In, m_zoomSelector);
- m_pageSelector->setMaximumWidth(150);
- ui->mainToolBar->addWidget(m_pageSelector);
-
- m_pageSelector->setPageNavigation(ui->pdfView->pageNavigation());
+ ui->mainToolBar->insertWidget(ui->actionForward, m_pageSelector);
+ connect(m_pageSelector, &QSpinBox::valueChanged, this, &MainWindow::pageSelected);
+ auto nav = ui->pdfView->pageNavigation();
+ connect(nav, &QPdfNavigationStack::currentPageChanged, m_pageSelector, &QSpinBox::setValue);
+ connect(nav, &QPdfNavigationStack::backAvailableChanged, ui->actionBack, &QAction::setEnabled);
+ connect(nav, &QPdfNavigationStack::forwardAvailableChanged, ui->actionForward, &QAction::setEnabled);
connect(m_zoomSelector, &ZoomSelector::zoomModeChanged, ui->pdfView, &QPdfView::setZoomMode);
connect(m_zoomSelector, &ZoomSelector::zoomFactorChanged, ui->pdfView, &QPdfView::setZoomFactor);
@@ -111,6 +113,8 @@ void MainWindow::open(const QUrl &docLocation)
m_document->load(docLocation.toLocalFile());
const auto documentTitle = m_document->metaData(QPdfDocument::Title).toString();
setWindowTitle(!documentTitle.isEmpty() ? documentTitle : QStringLiteral("PDF Viewer"));
+ pageSelected(0);
+ m_pageSelector->setMaximum(m_document->pageCount() - 1);
} else {
qCDebug(lcExample) << docLocation << "is not a valid local file";
QMessageBox::critical(this, tr("Failed to open"), tr("%1 is not a valid local file").arg(docLocation.toString()));
@@ -124,7 +128,14 @@ void MainWindow::bookmarkSelected(const QModelIndex &index)
return;
const int page = index.data(QPdfBookmarkModel::PageNumberRole).toInt();
- ui->pdfView->pageNavigation()->setCurrentPage(page);
+ const qreal zoomLevel = index.data(QPdfBookmarkModel::LevelRole).toReal();
+ ui->pdfView->pageNavigation()->jump(page, {}, zoomLevel);
+}
+
+void MainWindow::pageSelected(int page)
+{
+ auto nav = ui->pdfView->pageNavigation();
+ nav->jump(page, {}, nav->currentZoom());
}
void MainWindow::on_actionOpen_triggered()
@@ -162,15 +173,27 @@ void MainWindow::on_actionZoom_Out_triggered()
void MainWindow::on_actionPrevious_Page_triggered()
{
- ui->pdfView->pageNavigation()->goToPreviousPage();
+ auto nav = ui->pdfView->pageNavigation();
+ nav->jump(nav->currentPage() - 1, {}, nav->currentZoom());
}
void MainWindow::on_actionNext_Page_triggered()
{
- ui->pdfView->pageNavigation()->goToNextPage();
+ auto nav = ui->pdfView->pageNavigation();
+ nav->jump(nav->currentPage() + 1, {}, nav->currentZoom());
}
void MainWindow::on_actionContinuous_triggered()
{
ui->pdfView->setPageMode(ui->actionContinuous->isChecked() ? QPdfView::MultiPage : QPdfView::SinglePage);
}
+
+void MainWindow::on_actionBack_triggered()
+{
+ ui->pdfView->pageNavigation()->back();
+}
+
+void MainWindow::on_actionForward_triggered()
+{
+ ui->pdfView->pageNavigation()->forward();
+}
diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.h b/examples/pdfwidgets/pdfviewer/mainwindow.h
index 571dbd911..147f7fc01 100644
--- a/examples/pdfwidgets/pdfviewer/mainwindow.h
+++ b/examples/pdfwidgets/pdfviewer/mainwindow.h
@@ -63,9 +63,9 @@ class MainWindow;
class QPdfDocument;
class QPdfView;
+class QSpinBox;
QT_END_NAMESPACE
-class PageSelector;
class ZoomSelector;
class MainWindow : public QMainWindow
@@ -81,6 +81,7 @@ public slots:
private slots:
void bookmarkSelected(const QModelIndex &index);
+ void pageSelected(int page);
// action handlers
void on_actionOpen_triggered();
@@ -92,11 +93,13 @@ private slots:
void on_actionPrevious_Page_triggered();
void on_actionNext_Page_triggered();
void on_actionContinuous_triggered();
+ void on_actionBack_triggered();
+ void on_actionForward_triggered();
private:
Ui::MainWindow *ui;
ZoomSelector *m_zoomSelector;
- PageSelector *m_pageSelector;
+ QSpinBox *m_pageSelector;
QPdfDocument *m_document;
};
diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.ui b/examples/pdfwidgets/pdfviewer/mainwindow.ui
index 1164243f6..304d4cfc8 100644
--- a/examples/pdfwidgets/pdfviewer/mainwindow.ui
+++ b/examples/pdfwidgets/pdfviewer/mainwindow.ui
@@ -134,7 +134,7 @@
<x>0</x>
<y>0</y>
<width>700</width>
- <height>22</height>
+ <height>23</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@@ -184,6 +184,8 @@
<addaction name="actionZoom_Out"/>
<addaction name="actionZoom_In"/>
<addaction name="separator"/>
+ <addaction name="actionBack"/>
+ <addaction name="actionForward"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionOpen">
@@ -278,6 +280,36 @@
<string>Continuous</string>
</property>
</action>
+ <action name="actionBack">
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="icon">
+ <iconset resource="resources.qrc">
+ <normaloff>:/icons/images/go-previous-view.svgz</normaloff>:/icons/images/go-previous-view.svgz</iconset>
+ </property>
+ <property name="text">
+ <string>Back</string>
+ </property>
+ <property name="toolTip">
+ <string>back to previous view</string>
+ </property>
+ </action>
+ <action name="actionForward">
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="icon">
+ <iconset resource="resources.qrc">
+ <normaloff>:/icons/images/go-next-view.svgz</normaloff>:/icons/images/go-next-view.svgz</iconset>
+ </property>
+ <property name="text">
+ <string>Forward</string>
+ </property>
+ <property name="toolTip">
+ <string>forward to next view</string>
+ </property>
+ </action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
diff --git a/examples/pdfwidgets/pdfviewer/pageselector.cpp b/examples/pdfwidgets/pdfviewer/pageselector.cpp
deleted file mode 100644
index 28bff1359..000000000
--- a/examples/pdfwidgets/pdfviewer/pageselector.cpp
+++ /dev/null
@@ -1,125 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtPDF module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include "pageselector.h"
-
-#include <QHBoxLayout>
-#include <QLabel>
-#include <QLineEdit>
-#include <QPdfPageNavigation>
-#include <QToolButton>
-
-PageSelector::PageSelector(QWidget *parent)
- : QWidget(parent)
- , m_pageNavigation(nullptr)
-{
- QHBoxLayout *layout = new QHBoxLayout(this);
-
- m_previousPageButton = new QToolButton(this);
- m_previousPageButton->setText("<");
- m_previousPageButton->setEnabled(false);
-
- m_pageNumberEdit = new QLineEdit(this);
- m_pageNumberEdit->setAlignment(Qt::AlignRight);
-
- m_pageCountLabel = new QLabel(this);
- m_pageCountLabel->setText("0");
-
- m_nextPageButton = new QToolButton(this);
- m_nextPageButton->setText(">");
- m_nextPageButton->setEnabled(false);
-
- layout->addWidget(m_previousPageButton);
- layout->addWidget(m_pageNumberEdit);
- layout->addWidget(m_pageCountLabel);
- layout->addWidget(m_nextPageButton);
-}
-
-void PageSelector::setPageNavigation(QPdfPageNavigation *pageNavigation)
-{
- m_pageNavigation = pageNavigation;
-
- connect(m_previousPageButton, &QToolButton::clicked, m_pageNavigation, &QPdfPageNavigation::goToPreviousPage);
- connect(m_pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged, m_previousPageButton, &QToolButton::setEnabled);
-
- connect(m_pageNavigation, &QPdfPageNavigation::currentPageChanged, this, &PageSelector::onCurrentPageChanged);
- connect(m_pageNavigation, &QPdfPageNavigation::pageCountChanged, this, [this](int pageCount){ m_pageCountLabel->setText(QString::fromLatin1("/ %1").arg(pageCount)); });
-
- connect(m_pageNumberEdit, &QLineEdit::editingFinished, this, &PageSelector::pageNumberEdited);
-
- connect(m_nextPageButton, &QToolButton::clicked, m_pageNavigation, &QPdfPageNavigation::goToNextPage);
- connect(m_pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged, m_nextPageButton, &QToolButton::setEnabled);
-
- onCurrentPageChanged(m_pageNavigation->currentPage());
-}
-
-void PageSelector::onCurrentPageChanged(int page)
-{
- if (m_pageNavigation->pageCount() == 0)
- m_pageNumberEdit->setText(QString::number(0));
- else
- m_pageNumberEdit->setText(QString::number(page + 1));
-}
-
-void PageSelector::pageNumberEdited()
-{
- if (!m_pageNavigation)
- return;
-
- const QString text = m_pageNumberEdit->text();
-
- bool ok = false;
- const int pageNumber = text.toInt(&ok);
-
- if (!ok)
- onCurrentPageChanged(m_pageNavigation->currentPage());
- else
- m_pageNavigation->setCurrentPage(qBound(0, pageNumber - 1, m_pageNavigation->pageCount() - 1));
-}
diff --git a/examples/pdfwidgets/pdfviewer/pageselector.h b/examples/pdfwidgets/pdfviewer/pageselector.h
deleted file mode 100644
index 58fc6d2f0..000000000
--- a/examples/pdfwidgets/pdfviewer/pageselector.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtPDF module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#ifndef PAGESELECTOR_H
-#define PAGESELECTOR_H
-
-#include <QWidget>
-
-QT_BEGIN_NAMESPACE
-class QLabel;
-class QLineEdit;
-class QPdfDocument;
-class QPdfPageNavigation;
-class QToolButton;
-QT_END_NAMESPACE
-
-class PageSelector : public QWidget
-{
- Q_OBJECT
-
-public:
- explicit PageSelector(QWidget *parent = nullptr);
-
- void setPageNavigation(QPdfPageNavigation *pageNavigation);
-
-private slots:
- void onCurrentPageChanged(int page);
- void pageNumberEdited();
-
-private:
- QPdfPageNavigation *m_pageNavigation;
-
- QLineEdit *m_pageNumberEdit;
- QLabel *m_pageCountLabel;
- QToolButton *m_previousPageButton;
- QToolButton *m_nextPageButton;
-};
-
-#endif // PAGESELECTOR_H
diff --git a/examples/pdfwidgets/pdfviewer/pdfviewer.pro b/examples/pdfwidgets/pdfviewer/pdfviewer.pro
index ad0607ea5..08807fa70 100644
--- a/examples/pdfwidgets/pdfviewer/pdfviewer.pro
+++ b/examples/pdfwidgets/pdfviewer/pdfviewer.pro
@@ -5,12 +5,10 @@ QT += core gui widgets pdfwidgets
SOURCES += \
main.cpp \
mainwindow.cpp \
- pageselector.cpp \
zoomselector.cpp
HEADERS += \
mainwindow.h \
- pageselector.h \
zoomselector.h
FORMS += \
diff --git a/src/pdf/CMakeLists.txt b/src/pdf/CMakeLists.txt
index 9243b2a41..51e9c6045 100644
--- a/src/pdf/CMakeLists.txt
+++ b/src/pdf/CMakeLists.txt
@@ -20,7 +20,7 @@ qt_internal_add_module(Pdf
qpdffile.cpp qpdffile_p.h
qpdflink.cpp qpdflink.h qpdflink_p.h
qpdflinkmodel.cpp qpdflinkmodel_p.h qpdflinkmodel_p_p.h
- qpdfpagenavigation.cpp qpdfpagenavigation.h
+ qpdfnavigationstack.cpp qpdfnavigationstack.h
qpdfpagerenderer.cpp qpdfpagerenderer.h
qpdfsearchmodel.cpp qpdfsearchmodel.h qpdfsearchmodel_p.h
qpdfselection.cpp qpdfselection.h qpdfselection_p.h
diff --git a/src/pdf/qpdfnavigationstack.cpp b/src/pdf/qpdfnavigationstack.cpp
new file mode 100644
index 000000000..a1d193f3a
--- /dev/null
+++ b/src/pdf/qpdfnavigationstack.cpp
@@ -0,0 +1,326 @@
+/****************************************************************************
+**
+** Copyright (C) 2022 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtPDF module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qpdfnavigationstack.h"
+#include "qpdfdocument.h"
+#include "qpdflink_p.h"
+
+#include <QtCore/qloggingcategory.h>
+#include <QtCore/qpointer.h>
+
+QT_BEGIN_NAMESPACE
+
+Q_LOGGING_CATEGORY(qLcNav, "qt.pdf.navigationstack")
+
+struct QPdfNavigationStackPrivate
+{
+ QPdfNavigationStack *q = nullptr;
+
+ QList<QExplicitlySharedDataPointer<QPdfLinkPrivate>> pageHistory;
+ int currentHistoryIndex = 0;
+ bool changing = false;
+};
+
+/*!
+ \class QPdfNavigationStack
+ \since 6.4
+ \inmodule QtPdf
+ \brief Navigation history within a PDF document
+
+ The QPdfNavigationStack class remembers which destinations the user
+ has visited in a PDF document, and provides the ability to traverse
+ backward and forward. It is used to implement back and forward actions
+ similar to the back and forward buttons in a web browser.
+
+ \sa QPdfDocument
+*/
+
+/*!
+ Constructs a page navigation stack with parent object \a parent.
+*/
+QPdfNavigationStack::QPdfNavigationStack(QObject *parent)
+ : QObject(parent), d(new QPdfNavigationStackPrivate)
+{
+ d->q = this;
+}
+
+/*!
+ Destroys the page navigation stack.
+*/
+QPdfNavigationStack::~QPdfNavigationStack()
+{
+}
+
+/*!
+ Goes back to the page, location and zoom level that was being viewed before
+ back() was called, and then emits the \l jumped() signal.
+
+ If a new destination was pushed since the last time \l back() was called,
+ the forward() function does nothing, because there is a branch in the
+ timeline which causes the "future" to be lost.
+*/
+void QPdfNavigationStack::forward()
+{
+ if (d->currentHistoryIndex >= d->pageHistory.count() - 1)
+ return;
+ const bool backAvailableWas = backAvailable();
+ const bool forwardAvailableWas = forwardAvailable();
+ QPointF currentLocationWas = currentLocation();
+ qreal currentZoomWas = currentZoom();
+ ++d->currentHistoryIndex;
+ d->changing = true;
+ emit jumped(currentPage(), currentLocation(), currentZoom());
+ if (currentZoomWas != currentZoom())
+ emit currentZoomChanged(currentZoom());
+ emit currentPageChanged(currentPage());
+ if (currentLocationWas != currentLocation())
+ emit currentLocationChanged(currentLocation());
+ if (!backAvailableWas)
+ emit backAvailableChanged(backAvailable());
+ if (forwardAvailableWas != forwardAvailable())
+ emit forwardAvailableChanged(forwardAvailable());
+ d->changing = false;
+ qCDebug(qLcNav) << "forward: index" << d->currentHistoryIndex << "page" << currentPage()
+ << "@" << currentLocation() << "zoom" << currentZoom();
+}
+
+/*!
+ Pops the stack, updates the \l currentPage, \l currentLocation and
+ \l currentZoom properties to the most-recently-viewed destination, and then
+ emits the \l jumped() signal.
+*/
+void QPdfNavigationStack::back()
+{
+ if (d->currentHistoryIndex <= 0)
+ return;
+ const bool backAvailableWas = backAvailable();
+ const bool forwardAvailableWas = forwardAvailable();
+ QPointF currentLocationWas = currentLocation();
+ qreal currentZoomWas = currentZoom();
+ --d->currentHistoryIndex;
+ d->changing = true;
+ emit jumped(currentPage(), currentLocation(), currentZoom());
+ if (currentZoomWas != currentZoom())
+ emit currentZoomChanged(currentZoom());
+ emit currentPageChanged(currentPage());
+ if (currentLocationWas != currentLocation())
+ emit currentLocationChanged(currentLocation());
+ if (backAvailableWas != backAvailable())
+ emit backAvailableChanged(backAvailable());
+ if (!forwardAvailableWas)
+ emit forwardAvailableChanged(forwardAvailable());
+ d->changing = false;
+ qCDebug(qLcNav) << "back: index" << d->currentHistoryIndex << "page" << currentPage()
+ << "@" << currentLocation() << "zoom" << currentZoom();
+}
+/*!
+ \property QPdfNavigationStack::currentPage
+
+ This property holds the current page that is being viewed.
+ The default is \c 0.
+*/
+int QPdfNavigationStack::currentPage() const
+{
+ if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.count())
+ return -1; // only until ctor or clear() runs
+ return d->pageHistory.at(d->currentHistoryIndex)->page;
+}
+
+/*!
+ \property QPdfNavigationStack::currentLocation
+
+ This property holds the current location on the page that is being viewed
+ (the location that was last given to jump() or update()). The default is
+ \c {0, 0}.
+*/
+QPointF QPdfNavigationStack::currentLocation() const
+{
+ if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.count())
+ return QPointF();
+ return d->pageHistory.at(d->currentHistoryIndex)->location;
+}
+
+/*!
+ \property QPdfNavigationStack::currentZoom
+
+ This property holds the magnification scale (1 logical pixel = 1 point)
+ on the page that is being viewed. The default is \c 1.
+*/
+qreal QPdfNavigationStack::currentZoom() const
+{
+ if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.count())
+ return 1;
+ return d->pageHistory.at(d->currentHistoryIndex)->zoom;
+}
+
+/*!
+ Adds the given destination, consisting of \a page, \a location, and \a zoom,
+ to the history of visited locations. If \a emitJumped is \c false, the
+ \l jumped() signal will not be emitted.
+
+ The \a zoom argument represents magnification (where \c 1 is the default
+ scale, 1 logical pixel = 1 point). If \a zoom is given as \c 0, currentZoom
+ keeps its existing value, and currentZoomChanged is not emitted.
+
+ The \a location should be the same as QPdfLink::location() if the user is
+ following a link; and since that is specified as the upper-left corner of
+ the destination, it is best for consistency to always use the location
+ visible in the upper-left corner of the viewport, in points.
+
+ If forwardAvailable is \c true, calling this function represents a branch
+ in the timeline which causes the "future" to be lost, and therefore
+ forwardAvailable will change to \c false.
+*/
+void QPdfNavigationStack::jump(int page, const QPointF &location, qreal zoom)
+{
+ if (page == currentPage() && location == currentLocation() && zoom == currentZoom())
+ return;
+ if (qFuzzyIsNull(zoom))
+ zoom = currentZoom();
+ const bool zoomChange = !qFuzzyCompare(zoom, currentZoom());
+ const bool pageChange = (page != currentPage());
+ const bool locationChange = (location != currentLocation());
+ const bool backAvailableWas = backAvailable();
+ const bool forwardAvailableWas = forwardAvailable();
+ if (!d->changing) {
+ if (d->currentHistoryIndex >= 0 && forwardAvailableWas)
+ d->pageHistory.remove(d->currentHistoryIndex + 1, d->pageHistory.count() - d->currentHistoryIndex - 1);
+ d->pageHistory.append(QExplicitlySharedDataPointer<QPdfLinkPrivate>(new QPdfLinkPrivate(page, location, zoom)));
+ d->currentHistoryIndex = d->pageHistory.count() - 1;
+ }
+ if (zoomChange)
+ emit currentZoomChanged(currentZoom());
+ if (pageChange)
+ emit currentPageChanged(currentPage());
+ if (locationChange)
+ emit currentLocationChanged(currentLocation());
+ if (d->changing)
+ return;
+ if (!backAvailableWas)
+ emit backAvailableChanged(backAvailable());
+ if (forwardAvailableWas)
+ emit forwardAvailableChanged(forwardAvailable());
+ emit jumped(page, location, zoom);
+ qCDebug(qLcNav) << "push: index" << d->currentHistoryIndex << "page" << page
+ << "@" << location << "zoom" << zoom << "-> history" <<
+ [this]() {
+ QStringList ret;
+ for (auto d : d->pageHistory)
+ ret << QString::number(d->page);
+ return ret.join(QLatin1Char(','));
+ }();
+}
+
+/*!
+ Modifies the current destination, consisting of \a page, \a location and \a zoom.
+
+ This can be called periodically while the user is manually moving around
+ the document, so that after back() is called, forward() will jump back to
+ the most-recently-viewed destination rather than the destination that was
+ last specified by push().
+
+ The \c currentZoomChanged, \c currentPageChanged and \c currentLocationChanged
+ signals will be emitted if the respective properties are actually changed.
+ The \l jumped signal is not emitted, because this operation represents
+ smooth movement rather than a navigational jump.
+*/
+void QPdfNavigationStack::update(int page, const QPointF &location, qreal zoom)
+{
+ if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.count())
+ return;
+ int currentPageWas = currentPage();
+ QPointF currentLocationWas = currentLocation();
+ qreal currentZoomWas = currentZoom();
+ if (page == currentPageWas && location == currentLocationWas && zoom == currentZoomWas)
+ return;
+ d->pageHistory[d->currentHistoryIndex]->page = page;
+ d->pageHistory[d->currentHistoryIndex]->location = location;
+ d->pageHistory[d->currentHistoryIndex]->zoom = zoom;
+ if (currentZoomWas != zoom)
+ emit currentZoomChanged(currentZoom());
+ if (currentPageWas != page)
+ emit currentPageChanged(currentPage());
+ if (currentLocationWas != location)
+ emit currentLocationChanged(currentLocation());
+ qCDebug(qLcNav) << "update: index" << d->currentHistoryIndex << "page" << page
+ << "@" << location << "zoom" << zoom << "-> history" <<
+ [this]() {
+ QStringList ret;
+ for (auto d : d->pageHistory)
+ ret << QString::number(d->page);
+ return ret.join(QLatin1Char(','));
+ }();
+}
+
+/*!
+ \property QPdfNavigationStack::backAvailable
+ \readonly
+
+ Holds \c true if a \e back destination is available in the history:
+ that is, if push() or forward() has been called.
+*/
+bool QPdfNavigationStack::backAvailable() const
+{
+ return d->currentHistoryIndex > 0;
+}
+
+/*!
+ \property QPdfNavigationStack::forwardAvailable
+ \readonly
+
+ Holds \c true if a \e forward destination is available in the history:
+ that is, if back() has been previously called.
+*/
+bool QPdfNavigationStack::forwardAvailable() const
+{
+ return d->currentHistoryIndex < d->pageHistory.count() - 1;
+}
+
+/*!
+ \fn void QPdfNavigationStack::jumped(int page, const QPointF &location, qreal zoom)
+
+ This signal is emitted when an abrupt jump occurs, to the specified \a page
+ index, \a location on the page, and \a zoom level; but \e not when simply
+ scrolling through the document one page at a time. That is, jump(),
+ forward() and back() emit this signal, but update() does not.
+*/
+
+QT_END_NAMESPACE
+
+#include "moc_qpdfnavigationstack.cpp"
diff --git a/src/pdf/qpdfpagenavigation.h b/src/pdf/qpdfnavigationstack.h
index fe2071b68..c90697b61 100644
--- a/src/pdf/qpdfpagenavigation.h
+++ b/src/pdf/qpdfnavigationstack.h
@@ -1,7 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Tobias König <tobias.koenig@kdab.com>
-** Copyright (C) 2020 The Qt Company Ltd.
+** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtPDF module of the Qt Toolkit.
@@ -38,59 +37,56 @@
**
****************************************************************************/
-#ifndef QPDFPAGENAVIGATION_H
-#define QPDFPAGENAVIGATION_H
+#ifndef QPDFNAVIGATIONSTACK_H
+#define QPDFNAVIGATIONSTACK_H
#include <QtPdf/qtpdfglobal.h>
#include <QtCore/qobject.h>
QT_BEGIN_NAMESPACE
-class QPdfDocument;
-class QPdfPageNavigationPrivate;
+struct QPdfNavigationStackPrivate;
-class Q_PDF_EXPORT QPdfPageNavigation : public QObject
+class Q_PDF_EXPORT QPdfNavigationStack : public QObject
{
Q_OBJECT
- Q_PROPERTY(QPdfDocument* document READ document WRITE setDocument NOTIFY documentChanged)
-
- Q_PROPERTY(int currentPage READ currentPage WRITE setCurrentPage NOTIFY currentPageChanged)
- Q_PROPERTY(int pageCount READ pageCount NOTIFY pageCountChanged)
- Q_PROPERTY(bool canGoToPreviousPage READ canGoToPreviousPage NOTIFY canGoToPreviousPageChanged)
- Q_PROPERTY(bool canGoToNextPage READ canGoToNextPage NOTIFY canGoToNextPageChanged)
+ Q_PROPERTY(int currentPage READ currentPage NOTIFY currentPageChanged)
+ Q_PROPERTY(QPointF currentLocation READ currentLocation NOTIFY currentLocationChanged)
+ Q_PROPERTY(qreal currentZoom READ currentZoom NOTIFY currentZoomChanged)
+ Q_PROPERTY(bool backAvailable READ backAvailable NOTIFY backAvailableChanged)
+ Q_PROPERTY(bool forwardAvailable READ forwardAvailable NOTIFY forwardAvailableChanged)
public:
- QPdfPageNavigation() : QPdfPageNavigation(nullptr) {}
- explicit QPdfPageNavigation(QObject *parent);
- ~QPdfPageNavigation() override;
-
- QPdfDocument* document() const;
- void setDocument(QPdfDocument *document);
+ QPdfNavigationStack() : QPdfNavigationStack(nullptr) {}
+ explicit QPdfNavigationStack(QObject *parent);
+ ~QPdfNavigationStack() override;
int currentPage() const;
- void setCurrentPage(int currentPage);
-
- int pageCount() const;
+ QPointF currentLocation() const;
+ qreal currentZoom() const;
- bool canGoToPreviousPage() const;
- bool canGoToNextPage() const;
+ bool backAvailable() const;
+ bool forwardAvailable() const;
public Q_SLOTS:
- void goToPreviousPage();
- void goToNextPage();
+ void jump(int page, const QPointF &location, qreal zoom);
+ void update(int page, const QPointF &location, qreal zoom);
+ void forward();
+ void back();
Q_SIGNALS:
- void documentChanged(QPdfDocument *document);
- void currentPageChanged(int currentPage);
- void pageCountChanged(int pageCount);
- void canGoToPreviousPageChanged(bool canGo);
- void canGoToNextPageChanged(bool canGo);
+ void currentPageChanged(int page);
+ void currentLocationChanged(QPointF location);
+ void currentZoomChanged(qreal zoom);
+ void backAvailableChanged(bool available);
+ void forwardAvailableChanged(bool available);
+ void jumped(int page, const QPointF &location, qreal zoom);
private:
- QScopedPointer<QPdfPageNavigationPrivate> d_ptr;
+ QScopedPointer<QPdfNavigationStackPrivate> d;
};
QT_END_NAMESPACE
-#endif
+#endif // QPDFNAVIGATIONSTACK_H
diff --git a/src/pdf/qpdfpagenavigation.cpp b/src/pdf/qpdfpagenavigation.cpp
deleted file mode 100644
index 410662526..000000000
--- a/src/pdf/qpdfpagenavigation.cpp
+++ /dev/null
@@ -1,293 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Tobias König <tobias.koenig@kdab.com>
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtPDF module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include "qpdfpagenavigation.h"
-
-#include "qpdfdocument.h"
-
-#include <private/qobject_p.h>
-
-#include <QPointer>
-
-QT_BEGIN_NAMESPACE
-
-class QPdfPageNavigationPrivate
-{
-public:
- QPdfPageNavigationPrivate(QPdfPageNavigation *q) : q_ptr(q) { }
-
- void update()
- {
- const bool documentAvailable = m_document && m_document->status() == QPdfDocument::Ready;
-
- if (documentAvailable) {
- const int newPageCount = m_document->pageCount();
- if (m_pageCount != newPageCount) {
- m_pageCount = newPageCount;
- emit q_ptr->pageCountChanged(m_pageCount);
- }
- } else {
- if (m_pageCount != 0) {
- m_pageCount = 0;
- emit q_ptr->pageCountChanged(m_pageCount);
- }
- }
-
- if (m_currentPage != 0) {
- m_currentPage = 0;
- emit q_ptr->currentPageChanged(m_currentPage);
- }
-
- updatePrevNext();
- }
-
- void updatePrevNext()
- {
- const bool hasPreviousPage = m_currentPage > 0;
- const bool hasNextPage = m_currentPage < (m_pageCount - 1);
-
- if (m_canGoToPreviousPage != hasPreviousPage) {
- m_canGoToPreviousPage = hasPreviousPage;
- emit q_ptr->canGoToPreviousPageChanged(m_canGoToPreviousPage);
- }
-
- if (m_canGoToNextPage != hasNextPage) {
- m_canGoToNextPage = hasNextPage;
- emit q_ptr->canGoToNextPageChanged(m_canGoToNextPage);
- }
- }
-
- void documentStatusChanged()
- {
- update();
- }
-
- QPdfPageNavigation *q_ptr = nullptr;
- QPointer<QPdfDocument> m_document = nullptr;
- int m_currentPage = 0;
- int m_pageCount = 0;
- bool m_canGoToPreviousPage = false;
- bool m_canGoToNextPage = false;
-
- QMetaObject::Connection m_documentStatusChangedConnection;
-};
-
-/*!
- \class QPdfPageNavigation
- \since 5.10
- \inmodule QtPdf
-
- \brief The QPdfPageNavigation class handles the navigation through a PDF document.
-
- \sa QPdfDocument
-*/
-
-
-/*!
- Constructs a page navigation object with parent object \a parent.
-*/
-QPdfPageNavigation::QPdfPageNavigation(QObject *parent)
- : QObject(parent), d_ptr(new QPdfPageNavigationPrivate(this))
-{
-}
-
-/*!
- Destroys the page navigation object.
-*/
-QPdfPageNavigation::~QPdfPageNavigation()
-{
-}
-
-/*!
- \property QPdfPageNavigation::document
- \brief The document instance on which this object navigates.
-
- By default, this property is \c nullptr.
-
- \sa document(), setDocument(), QPdfDocument
-*/
-
-/*!
- Returns the document on which this object navigates, or a \c nullptr
- if none has set before.
-
- \sa QPdfDocument
-*/
-QPdfDocument* QPdfPageNavigation::document() const
-{
- return d_ptr->m_document;
-}
-
-/*!
- Sets the \a document this object navigates on.
-
- After a new document has been set, the currentPage will be \c 0.
-
- \sa QPdfDocument
-*/
-void QPdfPageNavigation::setDocument(QPdfDocument *document)
-{
- if (d_ptr->m_document == document)
- return;
-
- if (d_ptr->m_document)
- disconnect(d_ptr->m_documentStatusChangedConnection);
-
- d_ptr->m_document = document;
- emit documentChanged(d_ptr->m_document);
-
- if (d_ptr->m_document)
- d_ptr->m_documentStatusChangedConnection =
- connect(d_ptr->m_document.data(), &QPdfDocument::statusChanged, this,
- [this]() { d_ptr->documentStatusChanged(); });
-
- d_ptr->update();
-}
-
-/*!
- \property QPdfPageNavigation::currentPage
- \brief The current page number in the document.
-
- \sa currentPage(), setCurrentPage()
-*/
-
-/*!
- Returns the current page number or \c 0 if there is no document set.
-
- After a document has been loaded, the currentPage will always be \c 0.
-*/
-int QPdfPageNavigation::currentPage() const
-{
- return d_ptr->m_currentPage;
-}
-
-/*!
- \fn void QPdfPageNavigation::setCurrentPage(int page)
-
- Sets the current \a page number.
-*/
-void QPdfPageNavigation::setCurrentPage(int newPage)
-{
- if (newPage < 0 || newPage >= d_ptr->m_pageCount)
- return;
-
- if (d_ptr->m_currentPage == newPage)
- return;
-
- d_ptr->m_currentPage = newPage;
- emit currentPageChanged(d_ptr->m_currentPage);
-
- d_ptr->updatePrevNext();
-}
-
-/*!
- \property QPdfPageNavigation::pageCount
- \brief The number of pages in the document.
-
- \sa pageCount()
-*/
-
-/*!
- Returns the number of pages in the document or \c 0 if there
- is no document set.
-*/
-int QPdfPageNavigation::pageCount() const
-{
- return d_ptr->m_pageCount;
-}
-
-/*!
- \property QPdfPageNavigation::canGoToPreviousPage
- \brief Indicates whether there is a page before the current page.
-
- \sa canGoToPreviousPage(), goToPreviousPage()
-*/
-
-/*!
- Returns whether there is a page before the current one.
-*/
-bool QPdfPageNavigation::canGoToPreviousPage() const
-{
- return d_ptr->m_canGoToPreviousPage;
-}
-
-/*!
- \property QPdfPageNavigation::canGoToNextPage
- \brief Indicates whether there is a page after the current page.
-
- \sa canGoToNextPage(), goToNextPage()
-*/
-
-/*!
- Returns whether there is a page after the current one.
-*/
-bool QPdfPageNavigation::canGoToNextPage() const
-{
- return d_ptr->m_canGoToNextPage;
-}
-
-/*!
- Changes the current page to the previous page.
-
- If there is no previous page in the document, nothing happens.
-
- \sa canGoToPreviousPage
-*/
-void QPdfPageNavigation::goToPreviousPage()
-{
- if (d_ptr->m_currentPage > 0)
- setCurrentPage(d_ptr->m_currentPage - 1);
-}
-
-/*!
- Changes the current page to the next page.
-
- If there is no next page in the document, nothing happens.
-
- \sa canGoToNextPage
-*/
-void QPdfPageNavigation::goToNextPage()
-{
- if (d_ptr->m_currentPage < d_ptr->m_pageCount - 1)
- setCurrentPage(d_ptr->m_currentPage + 1);
-}
-
-QT_END_NAMESPACE
-
-#include "moc_qpdfpagenavigation.cpp"
diff --git a/src/pdfquick/PdfMultiPageView.qml b/src/pdfquick/PdfMultiPageView.qml
index dca052d87..e2148c1fd 100644
--- a/src/pdfquick/PdfMultiPageView.qml
+++ b/src/pdfquick/PdfMultiPageView.qml
@@ -196,12 +196,12 @@ Item {
*/
function goToLocation(page, location, zoom) {
if (zoom > 0) {
- navigationStack.jumping = true // don't call navigationStack.update() because we will push() instead
+ navigationStack.jumping = true // don't call navigationStack.update() because we will jump() instead
root.renderScale = zoom
tableView.forceLayout() // but do ensure that the table layout is correct before we try to jump
navigationStack.jumping = false
}
- navigationStack.push(page, location, zoom) // actually jump
+ navigationStack.jump(page, location, zoom) // actually jump
}
/*!
@@ -567,20 +567,25 @@ Item {
id: vscroll
property bool moved: false
onPositionChanged: moved = true
- onActiveChanged: {
+ onPressedChanged: if (pressed) {
+ // When the user starts scrolling, push the location where we came from so the user can go "back" there
const cell = tableView.cellAtPos(root.width / 2, root.height / 2)
const currentItem = tableView.itemAtCell(cell)
const currentLocation = currentItem
- ? Qt.point((tableView.contentX - currentItem.x + jumpLocationMargin.x) / root.renderScale,
- (tableView.contentY - currentItem.y + jumpLocationMargin.y) / root.renderScale)
+ ? Qt.point((tableView.contentX - currentItem.x + tableView.jumpLocationMargin.x) / root.renderScale,
+ (tableView.contentY - currentItem.y + tableView.jumpLocationMargin.y) / root.renderScale)
: Qt.point(0, 0) // maybe the delegate wasn't loaded yet
- if (active) {
- moved = false
- // emitJumped false to avoid interrupting a pinch if TableView thinks it should scroll at the same time
- navigationStack.push(cell.y, currentLocation, root.renderScale, false)
- } else if (moved) {
- navigationStack.update(cell.y, currentLocation, root.renderScale)
- }
+ navigationStack.jump(cell.y, currentLocation, root.renderScale)
+ }
+ onActiveChanged: if (!active ) {
+ // When the scrollbar stops moving, tell navstack where we are, so as to update currentPage etc.
+ const cell = tableView.cellAtPos(root.width / 2, root.height / 2)
+ const currentItem = tableView.itemAtCell(cell)
+ const currentLocation = currentItem
+ ? Qt.point((tableView.contentX - currentItem.x + tableView.jumpLocationMargin.x) / root.renderScale,
+ (tableView.contentY - currentItem.y + tableView.jumpLocationMargin.y) / root.renderScale)
+ : Qt.point(0, 0) // maybe the delegate wasn't loaded yet
+ navigationStack.update(cell.y, currentLocation, root.renderScale)
}
}
ScrollBar.horizontal: ScrollBar { }
diff --git a/src/pdfquick/PdfPageView.qml b/src/pdfquick/PdfPageView.qml
index b70860aaf..08e743fb5 100644
--- a/src/pdfquick/PdfPageView.qml
+++ b/src/pdfquick/PdfPageView.qml
@@ -64,7 +64,7 @@ Rectangle {
function goToLocation(page, location, zoom) {
if (zoom > 0)
root.renderScale = zoom
- navigationStack.push(page, location, zoom)
+ navigationStack.jump(page, location, zoom)
}
// page scaling
@@ -227,7 +227,7 @@ Rectangle {
TapHandler {
onTapped: {
if (page >= 0)
- navigationStack.push(page, Qt.point(0, 0), root.renderScale)
+ navigationStack.jump(page, Qt.point(0, 0), root.renderScale)
else
Qt.openUrlExternally(url)
}
diff --git a/src/pdfquick/PdfScrollablePageView.qml b/src/pdfquick/PdfScrollablePageView.qml
index e6668e424..5111968e5 100644
--- a/src/pdfquick/PdfScrollablePageView.qml
+++ b/src/pdfquick/PdfScrollablePageView.qml
@@ -68,7 +68,7 @@ Flickable {
function goToLocation(page, location, zoom) {
if (zoom > 0)
root.renderScale = zoom
- navigationStack.push(page, location, zoom)
+ navigationStack.jump(page, location, zoom)
}
// page scaling
@@ -241,7 +241,7 @@ Flickable {
TapHandler {
onTapped: {
if (page >= 0)
- navigationStack.push(page, Qt.point(0, 0), root.renderScale)
+ navigationStack.jump(page, Qt.point(0, 0), root.renderScale)
else
Qt.openUrlExternally(url)
}
diff --git a/src/pdfquick/qquickpdfnavigationstack.cpp b/src/pdfquick/qquickpdfnavigationstack.cpp
index 3e0f18576..c36e993d4 100644
--- a/src/pdfquick/qquickpdfnavigationstack.cpp
+++ b/src/pdfquick/qquickpdfnavigationstack.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2020 The Qt Company Ltd.
+** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtPDF module of the Qt Toolkit.
@@ -59,7 +59,6 @@ Q_LOGGING_CATEGORY(qLcNav, "qt.pdf.navigationstack")
QQuickPdfNavigationStack::QQuickPdfNavigationStack(QObject *parent)
: QObject(parent)
{
- push(0, QPointF(), 1);
}
/*!
@@ -68,6 +67,14 @@ QQuickPdfNavigationStack::QQuickPdfNavigationStack(QObject *parent)
QQuickPdfNavigationStack::~QQuickPdfNavigationStack() = default;
/*!
+ \internal
+*/
+QPdfNavigationStack *QQuickPdfNavigationStack::navStack()
+{
+ return static_cast<QPdfNavigationStack *>(qmlExtendedObject(this));
+}
+
+/*!
\qmlmethod void PdfNavigationStack::forward()
Goes back to the page, location and zoom level that was being viewed before
@@ -77,30 +84,6 @@ QQuickPdfNavigationStack::~QQuickPdfNavigationStack() = default;
the forward() function does nothing, because there is a branch in the
timeline which causes the "future" to be lost.
*/
-void QQuickPdfNavigationStack::forward()
-{
- if (m_currentHistoryIndex >= m_pageHistory.count() - 1)
- return;
- bool backAvailableWas = backAvailable();
- bool forwardAvailableWas = forwardAvailable();
- QPointF currentLocationWas = currentLocation();
- qreal currentZoomWas = currentZoom();
- ++m_currentHistoryIndex;
- m_changing = true;
- emit jumped(currentPage(), currentLocation(), currentZoom());
- if (currentZoomWas != currentZoom())
- emit currentZoomChanged();
- emit currentPageChanged();
- if (currentLocationWas != currentLocation())
- emit currentLocationChanged();
- if (!backAvailableWas)
- emit backAvailableChanged();
- if (forwardAvailableWas != forwardAvailable())
- emit forwardAvailableChanged();
- m_changing = false;
- qCDebug(qLcNav) << "forward: index" << m_currentHistoryIndex << "page" << currentPage()
- << "@" << currentLocation() << "zoom" << currentZoom();
-}
/*!
\qmlmethod void PdfNavigationStack::back()
@@ -109,30 +92,6 @@ void QQuickPdfNavigationStack::forward()
\l currentZoom properties to the most-recently-viewed destination, and then
emits the \l jumped() signal.
*/
-void QQuickPdfNavigationStack::back()
-{
- if (m_currentHistoryIndex <= 0)
- return;
- bool backAvailableWas = backAvailable();
- bool forwardAvailableWas = forwardAvailable();
- QPointF currentLocationWas = currentLocation();
- qreal currentZoomWas = currentZoom();
- --m_currentHistoryIndex;
- m_changing = true;
- emit jumped(currentPage(), currentLocation(), currentZoom());
- if (currentZoomWas != currentZoom())
- emit currentZoomChanged();
- emit currentPageChanged();
- if (currentLocationWas != currentLocation())
- emit currentLocationChanged();
- if (backAvailableWas != backAvailable())
- emit backAvailableChanged();
- if (!forwardAvailableWas)
- emit forwardAvailableChanged();
- m_changing = false;
- qCDebug(qLcNav) << "back: index" << m_currentHistoryIndex << "page" << currentPage()
- << "@" << currentLocation() << "zoom" << currentZoom();
-}
/*!
\qmlproperty int PdfNavigationStack::currentPage
@@ -140,82 +99,30 @@ void QQuickPdfNavigationStack::back()
This property holds the current page that is being viewed.
If there is no current page, it holds \c -1.
*/
-int QQuickPdfNavigationStack::currentPage() const
-{
- if (m_currentHistoryIndex < 0 || m_currentHistoryIndex >= m_pageHistory.count())
- return -1;
- return m_pageHistory.at(m_currentHistoryIndex)->page;
-}
/*!
\qmlproperty point PdfNavigationStack::currentLocation
This property holds the current location on the page that is being viewed.
*/
-QPointF QQuickPdfNavigationStack::currentLocation() const
-{
- if (m_currentHistoryIndex < 0 || m_currentHistoryIndex >= m_pageHistory.count())
- return QPointF();
- return m_pageHistory.at(m_currentHistoryIndex)->location;
-}
/*!
\qmlproperty real PdfNavigationStack::currentZoom
This property holds the magnification scale on the page that is being viewed.
*/
-qreal QQuickPdfNavigationStack::currentZoom() const
-{
- if (m_currentHistoryIndex < 0 || m_currentHistoryIndex >= m_pageHistory.count())
- return 1;
- return m_pageHistory.at(m_currentHistoryIndex)->zoom;
-}
/*!
- \qmlmethod void PdfNavigationStack::push(int page, point location, qreal zoom, bool emitJumped)
+ \qmlmethod void PdfNavigationStack::jump(int page, point location, qreal zoom, bool emitJumped)
Adds the given destination, consisting of \a page, \a location, and \a zoom,
to the history of visited locations. If \a emitJumped is \c false, the
\l jumped() signal will not be emitted.
- If forwardAvailable() is \c true, calling this function represents a branch
+ If forwardAvailable is \c true, calling this function represents a branch
in the timeline which causes the "future" to be lost, and therefore
forwardAvailable will change to \c false.
*/
-void QQuickPdfNavigationStack::push(int page, QPointF location, qreal zoom, bool emitJumped)
-{
- if (page == currentPage() && location == currentLocation() && zoom == currentZoom())
- return;
- if (qFuzzyIsNull(zoom))
- zoom = currentZoom();
- bool backAvailableWas = backAvailable();
- bool forwardAvailableWas = forwardAvailable();
- if (!m_changing) {
- if (m_currentHistoryIndex >= 0 && forwardAvailableWas)
- m_pageHistory.remove(m_currentHistoryIndex + 1, m_pageHistory.count() - m_currentHistoryIndex - 1);
- m_pageHistory.append(QExplicitlySharedDataPointer<QPdfLinkPrivate>(new QPdfLinkPrivate(page, location, zoom)));
- m_currentHistoryIndex = m_pageHistory.count() - 1;
- }
- emit currentZoomChanged();
- emit currentPageChanged();
- emit currentLocationChanged();
- if (m_changing)
- return;
- if (!backAvailableWas)
- emit backAvailableChanged();
- if (forwardAvailableWas)
- emit forwardAvailableChanged();
- if (emitJumped)
- emit jumped(page, location, zoom);
- qCDebug(qLcNav) << "push: index" << m_currentHistoryIndex << "page" << page
- << "@" << location << "zoom" << zoom << "-> history" <<
- [this]() {
- QStringList ret;
- for (auto d : m_pageHistory)
- ret << QString::number(d->page);
- return ret.join(QLatin1Char(','));
- }();
-}
/*!
\qmlmethod void PdfNavigationStack::update(int page, point location, qreal zoom)
@@ -225,40 +132,13 @@ void QQuickPdfNavigationStack::push(int page, QPointF location, qreal zoom, bool
This can be called periodically while the user is manually moving around
the document, so that after back() is called, forward() will jump back to
the most-recently-viewed destination rather than the destination that was
- last specified by push().
+ last specified by jump().
The \c currentZoomChanged, \c currentPageChanged and \c currentLocationChanged
signals will be emitted if the respective properties are actually changed.
The \l jumped signal is not emitted, because this operation
represents smooth movement rather than a navigational jump.
*/
-void QQuickPdfNavigationStack::update(int page, QPointF location, qreal zoom)
-{
- if (m_currentHistoryIndex < 0 || m_currentHistoryIndex >= m_pageHistory.count())
- return;
- int currentPageWas = currentPage();
- QPointF currentLocationWas = currentLocation();
- qreal currentZoomWas = currentZoom();
- if (page == currentPageWas && location == currentLocationWas && zoom == currentZoomWas)
- return;
- m_pageHistory[m_currentHistoryIndex]->page = page;
- m_pageHistory[m_currentHistoryIndex]->location = location;
- m_pageHistory[m_currentHistoryIndex]->zoom = zoom;
- if (currentZoomWas != zoom)
- emit currentZoomChanged();
- if (currentPageWas != page)
- emit currentPageChanged();
- if (currentLocationWas != location)
- emit currentLocationChanged();
- qCDebug(qLcNav) << "update: index" << m_currentHistoryIndex << "page" << page
- << "@" << location << "zoom" << zoom << "-> history" <<
- [this]() {
- QStringList ret;
- for (auto d : m_pageHistory)
- ret << QString::number(d->page);
- return ret.join(QLatin1Char(','));
- }();
-}
/*!
\qmlproperty bool PdfNavigationStack::backAvailable
@@ -267,11 +147,6 @@ void QQuickPdfNavigationStack::update(int page, QPointF location, qreal zoom)
Holds \c true if a \e back destination is available in the history.
*/
-bool QQuickPdfNavigationStack::backAvailable() const
-{
- return m_currentHistoryIndex > 0;
-}
-
/*!
\qmlproperty bool PdfNavigationStack::forwardAvailable
\readonly
@@ -279,18 +154,13 @@ bool QQuickPdfNavigationStack::backAvailable() const
Holds \c true if a \e forward destination is available in the history.
*/
-bool QQuickPdfNavigationStack::forwardAvailable() const
-{
- return m_currentHistoryIndex < m_pageHistory.count() - 1;
-}
-
/*!
\qmlsignal PdfNavigationStack::jumped(int page, point location, qreal zoom)
- This signal is emitted for the given \a page, \a location, and \a zoom,
- It is emitted on calling forward(), back(), or push() only.
-
- \note The signal is emitted on calling update().
+ This signal is emitted when an abrupt jump occurs, to the specified \a page
+ index, \a location on the page, and \a zoom level; but \e not when simply
+ scrolling through the document one page at a time. That is, forward(),
+ back() and jump() always emit this signal; update() does not.
*/
QT_END_NAMESPACE
diff --git a/src/pdfquick/qquickpdfnavigationstack_p.h b/src/pdfquick/qquickpdfnavigationstack_p.h
index 2c8b591e9..8d5958259 100644
--- a/src/pdfquick/qquickpdfnavigationstack_p.h
+++ b/src/pdfquick/qquickpdfnavigationstack_p.h
@@ -52,7 +52,7 @@
//
#include <QtPdfQuick/private/qtpdfquickglobal_p.h>
-#include <QtPdfQuick/private/qquickpdfdocument_p.h>
+#include <QtPdf/qpdfnavigationstack.h>
#include <QtPdf/private/qpdflink_p.h>
#include <QQmlEngine>
@@ -62,11 +62,7 @@ QT_BEGIN_NAMESPACE
class Q_PDFQUICK_EXPORT QQuickPdfNavigationStack : public QObject
{
Q_OBJECT
- Q_PROPERTY(int currentPage READ currentPage NOTIFY currentPageChanged)
- Q_PROPERTY(QPointF currentLocation READ currentLocation NOTIFY currentLocationChanged)
- Q_PROPERTY(qreal currentZoom READ currentZoom NOTIFY currentZoomChanged)
- Q_PROPERTY(bool backAvailable READ backAvailable NOTIFY backAvailableChanged)
- Q_PROPERTY(bool forwardAvailable READ forwardAvailable NOTIFY forwardAvailableChanged)
+ QML_EXTENDED(QPdfNavigationStack)
QML_NAMED_ELEMENT(PdfNavigationStack)
QML_ADDED_IN_VERSION(5, 15)
@@ -74,30 +70,8 @@ public:
explicit QQuickPdfNavigationStack(QObject *parent = nullptr);
~QQuickPdfNavigationStack() override;
- Q_INVOKABLE void push(int page, QPointF location, qreal zoom, bool emitJumped = true);
- Q_INVOKABLE void update(int page, QPointF location, qreal zoom);
- Q_INVOKABLE void forward();
- Q_INVOKABLE void back();
-
- int currentPage() const;
- QPointF currentLocation() const;
- qreal currentZoom() const;
-
- bool backAvailable() const;
- bool forwardAvailable() const;
-
-Q_SIGNALS:
- void currentPageChanged();
- void currentLocationChanged();
- void currentZoomChanged();
- void backAvailableChanged();
- void forwardAvailableChanged();
- void jumped(int page, QPointF location, qreal zoom);
-
private:
- QList<QExplicitlySharedDataPointer<QPdfLinkPrivate>> m_pageHistory;
- int m_currentHistoryIndex = 0;
- bool m_changing = false;
+ QPdfNavigationStack *navStack();
Q_DISABLE_COPY(QQuickPdfNavigationStack)
};
diff --git a/src/pdfquick/qquickpdfpageimage.cpp b/src/pdfquick/qquickpdfpageimage.cpp
index 26d3bd9d6..024408601 100644
--- a/src/pdfquick/qquickpdfpageimage.cpp
+++ b/src/pdfquick/qquickpdfpageimage.cpp
@@ -40,7 +40,6 @@
#include "qquickpdfpageimage_p.h"
#include "qquickpdfdocument_p.h"
#include <private/qpdffile_p.h>
-#include <QtPdf/QPdfPageNavigation>
#include <QtQuick/private/qquickimage_p_p.h>
QT_BEGIN_NAMESPACE
diff --git a/src/pdfwidgets/qpdfview.cpp b/src/pdfwidgets/qpdfview.cpp
index 9b161809f..5680daabf 100644
--- a/src/pdfwidgets/qpdfview.cpp
+++ b/src/pdfwidgets/qpdfview.cpp
@@ -46,7 +46,7 @@
#include <QPainter>
#include <QPaintEvent>
#include <QPdfDocument>
-#include <QPdfPageNavigation>
+#include <QPdfNavigationStack>
#include <QScreen>
#include <QScrollBar>
#include <QScroller>
@@ -73,7 +73,7 @@ void QPdfViewPrivate::init()
{
Q_Q(QPdfView);
- m_pageNavigation = new QPdfPageNavigation(q);
+ m_pageNavigation = new QPdfNavigationStack(q);
m_pageRenderer = new QPdfPageRenderer(q);
m_pageRenderer->setRenderMode(QPdfPageRenderer::RenderMode::MultiThreaded);
}
@@ -129,7 +129,7 @@ void QPdfViewPrivate::setViewport(QRect viewport)
if (m_pageMode == QPdfView::MultiPage) {
// An imaginary, 2px height line at the upper half of the viewport, which is used to
// determine which page is currently located there -> we propagate that as 'current' page
- // to the QPdfPageNavigation object
+ // to the QPdfNavigationStack object
const QRect currentPageLine(m_viewport.x(), m_viewport.y() + m_viewport.height() * 0.4, m_viewport.width(), 2);
int currentPage = 0;
@@ -143,7 +143,8 @@ void QPdfViewPrivate::setViewport(QRect viewport)
if (currentPage != m_pageNavigation->currentPage()) {
m_blockPageScrolling = true;
- m_pageNavigation->setCurrentPage(currentPage);
+ // ΤODO give location on the page
+ m_pageNavigation->jump(currentPage, {}, m_zoomFactor);
m_blockPageScrolling = false;
}
}
@@ -289,7 +290,7 @@ QPdfView::QPdfView(QWidget *parent)
d->init();
- connect(d->m_pageNavigation, &QPdfPageNavigation::currentPageChanged, this, [d](int page){ d->currentPageChanged(page); });
+ connect(d->m_pageNavigation, &QPdfNavigationStack::currentPageChanged, this, [d](int page){ d->currentPageChanged(page); });
connect(d->m_pageRenderer, &QPdfPageRenderer::pageRendered,
this, [d](int pageNumber, QSize imageSize, const QImage &image, QPdfDocumentRenderOptions, quint64 requestId){ d->pageRendered(pageNumber, imageSize, image, requestId); });
@@ -322,7 +323,6 @@ void QPdfView::setDocument(QPdfDocument *document)
if (d->m_document)
d->m_documentStatusChangedConnection = connect(d->m_document.data(), &QPdfDocument::statusChanged, this, [d](){ d->documentStatusChanged(); });
- d->m_pageNavigation->setDocument(d->m_document);
d->m_pageRenderer->setDocument(d->m_document);
d->documentStatusChanged();
@@ -335,7 +335,7 @@ QPdfDocument *QPdfView::document() const
return d->m_document;
}
-QPdfPageNavigation *QPdfView::pageNavigation() const
+QPdfNavigationStack *QPdfView::pageNavigation() const
{
Q_D(const QPdfView);
diff --git a/src/pdfwidgets/qpdfview.h b/src/pdfwidgets/qpdfview.h
index 92c397408..3ee8a1f7f 100644
--- a/src/pdfwidgets/qpdfview.h
+++ b/src/pdfwidgets/qpdfview.h
@@ -46,7 +46,7 @@
QT_BEGIN_NAMESPACE
class QPdfDocument;
-class QPdfPageNavigation;
+class QPdfNavigationStack;
class QPdfViewPrivate;
class Q_PDF_WIDGETS_EXPORT QPdfView : public QAbstractScrollArea
@@ -85,7 +85,7 @@ public:
void setDocument(QPdfDocument *document);
QPdfDocument *document() const;
- QPdfPageNavigation *pageNavigation() const;
+ QPdfNavigationStack *pageNavigation() const;
PageMode pageMode() const;
ZoomMode zoomMode() const;
diff --git a/src/pdfwidgets/qpdfview_p.h b/src/pdfwidgets/qpdfview_p.h
index c7ea47d83..4e6a68581 100644
--- a/src/pdfwidgets/qpdfview_p.h
+++ b/src/pdfwidgets/qpdfview_p.h
@@ -91,7 +91,7 @@ public:
QPdfView *q_ptr;
QPointer<QPdfDocument> m_document;
- QPdfPageNavigation* m_pageNavigation;
+ QPdfNavigationStack* m_pageNavigation;
QPdfPageRenderer *m_pageRenderer;
QPdfView::PageMode m_pageMode;
diff --git a/tests/auto/pdf/CMakeLists.txt b/tests/auto/pdf/CMakeLists.txt
index 1220581ca..c35d8055b 100644
--- a/tests/auto/pdf/CMakeLists.txt
+++ b/tests/auto/pdf/CMakeLists.txt
@@ -1,5 +1,5 @@
add_subdirectory(qpdfbookmarkmodel)
-add_subdirectory(qpdfpagenavigation)
+#add_subdirectory(qpdfpagenavigation)
add_subdirectory(qpdfpagerenderer)
if(TARGET Qt::PrintSupport)
add_subdirectory(qpdfdocument)
diff --git a/tests/auto/pdf/qpdfpagenavigation/CMakeLists.txt b/tests/auto/pdf/qpdfpagenavigation/CMakeLists.txt
deleted file mode 100644
index 12ece7507..000000000
--- a/tests/auto/pdf/qpdfpagenavigation/CMakeLists.txt
+++ /dev/null
@@ -1,8 +0,0 @@
-qt_internal_add_test(tst_qpdfpagenavigation
- SOURCES
- tst_qpdfpagenavigation.cpp
- PUBLIC_LIBRARIES
- Qt::Gui
- Qt::Network
- Qt::Pdf
-)
diff --git a/tests/auto/pdf/qpdfpagenavigation/pdf-sample.pagenavigation.pdf b/tests/auto/pdf/qpdfpagenavigation/pdf-sample.pagenavigation.pdf
deleted file mode 100644
index c4e1aa36e..000000000
--- a/tests/auto/pdf/qpdfpagenavigation/pdf-sample.pagenavigation.pdf
+++ /dev/null
Binary files differ
diff --git a/tests/auto/pdf/qpdfpagenavigation/tst_qpdfpagenavigation.cpp b/tests/auto/pdf/qpdfpagenavigation/tst_qpdfpagenavigation.cpp
deleted file mode 100644
index c4d481ca5..000000000
--- a/tests/auto/pdf/qpdfpagenavigation/tst_qpdfpagenavigation.cpp
+++ /dev/null
@@ -1,203 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2017 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-
-#include <QtTest/QtTest>
-
-#include <QPdfDocument>
-#include <QPdfPageNavigation>
-
-class tst_QPdfPageNavigation: public QObject
-{
- Q_OBJECT
-
-private slots:
- void defaultValues();
- void setEmptyDocument();
- void setEmptyDocumentAndLoad();
- void setLoadedDocument();
- void unloadDocument();
- void navigate();
-};
-
-void tst_QPdfPageNavigation::defaultValues()
-{
- QPdfPageNavigation pageNavigation;
-
- QCOMPARE(pageNavigation.document(), nullptr);
- QCOMPARE(pageNavigation.currentPage(), 0);
- QCOMPARE(pageNavigation.pageCount(), 0);
- QCOMPARE(pageNavigation.canGoToPreviousPage(), false);
- QCOMPARE(pageNavigation.canGoToNextPage(), false);
-}
-
-void tst_QPdfPageNavigation::setEmptyDocument()
-{
- QPdfDocument document;
- QPdfPageNavigation pageNavigation;
-
- pageNavigation.setDocument(&document);
-
- QCOMPARE(pageNavigation.document(), &document);
- QCOMPARE(pageNavigation.currentPage(), 0);
- QCOMPARE(pageNavigation.pageCount(), 0);
- QCOMPARE(pageNavigation.canGoToPreviousPage(), false);
- QCOMPARE(pageNavigation.canGoToNextPage(), false);
-}
-
-void tst_QPdfPageNavigation::setEmptyDocumentAndLoad()
-{
- QPdfDocument document;
- QPdfPageNavigation pageNavigation;
-
- pageNavigation.setDocument(&document);
-
- QSignalSpy currentPageChangedSpy(&pageNavigation, &QPdfPageNavigation::currentPageChanged);
- QSignalSpy pageCountChangedSpy(&pageNavigation, &QPdfPageNavigation::pageCountChanged);
- QSignalSpy canGoToPreviousPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged);
- QSignalSpy canGoToNextPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged);
-
- QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagenavigation.pdf")), QPdfDocument::NoError);
-
- QCOMPARE(currentPageChangedSpy.count(), 0); // current page stays '0'
- QCOMPARE(pageCountChangedSpy.count(), 1);
- QCOMPARE(pageCountChangedSpy[0][0].toInt(), 3);
- QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0); // still no previous page available
- QCOMPARE(canGoToNextPageChangedSpy.count(), 1);
- QCOMPARE(canGoToNextPageChangedSpy[0][0].toBool(), true);
-}
-
-void tst_QPdfPageNavigation::setLoadedDocument()
-{
- QPdfDocument document;
- QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagenavigation.pdf")), QPdfDocument::NoError);
-
- QPdfPageNavigation pageNavigation;
-
- QSignalSpy currentPageChangedSpy(&pageNavigation, &QPdfPageNavigation::currentPageChanged);
- QSignalSpy pageCountChangedSpy(&pageNavigation, &QPdfPageNavigation::pageCountChanged);
- QSignalSpy canGoToPreviousPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged);
- QSignalSpy canGoToNextPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged);
-
- pageNavigation.setDocument(&document);
-
- QCOMPARE(currentPageChangedSpy.count(), 0); // current page stays '0'
- QCOMPARE(pageCountChangedSpy.count(), 1);
- QCOMPARE(pageCountChangedSpy[0][0].toInt(), 3);
- QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0); // still no previous page available
- QCOMPARE(canGoToNextPageChangedSpy.count(), 1);
- QCOMPARE(canGoToNextPageChangedSpy[0][0].toBool(), true);
-}
-
-void tst_QPdfPageNavigation::unloadDocument()
-{
- QPdfDocument document;
- QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagenavigation.pdf")), QPdfDocument::NoError);
-
- QPdfPageNavigation pageNavigation;
- pageNavigation.setDocument(&document);
-
- QSignalSpy currentPageChangedSpy(&pageNavigation, &QPdfPageNavigation::currentPageChanged);
- QSignalSpy pageCountChangedSpy(&pageNavigation, &QPdfPageNavigation::pageCountChanged);
- QSignalSpy canGoToPreviousPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged);
- QSignalSpy canGoToNextPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged);
-
- document.close();
-
- QCOMPARE(currentPageChangedSpy.count(), 0); // current page stays '0'
- QCOMPARE(pageCountChangedSpy.count(), 1);
- QCOMPARE(pageCountChangedSpy[0][0].toInt(), 0);
- QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0); // still no previous page available
- QCOMPARE(canGoToNextPageChangedSpy.count(), 1);
- QCOMPARE(canGoToNextPageChangedSpy[0][0].toBool(), false);
-}
-
-void tst_QPdfPageNavigation::navigate()
-{
- QPdfDocument document;
- QCOMPARE(document.load(QFINDTESTDATA("pdf-sample.pagenavigation.pdf")), QPdfDocument::NoError);
-
- QPdfPageNavigation pageNavigation;
- pageNavigation.setDocument(&document);
-
- QSignalSpy currentPageChangedSpy(&pageNavigation, &QPdfPageNavigation::currentPageChanged);
- QSignalSpy canGoToPreviousPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToPreviousPageChanged);
- QSignalSpy canGoToNextPageChangedSpy(&pageNavigation, &QPdfPageNavigation::canGoToNextPageChanged);
-
- QCOMPARE(pageNavigation.currentPage(), 0);
-
- // try to go to previous page while there is none
- QCOMPARE(pageNavigation.canGoToPreviousPage(), false);
- pageNavigation.goToPreviousPage();
- QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0);
- QCOMPARE(pageNavigation.currentPage(), 0);
- QCOMPARE(pageNavigation.canGoToPreviousPage(), false);
-
- // try to go to next page
- QCOMPARE(pageNavigation.canGoToNextPage(), true);
- pageNavigation.goToNextPage();
- QCOMPARE(canGoToPreviousPageChangedSpy.count(), 1);
- QCOMPARE(canGoToNextPageChangedSpy.count(), 0);
- QCOMPARE(currentPageChangedSpy.count(), 1);
- QCOMPARE(pageNavigation.currentPage(), 1);
- QCOMPARE(pageNavigation.canGoToPreviousPage(), true);
-
- currentPageChangedSpy.clear();
- canGoToPreviousPageChangedSpy.clear();
- canGoToNextPageChangedSpy.clear();
-
- // try to go to last page
- pageNavigation.setCurrentPage(2);
- QCOMPARE(canGoToPreviousPageChangedSpy.count(), 0);
- QCOMPARE(canGoToNextPageChangedSpy.count(), 1);
- QCOMPARE(currentPageChangedSpy.count(), 1);
- QCOMPARE(pageNavigation.currentPage(), 2);
- QCOMPARE(pageNavigation.canGoToNextPage(), false);
-
- // check that invalid requests are ignored
- pageNavigation.setCurrentPage(-1);
- QCOMPARE(pageNavigation.currentPage(), 2);
-
- pageNavigation.setCurrentPage(3);
- QCOMPARE(pageNavigation.currentPage(), 2);
-}
-
-QTEST_MAIN(tst_QPdfPageNavigation)
-
-#include "tst_qpdfpagenavigation.moc"