diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2019-03-02 17:57:16 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2019-03-02 17:57:16 +0200 |
commit | 03cb04bd435ddc5a637166d3188c45bb7391d6a0 (patch) | |
tree | 59b5b1f7748a62160c74a58afac12e7b8a9b30a1 /src/mainwindow | |
download | cpdf-master.tar.xz |
Diffstat (limited to 'src/mainwindow')
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 103 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.h | 35 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.ui | 115 |
3 files changed, 253 insertions, 0 deletions
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 <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; +} 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 <QMainWindow> + +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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>640</width> + <height>480</height> + </rect> + </property> + <property name="windowTitle"> + <string>cpdf</string> + </property> + <widget class="QWidget" name="centralwidget"> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QToolButton" name="previous"> + <property name="text"> + <string>Previous</string> + </property> + <property name="shortcut"> + <string>Ctrl+Left</string> + </property> + </widget> + </item> + <item> + <widget class="QToolButton" name="next"> + <property name="text"> + <string>Next</string> + </property> + <property name="shortcut"> + <string>Ctrl+Right</string> + </property> + </widget> + </item> + <item> + <widget class="QLineEdit" name="pageNumber"/> + </item> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + <item> + <widget class="QScrollArea" name="scrollArea"> + <property name="widgetResizable"> + <bool>true</bool> + </property> + <widget class="QWidget" name="scrollAreaWidgetContents"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>620</width> + <height>380</height> + </rect> + </property> + </widget> + </widget> + </item> + </layout> + </widget> + <widget class="QMenuBar" name="menubar"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>640</width> + <height>23</height> + </rect> + </property> + <widget class="QMenu" name="menuFile"> + <property name="title"> + <string>File</string> + </property> + <addaction name="actionOpen"/> + <addaction name="actionQuit"/> + </widget> + <addaction name="menuFile"/> + </widget> + <widget class="QStatusBar" name="statusbar"/> + <action name="actionOpen"> + <property name="text"> + <string>Open</string> + </property> + <property name="shortcut"> + <string>Ctrl+O</string> + </property> + </action> + <action name="actionQuit"> + <property name="text"> + <string>Quit</string> + </property> + <property name="shortcut"> + <string>Ctrl+Q</string> + </property> + </action> + </widget> + <resources/> + <connections/> +</ui> |