From 3d32db204649dbc6d55ae9e5f71448f71fe2f215 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Wed, 18 Oct 2023 20:11:43 +0200 Subject: [PATCH] Issue #100: Annotation rectangle --- Pdf4QtLib/sources/pdfadvancedtools.cpp | 56 ++++++++++++++++- Pdf4QtLib/sources/pdfadvancedtools.h | 6 +- Pdf4QtViewer/pdf4qtviewer.qrc | 1 + Pdf4QtViewer/pdfprogramcontroller.cpp | 2 + Pdf4QtViewer/pdfprogramcontroller.h | 1 + Pdf4QtViewer/pdfviewermainwindow.cpp | 1 + Pdf4QtViewer/pdfviewermainwindow.ui | 13 ++++ Pdf4QtViewer/resources/annot-rectangle.svg | 73 ++++++++++++++++++++++ 8 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 Pdf4QtViewer/resources/annot-rectangle.svg diff --git a/Pdf4QtLib/sources/pdfadvancedtools.cpp b/Pdf4QtLib/sources/pdfadvancedtools.cpp index 5d0864d..aa3c8fb 100644 --- a/Pdf4QtLib/sources/pdfadvancedtools.cpp +++ b/Pdf4QtLib/sources/pdfadvancedtools.cpp @@ -202,9 +202,12 @@ PDFCreateLineTypeTool::PDFCreateLineTypeTool(PDFDrawWidgetProxy* proxy, PDFToolM m_strokeColor(Qt::red), m_fillColor(Qt::yellow) { - m_pickTool = new PDFPickTool(proxy, PDFPickTool::Mode::Points, this); + PDFPickTool::Mode mode = (type != Type::Rectangle) ? PDFPickTool::Mode::Points : PDFPickTool::Mode::Rectangles; + m_pickTool = new PDFPickTool(proxy, mode, this); addTool(m_pickTool); connect(m_pickTool, &PDFPickTool::pointPicked, this, &PDFCreateLineTypeTool::onPointPicked); + connect(m_pickTool, &PDFPickTool::rectanglePicked, this, &PDFCreateLineTypeTool::onRectanglePicked); + m_pickTool->setDrawSelectionRectangle(false); m_fillColor.setAlphaF(0.2f); @@ -222,6 +225,14 @@ void PDFCreateLineTypeTool::onPointPicked(PDFInteger pageIndex, QPointF pagePoin } } +void PDFCreateLineTypeTool::onRectanglePicked(PDFInteger pageIndex, QRectF pageRectangle) +{ + m_rectPageIndex = pageIndex; + m_rectOnPage = pageRectangle; + + finishDefinition(); +} + void PDFCreateLineTypeTool::finishDefinition() { const std::vector& pickedPoints = m_pickTool->getPickedPoints(); @@ -309,6 +320,36 @@ void PDFCreateLineTypeTool::finishDefinition() break; } + case Type::Rectangle: + { + if (!m_rectOnPage.isEmpty()) + { + PDFDocumentModifier modifier(getDocument()); + + QPolygonF polygon; + polygon << m_rectOnPage.topLeft(); + polygon << m_rectOnPage.topRight(); + polygon << m_rectOnPage.bottomRight(); + polygon << m_rectOnPage.bottomLeft(); + polygon << m_rectOnPage.topLeft(); + + QString userName = PDFSysUtils::getUserName(); + PDFObjectReference page = getDocument()->getCatalog()->getPage(m_pickTool->getPageIndex())->getPageReference(); + PDFObjectReference annotation = modifier.getBuilder()->createAnnotationPolygon(page, polygon, m_penWidth, m_fillColor, m_strokeColor, userName, QString(), QString()); + modifier.getBuilder()->setAnnotationFillOpacity(annotation, m_fillColor.alphaF()); + modifier.getBuilder()->updateAnnotationAppearanceStreams(annotation); + modifier.markAnnotationsChanged(); + + if (modifier.finalize()) + { + Q_EMIT m_toolManager->documentModified(PDFModifiedDocument(modifier.getDocument(), nullptr, modifier.getFlags())); + } + + setActive(false); + } + break; + } + default: Q_ASSERT(false); break; @@ -476,6 +517,19 @@ void PDFCreateLineTypeTool::drawPage(QPainter* painter, break; } + case Type::Rectangle: + { + QPointF startPoint = points.front(); + qreal x1 = qMin(startPoint.x(), mousePoint.x()); + qreal y1 = qMin(startPoint.y(), mousePoint.y()); + qreal x2 = qMax(startPoint.x(), mousePoint.x()); + qreal y2 = qMax(startPoint.y(), mousePoint.y()); + + QRectF rect(x1, y1, x2 - x1, y2 - y1); + painter->drawRect(rect); + break; + } + default: Q_ASSERT(false); break; diff --git a/Pdf4QtLib/sources/pdfadvancedtools.h b/Pdf4QtLib/sources/pdfadvancedtools.h index 90d9d63..5e60824 100644 --- a/Pdf4QtLib/sources/pdfadvancedtools.h +++ b/Pdf4QtLib/sources/pdfadvancedtools.h @@ -121,7 +121,8 @@ public: { Line, PolyLine, - Polygon + Polygon, + Rectangle }; explicit PDFCreateLineTypeTool(PDFDrawWidgetProxy* proxy, PDFToolManager* toolManager, Type type, QAction* action, QObject* parent); @@ -145,6 +146,7 @@ public: private: void onPointPicked(PDFInteger pageIndex, QPointF pagePoint); + void onRectanglePicked(pdf::PDFInteger pageIndex, QRectF pageRectangle); void finishDefinition(); PDFToolManager* m_toolManager; @@ -153,6 +155,8 @@ private: PDFReal m_penWidth; QColor m_strokeColor; QColor m_fillColor; + PDFInteger m_rectPageIndex = 0; + QRectF m_rectOnPage; }; /// Tool that creates ellipse annotation. diff --git a/Pdf4QtViewer/pdf4qtviewer.qrc b/Pdf4QtViewer/pdf4qtviewer.qrc index 997d02c..d37671e 100644 --- a/Pdf4QtViewer/pdf4qtviewer.qrc +++ b/Pdf4QtViewer/pdf4qtviewer.qrc @@ -98,5 +98,6 @@ resources/sidebar-thumbnails.png resources/sidebar-visibility.png resources/sidebar-attachment.png + resources/annot-rectangle.svg diff --git a/Pdf4QtViewer/pdfprogramcontroller.cpp b/Pdf4QtViewer/pdfprogramcontroller.cpp index 03ffd05..c24da2b 100644 --- a/Pdf4QtViewer/pdfprogramcontroller.cpp +++ b/Pdf4QtViewer/pdfprogramcontroller.cpp @@ -982,6 +982,8 @@ void PDFProgramController::initializeToolManager() m_toolManager->addTool(createStraightLineTool); pdf::PDFCreateLineTypeTool* createPolylineTool = new pdf::PDFCreateLineTypeTool(m_pdfWidget->getDrawWidgetProxy(), m_toolManager, pdf::PDFCreateLineTypeTool::Type::PolyLine, m_actionManager->getAction(PDFActionManager::CreatePolyline), this); m_toolManager->addTool(createPolylineTool); + pdf::PDFCreateLineTypeTool* createRectangleTool = new pdf::PDFCreateLineTypeTool(m_pdfWidget->getDrawWidgetProxy(), m_toolManager, pdf::PDFCreateLineTypeTool::Type::Rectangle, m_actionManager->getAction(PDFActionManager::CreateRectangle), this); + m_toolManager->addTool(createRectangleTool); pdf::PDFCreateLineTypeTool* createPolygonTool = new pdf::PDFCreateLineTypeTool(m_pdfWidget->getDrawWidgetProxy(), m_toolManager, pdf::PDFCreateLineTypeTool::Type::Polygon, m_actionManager->getAction(PDFActionManager::CreatePolygon), this); m_toolManager->addTool(createPolygonTool); pdf::PDFCreateEllipseTool* createEllipseTool = new pdf::PDFCreateEllipseTool(m_pdfWidget->getDrawWidgetProxy(), m_toolManager, m_actionManager->getAction(PDFActionManager::CreateEllipse), this); diff --git a/Pdf4QtViewer/pdfprogramcontroller.h b/Pdf4QtViewer/pdfprogramcontroller.h index ba051be..d02e997 100644 --- a/Pdf4QtViewer/pdfprogramcontroller.h +++ b/Pdf4QtViewer/pdfprogramcontroller.h @@ -134,6 +134,7 @@ public: CreateInlineText, CreateStraightLine, CreatePolyline, + CreateRectangle, CreatePolygon, CreateEllipse, CreateFreehandCurve, diff --git a/Pdf4QtViewer/pdfviewermainwindow.cpp b/Pdf4QtViewer/pdfviewermainwindow.cpp index 5a242db..c87e606 100644 --- a/Pdf4QtViewer/pdfviewermainwindow.cpp +++ b/Pdf4QtViewer/pdfviewermainwindow.cpp @@ -141,6 +141,7 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) : m_actionManager->setAction(PDFActionManager::CreateInlineText, ui->actionInlineText); m_actionManager->setAction(PDFActionManager::CreateStraightLine, ui->actionCreateStraightLine); m_actionManager->setAction(PDFActionManager::CreatePolyline, ui->actionCreatePolyline); + m_actionManager->setAction(PDFActionManager::CreateRectangle, ui->actionCreateRectangle); m_actionManager->setAction(PDFActionManager::CreatePolygon, ui->actionCreatePolygon); m_actionManager->setAction(PDFActionManager::CreateEllipse, ui->actionCreateEllipse); m_actionManager->setAction(PDFActionManager::CreateFreehandCurve, ui->actionCreateFreehandCurve); diff --git a/Pdf4QtViewer/pdfviewermainwindow.ui b/Pdf4QtViewer/pdfviewermainwindow.ui index b2f0028..21e9af7 100644 --- a/Pdf4QtViewer/pdfviewermainwindow.ui +++ b/Pdf4QtViewer/pdfviewermainwindow.ui @@ -181,6 +181,7 @@ + @@ -763,6 +764,18 @@ Polyline + + + true + + + + :/resources/annot-rectangle.svg:/resources/annot-rectangle.svg + + + Rectangle + + true diff --git a/Pdf4QtViewer/resources/annot-rectangle.svg b/Pdf4QtViewer/resources/annot-rectangle.svg new file mode 100644 index 0000000..e39697c --- /dev/null +++ b/Pdf4QtViewer/resources/annot-rectangle.svg @@ -0,0 +1,73 @@ + + + +image/svg+xml + + + + + + + + + + \ No newline at end of file