summaryrefslogtreecommitdiffstats
path: root/examples/widgets/pdfviewer/mainwindow.cpp
blob: 901e80128553d055d1031fea14f87e91fefba2df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/******************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt PDF Module.
**
** $QT_BEGIN_LICENSE:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
******************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "sequentialpagewidget.h"

#include <QFileDialog>
#include <QLineEdit>
#include <QMessageBox>
#include <QPdfDocument>
#include <QScroller>
#include <QtMath>

const qreal zoomMultiplier = qSqrt(2.0);

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_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_pageWidget->setDocument(m_document);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::open(const QUrl &docLocation)
{
    if (docLocation.isLocalFile()) {
        m_document->load(docLocation.toLocalFile());
    } 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()));
    }
    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::on_actionOpen_triggered()
{
    QUrl toOpen = QFileDialog::getOpenFileUrl(this, tr("Choose a PDF"), QUrl(), "Portable Documents (*.pdf)");
    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));
}