summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Koenig <tobias.koenig@kdab.com>2017-02-24 10:59:12 +0100
committerTobias Koenig <tobias.koenig@kdab.com>2018-02-18 11:16:30 +0000
commit246b53bbcc54339e7acf9ea991f3593b309a1752 (patch)
tree0e3f27eff1cbe2d481ebbca335b317a32700824e
parent133665c09b62a18e6f65363cc2ac66615c11a3d3 (diff)
Port pdfviewer example to QPdfView widget
Change-Id: Id651a2c179628506f0955751abda57cbae569ee9 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
-rw-r--r--examples/pdf/pdfviewer/mainwindow.cpp88
-rw-r--r--examples/pdf/pdfviewer/mainwindow.h15
-rw-r--r--examples/pdf/pdfviewer/mainwindow.ui42
-rw-r--r--examples/pdf/pdfviewer/pagerenderer.cpp105
-rw-r--r--examples/pdf/pdfviewer/pageselector.cpp111
-rw-r--r--examples/pdf/pdfviewer/pageselector.h (renamed from examples/pdf/pdfviewer/pagerenderer.h)50
-rw-r--r--examples/pdf/pdfviewer/pdfviewer.pro24
-rw-r--r--examples/pdf/pdfviewer/sequentialpagewidget.cpp192
-rw-r--r--examples/pdf/pdfviewer/zoomselector.cpp97
-rw-r--r--examples/pdf/pdfviewer/zoomselector.h (renamed from examples/pdf/pdfviewer/sequentialpagewidget.h)66
10 files changed, 311 insertions, 479 deletions
diff --git a/examples/pdf/pdfviewer/mainwindow.cpp b/examples/pdf/pdfviewer/mainwindow.cpp
index fd26563..5f9bf38 100644
--- a/examples/pdf/pdfviewer/mainwindow.cpp
+++ b/examples/pdf/pdfviewer/mainwindow.cpp
@@ -37,14 +37,14 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
-#include "sequentialpagewidget.h"
+#include "pageselector.h"
+#include "zoomselector.h"
#include <QFileDialog>
-#include <QLineEdit>
#include <QMessageBox>
#include <QPdfBookmarkModel>
#include <QPdfDocument>
-#include <QScroller>
+#include <QPdfPageNavigation>
#include <QtMath>
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/pdf/pdfviewer/mainwindow.h b/examples/pdf/pdfviewer/mainwindow.h
index f292058..afdfcad 100644
--- a/examples/pdf/pdfviewer/mainwindow.h
+++ b/examples/pdf/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/pdf/pdfviewer/mainwindow.ui b/examples/pdf/pdfviewer/mainwindow.ui
index 738cef4..2651525 100644
--- a/examples/pdf/pdfviewer/mainwindow.ui
+++ b/examples/pdf/pdfviewer/mainwindow.ui
@@ -113,26 +113,13 @@
</attribute>
</widget>
</widget>
- <widget class="QScrollArea" name="scrollArea">
+ <widget class="QPdfView" name="pdfView" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>10</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
- <property name="widgetResizable">
- <bool>true</bool>
- </property>
- <widget class="QWidget" name="scrollAreaWidgetContents">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>401</width>
- <height>515</height>
- </rect>
- </property>
- </widget>
</widget>
</widget>
</item>
@@ -172,6 +159,8 @@
<addaction name="actionZoom_Out"/>
<addaction name="actionPrevious_Page"/>
<addaction name="actionNext_Page"/>
+ <addaction name="separator"/>
+ <addaction name="actionContinuous"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuView"/>
@@ -195,7 +184,6 @@
<addaction name="actionZoom_Out"/>
<addaction name="actionZoom_In"/>
<addaction name="separator"/>
- <addaction name="actionGo"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionOpen">
@@ -252,14 +240,6 @@
<string>Ctrl+-</string>
</property>
</action>
- <action name="actionGo">
- <property name="text">
- <string>Go</string>
- </property>
- <property name="toolTip">
- <string>Go to Page</string>
- </property>
- </action>
<action name="actionPrevious_Page">
<property name="text">
<string>Previous Page</string>
@@ -276,8 +256,24 @@
<string>PgDown</string>
</property>
</action>
+ <action name="actionContinuous">
+ <property name="checkable">
+ <bool>true</bool>
+ </property>
+ <property name="text">
+ <string>Continuous</string>
+ </property>
+ </action>
</widget>
<layoutdefault spacing="6" margin="11"/>
+ <customwidgets>
+ <customwidget>
+ <class>QPdfView</class>
+ <extends>QWidget</extends>
+ <header location="global">qpdfview.h</header>
+ <container>1</container>
+ </customwidget>
+ </customwidgets>
<resources>
<include location="resources.qrc"/>
</resources>
diff --git a/examples/pdf/pdfviewer/pagerenderer.cpp b/examples/pdf/pdfviewer/pagerenderer.cpp
deleted file mode 100644
index 8cb1ed6..0000000
--- a/examples/pdf/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 <QElapsedTimer>
-#include <QLoggingCategory>
-#include <QPainter>
-#include <QPdfDocument>
-#include <QUrl>
-
-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/pdf/pdfviewer/pageselector.cpp b/examples/pdf/pdfviewer/pageselector.cpp
new file mode 100644
index 0000000..cd3c4ba
--- /dev/null
+++ b/examples/pdf/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 <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/pdf/pdfviewer/pagerenderer.h b/examples/pdf/pdfviewer/pageselector.h
index f1e4918..7a7283f 100644
--- a/examples/pdf/pdfviewer/pagerenderer.h
+++ b/examples/pdf/pdfviewer/pageselector.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** 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.
@@ -34,49 +34,39 @@
**
****************************************************************************/
-#ifndef PAGECACHE_H
-#define PAGECACHE_H
+#ifndef PAGESELECTOR_H
+#define PAGESELECTOR_H
-#include <QImage>
-#include <QThread>
+#include <QWidget>
QT_BEGIN_NAMESPACE
+class QLabel;
+class QLineEdit;
class QPdfDocument;
+class QPdfPageNavigation;
+class QToolButton;
QT_END_NAMESPACE
-class PageRenderer : public QThread
+class PageSelector : public QWidget
{
Q_OBJECT
public:
- explicit PageRenderer(QObject *parent = nullptr);
+ explicit PageSelector(QWidget *parent = nullptr);
-public slots:
- void setDocument(QPdfDocument *document);
+ void setPageNavigation(QPdfPageNavigation *pageNavigation);
- void requestPage(int page, qreal zoom, Priority priority = QThread::NormalPriority);
-
-signals:
- void pageReady(int page, qreal zoom, QImage image);
-
-protected:
- void run() override;
+private slots:
+ void onCurrentPageChanged(int page);
+ void pageNumberEdited();
private:
- void renderPage(int page, qreal zoom);
-
-private:
- QPdfDocument *m_document;
-
- // current request only
- int m_page;
- qreal m_zoom;
+ QPdfPageNavigation *m_pageNavigation;
- // performance statistics
- qreal m_minRenderTime;
- qreal m_maxRenderTime;
- qreal m_totalRenderTime;
- int m_totalPagesRendered;
+ QLineEdit *m_pageNumberEdit;
+ QLabel *m_pageCountLabel;
+ QToolButton *m_previousPageButton;
+ QToolButton *m_nextPageButton;
};
-#endif // PAGECACHE_H
+#endif // PAGESELECTOR_H
diff --git a/examples/pdf/pdfviewer/pdfviewer.pro b/examples/pdf/pdfviewer/pdfviewer.pro
index 10cacce..d3e7688 100644
--- a/examples/pdf/pdfviewer/pdfviewer.pro
+++ b/examples/pdf/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/pdf/pdfviewer/sequentialpagewidget.cpp b/examples/pdf/pdfviewer/sequentialpagewidget.cpp
deleted file mode 100644
index 904bc17..0000000
--- a/examples/pdf/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 <QPaintEvent>
-#include <QPainter>
-#include <QPdfDocument>
-#include <QGuiApplication>
-#include <QScreen>
-#include <QLoggingCategory>
-#include <QElapsedTimer>
-
-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/pdf/pdfviewer/zoomselector.cpp b/examples/pdf/pdfviewer/zoomselector.cpp
new file mode 100644
index 0000000..0205489
--- /dev/null
+++ b/examples/pdf/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 <QLineEdit>
+
+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<void(QComboBox::*)(const QString &)>(&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/pdf/pdfviewer/sequentialpagewidget.h b/examples/pdf/pdfviewer/zoomselector.h
index a84a37b..c58d099 100644
--- a/examples/pdf/pdfviewer/sequentialpagewidget.h
+++ b/examples/pdf/pdfviewer/zoomselector.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** 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.
@@ -34,66 +34,30 @@
**
****************************************************************************/
-#ifndef SEQUENTIALPAGEWIDGET_H
-#define SEQUENTIALPAGEWIDGET_H
+#ifndef ZOOMSELECTOR_H
+#define ZOOMSELECTOR_H
-#include <QWidget>
+#include <QComboBox>
+#include <QPdfView>
-QT_BEGIN_NAMESPACE
-class QPdfDocument;
-QT_END_NAMESPACE
-
-class PageRenderer;
-
-class SequentialPageWidget : public QWidget
+class ZoomSelector : public QComboBox
{
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:
+ explicit ZoomSelector(QWidget *parent = nullptr);
public slots:
- void setZoom(qreal factor);
- void invalidate();
+ void setZoomFactor(qreal zoomFactor);
+
+ void reset();
signals:
- void showingPageRange(int start, int end);
- void zoomChanged(qreal factor);
+ void zoomModeChanged(QPdfView::ZoomMode zoomMode);
+ void zoomFactorChanged(qreal zoomFactor);
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<int, QImage> m_pageCache;
- QVector<int> m_cachedPagesLRU;
- int m_pageCacheLimit;
- QVector<QSizeF> 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;
+ void onCurrentTextChanged(const QString &text);
};
-#endif // SEQUENTIALPAGEWIDGET_H
+#endif // ZOOMSELECTOR_H