Signature plugin: small refactoring

This commit is contained in:
Jakub Melka 2022-02-24 20:25:18 +01:00
parent f4e00f2f03
commit 0f4d8d41d7
8 changed files with 453 additions and 25 deletions

View File

@ -72,6 +72,7 @@ SOURCES += \
sources/pdfoptionalcontent.cpp \
sources/pdfoutline.cpp \
sources/pdfpagecontenteditortools.cpp \
sources/pdfpagecontenteditorwidget.cpp \
sources/pdfpagecontentelements.cpp \
sources/pdfpagenavigation.cpp \
sources/pdfpagetransition.cpp \
@ -149,6 +150,7 @@ HEADERS += \
sources/pdfoptionalcontent.h \
sources/pdfoutline.h \
sources/pdfpagecontenteditortools.h \
sources/pdfpagecontenteditorwidget.h \
sources/pdfpagecontentelements.h \
sources/pdfpagenavigation.h \
sources/pdfpagetransition.h \
@ -197,6 +199,7 @@ HEADERS += \
sources/pdfimage.h
FORMS += \
sources/pdfpagecontenteditorwidget.ui \
sources/pdfrenderingerrorswidget.ui \
sources/pdfselectpagesdialog.ui

View File

@ -0,0 +1,36 @@
// Copyright (C) 2022 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 <https://www.gnu.org/licenses/>.
#include "pdfpagecontenteditorwidget.h"
#include "ui_pdfpagecontenteditorwidget.h"
namespace pdf
{
PDFPageContentEditorWidget::PDFPageContentEditorWidget(QWidget *parent) :
QDockWidget(parent),
ui(new Ui::PDFPageContentEditorWidget)
{
ui->setupUi(this);
}
PDFPageContentEditorWidget::~PDFPageContentEditorWidget()
{
delete ui;
}
} // namespace pdf

View File

@ -0,0 +1,47 @@
// Copyright (C) 2022 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 <https://www.gnu.org/licenses/>.
#ifndef PDFPAGECONTENTEDITORWIDGET_H
#define PDFPAGECONTENTEDITORWIDGET_H
#include "pdfglobal.h"
#include <QDockWidget>
namespace Ui
{
class PDFPageContentEditorWidget;
}
namespace pdf
{
class PDF4QTLIBSHARED_EXPORT PDFPageContentEditorWidget : public QDockWidget
{
Q_OBJECT
public:
explicit PDFPageContentEditorWidget(QWidget* parent);
virtual ~PDFPageContentEditorWidget() override;
private:
Ui::PDFPageContentEditorWidget* ui;
};
} // namespace pdf
#endif // PDFPAGECONTENTEDITORWIDGET_H

View File

@ -0,0 +1,183 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PDFPageContentEditorWidget</class>
<widget class="QDockWidget" name="PDFPageContentEditorWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>333</width>
<height>607</height>
</rect>
</property>
<property name="allowedAreas">
<set>Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea</set>
</property>
<property name="windowTitle">
<string>Content editor</string>
</property>
<widget class="QWidget" name="dockWidgetContents">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="toolGroupBox">
<property name="title">
<string>Toolbox</string>
</property>
<layout class="QGridLayout" name="gridLayout_2"/>
</widget>
</item>
<item>
<widget class="QGroupBox" name="adjustBox">
<property name="title">
<string>Geometry Tools</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QToolButton" name="alignVertTopButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="alignVertMiddleButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QToolButton" name="alignVertBottomButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QToolButton" name="setSameWidthButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="4">
<widget class="QToolButton" name="setSameHeightButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="5">
<widget class="QToolButton" name="setSameSizeButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QToolButton" name="alignHorLeftButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QToolButton" name="alignHorMiddleButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QToolButton" name="alignHorRightButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QToolButton" name="centerHorizontallyButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="4">
<widget class="QToolButton" name="centerVerticallyButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="5">
<widget class="QToolButton" name="centerRectButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="layoutGroupBox">
<property name="title">
<string>Layout Tools</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QToolButton" name="layoutHorizontallyButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="layoutVerticallyButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="layoutFormButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="layoutGridButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="appearanceGroupBox">
<property name="title">
<string>Appearance</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="contentGroupBox">
<property name="title">
<string>Content</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QListView" name="listView"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -122,7 +122,8 @@ void PDFPageContentElementRectangle::drawPage(QPainter* painter,
}
PDFPageContentScene::PDFPageContentScene(QObject* parent) :
QObject(parent)
QObject(parent),
m_isActive(false)
{
}
@ -217,6 +218,11 @@ void PDFPageContentScene::drawPage(QPainter* painter,
const QMatrix& pagePointToDevicePointMatrix,
QList<PDFRenderError>& errors) const
{
if (!m_isActive)
{
return;
}
for (const auto& element : m_elements)
{
if (element->getPageIndex() != pageIndex)
@ -228,6 +234,20 @@ void PDFPageContentScene::drawPage(QPainter* painter,
}
}
bool PDFPageContentScene::isActive() const
{
return m_isActive;
}
void PDFPageContentScene::setActive(bool newIsActive)
{
if (m_isActive != newIsActive)
{
m_isActive = newIsActive;
emit sceneChanged();
}
}
PDFPageContentElementLine* PDFPageContentElementLine::clone() const
{
PDFPageContentElementLine* copy = new PDFPageContentElementLine();
@ -330,7 +350,6 @@ void PDFPageContentSvgElement::drawPage(QPainter* painter,
return;
}
PDFPainterStateGuard guard(painter);
painter->setWorldMatrix(pagePointToDevicePointMatrix, true);
painter->setRenderHint(QPainter::Antialiasing);
@ -378,4 +397,46 @@ void PDFPageContentSvgElement::setRectangle(const QRectF& newRectangle)
m_rectangle = newRectangle;
}
PDFPageContentElementDot* PDFPageContentElementDot::clone() const
{
PDFPageContentElementDot* copy = new PDFPageContentElementDot();
copy->setPageIndex(getPageIndex());
copy->setPen(getPen());
copy->setBrush(getBrush());
copy->setPoint(getPoint());
return copy;
}
void PDFPageContentElementDot::drawPage(QPainter* painter,
PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix,
QList<PDFRenderError>& errors) const
{
Q_UNUSED(compiledPage);
Q_UNUSED(layoutGetter);
Q_UNUSED(errors);
if (pageIndex != getPageIndex())
{
return;
}
PDFPainterStateGuard guard(painter);
painter->setWorldMatrix(pagePointToDevicePointMatrix, true);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawPoint(m_point);
}
QPointF PDFPageContentElementDot::getPoint() const
{
return m_point;
}
void PDFPageContentElementDot::setPoint(QPointF newPoint)
{
m_point = newPoint;
}
} // namespace pdf

View File

@ -125,6 +125,28 @@ private:
QLineF m_line;
};
class PDF4QTLIBSHARED_EXPORT PDFPageContentElementDot : public PDFPageContentStyledElement
{
public:
virtual ~PDFPageContentElementDot() = default;
virtual PDFPageContentElementDot* clone() const override;
virtual void drawPage(QPainter* painter,
PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix,
QList<PDFRenderError>& errors) const override;
QPointF getPoint() const;
void setPoint(QPointF newPoint);
private:
QPointF m_point;
};
class PDF4QTLIBSHARED_EXPORT PDFPageContentSvgElement : public PDFPageContentElement
{
public:
@ -196,11 +218,15 @@ public:
const QMatrix& pagePointToDevicePointMatrix,
QList<PDFRenderError>& errors) const override;
bool isActive() const;
void setActive(bool newIsActive);
signals:
/// This signal is emitted when scene has changed (including graphics)
void sceneChanged();
private:
bool m_isActive;
std::vector<std::unique_ptr<PDFPageContentElement>> m_elements;
std::optional<QCursor> m_cursor;
};

View File

@ -38,7 +38,9 @@ void SignaturePlugin::setWidget(pdf::PDFWidget* widget)
BaseClass::setWidget(widget);
QAction* activateAction = new QAction(QIcon(":/pdfplugins/signaturetool/activate.svg"), tr("Activate signature creator"), this);
QAction* createTextAction = new QAction(QIcon(":/pdfplugins/signaturetool/create-text.svg"), tr("Create Text Label"), this);
QAction* createFreehandCurveAction = new QAction(QIcon(":/pdfplugins/signaturetool/create-freehand-curve.svg"), tr("Create Freehand Curve"), this);
QAction* createAcceptMarkAction = new QAction(QIcon(":/pdfplugins/signaturetool/create-yes-mark.svg"), tr("Create Accept Mark"), this);
QAction* createRejectMarkAction = new QAction(QIcon(":/pdfplugins/signaturetool/create-no-mark.svg"), tr("Create Reject Mark"), this);
QAction* createRectangleAction = new QAction(QIcon(":/pdfplugins/signaturetool/create-rectangle.svg"), tr("Create Rectangle"), this);
@ -49,14 +51,13 @@ void SignaturePlugin::setWidget(pdf::PDFWidget* widget)
QAction* createDotAction = new QAction(QIcon(":/pdfplugins/signaturetool/create-dot.svg"), tr("Create Dot"), this);
QAction* createSvgImageAction = new QAction(QIcon(":/pdfplugins/signaturetool/create-svg-image.svg"), tr("Create SVG Image"), this);
QAction* clearAction = new QAction(QIcon(":/pdfplugins/signaturetool/clear.svg"), tr("Clear All Graphics"), this);
QAction* setColorAction = new QAction(QIcon(":/pdfplugins/signaturetool/set-color.svg"), tr("Set Color"), this);
QAction* setPenAction = new QAction(QIcon(":/pdfplugins/signaturetool/set-pen.svg"), tr("Set Pen"), this);
QAction* setBrushAction = new QAction(QIcon(":/pdfplugins/signaturetool/set-brush.svg"), tr("Set Brush"), this);
QAction* signElectronicallyAction = new QAction(QIcon(":/pdfplugins/signaturetool/sign-electronically.svg"), tr("Sign Electronically"), this);
QAction* signDigitallyAction = new QAction(QIcon(":/pdfplugins/signaturetool/sign-digitally.svg"), tr("Sign Digitally With Certificate"), this);
QAction* certificatesAction = new QAction(QIcon(":/pdfplugins/signaturetool/certificates.svg"), tr("Certificates Manager"), this);
activateAction->setObjectName("signaturetool_activateAction");
createTextAction->setObjectName("signaturetool_createTextAction");
createFreehandCurveAction->setObjectName("signaturetool_createFreehandCurveAction");
createAcceptMarkAction->setObjectName("signaturetool_createAcceptMarkAction");
createRejectMarkAction->setObjectName("signaturetool_createRejectMarkAction");
createRectangleAction->setObjectName("signaturetool_createRectangleAction");
@ -67,14 +68,13 @@ void SignaturePlugin::setWidget(pdf::PDFWidget* widget)
createDotAction->setObjectName("signaturetool_createDotAction");
createSvgImageAction->setObjectName("signaturetool_createSvgImageAction");
clearAction->setObjectName("signaturetool_clearAction");
setColorAction->setObjectName("signaturetool_setColorAction");
setPenAction->setObjectName("signaturetool_setPenAction");
setBrushAction->setObjectName("signaturetool_setBrushAction");
signElectronicallyAction->setObjectName("signaturetool_signElectronicallyAction");
signDigitallyAction->setObjectName("signaturetool_signDigitallyAction");
certificatesAction->setObjectName("signaturetool_certificatesAction");
activateAction->setCheckable(true);
createTextAction->setCheckable(true);
createFreehandCurveAction->setCheckable(true);
createAcceptMarkAction->setCheckable(true);
createRejectMarkAction->setCheckable(true);
createRectangleAction->setCheckable(true);
@ -85,7 +85,9 @@ void SignaturePlugin::setWidget(pdf::PDFWidget* widget)
createDotAction->setCheckable(true);
createSvgImageAction->setCheckable(true);
m_actions[Activate] = activateAction;
m_actions[Text] = createTextAction;
m_actions[FreehandCurve] = createFreehandCurveAction;
m_actions[AcceptMark] = createAcceptMarkAction;
m_actions[RejectMark] = createRejectMarkAction;
m_actions[Rectangle] = createRectangleAction;
@ -96,12 +98,9 @@ void SignaturePlugin::setWidget(pdf::PDFWidget* widget)
m_actions[Dot] = createDotAction;
m_actions[SvgImage] = createSvgImageAction;
m_actions[Clear] = clearAction;
m_actions[SetColor] = setColorAction;
m_actions[SetPen] = setPenAction;
m_actions[SetBrush] = setBrushAction;
m_actions[SignElectronically] = signElectronicallyAction;
m_actions[SignDigitally] = signDigitallyAction;
m_actions[Ceritificates] = certificatesAction;
m_actions[Certificates] = certificatesAction;
QFile acceptMarkFile(":/pdfplugins/signatureplugin/accept-mark.svg");
QByteArray acceptMarkContent;
@ -134,6 +133,9 @@ void SignaturePlugin::setWidget(pdf::PDFWidget* widget)
}
m_widget->getDrawWidgetProxy()->registerDrawInterface(&m_scene);
connect(&m_scene, &pdf::PDFPageContentScene::sceneChanged, this, &SignaturePlugin::onSceneChanged);
connect(clearAction, &QAction::triggered, &m_scene, &pdf::PDFPageContentScene::clear);
connect(activateAction, &QAction::triggered, this, &SignaturePlugin::setActive);
updateActions();
}
@ -144,7 +146,7 @@ void SignaturePlugin::setDocument(const pdf::PDFModifiedDocument& document)
if (document.hasReset())
{
m_scene.clear();
setActive(false);
updateActions();
}
}
@ -153,7 +155,9 @@ std::vector<QAction*> SignaturePlugin::getActions() const
{
std::vector<QAction*> result;
result.push_back(m_actions[Activate]);
result.push_back(m_actions[Text]);
result.push_back(m_actions[FreehandCurve]);
result.push_back(m_actions[AcceptMark]);
result.push_back(m_actions[RejectMark]);
result.push_back(m_actions[Rectangle]);
@ -164,21 +168,87 @@ std::vector<QAction*> SignaturePlugin::getActions() const
result.push_back(m_actions[Dot]);
result.push_back(m_actions[Clear]);
result.push_back(nullptr);
result.push_back(m_actions[SetColor]);
result.push_back(m_actions[SetPen]);
result.push_back(m_actions[SetBrush]);
result.push_back(nullptr);
result.push_back(m_actions[SignElectronically]);
result.push_back(m_actions[SignDigitally]);
result.push_back(m_actions[Ceritificates]);
result.push_back(m_actions[Certificates]);
return result;
}
void SignaturePlugin::onSceneChanged()
{
updateActions();
updateGraphics();
}
void SignaturePlugin::setActive(bool active)
{
if (m_scene.isActive() != active)
{
// Abort active tool, if we are deactivating the plugin
if (!active)
{
if (pdf::PDFWidgetTool* tool = m_widget->getToolManager()->getActiveTool())
{
auto it = std::find(m_tools.cbegin(), m_tools.cend(), tool);
if (it == m_tools.cend())
{
m_widget->getToolManager()->setActiveTool(nullptr);
}
}
}
m_scene.setActive(active);
if (!active)
{
m_scene.clear();
}
m_actions[Activate]->setChecked(active);
updateActions();
}
}
void SignaturePlugin::updateActions()
{
m_actions[Activate]->setEnabled(m_document);
if (!m_scene.isActive() || !m_document)
{
// Inactive scene - disable all except activate action and certificates
for (QAction* action : m_actions)
{
if (action == m_actions[Activate] ||
action == m_actions[Certificates])
{
continue;
}
action->setEnabled(false);
}
return;
}
const bool isSceneNonempty = !m_scene.isEmpty();
// Tool actions
for (auto actionId : { Text, FreehandCurve, AcceptMark, RejectMark,
Rectangle, RoundedRectangle, HorizontalLine,
VerticalLine, Line, Dot, SvgImage })
{
m_actions[actionId]->setEnabled(true);
}
// Clear action
QAction* clearAction = m_actions[Clear];
clearAction->setEnabled(!m_scene.isEmpty());
clearAction->setEnabled(isSceneNonempty);
// Sign actions
QAction* signElectronicallyAction = m_actions[SignElectronically];
signElectronicallyAction->setEnabled(isSceneNonempty);
QAction* signDigitallyAction = m_actions[SignDigitally];
signDigitallyAction->setEnabled(isSceneNonempty);
}
void SignaturePlugin::updateGraphics()

View File

@ -43,11 +43,16 @@ public:
virtual std::vector<QAction*> getActions() const override;
private:
void onSceneChanged();
enum Action
{
// Activate action
Activate,
// Create graphics actions
Text,
FreehandCurve,
AcceptMark,
RejectMark,
Rectangle,
@ -59,15 +64,10 @@ private:
SvgImage,
Clear,
// Settings actions
SetColor,
SetPen,
SetBrush,
// Sign actions
SignElectronically,
SignDigitally,
Ceritificates,
Certificates,
LastAction
};
@ -84,6 +84,8 @@ private:
LastTool
};
void setActive(bool active);
void updateActions();
void updateGraphics();