Redacting tool

This commit is contained in:
Jakub Melka
2020-12-29 18:33:25 +01:00
parent 0c6903d5b8
commit 59721d7de8
18 changed files with 735 additions and 4 deletions

View File

@ -33,10 +33,12 @@ DESTDIR = $$OUT_PWD/../../pdfplugins
CONFIG += c++11
SOURCES += \
createredacteddocumentdialog.cpp \
redactplugin.cpp \
selectpagestoredactdialog.cpp
HEADERS += \
createredacteddocumentdialog.h \
redactplugin.h \
selectpagestoredactdialog.h
@ -49,6 +51,7 @@ RESOURCES += \
icons.qrc
FORMS += \
createredacteddocumentdialog.ui \
selectpagestoredactdialog.ui

View File

@ -0,0 +1,122 @@
// Copyright (C) 2020 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
// (at your option) 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 <https://www.gnu.org/licenses/>.
#include "createredacteddocumentdialog.h"
#include "ui_createredacteddocumentdialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include "pdfwidgetutils.h"
namespace pdfplugin
{
CreateRedactedDocumentDialog::CreateRedactedDocumentDialog(QString fileName, QColor fillColor, QWidget* parent) :
QDialog(parent),
ui(new Ui::CreateRedactedDocumentDialog)
{
ui->setupUi(this);
ui->fileNameEdit->setText(fileName);
ui->fillRedactedAreaColorEdit->setText(fillColor.name(QColor::HexRgb));
connect(ui->copyMetadataCheckBox, &QCheckBox::clicked, this, &CreateRedactedDocumentDialog::updateUi);
updateUi();
setMinimumWidth(pdf::PDFWidgetUtils::scaleDPI_x(this, 300));
}
CreateRedactedDocumentDialog::~CreateRedactedDocumentDialog()
{
delete ui;
}
QString CreateRedactedDocumentDialog::getFileName() const
{
return ui->fileNameEdit->text();
}
QColor CreateRedactedDocumentDialog::getRedactColor() const
{
QColor color;
if (ui->fillRedactedAreaCheckBox->isChecked())
{
color.setNamedColor(ui->fillRedactedAreaColorEdit->text());
}
return color;
}
bool CreateRedactedDocumentDialog::isCopyingTitle() const
{
return ui->copyTitleCheckBox->isChecked();
}
bool CreateRedactedDocumentDialog::isCopyingMetadata() const
{
return ui->copyMetadataCheckBox->isChecked();
}
bool CreateRedactedDocumentDialog::isCopyingOutline() const
{
return ui->copyOutlineCheckBox->isChecked();
}
void CreateRedactedDocumentDialog::on_selectDirectoryButton_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("File Name"), ui->fileNameEdit->text());
if (!fileName.isEmpty())
{
ui->fileNameEdit->setText(fileName);
}
}
void CreateRedactedDocumentDialog::updateUi()
{
if (ui->copyMetadataCheckBox->isChecked())
{
ui->copyTitleCheckBox->setChecked(true);
ui->copyTitleCheckBox->setEnabled(false);
}
else
{
ui->copyTitleCheckBox->setEnabled(true);
}
ui->fillRedactedAreaColorEdit->setEnabled(ui->fillRedactedAreaCheckBox->isChecked());
}
void CreateRedactedDocumentDialog::accept()
{
if (ui->fillRedactedAreaCheckBox->isChecked())
{
QColor color;
color.setNamedColor(ui->fillRedactedAreaColorEdit->text());
if (!color.isValid())
{
QMessageBox::critical(this, tr("Error"), tr("Cannot convert '%1' to color value.").arg(ui->fillRedactedAreaColorEdit->text()));
return;
}
}
QDialog::accept();
}
} // namespace pdfplugin

View File

@ -0,0 +1,59 @@
// Copyright (C) 2020 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
// (at your option) 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 <https://www.gnu.org/licenses/>.
#ifndef CREATEREDACTEDDOCUMENTDIALOG_H
#define CREATEREDACTEDDOCUMENTDIALOG_H
#include <QDialog>
namespace Ui
{
class CreateRedactedDocumentDialog;
}
namespace pdfplugin
{
class CreateRedactedDocumentDialog : public QDialog
{
Q_OBJECT
public:
explicit CreateRedactedDocumentDialog(QString fileName, QColor fillColor, QWidget* parent);
virtual ~CreateRedactedDocumentDialog() override;
virtual void accept() override;
QString getFileName() const;
QColor getRedactColor() const;
bool isCopyingTitle() const;
bool isCopyingMetadata() const;
bool isCopyingOutline() const;
private slots:
void on_selectDirectoryButton_clicked();
private:
void updateUi();
Ui::CreateRedactedDocumentDialog* ui;
};
} // namespace pdfplugin
#endif // CREATEREDACTEDDOCUMENTDIALOG_H

View File

@ -0,0 +1,164 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CreateRedactedDocumentDialog</class>
<widget class="QDialog" name="CreateRedactedDocumentDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>631</width>
<height>329</height>
</rect>
</property>
<property name="windowTitle">
<string>Create Redacted Document</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="redactedDocumentGroupBox">
<property name="title">
<string>Redacted document</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="redactedDocumentLabel">
<property name="text">
<string>Output file name</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="fileNameEdit"/>
</item>
<item>
<widget class="QToolButton" name="selectDirectoryButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="appearanceGroupBox">
<property name="title">
<string>Appearance</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QCheckBox" name="fillRedactedAreaCheckBox">
<property name="text">
<string>Fill redacted area with color:</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="fillRedactedAreaColorEdit"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="optionsGroupBox">
<property name="title">
<string>Options</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="copyTitleCheckBox">
<property name="text">
<string>Copy document title into redacted document</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="copyMetadataCheckBox">
<property name="text">
<string>Copy document metadata into redacted document</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="copyOutlineCheckBox">
<property name="text">
<string>Copy outline into redacted document</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>26</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>CreateRedactedDocumentDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>CreateRedactedDocumentDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -1,4 +1,4 @@
// Copyright (C) 2020 Jakub Melka
// Copyright (C) 2020 Jakub Melka
//
// This file is part of Pdf4Qt.
//
@ -17,10 +17,13 @@
#include "redactplugin.h"
#include "selectpagestoredactdialog.h"
#include "createredacteddocumentdialog.h"
#include "pdfdrawwidget.h"
#include "pdfadvancedtools.h"
#include "pdfdocumentbuilder.h"
#include "pdfredact.h"
#include "pdfdocumentwriter.h"
#include <QAction>
@ -124,7 +127,37 @@ void RedactPlugin::onRedactPageTriggered()
void RedactPlugin::onCreateRedactedDocumentTriggered()
{
CreateRedactedDocumentDialog dialog(getRedactedFileName(), Qt::black, m_widget);
if (dialog.exec() == QDialog::Accepted)
{
pdf::PDFCMSPointer cms = m_widget->getCMSManager()->getCurrentCMS();
pdf::PDFRedact redactProcessor(m_document,
m_widget->getDrawWidgetProxy()->getFontCache(),
cms.data(),
m_widget->getDrawWidgetProxy()->getOptionalContentActivity(),
&m_widget->getDrawWidgetProxy()->getMeshQualitySettings(),
dialog.getRedactColor());
pdf::PDFRedact::Options options;
options.setFlag(pdf::PDFRedact::CopyTitle, dialog.isCopyingTitle());
options.setFlag(pdf::PDFRedact::CopyMetadata, dialog.isCopyingMetadata());
options.setFlag(pdf::PDFRedact::CopyOutline, dialog.isCopyingOutline());
pdf::PDFDocument redactedDocument = redactProcessor.perform(options);
pdf::PDFDocumentWriter writer(m_widget->getDrawWidgetProxy()->getProgress());
pdf::PDFOperationResult result = writer.write(dialog.getFileName(), &redactedDocument, false);
if (!result)
{
}
}
}
QString RedactPlugin::getRedactedFileName() const
{
QFileInfo fileInfo(m_dataExchangeInterface->getOriginalFileName());
return fileInfo.path() + "/" + fileInfo.baseName() + "_REDACTED.pdf";
}
}

View File

@ -46,6 +46,8 @@ private:
void onRedactPageTriggered();
void onCreateRedactedDocumentTriggered();
QString getRedactedFileName() const;
QAction* m_actionRedactRectangle;
QAction* m_actionRedactText;
QAction* m_actionRedactPage;