diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/widgets/pdfviewer/images/fileopen.png | bin | 0 -> 1771 bytes | |||
-rw-r--r-- | examples/widgets/pdfviewer/images/go-next-24.png | bin | 0 -> 782 bytes | |||
-rw-r--r-- | examples/widgets/pdfviewer/images/go-previous-24.png | bin | 0 -> 797 bytes | |||
-rw-r--r-- | examples/widgets/pdfviewer/images/zoom-in-24.png | bin | 0 -> 1302 bytes | |||
-rw-r--r-- | examples/widgets/pdfviewer/images/zoom-in-32.png | bin | 0 -> 1873 bytes | |||
-rw-r--r-- | examples/widgets/pdfviewer/images/zoom-out-24.png | bin | 0 -> 1247 bytes | |||
-rw-r--r-- | examples/widgets/pdfviewer/images/zoom-out-32.png | bin | 0 -> 1749 bytes | |||
-rw-r--r-- | examples/widgets/pdfviewer/mainwindow.cpp | 74 | ||||
-rw-r--r-- | examples/widgets/pdfviewer/mainwindow.h | 15 | ||||
-rw-r--r-- | examples/widgets/pdfviewer/mainwindow.ui | 103 | ||||
-rw-r--r-- | examples/widgets/pdfviewer/pdfviewer.pro | 3 | ||||
-rw-r--r-- | examples/widgets/pdfviewer/resources.qrc | 11 | ||||
-rw-r--r-- | examples/widgets/pdfviewer/sequentialpagewidget.cpp | 22 | ||||
-rw-r--r-- | examples/widgets/pdfviewer/sequentialpagewidget.h | 8 |
14 files changed, 230 insertions, 6 deletions
diff --git a/examples/widgets/pdfviewer/images/fileopen.png b/examples/widgets/pdfviewer/images/fileopen.png Binary files differnew file mode 100644 index 000000000..33e0d6394 --- /dev/null +++ b/examples/widgets/pdfviewer/images/fileopen.png diff --git a/examples/widgets/pdfviewer/images/go-next-24.png b/examples/widgets/pdfviewer/images/go-next-24.png Binary files differnew file mode 100644 index 000000000..9a55ef3d8 --- /dev/null +++ b/examples/widgets/pdfviewer/images/go-next-24.png diff --git a/examples/widgets/pdfviewer/images/go-previous-24.png b/examples/widgets/pdfviewer/images/go-previous-24.png Binary files differnew file mode 100644 index 000000000..2ea769eb8 --- /dev/null +++ b/examples/widgets/pdfviewer/images/go-previous-24.png diff --git a/examples/widgets/pdfviewer/images/zoom-in-24.png b/examples/widgets/pdfviewer/images/zoom-in-24.png Binary files differnew file mode 100644 index 000000000..d29b142b6 --- /dev/null +++ b/examples/widgets/pdfviewer/images/zoom-in-24.png diff --git a/examples/widgets/pdfviewer/images/zoom-in-32.png b/examples/widgets/pdfviewer/images/zoom-in-32.png Binary files differnew file mode 100644 index 000000000..34d70af37 --- /dev/null +++ b/examples/widgets/pdfviewer/images/zoom-in-32.png diff --git a/examples/widgets/pdfviewer/images/zoom-out-24.png b/examples/widgets/pdfviewer/images/zoom-out-24.png Binary files differnew file mode 100644 index 000000000..19703474f --- /dev/null +++ b/examples/widgets/pdfviewer/images/zoom-out-24.png diff --git a/examples/widgets/pdfviewer/images/zoom-out-32.png b/examples/widgets/pdfviewer/images/zoom-out-32.png Binary files differnew file mode 100644 index 000000000..b83220661 --- /dev/null +++ b/examples/widgets/pdfviewer/images/zoom-out-32.png diff --git a/examples/widgets/pdfviewer/mainwindow.cpp b/examples/widgets/pdfviewer/mainwindow.cpp index e5b29edfd..df0c5a366 100644 --- a/examples/widgets/pdfviewer/mainwindow.cpp +++ b/examples/widgets/pdfviewer/mainwindow.cpp @@ -2,10 +2,14 @@ #include "ui_mainwindow.h" #include <QFileDialog> +#include <QLineEdit> #include <QMessageBox> #include <QPdfDocument> +#include <QtMath> #include "sequentialpagewidget.h" +const qreal zoomMultiplier = qSqrt(2.0); + Q_LOGGING_CATEGORY(lcExample, "qt.examples.pdfviewer") MainWindow::MainWindow(QWidget *parent) @@ -13,12 +17,24 @@ MainWindow::MainWindow(QWidget *parent) , ui(new Ui::MainWindow) , m_doc(new QPdfDocument(this)) , m_pageWidget(new SequentialPageWidget(this)) + , m_zoomEdit(new QLineEdit(this)) + , m_pageEdit(new QLineEdit(this)) { ui->setupUi(this); m_pageWidget->setDocument(m_doc); 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())); } MainWindow::~MainWindow() @@ -47,6 +63,20 @@ void MainWindow::showingPageRange(int start, int 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::on_actionOpen_triggered() @@ -55,3 +85,47 @@ void MainWindow::on_actionOpen_triggered() if (toOpen.isValid()) open(toOpen); } + +void MainWindow::on_actionQuit_triggered() +{ + QApplication::quit(); +} + +void MainWindow::on_actionAbout_triggered() +{ + QMessageBox::about(this, tr("About PdfViewer"), + tr("An example using QPdfDocument")); +} + +void MainWindow::on_actionAbout_Qt_triggered() +{ + QMessageBox::aboutQt(this); +} + +void MainWindow::on_actionZoom_In_triggered() +{ + m_pageWidget->setZoom(m_pageWidget->zoom() * zoomMultiplier); +} + +void MainWindow::on_actionZoom_Out_triggered() +{ + m_pageWidget->setZoom(m_pageWidget->zoom() / zoomMultiplier); +} + +void MainWindow::on_actionGo_triggered() +{ + bool ok = false; + int page = m_pageEdit->text().toInt(&ok); + if (!ok) return; + ui->scrollArea->ensureVisible(0, m_pageWidget->yForPage(page)); +} + +void MainWindow::on_actionPrevious_Page_triggered() +{ + ui->scrollArea->ensureVisible(0, m_pageWidget->yForPage(m_pageWidget->topPageShowing() - 1)); +} + +void MainWindow::on_actionNext_Page_triggered() +{ + ui->scrollArea->ensureVisible(0, m_pageWidget->yForPage(m_pageWidget->bottomPageShowing() + 1)); +} diff --git a/examples/widgets/pdfviewer/mainwindow.h b/examples/widgets/pdfviewer/mainwindow.h index bcad93ebb..5d638ec33 100644 --- a/examples/widgets/pdfviewer/mainwindow.h +++ b/examples/widgets/pdfviewer/mainwindow.h @@ -10,6 +10,7 @@ namespace Ui { class MainWindow; } +class QLineEdit; class QPdfDocument; class SequentialPageWidget; @@ -26,12 +27,26 @@ public slots: private slots: void showingPageRange(int start, int end); + void zoomChanged(qreal factor); + void zoomEdited(); + + // action handlers void on_actionOpen_triggered(); + void on_actionQuit_triggered(); + void on_actionAbout_triggered(); + 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(); private: Ui::MainWindow *ui; QPdfDocument *m_doc; SequentialPageWidget *m_pageWidget; + QLineEdit *m_zoomEdit; + QLineEdit *m_pageEdit; }; #endif // MAINWINDOW_H diff --git a/examples/widgets/pdfviewer/mainwindow.ui b/examples/widgets/pdfviewer/mainwindow.ui index f1f90b307..7070619b9 100644 --- a/examples/widgets/pdfviewer/mainwindow.ui +++ b/examples/widgets/pdfviewer/mainwindow.ui @@ -26,7 +26,7 @@ <x>0</x> <y>0</y> <width>380</width> - <height>210</height> + <height>200</height> </rect> </property> <layout class="QVBoxLayout" name="verticalLayout_2"/> @@ -49,8 +49,27 @@ <string>File</string> </property> <addaction name="actionOpen"/> + <addaction name="actionQuit"/> + </widget> + <widget class="QMenu" name="menuHelp"> + <property name="title"> + <string>Help</string> + </property> + <addaction name="actionAbout"/> + <addaction name="actionAbout_Qt"/> + </widget> + <widget class="QMenu" name="menuView"> + <property name="title"> + <string>View</string> + </property> + <addaction name="actionZoom_In"/> + <addaction name="actionZoom_Out"/> + <addaction name="actionPrevious_Page"/> + <addaction name="actionNext_Page"/> </widget> <addaction name="menuFile"/> + <addaction name="menuView"/> + <addaction name="menuHelp"/> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> @@ -60,15 +79,95 @@ <bool>false</bool> </attribute> <addaction name="actionOpen"/> + <addaction name="separator"/> + <addaction name="actionZoom_Out"/> + <addaction name="actionZoom_In"/> + <addaction name="separator"/> + <addaction name="actionGo"/> </widget> <widget class="QStatusBar" name="statusBar"/> <action name="actionOpen"> + <property name="icon"> + <iconset resource="resources.qrc"> + <normaloff>:/icons/images/fileopen.png</normaloff>:/icons/images/fileopen.png</iconset> + </property> <property name="text"> <string>Open...</string> </property> + <property name="shortcut"> + <string>Ctrl+O</string> + </property> + </action> + <action name="actionQuit"> + <property name="text"> + <string>Quit</string> + </property> + <property name="shortcut"> + <string>Ctrl+Q</string> + </property> + </action> + <action name="actionAbout"> + <property name="text"> + <string>About</string> + </property> + </action> + <action name="actionAbout_Qt"> + <property name="text"> + <string>About Qt</string> + </property> + </action> + <action name="actionZoom_In"> + <property name="icon"> + <iconset resource="resources.qrc"> + <normaloff>:/icons/images/zoom-in-24.png</normaloff>:/icons/images/zoom-in-24.png</iconset> + </property> + <property name="text"> + <string>Zoom In</string> + </property> + <property name="shortcut"> + <string>Ctrl+=</string> + </property> + </action> + <action name="actionZoom_Out"> + <property name="icon"> + <iconset resource="resources.qrc"> + <normaloff>:/icons/images/zoom-out-24.png</normaloff>:/icons/images/zoom-out-24.png</iconset> + </property> + <property name="text"> + <string>Zoom Out</string> + </property> + <property name="shortcut"> + <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> + </property> + <property name="shortcut"> + <string>PgUp</string> + </property> + </action> + <action name="actionNext_Page"> + <property name="text"> + <string>Next Page</string> + </property> + <property name="shortcut"> + <string>PgDown</string> + </property> </action> </widget> <layoutdefault spacing="6" margin="11"/> - <resources/> + <resources> + <include location="resources.qrc"/> + </resources> <connections/> </ui> diff --git a/examples/widgets/pdfviewer/pdfviewer.pro b/examples/widgets/pdfviewer/pdfviewer.pro index 47a13f508..babcd85e6 100644 --- a/examples/widgets/pdfviewer/pdfviewer.pro +++ b/examples/widgets/pdfviewer/pdfviewer.pro @@ -10,3 +10,6 @@ HEADERS += mainwindow.h \ sequentialpagewidget.h FORMS += mainwindow.ui + +RESOURCES += \ + resources.qrc diff --git a/examples/widgets/pdfviewer/resources.qrc b/examples/widgets/pdfviewer/resources.qrc new file mode 100644 index 000000000..945c59565 --- /dev/null +++ b/examples/widgets/pdfviewer/resources.qrc @@ -0,0 +1,11 @@ +<RCC> + <qresource prefix="/icons"> + <file>images/fileopen.png</file> + <file>images/go-next-24.png</file> + <file>images/go-previous-24.png</file> + <file>images/zoom-in-24.png</file> + <file>images/zoom-in-32.png</file> + <file>images/zoom-out-24.png</file> + <file>images/zoom-out-32.png</file> + </qresource> +</RCC> diff --git a/examples/widgets/pdfviewer/sequentialpagewidget.cpp b/examples/widgets/pdfviewer/sequentialpagewidget.cpp index e7dd08746..a9d5c428b 100644 --- a/examples/widgets/pdfviewer/sequentialpagewidget.cpp +++ b/examples/widgets/pdfviewer/sequentialpagewidget.cpp @@ -13,6 +13,7 @@ SequentialPageWidget::SequentialPageWidget(QWidget *parent) , m_doc(Q_NULLPTR) , m_background(Qt::darkGray) , m_pageSpacing(3) + , m_topPageShowing(0) , m_zoom(1.) , m_screenResolution(QGuiApplication::primaryScreen()->logicalDotsPerInch() / 72.0) { @@ -25,12 +26,14 @@ SequentialPageWidget::~SequentialPageWidget() void SequentialPageWidget::setDocument(QPdfDocument *doc) { m_doc = doc; + m_topPageShowing = 0; invalidate(); } void SequentialPageWidget::setZoom(qreal factor) { m_zoom = factor; + emit zoomChanged(factor); invalidate(); } @@ -52,6 +55,7 @@ void SequentialPageWidget::invalidate() } m_totalSize = totalSize.toSize(); setMinimumSize(m_totalSize); + emit zoomChanged(m_zoom); qCDebug(lcExample) << "total size" << m_totalSize; update(); } @@ -74,11 +78,11 @@ void SequentialPageWidget::paintEvent(QPaintEvent * event) int height = size.toSize().height(); if (y + height >= event->rect().top()) break; - y += height; + y += height + m_pageSpacing; ++page; } y += m_pageSpacing; - int startPage = page; + m_topPageShowing = page; // Actually render pages while (y < event->rect().bottom() && page < m_doc->pageCount()) { @@ -92,6 +96,18 @@ void SequentialPageWidget::paintEvent(QPaintEvent * event) y += pm.height() + m_pageSpacing; ++page; } + m_bottomPageShowing = page - 1; + emit showingPageRange(m_topPageShowing, m_bottomPageShowing); +} - emit showingPageRange(startPage, page - 1); +qreal SequentialPageWidget::yForPage(int endPage) +{ + // TODO maybe put this loop into a page iterator class + int y = m_pageSpacing; + for (int page = 0; page < m_doc->pageCount() && page < endPage; ++page) { + QSizeF size = pageSize(page); + int height = size.toSize().height(); + y += height + m_pageSpacing; + } + return y; } diff --git a/examples/widgets/pdfviewer/sequentialpagewidget.h b/examples/widgets/pdfviewer/sequentialpagewidget.h index 44b27faad..07fef477d 100644 --- a/examples/widgets/pdfviewer/sequentialpagewidget.h +++ b/examples/widgets/pdfviewer/sequentialpagewidget.h @@ -2,7 +2,6 @@ #define SEQUENTIALPAGEWIDGET_H #include <QWidget> -#include <QDebug> class QPdfDocument; @@ -14,6 +13,10 @@ public: ~SequentialPageWidget(); void paintEvent(QPaintEvent * event); + qreal zoom() { return m_zoom; } + qreal yForPage(int page); + int topPageShowing() { return m_topPageShowing; } + int bottomPageShowing() { return m_bottomPageShowing; } public slots: void setDocument(QPdfDocument *doc); @@ -22,6 +25,7 @@ public slots: signals: void showingPageRange(int start, int end); + void zoomChanged(qreal factor); private: QSizeF pageSize(int page); @@ -32,6 +36,8 @@ private: QHash<int, QPixmap> m_pageCache; QBrush m_background; int m_pageSpacing; + int m_topPageShowing; + int m_bottomPageShowing; QSize m_totalSize; qreal m_zoom; qreal m_screenResolution; // pixels per point |