From 03cb04bd435ddc5a637166d3188c45bb7391d6a0 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 2 Mar 2019 17:57:16 +0200 Subject: Initial commit --- Readme.md | 14 ++++ meson.build | 31 +++++++++ src/contentswidget/contentswidget.cpp | 61 +++++++++++++++++ src/contentswidget/contentswidget.h | 25 +++++++ src/contentswidget/contentswidget.ui | 52 +++++++++++++++ src/infowidget/infowidget.cpp | 42 ++++++++++++ src/infowidget/infowidget.h | 35 ++++++++++ src/infowidget/infowidget.ui | 119 ++++++++++++++++++++++++++++++++++ src/main.cpp | 44 +++++++++++++ src/mainwindow/mainwindow.cpp | 103 +++++++++++++++++++++++++++++ src/mainwindow/mainwindow.h | 35 ++++++++++ src/mainwindow/mainwindow.ui | 115 ++++++++++++++++++++++++++++++++ 12 files changed, 676 insertions(+) create mode 100644 Readme.md create mode 100644 meson.build create mode 100644 src/contentswidget/contentswidget.cpp create mode 100644 src/contentswidget/contentswidget.h create mode 100644 src/contentswidget/contentswidget.ui create mode 100644 src/infowidget/infowidget.cpp create mode 100644 src/infowidget/infowidget.h create mode 100644 src/infowidget/infowidget.ui create mode 100644 src/main.cpp create mode 100644 src/mainwindow/mainwindow.cpp create mode 100644 src/mainwindow/mainwindow.h create mode 100644 src/mainwindow/mainwindow.ui diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e51c6fa --- /dev/null +++ b/Readme.md @@ -0,0 +1,14 @@ +# cpdf + +## What is this? +A simple PDF viewer. + +## Building from source +You need meson, ninja, qt5 and poppler-qt5. + +```sh +mkdir build +meson build +ninja -C build +``` + diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..36418ea --- /dev/null +++ b/meson.build @@ -0,0 +1,31 @@ +project('cpdf', 'cpp', + default_options: ['cpp_std=c++17'], + license: 'GPL' +) + +if not get_option('debug') + add_project_arguments('-DQT_NO_DEBUG', language: 'cpp') +endif + +mod_qt5 = import('qt5') +dep_qt5 = dependency('qt5', modules: ['Core', 'Xml', 'Widgets'], required: true) + +poppler = dependency('poppler-qt5', required: true) + +moc = mod_qt5.preprocess( + moc_headers: ['src/mainwindow/mainwindow.h', 'src/contentswidget/contentswidget.h', 'src/infowidget/infowidget.h'], + ui_files: ['src/mainwindow/mainwindow.ui', 'src/contentswidget/contentswidget.ui', 'src/infowidget/infowidget.ui'], + dependencies: dep_qt5 +) + +cpdf = executable('cpdf', install: true, + dependencies: [dep_qt5, poppler], + include_directories: include_directories('src'), + sources: ['src/main.cpp', + 'src/mainwindow/mainwindow.cpp', + 'src/contentswidget/contentswidget.cpp', + 'src/infowidget/infowidget.cpp', + moc + ] +) + diff --git a/src/contentswidget/contentswidget.cpp b/src/contentswidget/contentswidget.cpp new file mode 100644 index 0000000..40ea5f9 --- /dev/null +++ b/src/contentswidget/contentswidget.cpp @@ -0,0 +1,61 @@ +#include "contentswidget.h" +#include "ui_contentswidget.h" +#include + +ContentsForm::ContentsForm(QWidget *parent) + : QWidget(parent) + , ui(new Ui::ContentsForm) +{ + ui->setupUi(this); +} + +ContentsForm::~ContentsForm() +{ + delete ui; +} + +QTreeWidgetItem *treeNode(QTreeWidgetItem *parent, const QDomNode &node) +{ + auto *item = new QTreeWidgetItem(parent); + item->setText(0, node.nodeName()); + + if(node.hasAttributes()) { + const auto attr = node.attributes(); + for(int i = 0; i < attr.length(); ++i) { + const QString name = attr.item(i).nodeName(); + const QString value = attr.item(i).nodeValue(); + + if(name == "Open") + item->setText(1, value); + else if(name == "Destination") + item->setText(2, value); + else + qDebug("Unknown attribute name %s\nattr.value=%s", qUtf8Printable(name), qUtf8Printable(value)); + } + } + + auto child = node.firstChild(); + while(!child.isNull()) { + treeNode(item, child); + child = child.nextSibling(); + } + + return item; +} + +void ContentsForm::setContents(QDomDocument *document) +{ + ui->treeWidget->clear(); + + if(document && !document->isNull()) { + auto node = document->firstChild(); + while(!node.isNull()) { + auto *item = treeNode(nullptr, node); + ui->treeWidget->addTopLevelItem(item); + + node = node.nextSibling(); + } + } + delete document; +} + diff --git a/src/contentswidget/contentswidget.h b/src/contentswidget/contentswidget.h new file mode 100644 index 0000000..9276bf4 --- /dev/null +++ b/src/contentswidget/contentswidget.h @@ -0,0 +1,25 @@ +#ifndef CPDF_CONTENTSWIDGET_H +#define CPDF_CONTENTSWIDGET_H + +#include + +namespace Ui { + class ContentsForm; +} + +class QDomDocument; +class ContentsForm : public QWidget +{ + Q_OBJECT +public: + explicit ContentsForm(QWidget *parent = nullptr); + ~ContentsForm(); + +public slots: + void setContents(QDomDocument *document); + +private: + Ui::ContentsForm *ui = nullptr; +}; + +#endif // CPDF_CONTENTSWIDGET_H diff --git a/src/contentswidget/contentswidget.ui b/src/contentswidget/contentswidget.ui new file mode 100644 index 0000000..8b74c36 --- /dev/null +++ b/src/contentswidget/contentswidget.ui @@ -0,0 +1,52 @@ + + + ContentsForm + + + + 0 + 0 + 240 + 320 + + + + Contents + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + Item + + + + + Open + + + + + Destination + + + + + + + + + diff --git a/src/infowidget/infowidget.cpp b/src/infowidget/infowidget.cpp new file mode 100644 index 0000000..9541b29 --- /dev/null +++ b/src/infowidget/infowidget.cpp @@ -0,0 +1,42 @@ +#include "infowidget.h" +#include "ui_infowidget.h" + +InfoForm::InfoForm(QWidget *parent) + : QWidget(parent) + , ui(new Ui::InfoForm) +{ + ui->setupUi(this); +} + +InfoForm::~InfoForm() +{ + delete ui; +} + +void InfoForm::setMetadata(Metadata which, const QString &value) +{ + switch(which) { + case InfoForm::Title: + ui->title->setText(value); + return; + case InfoForm::Subject: + ui->subject->setText(value); + return; + case InfoForm::Author: + ui->author->setText(value); + return; + case InfoForm::Creator: + ui->creator->setText(value); + return; + case InfoForm::CreationDate: + ui->creationDate->setText(value); + return; + case InfoForm::ModificationDate: + ui->modificationDate->setText(value); + return; + case InfoForm::Producer: + ui->producer->setText(value); + return; + } +} + diff --git a/src/infowidget/infowidget.h b/src/infowidget/infowidget.h new file mode 100644 index 0000000..d419901 --- /dev/null +++ b/src/infowidget/infowidget.h @@ -0,0 +1,35 @@ +#ifndef CPDF_INFOWIDGET_H +#define CPDF_INFOWIDGET_H + +#include + +namespace Ui { + class InfoForm; +} + +class InfoForm : public QWidget +{ + Q_OBJECT + +public: + enum Metadata { + Title, + Subject, + Author, + Creator, + CreationDate, + ModificationDate, + Producer + }; + + explicit InfoForm(QWidget *parent = nullptr); + ~InfoForm(); + +public slots: + void setMetadata(Metadata which, const QString &value); + +private: + Ui::InfoForm *ui = nullptr; +}; + +#endif // CPDF_INFOWIDGET_H diff --git a/src/infowidget/infowidget.ui b/src/infowidget/infowidget.ui new file mode 100644 index 0000000..e22c0fa --- /dev/null +++ b/src/infowidget/infowidget.ui @@ -0,0 +1,119 @@ + + + InfoForm + + + + 0 + 0 + 320 + 180 + + + + Form + + + + + + Title + + + + + + + + + + + + + + Subject + + + + + + + Author + + + + + + + Creator + + + + + + + Creation date + + + + + + + Modification date + + + + + + + Producer + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..494153e --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,44 @@ +#include +#include +#include "mainwindow/mainwindow.h" +#include + +inline MainWindow *openWindow(const QString &path) +{ + auto *window = new MainWindow; + window->setAttribute(Qt::WA_DeleteOnClose, true); + + if(!path.isEmpty()) + window->open(path); + + window->show(); + return window; +} + +int main(int argc, char** argv) +{ + QApplication app(argc, argv); + QCoreApplication::setApplicationName(QLatin1Literal("cpdf")); + QCoreApplication::setApplicationVersion(QLatin1Literal("0.1.0")); + + QCommandLineParser parser; + parser.setApplicationDescription(QLatin1Literal("Simple PDF viewer")); + parser.addHelpOption(); + parser.addVersionOption(); + parser.addPositionalArgument(QLatin1Literal("URLs"), QLatin1Literal("PDF files to open")); + + parser.process(app); + + { + const auto files = parser.positionalArguments(); + if(files.count() == 0) { + openWindow(QString()); + } else { + for(const auto file : files) { + openWindow(file); + } + } + } + + return app.exec(); +} 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