Signature plugin: Freehand curve

This commit is contained in:
Jakub Melka 2022-02-27 19:59:53 +01:00
parent 7d84f26476
commit f34f10ebb7
6 changed files with 252 additions and 0 deletions

View File

@ -21,6 +21,7 @@
#include <QPen>
#include <QPainter>
#include <QMouseEvent>
namespace pdf
{
@ -368,4 +369,129 @@ void PDFCreatePCElementDotTool::onPointPicked(PDFInteger pageIndex, QPointF page
setActive(false);
}
PDFCreatePCElementFreehandCurveTool::PDFCreatePCElementFreehandCurveTool(PDFDrawWidgetProxy* proxy,
PDFPageContentScene* scene,
QAction* action,
QObject* parent) :
BaseClass(proxy, scene, action, parent),
m_element(nullptr)
{
QPen pen(Qt::SolidLine);
pen.setWidthF(2.0);
pen.setCapStyle(Qt::RoundCap);
m_element = new PDFPageContentElementFreehandCurve();
m_element->setBrush(Qt::NoBrush);
m_element->setPen(std::move(pen));
}
PDFCreatePCElementFreehandCurveTool::~PDFCreatePCElementFreehandCurveTool()
{
delete m_element;
}
void PDFCreatePCElementFreehandCurveTool::drawPage(QPainter* painter,
PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix,
QList<PDFRenderError>& errors) const
{
BaseClass::drawPage(painter, pageIndex, compiledPage, layoutGetter, pagePointToDevicePointMatrix, errors);
if (pageIndex != m_element->getPageIndex() || m_element->isEmpty())
{
return;
}
m_element->drawPage(painter, pageIndex, compiledPage, layoutGetter, pagePointToDevicePointMatrix, errors);
}
void PDFCreatePCElementFreehandCurveTool::mousePressEvent(QWidget* widget, QMouseEvent* event)
{
Q_UNUSED(widget);
event->accept();
if (event->button() == Qt::LeftButton)
{
// Try to perform pick point
QPointF pagePoint;
PDFInteger pageIndex = getProxy()->getPageUnderPoint(event->pos(), &pagePoint);
if (pageIndex != -1 && // We have picked some point on page
(m_element->getPageIndex() == -1 || m_element->getPageIndex() == pageIndex)) // We are under current page
{
m_element->setPageIndex(pageIndex);
m_element->addStartPoint(pagePoint);
}
}
else if (event->button() == Qt::RightButton)
{
resetTool();
}
emit getProxy()->repaintNeeded();
}
void PDFCreatePCElementFreehandCurveTool::mouseReleaseEvent(QWidget* widget, QMouseEvent* event)
{
Q_UNUSED(widget);
event->accept();
if (event->button() == Qt::LeftButton)
{
// Try to perform pick point
QPointF pagePoint;
PDFInteger pageIndex = getProxy()->getPageUnderPoint(event->pos(), &pagePoint);
if (pageIndex != -1 && // We have picked some point on page
(m_element->getPageIndex() == pageIndex)) // We are under current page
{
m_element->setPageIndex(pageIndex);
m_element->addPoint(pagePoint);
if (!m_element->isEmpty())
{
m_scene->addElement(m_element->clone());
}
}
resetTool();
}
emit getProxy()->repaintNeeded();
}
void PDFCreatePCElementFreehandCurveTool::mouseMoveEvent(QWidget* widget, QMouseEvent* event)
{
Q_UNUSED(widget);
event->accept();
if (event->buttons() & Qt::LeftButton && m_element->getPageIndex() != -1)
{
// Try to add point to the path
QPointF pagePoint;
PDFInteger pageIndex = getProxy()->getPageUnderPoint(event->pos(), &pagePoint);
if (pageIndex == m_element->getPageIndex())
{
m_element->addPoint(pagePoint);
}
emit getProxy()->repaintNeeded();
}
}
void PDFCreatePCElementFreehandCurveTool::setActiveImpl(bool active)
{
BaseClass::setActiveImpl(active);
if (!active)
{
resetTool();
}
}
void PDFCreatePCElementFreehandCurveTool::resetTool()
{
m_element->clear();
}
} // namespace pdf

View File

@ -28,6 +28,7 @@ class PDFPageContentSvgElement;
class PDFPageContentElementDot;
class PDFPageContentElementLine;
class PDFPageContentElementRectangle;
class PDFPageContentElementFreehandCurve;
class PDFCreatePCElementTool : public PDFWidgetTool
{
@ -165,6 +166,41 @@ private:
PDFPageContentElementDot* m_element;
};
/// Tool that creates freehand curve element.
class PDF4QTLIBSHARED_EXPORT PDFCreatePCElementFreehandCurveTool : public PDFCreatePCElementTool
{
Q_OBJECT
private:
using BaseClass = PDFCreatePCElementTool;
public:
explicit PDFCreatePCElementFreehandCurveTool(PDFDrawWidgetProxy* proxy,
PDFPageContentScene* scene,
QAction* action,
QObject* parent);
virtual ~PDFCreatePCElementFreehandCurveTool() override;
virtual void drawPage(QPainter* painter,
PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix,
QList<PDFRenderError>& errors) const override;
virtual void mousePressEvent(QWidget* widget, QMouseEvent* event) override;
virtual void mouseReleaseEvent(QWidget* widget, QMouseEvent* event) override;
virtual void mouseMoveEvent(QWidget* widget, QMouseEvent* event) override;
protected:
virtual void setActiveImpl(bool active);
private:
void resetTool();
PDFPageContentElementFreehandCurve* m_element;
};
} // namespace pdf
#endif // PDFPAGECONTENTEDITORTOOLS_H

View File

@ -441,4 +441,65 @@ void PDFPageContentElementDot::setPoint(QPointF newPoint)
m_point = newPoint;
}
PDFPageContentElementFreehandCurve* PDFPageContentElementFreehandCurve::clone() const
{
PDFPageContentElementFreehandCurve* copy = new PDFPageContentElementFreehandCurve();
copy->setPageIndex(getPageIndex());
copy->setPen(getPen());
copy->setBrush(getBrush());
copy->setCurve(getCurve());
return copy;
}
void PDFPageContentElementFreehandCurve::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->setPen(getPen());
painter->setBrush(getBrush());
painter->setRenderHint(QPainter::Antialiasing);
painter->drawPath(getCurve());
}
QPainterPath PDFPageContentElementFreehandCurve::getCurve() const
{
return m_curve;
}
void PDFPageContentElementFreehandCurve::setCurve(QPainterPath newCurve)
{
m_curve = newCurve;
}
void PDFPageContentElementFreehandCurve::addStartPoint(const QPointF& point)
{
m_curve.moveTo(point);
}
void PDFPageContentElementFreehandCurve::addPoint(const QPointF& point)
{
m_curve.lineTo(point);
}
void PDFPageContentElementFreehandCurve::clear()
{
setPageIndex(-1);
m_curve = QPainterPath();
}
} // namespace pdf

View File

@ -23,6 +23,7 @@
#include <QPen>
#include <QBrush>
#include <QCursor>
#include <QPainterPath>
class QSvgRenderer;
@ -147,6 +148,32 @@ private:
QPointF m_point;
};
class PDF4QTLIBSHARED_EXPORT PDFPageContentElementFreehandCurve : public PDFPageContentStyledElement
{
public:
virtual ~PDFPageContentElementFreehandCurve() = default;
virtual PDFPageContentElementFreehandCurve* clone() const override;
virtual void drawPage(QPainter* painter,
PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix,
QList<PDFRenderError>& errors) const override;
QPainterPath getCurve() const;
void setCurve(QPainterPath newCurve);
bool isEmpty() const { return m_curve.isEmpty(); }
void addStartPoint(const QPointF& point);
void addPoint(const QPointF& point);
void clear();
private:
QPainterPath m_curve;
};
class PDF4QTLIBSHARED_EXPORT PDFPageContentSvgElement : public PDFPageContentElement
{
public:

View File

@ -121,6 +121,7 @@ void SignaturePlugin::setWidget(pdf::PDFWidget* widget)
rejectMarkFile.close();
}
m_tools[FreehandCurveTool] = new pdf::PDFCreatePCElementFreehandCurveTool(widget->getDrawWidgetProxy(), &m_scene, createFreehandCurveAction, this);
m_tools[AcceptMarkTool] = new pdf::PDFCreatePCElementSvgTool(widget->getDrawWidgetProxy(), &m_scene, createAcceptMarkAction, acceptMarkContent, this);
m_tools[RejectMarkTool] = new pdf::PDFCreatePCElementSvgTool(widget->getDrawWidgetProxy(), &m_scene, createRejectMarkAction, rejectMarkContent, this);
m_tools[RectangleTool] = new pdf::PDFCreatePCElementRectangleTool(widget->getDrawWidgetProxy(), &m_scene, createRectangleAction, false, this);

View File

@ -79,6 +79,7 @@ private:
enum Tools
{
FreehandCurveTool,
AcceptMarkTool,
RejectMarkTool,
RectangleTool,