From b2b219dcd218260c6a304f1cdf016f4dde250dd2 Mon Sep 17 00:00:00 2001 From: Tobias Koenig Date: Fri, 24 Feb 2017 10:59:12 +0100 Subject: Port pdfviewer example to QPdfView widget Change-Id: Id651a2c179628506f0955751abda57cbae569ee9 Reviewed-by: Simon Hausmann --- examples/pdfwidgets/pdfviewer/mainwindow.cpp | 88 ++++------ examples/pdfwidgets/pdfviewer/mainwindow.h | 15 +- examples/pdfwidgets/pdfviewer/mainwindow.ui | 42 ++--- examples/pdfwidgets/pdfviewer/pagerenderer.cpp | 105 ----------- examples/pdfwidgets/pdfviewer/pagerenderer.h | 82 --------- examples/pdfwidgets/pdfviewer/pageselector.cpp | 111 ++++++++++++ examples/pdfwidgets/pdfviewer/pageselector.h | 72 ++++++++ examples/pdfwidgets/pdfviewer/pdfviewer.pro | 24 +-- .../pdfwidgets/pdfviewer/sequentialpagewidget.cpp | 192 --------------------- .../pdfwidgets/pdfviewer/sequentialpagewidget.h | 99 ----------- examples/pdfwidgets/pdfviewer/zoomselector.cpp | 97 +++++++++++ examples/pdfwidgets/pdfviewer/zoomselector.h | 63 +++++++ 12 files changed, 411 insertions(+), 579 deletions(-) delete mode 100644 examples/pdfwidgets/pdfviewer/pagerenderer.cpp delete mode 100644 examples/pdfwidgets/pdfviewer/pagerenderer.h create mode 100644 examples/pdfwidgets/pdfviewer/pageselector.cpp create mode 100644 examples/pdfwidgets/pdfviewer/pageselector.h delete mode 100644 examples/pdfwidgets/pdfviewer/sequentialpagewidget.cpp delete mode 100644 examples/pdfwidgets/pdfviewer/sequentialpagewidget.h create mode 100644 examples/pdfwidgets/pdfviewer/zoomselector.cpp create mode 100644 examples/pdfwidgets/pdfviewer/zoomselector.h (limited to 'examples') diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.cpp b/examples/pdfwidgets/pdfviewer/mainwindow.cpp index fd2656305..5f9bf389f 100644 --- a/examples/pdfwidgets/pdfviewer/mainwindow.cpp +++ b/examples/pdfwidgets/pdfviewer/mainwindow.cpp @@ -37,14 +37,14 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include "sequentialpagewidget.h" +#include "pageselector.h" +#include "zoomselector.h" #include -#include #include #include #include -#include +#include #include const qreal zoomMultiplier = qSqrt(2.0); @@ -54,27 +54,23 @@ Q_LOGGING_CATEGORY(lcExample, "qt.examples.pdfviewer") MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) - , m_pageWidget(new SequentialPageWidget(this)) - , m_zoomEdit(new QLineEdit(this)) - , m_pageEdit(new QLineEdit(this)) + , m_zoomSelector(new ZoomSelector(this)) + , m_pageSelector(new PageSelector(this)) , m_document(new QPdfDocument(this)) { ui->setupUi(this); - ui->scrollArea->setWidget(m_pageWidget); - m_zoomEdit->setMaximumWidth(50); - m_zoomEdit->setAlignment(Qt::AlignHCenter); - ui->mainToolBar->insertWidget(ui->actionZoom_In, m_zoomEdit); - m_pageEdit->setMaximumWidth(50); - m_pageEdit->setAlignment(Qt::AlignHCenter); - ui->mainToolBar->insertWidget(ui->actionGo, m_pageEdit); - connect(m_pageWidget, SIGNAL(showingPageRange(int,int)), - this, SLOT(showingPageRange(int,int)), Qt::QueuedConnection); - connect(m_pageWidget, SIGNAL(zoomChanged(qreal)), - this, SLOT(zoomChanged(qreal))); - connect(m_zoomEdit, SIGNAL(returnPressed()), this, SLOT(zoomEdited())); - connect(m_pageEdit, SIGNAL(returnPressed()), this, SLOT(on_actionGo_triggered())); - - QScroller::grabGesture(ui->scrollArea); + + 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()); + + connect(m_zoomSelector, &ZoomSelector::zoomModeChanged, ui->pdfView, &QPdfView::setZoomMode); + connect(m_zoomSelector, &ZoomSelector::zoomFactorChanged, ui->pdfView, &QPdfView::setZoomFactor); + m_zoomSelector->reset(); QPdfBookmarkModel *bookmarkModel = new QPdfBookmarkModel(this); bookmarkModel->setDocument(m_document); @@ -84,7 +80,10 @@ MainWindow::MainWindow(QWidget *parent) ui->tabWidget->setTabEnabled(1, false); // disable 'Pages' tab for now - m_pageWidget->setDocument(m_document); + ui->pdfView->setDocument(m_document); + + connect(ui->pdfView, &QPdfView::zoomFactorChanged, + m_zoomSelector, &ZoomSelector::setZoomFactor); } MainWindow::~MainWindow() @@ -103,30 +102,6 @@ void MainWindow::open(const QUrl &docLocation) QMessageBox::critical(this, tr("Failed to open"), tr("%1 is not a valid local file").arg(docLocation.toString())); } qCDebug(lcExample) << docLocation; - ui->scrollArea->ensureVisible(0, 0, 0, 0); -} - -void MainWindow::showingPageRange(int start, int end) -{ - ui->statusBar->clearMessage(); - if (start == end) - ui->statusBar->showMessage(tr("showing page %1").arg(start)); - else - ui->statusBar->showMessage(tr("showing pages %1 to %2").arg(start).arg(end)); - m_pageEdit->setText(QString::number(end)); -} - -void MainWindow::zoomChanged(qreal factor) -{ - m_zoomEdit->setText(tr("%1%").arg(factor * 100., 0, 'f', 0)); -} - -void MainWindow::zoomEdited() -{ - bool ok = false; - qreal factor = m_zoomEdit->text().remove(QChar('%')).toDouble(&ok); - if (ok) - m_pageWidget->setZoom(factor / 100.); } void MainWindow::bookmarkSelected(const QModelIndex &index) @@ -135,7 +110,7 @@ void MainWindow::bookmarkSelected(const QModelIndex &index) return; const int page = index.data(QPdfBookmarkModel::PageNumberRole).toInt(); - ui->scrollArea->ensureVisible(0, m_pageWidget->yForPage(page)); + ui->pdfView->pageNavigation()->setCurrentPage(page); } void MainWindow::on_actionOpen_triggered() @@ -163,28 +138,25 @@ void MainWindow::on_actionAbout_Qt_triggered() void MainWindow::on_actionZoom_In_triggered() { - m_pageWidget->setZoom(m_pageWidget->zoom() * zoomMultiplier); + ui->pdfView->setZoomFactor(ui->pdfView->zoomFactor() * zoomMultiplier); } void MainWindow::on_actionZoom_Out_triggered() { - m_pageWidget->setZoom(m_pageWidget->zoom() / zoomMultiplier); + ui->pdfView->setZoomFactor(ui->pdfView->zoomFactor() / zoomMultiplier); } -void MainWindow::on_actionGo_triggered() +void MainWindow::on_actionPrevious_Page_triggered() { - bool ok = false; - int page = m_pageEdit->text().toInt(&ok); - if (!ok) return; - ui->scrollArea->ensureVisible(0, m_pageWidget->yForPage(page)); + ui->pdfView->pageNavigation()->goToPreviousPage(); } -void MainWindow::on_actionPrevious_Page_triggered() +void MainWindow::on_actionNext_Page_triggered() { - ui->scrollArea->ensureVisible(0, m_pageWidget->yForPage(m_pageWidget->topPageShowing() - 1)); + ui->pdfView->pageNavigation()->goToNextPage(); } -void MainWindow::on_actionNext_Page_triggered() +void MainWindow::on_actionContinuous_triggered() { - ui->scrollArea->ensureVisible(0, m_pageWidget->yForPage(m_pageWidget->bottomPageShowing() + 1)); + ui->pdfView->setPageMode(ui->actionContinuous->isChecked() ? QPdfView::MultiPage : QPdfView::SinglePage); } diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.h b/examples/pdfwidgets/pdfviewer/mainwindow.h index f29205840..afdfcade4 100644 --- a/examples/pdfwidgets/pdfviewer/mainwindow.h +++ b/examples/pdfwidgets/pdfviewer/mainwindow.h @@ -47,11 +47,12 @@ namespace Ui { class MainWindow; } -class QLineEdit; class QPdfDocument; +class QPdfView; QT_END_NAMESPACE -class SequentialPageWidget; +class PageSelector; +class ZoomSelector; class MainWindow : public QMainWindow { @@ -65,9 +66,6 @@ public slots: void open(const QUrl &docLocation); private slots: - void showingPageRange(int start, int end); - void zoomChanged(qreal factor); - void zoomEdited(); void bookmarkSelected(const QModelIndex &index); // action handlers @@ -77,15 +75,14 @@ private slots: void on_actionAbout_Qt_triggered(); void on_actionZoom_In_triggered(); void on_actionZoom_Out_triggered(); - void on_actionGo_triggered(); void on_actionPrevious_Page_triggered(); void on_actionNext_Page_triggered(); + void on_actionContinuous_triggered(); private: Ui::MainWindow *ui; - SequentialPageWidget *m_pageWidget; - QLineEdit *m_zoomEdit; - QLineEdit *m_pageEdit; + ZoomSelector *m_zoomSelector; + PageSelector *m_pageSelector; QPdfDocument *m_document; }; diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.ui b/examples/pdfwidgets/pdfviewer/mainwindow.ui index 738cef48d..2651525a5 100644 --- a/examples/pdfwidgets/pdfviewer/mainwindow.ui +++ b/examples/pdfwidgets/pdfviewer/mainwindow.ui @@ -113,26 +113,13 @@ - + 10 0 - - true - - - - - 0 - 0 - 401 - 515 - - - @@ -172,6 +159,8 @@ + + @@ -195,7 +184,6 @@ - @@ -252,14 +240,6 @@ Ctrl+- - - - Go - - - Go to Page - - Previous Page @@ -276,8 +256,24 @@ PgDown + + + true + + + Continuous + + + + + QPdfView + QWidget +
qpdfview.h
+ 1 +
+
diff --git a/examples/pdfwidgets/pdfviewer/pagerenderer.cpp b/examples/pdfwidgets/pdfviewer/pagerenderer.cpp deleted file mode 100644 index 8cb1ed69c..000000000 --- a/examples/pdfwidgets/pdfviewer/pagerenderer.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtPDF module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** 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 http://www.qt.io/terms-conditions. For further -** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "pagerenderer.h" - -#include -#include -#include -#include -#include - -Q_DECLARE_LOGGING_CATEGORY(lcExample) - -PageRenderer::PageRenderer(QObject *parent) - : QThread(parent) - , m_document(nullptr) - , m_page(0) - , m_zoom(1.) - , m_minRenderTime(1000000000.) - , m_maxRenderTime(0.) - , m_totalRenderTime(0.) - , m_totalPagesRendered(0) -{ -} - -void PageRenderer::setDocument(QPdfDocument *document) -{ - m_document = document; -} - -void PageRenderer::requestPage(int page, qreal zoom, Priority priority) -{ - // TODO maybe queue up the requests - m_page = page; - m_zoom = zoom; - start(priority); -} - -void PageRenderer::run() -{ - renderPage(m_page, m_zoom); -} - -void PageRenderer::renderPage(int page, qreal zoom) -{ - if (!m_document || m_document->status() != QPdfDocument::Ready) - return; - - const QSizeF size = m_document->pageSize(page) * m_zoom; - - QElapsedTimer timer; - timer.start(); - - const QImage &img = m_document->render(page, size.toSize()); - - const qreal secs = timer.nsecsElapsed() / 1000000000.0; - if (secs < m_minRenderTime) - m_minRenderTime = secs; - - if (secs > m_maxRenderTime) - m_maxRenderTime = secs; - - m_totalRenderTime += secs; - ++m_totalPagesRendered; - - emit pageReady(page, zoom, img); - - qCDebug(lcExample) << "page" << page << "zoom" << m_zoom << "size" << size << "in" << secs << - "secs; min" << m_minRenderTime << - "avg" << m_totalRenderTime / m_totalPagesRendered << - "max" << m_maxRenderTime; -} diff --git a/examples/pdfwidgets/pdfviewer/pagerenderer.h b/examples/pdfwidgets/pdfviewer/pagerenderer.h deleted file mode 100644 index f1e491889..000000000 --- a/examples/pdfwidgets/pdfviewer/pagerenderer.h +++ /dev/null @@ -1,82 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtPDF module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** 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 http://www.qt.io/terms-conditions. For further -** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef PAGECACHE_H -#define PAGECACHE_H - -#include -#include - -QT_BEGIN_NAMESPACE -class QPdfDocument; -QT_END_NAMESPACE - -class PageRenderer : public QThread -{ - Q_OBJECT - -public: - explicit PageRenderer(QObject *parent = nullptr); - -public slots: - void setDocument(QPdfDocument *document); - - void requestPage(int page, qreal zoom, Priority priority = QThread::NormalPriority); - -signals: - void pageReady(int page, qreal zoom, QImage image); - -protected: - void run() override; - -private: - void renderPage(int page, qreal zoom); - -private: - QPdfDocument *m_document; - - // current request only - int m_page; - qreal m_zoom; - - // performance statistics - qreal m_minRenderTime; - qreal m_maxRenderTime; - qreal m_totalRenderTime; - int m_totalPagesRendered; -}; - -#endif // PAGECACHE_H diff --git a/examples/pdfwidgets/pdfviewer/pageselector.cpp b/examples/pdfwidgets/pdfviewer/pageselector.cpp new file mode 100644 index 000000000..cd3c4ba0e --- /dev/null +++ b/examples/pdfwidgets/pdfviewer/pageselector.cpp @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtPDF module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "pageselector.h" + +#include +#include +#include +#include +#include + +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 new file mode 100644 index 000000000..7a7283f99 --- /dev/null +++ b/examples/pdfwidgets/pdfviewer/pageselector.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtPDF module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PAGESELECTOR_H +#define PAGESELECTOR_H + +#include + +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 10cacce6e..d3e768892 100644 --- a/examples/pdfwidgets/pdfviewer/pdfviewer.pro +++ b/examples/pdfwidgets/pdfviewer/pdfviewer.pro @@ -1,21 +1,23 @@ -QT += core gui widgets pdf -TARGET = pdfviewer TEMPLATE = app +TARGET = pdfviewer +QT += core gui widgets pdfwidgets -SOURCES += main.cpp\ - mainwindow.cpp \ - sequentialpagewidget.cpp \ - pagerenderer.cpp +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + pageselector.cpp \ + zoomselector.cpp -HEADERS += mainwindow.h \ - sequentialpagewidget.h \ - pagerenderer.h +HEADERS += \ + mainwindow.h \ + pageselector.h \ + zoomselector.h -FORMS += mainwindow.ui +FORMS += \ + mainwindow.ui RESOURCES += \ resources.qrc target.path = $$[QT_INSTALL_EXAMPLES]/pdf/pdfviewer INSTALLS += target - diff --git a/examples/pdfwidgets/pdfviewer/sequentialpagewidget.cpp b/examples/pdfwidgets/pdfviewer/sequentialpagewidget.cpp deleted file mode 100644 index 904bc17d2..000000000 --- a/examples/pdfwidgets/pdfviewer/sequentialpagewidget.cpp +++ /dev/null @@ -1,192 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtPDF module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** 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 http://www.qt.io/terms-conditions. For further -** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "sequentialpagewidget.h" -#include "pagerenderer.h" -#include -#include -#include -#include -#include -#include -#include - -Q_DECLARE_LOGGING_CATEGORY(lcExample) - -SequentialPageWidget::SequentialPageWidget(QWidget *parent) - : QWidget(parent) - , m_pageCacheLimit(20) - , m_pageRenderer(new PageRenderer()) - , m_background(Qt::darkGray) - , m_placeholderIcon(":icons/images/busy.png") - , m_placeholderBackground(Qt::white) - , m_pageSpacing(3) - , m_topPageShowing(0) - , m_zoom(1.) - , m_screenResolution(QGuiApplication::primaryScreen()->logicalDotsPerInch() / 72.0) - , m_document(nullptr) -{ - connect(m_pageRenderer, SIGNAL(pageReady(int, qreal, QImage)), this, SLOT(pageLoaded(int, qreal, QImage)), Qt::QueuedConnection); - grabGesture(Qt::SwipeGesture); -} - -SequentialPageWidget::~SequentialPageWidget() -{ - delete m_pageRenderer; -} - -void SequentialPageWidget::setDocument(QPdfDocument *document) -{ - m_pageRenderer->setDocument(document); - - m_document = document; - connect(m_document, &QPdfDocument::statusChanged, this, &SequentialPageWidget::documentStatusChanged); - - documentStatusChanged(); -} - -void SequentialPageWidget::setZoom(qreal factor) -{ - m_zoom = factor; - emit zoomChanged(factor); - invalidate(); -} - -QSizeF SequentialPageWidget::pageSize(int page) -{ -// if (!m_pageSizes.length() <= page) -// return QSizeF(); - return m_pageSizes[page] * m_screenResolution * m_zoom; -} - -void SequentialPageWidget::invalidate() -{ - QSizeF totalSize(0, m_pageSpacing); - for (int page = 0; page < pageCount(); ++page) { - QSizeF size = pageSize(page); - totalSize.setHeight(totalSize.height() + size.height()); - if (size.width() > totalSize.width()) - totalSize.setWidth(size.width()); - } - m_totalSize = totalSize.toSize(); - setMinimumSize(m_totalSize); - emit zoomChanged(m_zoom); - qCDebug(lcExample) << "total size" << m_totalSize; - m_pageCache.clear(); - update(); -} - -void SequentialPageWidget::documentStatusChanged() -{ - m_pageSizes.clear(); - m_topPageShowing = 0; - - if (m_document->status() == QPdfDocument::Ready) { - for (int page = 0; page < m_document->pageCount(); ++page) - m_pageSizes.append(m_document->pageSize(page)); - } - - invalidate(); -} - -void SequentialPageWidget::pageLoaded(int page, qreal zoom, QImage image) -{ - Q_UNUSED(zoom) - if (m_cachedPagesLRU.length() > m_pageCacheLimit) - m_pageCache.remove(m_cachedPagesLRU.takeFirst()); - m_pageCache.insert(page, image); - m_cachedPagesLRU.append(page); - update(); -} - -int SequentialPageWidget::pageCount() -{ - return m_pageSizes.count(); -} - -void SequentialPageWidget::paintEvent(QPaintEvent * event) -{ - QPainter painter(this); - painter.fillRect(event->rect(), m_background); - - if (m_pageSizes.isEmpty()) - return; - - // Find the first page that needs to be rendered - int page = 0; - int y = 0; - while (page < pageCount()) { - QSizeF size = pageSize(page); - int height = size.toSize().height(); - if (y + height >= event->rect().top()) - break; - y += height + m_pageSpacing; - ++page; - } - y += m_pageSpacing; - m_topPageShowing = page; - - // Actually render pages - while (y < event->rect().bottom() && page < pageCount()) { - QSizeF size = pageSize(page); - if (m_pageCache.contains(page)) { - const QImage &img = m_pageCache[page]; - painter.fillRect((width() - img.width()) / 2, y, size.width(), size.height(), Qt::white); - painter.drawImage((width() - img.width()) / 2, y, img); - } else { - painter.fillRect((width() - size.width()) / 2, y, size.width(), size.height(), m_placeholderBackground); - painter.drawPixmap((size.width() - m_placeholderIcon.width()) / 2, - (size.height() - m_placeholderIcon.height()) / 2, m_placeholderIcon); - m_pageRenderer->requestPage(page, m_screenResolution * m_zoom); - } - y += size.height() + m_pageSpacing; - ++page; - } - m_bottomPageShowing = page - 1; - emit showingPageRange(m_topPageShowing, m_bottomPageShowing); -} - -qreal SequentialPageWidget::yForPage(int endPage) -{ - // TODO maybe put this loop into a page iterator class - int y = m_pageSpacing; - for (int page = 0; page < pageCount() && page < endPage; ++page) { - QSizeF size = pageSize(page); - int height = size.toSize().height(); - y += height + m_pageSpacing; - } - return y; -} diff --git a/examples/pdfwidgets/pdfviewer/sequentialpagewidget.h b/examples/pdfwidgets/pdfviewer/sequentialpagewidget.h deleted file mode 100644 index a84a37bb0..000000000 --- a/examples/pdfwidgets/pdfviewer/sequentialpagewidget.h +++ /dev/null @@ -1,99 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtPDF module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** 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 http://www.qt.io/terms-conditions. For further -** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef SEQUENTIALPAGEWIDGET_H -#define SEQUENTIALPAGEWIDGET_H - -#include - -QT_BEGIN_NAMESPACE -class QPdfDocument; -QT_END_NAMESPACE - -class PageRenderer; - -class SequentialPageWidget : public QWidget -{ - Q_OBJECT -public: - explicit SequentialPageWidget(QWidget *parent = 0); - ~SequentialPageWidget(); - - void paintEvent(QPaintEvent * event); - qreal zoom() { return m_zoom; } - qreal yForPage(int page); - int topPageShowing() { return m_topPageShowing; } - int bottomPageShowing() { return m_bottomPageShowing; } - - void setDocument(QPdfDocument *document); - -public slots: - void setZoom(qreal factor); - void invalidate(); - -signals: - void showingPageRange(int start, int end); - void zoomChanged(qreal factor); - -private slots: - void documentStatusChanged(); - void pageLoaded(int page, qreal zoom, QImage image); - -private: - int pageCount(); - QSizeF pageSize(int page); - void render(int page); - -private: - QHash m_pageCache; - QVector m_cachedPagesLRU; - int m_pageCacheLimit; - QVector m_pageSizes; - PageRenderer *m_pageRenderer; - QBrush m_background; - QPixmap m_placeholderIcon; - QBrush m_placeholderBackground; - int m_pageSpacing; - int m_topPageShowing; - int m_bottomPageShowing; - QSize m_totalSize; - qreal m_zoom; - qreal m_screenResolution; // pixels per point - - QPdfDocument *m_document; -}; - -#endif // SEQUENTIALPAGEWIDGET_H diff --git a/examples/pdfwidgets/pdfviewer/zoomselector.cpp b/examples/pdfwidgets/pdfviewer/zoomselector.cpp new file mode 100644 index 000000000..0205489aa --- /dev/null +++ b/examples/pdfwidgets/pdfviewer/zoomselector.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtPDF module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "zoomselector.h" + +#include + +ZoomSelector::ZoomSelector(QWidget *parent) + : QComboBox(parent) +{ + setEditable(true); + + addItem(QLatin1String("Fit Width")); + addItem(QLatin1String("Fit Page")); + addItem(QLatin1String("12%")); + addItem(QLatin1String("25%")); + addItem(QLatin1String("33%")); + addItem(QLatin1String("50%")); + addItem(QLatin1String("66%")); + addItem(QLatin1String("75%")); + addItem(QLatin1String("100%")); + addItem(QLatin1String("125%")); + addItem(QLatin1String("150%")); + addItem(QLatin1String("200%")); + addItem(QLatin1String("400%")); + + connect(this, static_cast(&QComboBox::currentIndexChanged), + this, &ZoomSelector::onCurrentTextChanged); + + connect(lineEdit(), &QLineEdit::editingFinished, + this, [this](){onCurrentTextChanged(lineEdit()->text()); }); +} + +void ZoomSelector::setZoomFactor(qreal zoomFactor) +{ + setCurrentText(QString::number(qRound(zoomFactor * 100)) + QLatin1String("%")); +} + +void ZoomSelector::reset() +{ + setCurrentIndex(8); // 100% +} + +void ZoomSelector::onCurrentTextChanged(const QString &text) +{ + if (text == QLatin1String("Fit Width")) { + emit zoomModeChanged(QPdfView::FitToWidth); + } else if (text == QLatin1String("Fit Page")) { + emit zoomModeChanged(QPdfView::FitInView); + } else { + qreal factor = 1.0; + + QString withoutPercent(text); + withoutPercent.remove(QLatin1Char('%')); + + bool ok = false; + const int zoomLevel = withoutPercent.toInt(&ok); + if (ok) + factor = zoomLevel / 100.0; + + emit zoomModeChanged(QPdfView::CustomZoom); + emit zoomFactorChanged(factor); + } +} diff --git a/examples/pdfwidgets/pdfviewer/zoomselector.h b/examples/pdfwidgets/pdfviewer/zoomselector.h new file mode 100644 index 000000000..c58d09970 --- /dev/null +++ b/examples/pdfwidgets/pdfviewer/zoomselector.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtPDF module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ZOOMSELECTOR_H +#define ZOOMSELECTOR_H + +#include +#include + +class ZoomSelector : public QComboBox +{ + Q_OBJECT + +public: + explicit ZoomSelector(QWidget *parent = nullptr); + +public slots: + void setZoomFactor(qreal zoomFactor); + + void reset(); + +signals: + void zoomModeChanged(QPdfView::ZoomMode zoomMode); + void zoomFactorChanged(qreal zoomFactor); + +private slots: + void onCurrentTextChanged(const QString &text); +}; + +#endif // ZOOMSELECTOR_H -- cgit v1.2.3