Krita Source Code Documentation
Loading...
Searching...
No Matches
SvgTextOnPathDecorationHelper.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
8#include <KoSvgTextShape.h>
10#include <KoViewConverter.h>
11#include <QDebug>
12#include <KoPathShape.h>
15 int pos = 0;
16
17 qreal handleRadius = 7;
19
20 bool isHovered = false;
21 bool isActive = false;
22
23 QLineF getLineAnchorTextNode(KoSvgTextNodeIndex &index, qreal lineLength) {
24 KoPathShape *s = dynamic_cast<KoPathShape*>(index.textPath());
25 QPainterPath outline = s->transformation().map(s->outline());
26 QLineF line;
28 outline = outline.toReversed();
29 }
30 qreal percent = index.textPathInfo()->startOffsetIsPercentage?
31 outline.percentAtLength(index.textPathInfo()->startOffset * 0.01 * outline.length()):
32 outline.percentAtLength(index.textPathInfo()->startOffset);
33 if (s->isClosedSubpath(s->subpathCount()-1)) {
34 percent = fmod(percent, 1.0);
35 } else {
36 percent = qBound(0.0, percent, 1.0);
37 }
38 line.setP1(outline.pointAtPercent(percent));
39 line.setAngle(outline.angleAtPercent(percent) - 90);
40 line.setLength(lineLength);
41 return line;
42 }
43};
44
49
54
56{
57 d->pos = pos;
58}
59
61{
62 d->shape = shape;
63}
64
66{
67 d->handleRadius = radius;
68}
69
71{
72 d->decorationThickness = thickness;
73}
74
75bool SvgTextOnPathDecorationHelper::hitTest(QPointF mouseInPts, const QTransform viewToDocument)
76{
77 if (!d->shape) return false;
78 KoSvgTextNodeIndex index = d->shape->topLevelNodeForPos(d->pos);
79 if (!(index.textPath() && index.textPathInfo())) return false;
80 QPointF handleInPts = viewToDocument.map(QPointF(d->handleRadius, d->handleRadius));
81
82 QLineF line = d->getLineAnchorTextNode(index, handleInPts.x()*2);
83 line = d->shape->absoluteTransformation().map(line);
84 bool hit = (QLineF(line.p2(), mouseInPts).length() <= handleInPts.x()*2);
85 d->isHovered = hit;
86 return hit;
87}
88
90{
91 if (!d->shape) return;
92 KoSvgTextNodeIndex index = d->shape->topLevelNodeForPos(d->pos);
93 if (!(index.textPath() && index.textPathInfo())) return;
94
95 QPointF handleInPts = converter.viewToDocument().map(QPointF(d->handleRadius, d->handleRadius));
96 QLineF line = d->getLineAnchorTextNode(index, handleInPts.x()*3);
97 p->save();
99 KoShape::createHandlePainterHelperView(p, d->shape, converter, d->handleRadius, d->decorationThickness);
100
101 if (d->isActive) {
103 } else if (d->isHovered) {
105 } else {
107 }
108
109 helper.drawConnectionLine(line);
110 helper.drawHandleCircle(line.p2());
111 //qDebug() << line.p2();
112 p->restore();
113}
114
115QRectF SvgTextOnPathDecorationHelper::decorationRect(const QTransform documentToView) const
116{
117 QRectF r;
118 if (!d->shape) return r;
119 KoSvgTextNodeIndex index = d->shape->topLevelNodeForPos(d->pos);
120 if (!(index.textPath() && index.textPathInfo())) return r;
121 QPointF handleInPts = documentToView.inverted().map(QPointF(d->handleRadius, d->handleRadius));
122
123 QLineF line = d->getLineAnchorTextNode(index, handleInPts.x()*3);
124
125 line = QTransform(d->shape->absoluteTransformation()*documentToView).map(line);
126 r |= QRectF(line.p1()-QPointF(d->decorationThickness, d->decorationThickness)
127 , line.p2()+QPointF(d->decorationThickness, d->decorationThickness));
128
129 QPointF handle(d->handleRadius, d->handleRadius);
130 r |= QRectF(line.p2() - handle, line.p2() + handle);
131
132 //qDebug() << "decor rect" << r << line.p2();
133 return r;
134}
135
137{
138 d->isActive = isActive;
139}
const Params2D p
The KisHandlePainterHelper class is a special helper for painting handles around objects....
void drawConnectionLine(const QLineF &line)
void setHandleStyle(const KisHandleStyle &style)
void drawHandleCircle(const QPointF &center, qreal radius)
static KisHandleStyle & highlightedPrimaryHandles()
static KisHandleStyle & selectedPrimaryHandles()
static KisHandleStyle & primarySelection()
The position of a path point within a path shape.
Definition KoPathShape.h:63
bool isClosedSubpath(int subpathIndex) const
Checks if a subpath is closed.
QPainterPath outline() const override
reimplemented
int subpathCount() const
Returns the number of subpaths in the path.
static KisHandlePainterHelper createHandlePainterHelperView(QPainter *painter, KoShape *shape, const KoViewConverter &converter, qreal handleRadius=0.0, int decorationThickness=1)
Definition KoShape.cpp:982
QTransform transformation() const
Returns the shapes local transformation matrix.
Definition KoShape.cpp:383
The KoSvgTextNodeIndex class.
KoShape * textPath()
textPath
KoSvgText::TextOnPathInfo * textPathInfo()
textPathInfo the text path info for this node as a pointer.
virtual QPointF viewToDocument(const QPointF &viewPoint) const
QRectF decorationRect(const QTransform documentToView) const
decorationRect
void setShape(KoSvgTextShape *shape)
setShape Set the shape for which to draw the text path.
void paint(QPainter *painter, const KoViewConverter &converter)
paint Paint the handles for the text path.
bool hitTest(QPointF mouseInPts, const QTransform viewToDocument)
hitTest
void setPos(int pos)
setPos the the position of the cursor, where the text path is sought. A single text shape can have mu...
@ TextPathSideRight
Definition KoSvgText.h:301
QLineF getLineAnchorTextNode(KoSvgTextNodeIndex &index, qreal lineLength)