Krita Source Code Documentation
Loading...
Searching...
No Matches
SvgChangeTextPathInfoStrategy.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
7
9#include <KoPathSegment.h>
10#include <KoPathShape.h>
11#include <KoToolBase.h>
12#include "SvgTextTool.h"
13#include <qmath.h>
14#include <QDebug>
15
16SvgChangeTextPathInfoStrategy::SvgChangeTextPathInfoStrategy(SvgTextTool *tool, KoSvgTextShape *shape, const QPointF &clicked, int textCursorPos)
18 , m_shape(shape)
19 , m_currentMousePos(clicked)
20 , m_textCursorPos(textCursorPos)
21{
23 m_oldInfo = *(index.textPathInfo());
24
25}
26
27void SvgChangeTextPathInfoStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
28{
29 Q_UNUSED(modifiers)
30
31 m_currentMousePos = mouseLocation;
32
34 if (cmd) {
35 cmd->redo();
36 }
38}
39
41{
42 SvgTextTool *const tool = qobject_cast<SvgTextTool *>(this->tool());
44 KoShape *shape = index.textPath();
45 if (!shape) {
46 return nullptr;
47 }
48
49 KoPathShape *path = dynamic_cast<KoPathShape*>(shape);
51
52 const qreal grab = tool->grabSensitivityInPt()*4;
53 QRectF roi = QRect(0, 0, grab, grab);
54 roi.moveCenter(m_currentMousePos);
55 KoPathSegment segment = path->segmentAtPoint(m_currentMousePos, roi);
56
57 if (!segment.isValid()) {
58 return nullptr;
59 }
60
61 QList<KoPathSegment> segments = path->segmentsAt(path->outlineRect().adjusted(-grab, -grab, grab, grab));
62
63 double length = 0;
64 Q_FOREACH(KoPathSegment s, segments) {
65 if (s == segment) {
66 const QPointF mouseInShape = path->documentToShape(m_currentMousePos);
67 const qreal t = segment.nearestPoint(mouseInShape);
68 info.startOffset = length + (t * segment.length());
69
70 const QLineF l = QLineF(segment.pointAt(t), mouseInShape).unitVector();
71 const QVector2D p1(l.p2()-l.p1());
72 const QVector2D tangent = QVector2D(segment.angleVectorAtParam(t));
73 const QVector2D normal(-tangent.y(), tangent.x());
74 float dot = QVector2D::dotProduct(p1, normal);
75 if (dot <= 0) {
77 } else {
79 }
80 }
81 length += s.length();
82 }
84 info.startOffset = length - info.startOffset;
85 }
86
88 cmd->setText(kundo2_i18n("Change Text On Path Position"));
89 return cmd;
90}
91
100
101void SvgChangeTextPathInfoStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
102{
103
104}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
QPointF p1
virtual void undo()
void setText(const KUndo2MagicString &text)
virtual void redo()
A KoPathSegment consist of two neighboring KoPathPoints.
qreal length(qreal error=0.005) const
QPointF pointAt(qreal t) const
Returns point at given t.
QPointF angleVectorAtParam(qreal t)
angleVectorAtParam Calculates the angle vector at t.
qreal nearestPoint(const QPointF &point) const
bool isValid() const
Returns if segment is valid, e.g. has two valid points.
The position of a path point within a path shape.
Definition KoPathShape.h:63
The KoSvgTextNodeIndex class.
KoShape * textPath()
textPath
KoSvgText::TextOnPathInfo * textPathInfo()
textPathInfo the text path info for this node as a pointer.
KoSvgTextNodeIndex topLevelNodeForPos(int pos) const
topLevelNodeForPos Get, if possible, an index for the child element of the root at pos....
virtual void repaintDecorations()
SvgChangeTextPathInfoStrategy(SvgTextTool *tool, KoSvgTextShape *shape, const QPointF &clicked, int textCursorPos)
void handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override
void finishInteraction(Qt::KeyboardModifiers modifiers) override
KUndo2MagicString kundo2_i18n(const char *text)
@ TextPathSideRight
Definition KoSvgText.h:301
@ TextPathSideLeft
Definition KoSvgText.h:302