diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2022-05-30 17:36:55 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2023-02-16 06:57:23 +0100 |
commit | b8419577eb99c1589768d55ddfdc136818df87ae (patch) | |
tree | 64c90b32d6d6aea84f01c2300f90e38b42bc5b3c /examples | |
parent | b385e76b1fc60ce1f48d2b805a4a2d2a2b23d1bd (diff) |
Add QPdfPageSelector
This is a QSpinBox subclass that shows PDF page "labels" rather than a
1-based or 0-based page index. If a book starts with Roman numerals in
the preface, and then page 1 is the first page of Chapter 1, the spinbox
should be in sync with the page numbers as printed on the pages, and
with the labels that will eventually be shown under the thumbnails
on the Pages sidebar tab.
On the other hand, the user probably needs to see the 1-based page index
somewhere, at least to be able to make sense of the print dialog,
because there the range of pages to print will be 1-based. So we put
the page index into the title bar: title, page label, index, count.
Task-number: QTBUG-102271
Change-Id: Ic461094ba4caae3067409f7f436bd4e7504a4bdb
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/pdfwidgets/pdfviewer/mainwindow.cpp | 17 | ||||
-rw-r--r-- | examples/pdfwidgets/pdfviewer/mainwindow.h | 3 |
2 files changed, 12 insertions, 8 deletions
diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.cpp b/examples/pdfwidgets/pdfviewer/mainwindow.cpp index 02caffa48..d7527a8d5 100644 --- a/examples/pdfwidgets/pdfviewer/mainwindow.cpp +++ b/examples/pdfwidgets/pdfviewer/mainwindow.cpp @@ -8,10 +8,10 @@ #include <QFileDialog> #include <QMessageBox> -#include <QSpinBox> #include <QPdfBookmarkModel> #include <QPdfDocument> #include <QPdfPageNavigator> +#include <QPdfPageSelector> #include <QStandardPaths> #include <QtMath> @@ -23,7 +23,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_zoomSelector(new ZoomSelector(this)) - , m_pageSelector(new QSpinBox(this)) + , m_pageSelector(new QPdfPageSelector(this)) , m_document(new QPdfDocument(this)) { ui->setupUi(this); @@ -32,9 +32,10 @@ MainWindow::MainWindow(QWidget *parent) ui->mainToolBar->insertWidget(ui->actionZoom_In, m_zoomSelector); ui->mainToolBar->insertWidget(ui->actionForward, m_pageSelector); - connect(m_pageSelector, &QSpinBox::valueChanged, this, &MainWindow::pageSelected); + connect(m_pageSelector, &QPdfPageSelector::valueChanged, this, &MainWindow::pageSelected); + m_pageSelector->setDocument(m_document); auto nav = ui->pdfView->pageNavigator(); - connect(nav, &QPdfPageNavigator::currentPageChanged, m_pageSelector, &QSpinBox::setValue); + connect(nav, &QPdfPageNavigator::currentPageChanged, m_pageSelector, &QPdfPageSelector::setValue); connect(nav, &QPdfPageNavigator::backAvailableChanged, ui->actionBack, &QAction::setEnabled); connect(nav, &QPdfPageNavigator::forwardAvailableChanged, ui->actionForward, &QAction::setEnabled); @@ -65,10 +66,7 @@ void MainWindow::open(const QUrl &docLocation) { if (docLocation.isLocalFile()) { m_document->load(docLocation.toLocalFile()); - const auto documentTitle = m_document->metaData(QPdfDocument::MetaDataField::Title).toString(); - setWindowTitle(!documentTitle.isEmpty() ? documentTitle : QStringLiteral("PDF Viewer")); pageSelected(0); - m_pageSelector->setMaximum(m_document->pageCount() - 1); } else { const QString message = tr("%1 is not a valid local file").arg(docLocation.toString()); qCDebug(lcExample).noquote() << message; @@ -91,6 +89,11 @@ void MainWindow::pageSelected(int page) { auto nav = ui->pdfView->pageNavigator(); nav->jump(page, {}, nav->currentZoom()); + const auto documentTitle = m_document->metaData(QPdfDocument::MetaDataField::Title).toString(); + setWindowTitle(!documentTitle.isEmpty() ? documentTitle : QStringLiteral("PDF Viewer")); + setWindowTitle(tr("%1: page %2 (%3 of %4)") + .arg(documentTitle.isEmpty() ? u"PDF Viewer"_qs : documentTitle, + m_pageSelector->text(), QString::number(page + 1), QString::number(m_document->pageCount()))); } void MainWindow::on_actionOpen_triggered() diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.h b/examples/pdfwidgets/pdfviewer/mainwindow.h index 958e11061..2827fb2b6 100644 --- a/examples/pdfwidgets/pdfviewer/mainwindow.h +++ b/examples/pdfwidgets/pdfviewer/mainwindow.h @@ -16,6 +16,7 @@ class MainWindow; class QFileDialog; class QPdfDocument; +class QPdfPageSelector; class QPdfView; class QSpinBox; QT_END_NAMESPACE @@ -53,7 +54,7 @@ private slots: private: Ui::MainWindow *ui; ZoomSelector *m_zoomSelector; - QSpinBox *m_pageSelector; + QPdfPageSelector *m_pageSelector; QFileDialog *m_fileDialog = nullptr; QPdfDocument *m_document; |