From 6f0b72ba3bdfe1c6937a482317eae0d5c4381a87 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Wed, 30 Dec 2020 15:45:24 +0100 Subject: [PATCH] Redact bugfixing --- CodeGenerator/codegenerator.h | 3 +- Pdf4QtLib/sources/pdfannotation.cpp | 38 ++++++++++- Pdf4QtLib/sources/pdfannotation.h | 3 + Pdf4QtLib/sources/pdfdocumentbuilder.cpp | 38 +++++++++++ Pdf4QtLib/sources/pdfdocumentbuilder.h | 8 +++ Pdf4QtLib/sources/pdfredact.cpp | 9 ++- .../RedactPlugin/redactplugin.cpp | 3 +- generated_code_definition.xml | 64 +++++++++++++++++++ 8 files changed, 161 insertions(+), 5 deletions(-) diff --git a/CodeGenerator/codegenerator.h b/CodeGenerator/codegenerator.h index 7a30643..ff91331 100644 --- a/CodeGenerator/codegenerator.h +++ b/CodeGenerator/codegenerator.h @@ -160,7 +160,8 @@ public: _AnnotationBorderStyle, _FileAttachmentIcon, _Stamp, - _PDFDestination + _PDFDestination, + _PageRotation }; Q_ENUM(DataType) diff --git a/Pdf4QtLib/sources/pdfannotation.cpp b/Pdf4QtLib/sources/pdfannotation.cpp index 4bfbeb5..a840891 100644 --- a/Pdf4QtLib/sources/pdfannotation.cpp +++ b/Pdf4QtLib/sources/pdfannotation.cpp @@ -1790,11 +1790,17 @@ void PDFWidgetAnnotationManager::mousePressEvent(QWidget* widget, QMouseEvent* e if (m_editableAnnotation.isValid()) { QMenu menu(tr("Annotation"), widget); + QAction* showPopupAction = menu.addAction(tr("Show Popup Window")); + QAction* copyAction = menu.addAction(tr("Copy to Multiple Pages")); QAction* editAction = menu.addAction(tr("Edit")); QAction* deleteAction = menu.addAction(tr("Delete")); + connect(showPopupAction, &QAction::triggered, this, &PDFWidgetAnnotationManager::onShowPopupAnnotation); + connect(copyAction, &QAction::triggered, this, &PDFWidgetAnnotationManager::onCopyAnnotation); connect(editAction, &QAction::triggered, this, &PDFWidgetAnnotationManager::onEditAnnotation); connect(deleteAction, &QAction::triggered, this, &PDFWidgetAnnotationManager::onDeleteAnnotation); - menu.exec(widget->mapToGlobal(event->pos())); + + m_editableAnnotationGlobalPosition = widget->mapToGlobal(event->pos()); + menu.exec(m_editableAnnotationGlobalPosition); } } } @@ -1977,6 +1983,36 @@ void PDFWidgetAnnotationManager::updateFromMouseEvent(QMouseEvent* event) } } +void PDFWidgetAnnotationManager::onShowPopupAnnotation() +{ + PDFWidgetSnapshot snapshot = m_proxy->getSnapshot(); + for (const PDFWidgetSnapshot::SnapshotItem& snapshotItem : snapshot.items) + { + PageAnnotations& pageAnnotations = getPageAnnotations(snapshotItem.pageIndex); + for (PageAnnotation& pageAnnotation : pageAnnotations.annotations) + { + if (pageAnnotation.annotation->isReplyTo()) + { + // Annotation is reply to another annotation, do not interact with it + continue; + } + + if (pageAnnotation.annotation->getSelfReference() == m_editableAnnotation) + { + QDialog* dialog = createDialogForMarkupAnnotations(m_proxy->getWidget(), pageAnnotation, pageAnnotations); + dialog->move(m_editableAnnotationGlobalPosition); + dialog->exec(); + return; + } + } + } +} + +void PDFWidgetAnnotationManager::onCopyAnnotation() +{ + +} + void PDFWidgetAnnotationManager::onEditAnnotation() { PDFEditObjectDialog dialog(EditObjectType::Annotation, m_proxy->getWidget()); diff --git a/Pdf4QtLib/sources/pdfannotation.h b/Pdf4QtLib/sources/pdfannotation.h index 6cad000..5d90d03 100644 --- a/Pdf4QtLib/sources/pdfannotation.h +++ b/Pdf4QtLib/sources/pdfannotation.h @@ -1661,6 +1661,8 @@ signals: private: void updateFromMouseEvent(QMouseEvent* event); + void onShowPopupAnnotation(); + void onCopyAnnotation(); void onEditAnnotation(); void onDeleteAnnotation(); @@ -1685,6 +1687,7 @@ private: PDFDrawWidgetProxy* m_proxy; QString m_tooltip; std::optional m_cursor; + QPoint m_editableAnnotationGlobalPosition; ///< Position, where action on annotation was executed PDFObjectReference m_editableAnnotation; ///< Annotation to be edited or deleted PDFObjectReference m_editableAnnotationPage; ///< Page of annotation above }; diff --git a/Pdf4QtLib/sources/pdfdocumentbuilder.cpp b/Pdf4QtLib/sources/pdfdocumentbuilder.cpp index 4249f5b..39a5ac4 100644 --- a/Pdf4QtLib/sources/pdfdocumentbuilder.cpp +++ b/Pdf4QtLib/sources/pdfdocumentbuilder.cpp @@ -462,6 +462,30 @@ PDFObjectFactory& PDFObjectFactory::operator<<(TextAnnotationIcon icon) return *this; } +PDFObjectFactory& PDFObjectFactory::operator<<(PageRotation pageRotation) +{ + switch (pageRotation) + { + case pdf::PageRotation::None: + *this << PDFInteger(0); + break; + case pdf::PageRotation::Rotate90: + *this << PDFInteger(90); + break; + case pdf::PageRotation::Rotate180: + *this << PDFInteger(180); + break; + case pdf::PageRotation::Rotate270: + *this << PDFInteger(270); + break; + + default: + Q_ASSERT(false); + } + + return *this; +} + PDFObjectFactory& PDFObjectFactory::operator<<(WrapEmptyArray) { beginArray(); @@ -4788,6 +4812,20 @@ void PDFDocumentBuilder::setPageMediaBox(PDFObjectReference page, } +void PDFDocumentBuilder::setPageRotation(PDFObjectReference page, + PageRotation rotation) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("Rotate"); + objectBuilder << rotation; + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject updatedPageObject = objectBuilder.takeObject(); + mergeTo(page, updatedPageObject); +} + void PDFDocumentBuilder::setPageTrimBox(PDFObjectReference page, QRectF box) { diff --git a/Pdf4QtLib/sources/pdfdocumentbuilder.h b/Pdf4QtLib/sources/pdfdocumentbuilder.h index f8565b8..ec60226 100644 --- a/Pdf4QtLib/sources/pdfdocumentbuilder.h +++ b/Pdf4QtLib/sources/pdfdocumentbuilder.h @@ -118,6 +118,7 @@ public: PDFObjectFactory& operator<<(Stamp stamp); PDFObjectFactory& operator<<(FileAttachmentIcon icon); PDFObjectFactory& operator<<(const PDFDestination& destination); + PDFObjectFactory& operator<<(PageRotation pageRotation); /// Treat containers - write them as array template()))> @@ -1421,6 +1422,13 @@ public: QRectF box); + /// Set page's rotation. + /// \param page Page + /// \param rotation Rotation + void setPageRotation(PDFObjectReference page, + PageRotation rotation); + + /// Sets trim box to the page. Trim box is physical region, of the printed page after trimming. /// \param page Page /// \param box Box diff --git a/Pdf4QtLib/sources/pdfredact.cpp b/Pdf4QtLib/sources/pdfredact.cpp index 6661c08..cde2ff1 100644 --- a/Pdf4QtLib/sources/pdfredact.cpp +++ b/Pdf4QtLib/sources/pdfredact.cpp @@ -18,6 +18,7 @@ #include "pdfredact.h" #include "pdfpainter.h" #include "pdfdocumentbuilder.h" +#include "pdfoptimizer.h" namespace pdf { @@ -75,8 +76,8 @@ PDFDocument PDFRedact::perform(Options options) { builder.setPageArtBox(newPageReference, page->getArtBox()); } + builder.setPageRotation(newPageReference, page->getPageRotation()); - // TODO: Nastavit natoceni stranky // TODO: Popisek redakce anotace, Overlay text // TODO: Redact searched text // TODO: Duplikace redakce na vice stranek @@ -133,7 +134,11 @@ PDFDocument PDFRedact::perform(Options options) builder.setOutline(m_document->getCatalog()->getOutlineRootPtr().data()); } - return builder.build(); + PDFDocument redactedDocument = builder.build(); + PDFOptimizer optimizer(PDFOptimizer::All, nullptr); + optimizer.setDocument(&redactedDocument); + optimizer.optimize(); + return optimizer.takeOptimizedDocument(); } diff --git a/Pdf4QtViewerPlugins/RedactPlugin/redactplugin.cpp b/Pdf4QtViewerPlugins/RedactPlugin/redactplugin.cpp index a491efc..088bcca 100644 --- a/Pdf4QtViewerPlugins/RedactPlugin/redactplugin.cpp +++ b/Pdf4QtViewerPlugins/RedactPlugin/redactplugin.cpp @@ -26,6 +26,7 @@ #include "pdfdocumentwriter.h" #include +#include namespace pdfplugin { @@ -148,7 +149,7 @@ void RedactPlugin::onCreateRedactedDocumentTriggered() pdf::PDFOperationResult result = writer.write(dialog.getFileName(), &redactedDocument, false); if (!result) { - + QMessageBox::critical(m_widget, tr("Error"), result.getErrorMessage()); } } } diff --git a/generated_code_definition.xml b/generated_code_definition.xml index 72f08d2..99dd492 100644 --- a/generated_code_definition.xml +++ b/generated_code_definition.xml @@ -11189,6 +11189,70 @@ return rootNodeReference; Sets media box to the page. The media box defines size of physical medium, onto which the page is to be printed. _void + + + + + + + + + + page + _PDFObjectReference + Page + + + + + rotation + _PageRotation + Rotation + + + Parameters + + _void + + + + + + + + + + + + Rotate + DictionaryItemSimple + rotation + + + + Dictionary + + + + CreateObject + updatedPageObject + _PDFObject + + + + + + Code + + _void + mergeTo(page, updatedPageObject); + + + Structure + setPageRotation + Set page's rotation. + _void +