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
|
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDockWidget>
#include <QLabel>
#include <poppler-qt5.h>
#include "infowidget/infowidget.h"
#include "contentswidget/contentswidget.h"
MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto *infoDock = new QDockWidget(tr("Info"), this);
info = new InfoForm(infoDock);
infoDock->setWidget(info);
addDockWidget(Qt::LeftDockWidgetArea, infoDock);
auto *contentsDock = new QDockWidget(tr("Contents"), this);
contents = new ContentsForm(contentsDock);
contentsDock->setWidget(contents);
addDockWidget(Qt::LeftDockWidgetArea, contentsDock);
connect(ui->actionOpen, &QAction::triggered, this, [this]() {
const QString userPath = QFileDialog::getOpenFileName(this, tr("Select PDF to open"), QDir::homePath(), tr("PDF Files (*.pdf);;All Files (*)"));
if(!userPath.isEmpty())
this->open(userPath);
});
connect(ui->actionQuit, &QAction::triggered, qApp, &QApplication::quit);
// navigation
connect(ui->previous, &QToolButton::clicked, this, [this]() {
const int currentPage = ui->pageNumber->text().toInt();
this->showPage(currentPage - 1);
});
connect(ui->next, &QToolButton::clicked, this, [this]() {
const int currentPage = ui->pageNumber->text().toInt();
this->showPage(currentPage + 1);
});
connect(ui->pageNumber, &QLineEdit::textEdited, this, [this](const QString &text) {
const int page = text.toInt();
this->showPage(page);
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::open(const QString &path)
{
if(path.isEmpty()) {
return;
}
if(!QFile::exists(path)) {
statusBar()->showMessage(tr("Couldn't open file because it doesn't exist"), 3000);
return;
}
qDebug("open path: %s", qUtf8Printable(path));
delete document;
document = Poppler::Document::load(path);
if(!document || document->isLocked()) {
statusBar()->showMessage(tr("Couldn't parse file"), 3000);
return;
}
info->setMetadata(InfoForm::Title, document->title());
info->setMetadata(InfoForm::Subject, document->subject());
info->setMetadata(InfoForm::Author, document->author());
info->setMetadata(InfoForm::Creator, document->creator());
info->setMetadata(InfoForm::CreationDate, document->creationDate().toString(Qt::RFC2822Date));
info->setMetadata(InfoForm::ModificationDate, document->modificationDate().toString(Qt::RFC2822Date));
info->setMetadata(InfoForm::Producer, document->producer());
contents->setContents(document->toc());
showPage(0);
statusBar()->showMessage(tr("Opened file %1").arg(path), 3000);
}
void MainWindow::showPage(int pageNumber)
{
auto *page = document->page(pageNumber);
if(page == nullptr)
return;
ui->pageNumber->setText(QString::number(pageNumber));
const auto image = page->renderToImage();
auto *label = new QLabel(this);
label->setPixmap(QPixmap::fromImage(image));
ui->scrollArea->setWidget(label);
delete page;
}
|