From 03cb04bd435ddc5a637166d3188c45bb7391d6a0 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 2 Mar 2019 17:57:16 +0200 Subject: Initial commit --- src/mainwindow/mainwindow.cpp | 103 +++++++++++++++++++++++++++++++++++++ src/mainwindow/mainwindow.h | 35 +++++++++++++ src/mainwindow/mainwindow.ui | 115 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 253 insertions(+) create mode 100644 src/mainwindow/mainwindow.cpp create mode 100644 src/mainwindow/mainwindow.h create mode 100644 src/mainwindow/mainwindow.ui (limited to 'src/mainwindow') diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp new file mode 100644 index 0000000..3f854da --- /dev/null +++ b/src/mainwindow/mainwindow.cpp @@ -0,0 +1,103 @@ +#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; +} diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h new file mode 100644 index 0000000..14ceb49 --- /dev/null +++ b/src/mainwindow/mainwindow.h @@ -0,0 +1,35 @@ +#ifndef CPDF_MAINWINDOW_H +#define CPDF_MAINWINDOW_H + +#include + +namespace Poppler { + class Document; +} + +namespace Ui { + class MainWindow; +} + +class InfoForm; +class ContentsForm; +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); + ~MainWindow(); + +public slots: + void open(const QString &path); + void showPage(int pageNumber); + +private: + Ui::MainWindow *ui = nullptr; + InfoForm *info = nullptr; + ContentsForm *contents = nullptr; + Poppler::Document *document = nullptr; +}; + +#endif // CPDF_MAINWINDOW_H diff --git a/src/mainwindow/mainwindow.ui b/src/mainwindow/mainwindow.ui new file mode 100644 index 0000000..6c37ddf --- /dev/null +++ b/src/mainwindow/mainwindow.ui @@ -0,0 +1,115 @@ + + + MainWindow + + + + 0 + 0 + 640 + 480 + + + + cpdf + + + + + + + + + Previous + + + Ctrl+Left + + + + + + + Next + + + Ctrl+Right + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + true + + + + + 0 + 0 + 620 + 380 + + + + + + + + + + + 0 + 0 + 640 + 23 + + + + + File + + + + + + + + + + Open + + + Ctrl+O + + + + + Quit + + + Ctrl+Q + + + + + + -- cgit v1.2.1