#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include #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; }