// Copyright (C) 2021 Jakub Melka // // This file is part of Pdf4Qt. // // Pdf4Qt 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 // with the written consent of the copyright owner, any later version. // // Pdf4Qt 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 Pdf4Qt. If not, see . #include "mainwindow.h" #include "ui_mainwindow.h" #include "pdfwidgetutils.h" #include "pdfdocumentreader.h" #include #include #include #include namespace pdfdocpage { MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_model(new PageItemModel(this)), m_delegate(new PageItemDelegate(m_model, this)) { ui->setupUi(this); m_delegate->setPageImageSize(getDefaultPageImageSize()); ui->documentItemsView->setModel(m_model); ui->documentItemsView->setItemDelegate(m_delegate); setMinimumSize(pdf::PDFWidgetUtils::scaleDPI(this, QSize(800, 600))); loadSettings(); } MainWindow::~MainWindow() { saveSettings(); delete ui; } QSize MainWindow::getMinPageImageSize() const { return pdf::PDFWidgetUtils::scaleDPI(this, QSize(40, 40)); } QSize MainWindow::getDefaultPageImageSize() const { return pdf::PDFWidgetUtils::scaleDPI(this, QSize(100, 100)); } QSize MainWindow::getMaxPageImageSize() const { return pdf::PDFWidgetUtils::scaleDPI(this, QSize(250, 250)); } void MainWindow::on_actionClose_triggered() { close(); } void MainWindow::on_actionAddDocument_triggered() { QString fileName = QFileDialog::getOpenFileName(this, tr("Select PDF document"), m_settings.directory, tr("PDF document (*.pdf)")); if (!fileName.isEmpty()) { addDocument(fileName); } } void MainWindow::updateActions() { } void MainWindow::loadSettings() { QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName()); settings.beginGroup("MainWindow"); 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); } settings.endGroup(); settings.beginGroup("Settings"); m_settings.directory = settings.value("directory").toString(); settings.endGroup(); } void MainWindow::saveSettings() { QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName()); settings.beginGroup("MainWindow"); settings.setValue("geometry", saveGeometry()); settings.setValue("windowState", saveState()); settings.endGroup(); settings.beginGroup("Settings"); settings.setValue("directory", m_settings.directory); settings.endGroup(); } void MainWindow::addDocument(const QString& fileName) { auto queryPassword = [this](bool* ok) { *ok = false; return QInputDialog::getText(this, tr("Encrypted document"), tr("Enter password to access document content"), QLineEdit::Password, QString(), ok); }; // Mark current directory as this QFileInfo fileInfo(fileName); m_settings.directory = fileInfo.dir().absolutePath(); // Try to open a new document pdf::PDFDocumentReader reader(nullptr, qMove(queryPassword), true, false); pdf::PDFDocument document = reader.readFromFile(fileName); QString errorMessage = reader.getErrorMessage(); pdf::PDFDocumentReader::Result result = reader.getReadingResult(); if (result == pdf::PDFDocumentReader::Result::OK) { const pdf::PDFSecurityHandler* securityHandler = document.getStorage().getSecurityHandler(); if (securityHandler->isAllowed(pdf::PDFSecurityHandler::Permission::Assemble) || securityHandler->isAllowed(pdf::PDFSecurityHandler::Permission::Modify)) { m_model->addDocument(fileName, qMove(document)); } else { QMessageBox::critical(this, tr("Error"), tr("Document security doesn't permit to organize pages.")); } } else if (result == pdf::PDFDocumentReader::Result::Failed) { QMessageBox::critical(this, tr("Error"), errorMessage); } updateActions(); } } // namespace pdfdocpage