mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
PDF draw widget (first part)
This commit is contained in:
@ -1,12 +1,41 @@
|
||||
// Copyright (C) 2019 Jakub Melka
|
||||
//
|
||||
// This file is part of PdfForQt.
|
||||
//
|
||||
// PdfForQt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// PdfForQt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include "pdfviewermainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QCommandLineParser>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
pdfviewer::PDFViewerMainWindow w;
|
||||
w.show();
|
||||
QApplication application(argc, argv);
|
||||
|
||||
return a.exec();
|
||||
QCoreApplication::setOrganizationName("MelkaJ");
|
||||
QCoreApplication::setApplicationName("PDF Viewer");
|
||||
QCoreApplication::setApplicationVersion("0.1");
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription(QCoreApplication::applicationName());
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
parser.addPositionalArgument("file", "The PDF file to open.");
|
||||
parser.process(application);
|
||||
|
||||
pdfviewer::PDFViewerMainWindow mainWindow;
|
||||
mainWindow.show();
|
||||
|
||||
return application.exec();
|
||||
}
|
||||
|
@ -1,23 +1,60 @@
|
||||
// Copyright (C) 2019 Jakub Melka
|
||||
//
|
||||
// This file is part of PdfForQt.
|
||||
//
|
||||
// PdfForQt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// PdfForQt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include "pdfviewermainwindow.h"
|
||||
#include "ui_pdfviewermainwindow.h"
|
||||
|
||||
#include "pdfdocumentreader.h"
|
||||
#include "pdfvisitor.h"
|
||||
#include "pdfstreamfilters.h"
|
||||
#include "pdfdrawwidget.h"
|
||||
|
||||
#include <QSettings>
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <QCloseEvent>
|
||||
#include <QApplication>
|
||||
#include <QDesktopWidget>
|
||||
#include <QStandardPaths>
|
||||
|
||||
namespace pdfviewer
|
||||
{
|
||||
|
||||
PDFViewerMainWindow::PDFViewerMainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::PDFViewerMainWindow)
|
||||
ui(new Ui::PDFViewerMainWindow),
|
||||
m_pdfWidget(nullptr)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
// Initialize shortcuts
|
||||
ui->actionOpen->setShortcut(QKeySequence::Open);
|
||||
ui->actionClose->setShortcut(QKeySequence::Close);
|
||||
ui->actionQuit->setShortcut(QKeySequence::Quit);
|
||||
|
||||
connect(ui->actionOpen, &QAction::triggered, this, &PDFViewerMainWindow::onActionOpenTriggered);
|
||||
connect(ui->actionClose, &QAction::triggered, this, &PDFViewerMainWindow::onActionCloseTriggered);
|
||||
connect(ui->actionQuit, &QAction::triggered, this, &PDFViewerMainWindow::onActionQuitTriggered);
|
||||
|
||||
m_pdfWidget = new pdf::PDFWidget(this);
|
||||
setCentralWidget(m_pdfWidget);
|
||||
setFocusProxy(m_pdfWidget);
|
||||
|
||||
readSettings();
|
||||
}
|
||||
|
||||
PDFViewerMainWindow::~PDFViewerMainWindow()
|
||||
@ -27,35 +64,120 @@ PDFViewerMainWindow::~PDFViewerMainWindow()
|
||||
|
||||
void PDFViewerMainWindow::onActionOpenTriggered()
|
||||
{
|
||||
QString fileName = QFileDialog::getOpenFileName(this, tr("Select PDF document"), "K:/Programming/PDF/testpdf", tr("PDF document (*.pdf)"));
|
||||
QString fileName = QFileDialog::getOpenFileName(this, tr("Select PDF document"), m_directory, tr("PDF document (*.pdf)"));
|
||||
if (!fileName.isEmpty())
|
||||
{
|
||||
pdf::PDFDocumentReader reader;
|
||||
pdf::PDFDocument document = reader.readFromFile(fileName);
|
||||
pdf::PDFStatisticsCollector collector;
|
||||
pdf::PDFApplyVisitor(document, &collector);
|
||||
|
||||
if (reader.isSuccessfull())
|
||||
{
|
||||
QMessageBox::information(this, tr("PDF Reader"), tr("Document '%1' was successfully loaded!").arg(fileName));
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::information(this, tr("PDF Reader"), tr("Document read error: %1").arg(reader.getErrorMessage()));
|
||||
}
|
||||
|
||||
const pdf::PDFCatalog* catalog = document.getCatalog();
|
||||
const pdf::PDFPage* page = catalog->getPage(0);
|
||||
const pdf::PDFObject& contents = page->getContents();
|
||||
|
||||
if (contents.isStream())
|
||||
{
|
||||
const pdf::PDFStream* stream = contents.getStream();
|
||||
const QByteArray* compressed = stream->getContent();
|
||||
pdf::PDFFlateDecodeFilter fd;
|
||||
QByteArray uncompressed = fd.apply(*compressed, &document, pdf::PDFObject());
|
||||
}
|
||||
openDocument(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
void PDFViewerMainWindow::onActionCloseTriggered()
|
||||
{
|
||||
closeDocument();
|
||||
}
|
||||
|
||||
void PDFViewerMainWindow::onActionQuitTriggered()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
void PDFViewerMainWindow::readSettings()
|
||||
{
|
||||
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
|
||||
|
||||
QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
|
||||
if (geometry.isEmpty())
|
||||
{
|
||||
QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
|
||||
QRect windowRect(0, 0, availableGeometry.width() / 2, availableGeometry.height() / 2);
|
||||
windowRect = windowRect.translated(availableGeometry.center() - windowRect.center());
|
||||
setGeometry(windowRect);
|
||||
}
|
||||
else
|
||||
{
|
||||
restoreGeometry(geometry);
|
||||
}
|
||||
|
||||
QByteArray state = settings.value("windowState", QByteArray()).toByteArray();
|
||||
if (!state.isEmpty())
|
||||
{
|
||||
restoreState(state);
|
||||
}
|
||||
|
||||
m_directory = settings.value("defaultDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
|
||||
}
|
||||
|
||||
void PDFViewerMainWindow::writeSettings()
|
||||
{
|
||||
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
|
||||
settings.setValue("geometry", saveGeometry());
|
||||
settings.setValue("windowState", saveState());
|
||||
settings.setValue("defaultDirectory", m_directory);
|
||||
}
|
||||
|
||||
void PDFViewerMainWindow::updateTitle()
|
||||
{
|
||||
if (m_pdfDocument)
|
||||
{
|
||||
QString title = m_pdfDocument->getInfo()->title;
|
||||
if (title.isEmpty())
|
||||
{
|
||||
title = m_currentFile;
|
||||
}
|
||||
setWindowTitle(tr("%1 - PDF Viewer").arg(m_currentFile));
|
||||
}
|
||||
else
|
||||
{
|
||||
setWindowTitle(tr("PDF Viewer"));
|
||||
}
|
||||
}
|
||||
|
||||
void PDFViewerMainWindow::openDocument(const QString& fileName)
|
||||
{
|
||||
// First close old document
|
||||
closeDocument();
|
||||
|
||||
// Try to open a new document
|
||||
QApplication::setOverrideCursor(Qt::WaitCursor);
|
||||
pdf::PDFDocumentReader reader;
|
||||
pdf::PDFDocument document = reader.readFromFile(fileName);
|
||||
QApplication::restoreOverrideCursor();
|
||||
|
||||
if (reader.isSuccessfull())
|
||||
{
|
||||
// Mark current directory as this
|
||||
QFileInfo fileInfo(fileName);
|
||||
m_directory = fileInfo.dir().absolutePath();
|
||||
m_currentFile = fileInfo.fileName();
|
||||
|
||||
m_pdfDocument.reset(new pdf::PDFDocument(std::move(document)));
|
||||
setDocument(m_pdfDocument.data());
|
||||
|
||||
statusBar()->showMessage(tr("Document '%1' was successfully loaded!").arg(fileName), 4000);
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::critical(this, tr("PDF Viewer"), tr("Document read error: %1").arg(reader.getErrorMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
void PDFViewerMainWindow::setDocument(const pdf::PDFDocument* document)
|
||||
{
|
||||
m_pdfWidget->setDocument(document);
|
||||
updateTitle();
|
||||
}
|
||||
|
||||
void PDFViewerMainWindow::closeDocument()
|
||||
{
|
||||
setDocument(nullptr);
|
||||
m_pdfDocument.reset();
|
||||
}
|
||||
|
||||
void PDFViewerMainWindow::closeEvent(QCloseEvent* event)
|
||||
{
|
||||
writeSettings();
|
||||
closeDocument();
|
||||
event->accept();
|
||||
}
|
||||
|
||||
} // namespace pdfviewer
|
||||
|
@ -1,13 +1,37 @@
|
||||
// Copyright (C) 2019 Jakub Melka
|
||||
//
|
||||
// This file is part of PdfForQt.
|
||||
//
|
||||
// PdfForQt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// PdfForQt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef PDFVIEWERMAINWINDOW_H
|
||||
#define PDFVIEWERMAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QSharedPointer>
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class PDFViewerMainWindow;
|
||||
}
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
class PDFWidget;
|
||||
class PDFDocument;
|
||||
}
|
||||
|
||||
namespace pdfviewer
|
||||
{
|
||||
|
||||
@ -19,10 +43,27 @@ public:
|
||||
explicit PDFViewerMainWindow(QWidget *parent = nullptr);
|
||||
virtual ~PDFViewerMainWindow() override;
|
||||
|
||||
virtual void closeEvent(QCloseEvent* event) override;
|
||||
|
||||
private:
|
||||
void onActionOpenTriggered();
|
||||
void onActionCloseTriggered();
|
||||
void onActionQuitTriggered();
|
||||
|
||||
void readSettings();
|
||||
void writeSettings();
|
||||
|
||||
void updateTitle();
|
||||
|
||||
void openDocument(const QString& fileName);
|
||||
void setDocument(const pdf::PDFDocument* document);
|
||||
void closeDocument();
|
||||
|
||||
Ui::PDFViewerMainWindow* ui;
|
||||
pdf::PDFWidget* m_pdfWidget;
|
||||
QSharedPointer<pdf::PDFDocument> m_pdfDocument;
|
||||
QString m_directory;
|
||||
QString m_currentFile;
|
||||
};
|
||||
|
||||
} // namespace pdfviewer
|
||||
|
@ -6,12 +6,12 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
<width>431</width>
|
||||
<height>333</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>PDFViewerMainWindow</string>
|
||||
<string>PDF Viewer</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralWidget"/>
|
||||
<widget class="QMenuBar" name="menuBar">
|
||||
@ -19,7 +19,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<width>431</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -28,6 +28,8 @@
|
||||
<string>File</string>
|
||||
</property>
|
||||
<addaction name="actionOpen"/>
|
||||
<addaction name="actionClose"/>
|
||||
<addaction name="actionQuit"/>
|
||||
</widget>
|
||||
<addaction name="menuFile"/>
|
||||
</widget>
|
||||
@ -44,8 +46,15 @@
|
||||
<property name="text">
|
||||
<string>Open</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+O</string>
|
||||
</action>
|
||||
<action name="actionClose">
|
||||
<property name="text">
|
||||
<string>Close</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionQuit">
|
||||
<property name="text">
|
||||
<string>Quit</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
|
Reference in New Issue
Block a user