summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2022-03-28 12:31:13 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2022-04-08 14:56:56 +0200
commit3ad445f9f24a9d3f259ed1781460a63346a728e4 (patch)
treedd20d47c7dcdb10d3e66a5b8e6728a38e2c404e6 /examples
parenta4e32eac5cb858ffa5668b01cef10cc42854713b (diff)
Move QQuickPdfNavStack core into QPdfNavigationStack; use in QPdfView
QQuickPdfNavigationStack was implemented independently until now, but users will need the same functionality in widget-based PDF viewers. QPdfPageNavigation on the other hand may have eventually had the same aspiration, but was just a glorified up/down counter thus far, with questionable API; so we get rid of it, and make the API for page navigation as much the same as possible between Quick and Widgets. We rename push() to jump() along with removing the emitJumped argument. Now jump() always emits (so we have to be more careful when to call it). Change-Id: Icb07158a351e29b81e58ec037cd323bc0f54a1a1 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'examples')
-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
7 files changed, 71 insertions, 227 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 += \