Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Fix text on path start position strategy and cleanup some code.

Wolthera van Hövell tot Westerflier73afcac2b4e2
diff --git a/libs/flake/KoPathSegment.cpp b/libs/flake/KoPathSegment.cpp
index d76f049fb5..b0f1cbf259 100644
--- a/libs/flake/KoPathSegment.cpp
+++ b/libs/flake/KoPathSegment.cpp
@@ -1062,6 +1062,17 @@ KoPathSegment KoPathSegment::interpolate(const QPointF &p0, const QPointF &p1, c
return KoPathSegment(p0, c1, p2);
}
+QPointF KoPathSegment::angleVectorAtParam(qreal t)
+{
+ const QPointF dA1 = QLineF(d->first->point(), d->first->controlPoint2()).pointAt(t);
+ const QPointF dA2 = QLineF(d->first->controlPoint2(), d->second->controlPoint1()).pointAt(t);
+ const QPointF dA3 = QLineF(d->second->controlPoint1(), d->second->point()).pointAt(t);
+
+ QLineF finalDerivative = QLineF(QLineF(dA1, dA2).pointAt(t), QLineF(dA2, dA3).pointAt(t)).unitVector();
+
+ return finalDerivative.p2() - finalDerivative.p1();
+}
+
#if 0
void KoPathSegment::printDebug() const
{
diff --git a/libs/flake/KoPathSegment.h b/libs/flake/KoPathSegment.h
index c0522d2d82..3a638b143c 100644
--- a/libs/flake/KoPathSegment.h
+++ b/libs/flake/KoPathSegment.h
@@ -139,6 +139,14 @@ public:
*/
static KoPathSegment interpolate(const QPointF &p0, const QPointF &p1, const QPointF &p2, qreal t);
+ /**
+ * @brief angleVectorAtParam
+ * Calculates the angle vector at t.
+ * @param t -- param for this segment.
+ * @return normalized angle vector.
+ */
+ QPointF angleVectorAtParam(qreal t);
+
private:
class Private;
Private * const d;
diff --git a/plugins/tools/svgtexttool/SvgChangeTextPathInfoStrategy.cpp b/plugins/tools/svgtexttool/SvgChangeTextPathInfoStrategy.cpp
index b3d660e22e..13ced9c52f 100644
--- a/plugins/tools/svgtexttool/SvgChangeTextPathInfoStrategy.cpp
+++ b/plugins/tools/svgtexttool/SvgChangeTextPathInfoStrategy.cpp
@@ -49,10 +49,10 @@ KUndo2Command *SvgChangeTextPathInfoStrategy::createCommand()
KoPathShape *path = dynamic_cast<KoPathShape*>(shape);
KoSvgText::TextOnPathInfo info;
- const qreal grab = tool->grabSensitivityInPt();
- /*QRectF roi = path->boundingRect();
- roi.adjust(-grab, -grab, grab, grab);*/
- KoPathSegment segment = path->segmentAtPoint(m_currentMousePos, tool->handleGrabRect(m_currentMousePos));
+ const qreal grab = tool->grabSensitivityInPt()*4;
+ QRectF roi = QRect(0, 0, grab, grab);
+ roi.moveCenter(m_currentMousePos);
+ KoPathSegment segment = path->segmentAtPoint(m_currentMousePos, roi);
if (!segment.isValid()) {
return nullptr;
@@ -60,25 +60,28 @@ KUndo2Command *SvgChangeTextPathInfoStrategy::createCommand()
QList<KoPathSegment> segments = path->segmentsAt(path->outlineRect().adjusted(-grab, -grab, grab, grab));
+ double length = 0;
Q_FOREACH(KoPathSegment s, segments) {
if (s == segment) {
- const qreal t = segment.nearestPoint(path->documentToShape(m_currentMousePos));
- info.startOffset += (t * segment.length());
-
- QPainterPath p = path->transformation().map(path->outline());
- qDebug() << info.startOffset/p.length();
- const qreal angle = p.angleAtPercent(fmod(info.startOffset/p.length(), 1.0))+90;
- if (fabs(QLineF(segment.pointAt(t), m_currentMousePos).angle() - angle) < 90) {
+ const QPointF mouseInShape = path->documentToShape(m_currentMousePos);
+ const qreal t = segment.nearestPoint(mouseInShape);
+ info.startOffset = length + (t * segment.length());
+
+ const QLineF l = QLineF(segment.pointAt(t), mouseInShape).unitVector();
+ const QVector2D p1(l.p2()-l.p1());
+ const QVector2D tangent = QVector2D(segment.angleVectorAtParam(t));
+ const QVector2D normal(-tangent.y(), tangent.x());
+ float dot = QVector2D::dotProduct(p1, normal);
+ if (dot <= 0) {
info.side = KoSvgText::TextPathSideRight;
} else {
info.side = KoSvgText::TextPathSideLeft;
}
- if (info.side == KoSvgText::TextPathSideRight) {
- info.startOffset = p.length() - info.startOffset;
- }
- break;
}
- info.startOffset += s.length();
+ length += s.length();
+ }
+ if (info.side == KoSvgText::TextPathSideRight) {
+ info.startOffset = length - info.startOffset;
}
KUndo2Command *cmd = new SvgTextPathInfoChangeCommand(m_shape, m_textCursorPos, info);
diff --git a/plugins/tools/svgtexttool/SvgTextOnPathDecorationHelper.cpp b/plugins/tools/svgtexttool/SvgTextOnPathDecorationHelper.cpp
index 46d3756cf2..043b0ba812 100644
--- a/plugins/tools/svgtexttool/SvgTextOnPathDecorationHelper.cpp
+++ b/plugins/tools/svgtexttool/SvgTextOnPathDecorationHelper.cpp
@@ -16,25 +16,27 @@ struct SvgTextOnPathDecorationHelper::Private {
qreal handleRadius = 7;
qreal decorationThickness = 1;
- QPointF lastMousePos;
+
+ bool isHovered = false;
+ bool isActive = false;
QLineF getLineAnchorTextNode(KoSvgTextNodeIndex &index, qreal lineLength) {
KoPathShape *s = dynamic_cast<KoPathShape*>(index.textPath());
QPainterPath outline = s->transformation().map(s->outline());
QLineF line;
+ if (index.textPathInfo()->side == KoSvgText::TextPathSideRight) {
+ outline = outline.toReversed();
+ }
qreal percent = index.textPathInfo()->startOffsetIsPercentage?
- index.textPathInfo()->startOffset * 0.01:
- index.textPathInfo()->startOffset / outline.length();
+ outline.percentAtLength(index.textPathInfo()->startOffset * 0.01 * outline.length()):
+ outline.percentAtLength(index.textPathInfo()->startOffset);
if (s->isClosedSubpath(s->subpathCount()-1)) {
percent = fmod(percent, 1.0);
} else {
percent = qBound(0.0, percent, 1.0);
}
- if (index.textPathInfo()->side == KoSvgText::TextPathSideRight) {
- percent = 1.0 - percent;
- }
line.setP1(outline.pointAtPercent(percent));
- line.setAngle(outline.angleAtPercent(percent)-90.0);
+ line.setAngle(outline.angleAtPercent(percent) - 90);
line.setLength(lineLength);
return line;
}
@@ -70,18 +72,18 @@ void SvgTextOnPathDecorationHelper::setDecorationThickness(qreal thickness)
d->decorationThickness = thickness;
}
-bool SvgTextOnPathDecorationHelper::hitTest(QPointF mouseInPts)
+bool SvgTextOnPathDecorationHelper::hitTest(QPointF mouseInPts, const QTransform viewToDocument)
{
- if (d->lastMousePos != mouseInPts) {
- d->lastMousePos = mouseInPts;
- }
if (!d->shape) return false;
KoSvgTextNodeIndex index = d->shape->topLevelNodeForPos(d->pos);
if (!(index.textPath() && index.textPathInfo())) return false;
+ QPointF handleInPts = viewToDocument.map(QPointF(d->handleRadius, d->handleRadius));
- QLineF line = d->getLineAnchorTextNode(index, 0);
+ QLineF line = d->getLineAnchorTextNode(index, handleInPts.x()*2);
line = d->shape->absoluteTransformation().map(line);
- return (QLineF(line.p2(), mouseInPts).length() <= d->handleRadius);
+ bool hit = (QLineF(line.p2(), mouseInPts).length() <= handleInPts.x()*2);
+ d->isHovered = hit;
+ return hit;
}
void SvgTextOnPathDecorationHelper::paint(QPainter *p, const KoViewConverter &converter)
@@ -90,15 +92,18 @@ void SvgTextOnPathDecorationHelper::paint(QPainter *p, const KoViewConverter &co
KoSvgTextNodeIndex index = d->shape->topLevelNodeForPos(d->pos);
if (!(index.textPath() && index.textPathInfo())) return;
- QLineF line = d->getLineAnchorTextNode(index, 0);
+ QPointF handleInPts = converter.viewToDocument().map(QPointF(d->handleRadius, d->handleRadius));
+ QLineF line = d->getLineAnchorTextNode(index, handleInPts.x()*3);
p->save();
KisHandlePainterHelper helper =
KoShape::createHandlePainterHelperView(p, d->shape, converter, d->handleRadius, d->decorationThickness);
- // TODO: fix handle.
- if (QLineF(converter.documentToView().map(line.p2()), d->lastMousePos).length() <= converter.viewToDocumentX(d->handleRadius)) {
+
+ if (d->isActive) {
+ helper.setHandleStyle(KisHandleStyle::selectedPrimaryHandles());
+ } else if (d->isHovered) {
helper.setHandleStyle(KisHandleStyle::highlightedPrimaryHandles());
} else {
- helper.setHandleStyle(KisHandleStyle::secondarySelection());
+ helper.setHandleStyle(KisHandleStyle::primarySelection());
}
helper.drawConnectionLine(line);
@@ -113,10 +118,13 @@ QRectF SvgTextOnPathDecorationHelper::decorationRect(const QTransform documentTo
if (!d->shape) return r;
KoSvgTextNodeIndex index = d->shape->topLevelNodeForPos(d->pos);
if (!(index.textPath() && index.textPathInfo())) return r;
+ QPointF handleInPts = documentToView.inverted().map(QPointF(d->handleRadius, d->handleRadius));
+
+ QLineF line = d->getLineAnchorTextNode(index, handleInPts.x()*3);
- QLineF line = d->getLineAnchorTextNode(index, 0);
- line = documentToView.map(line);
- r |= QRectF(line.p1()-QPointF(d->decorationThickness, d->decorationThickness), line.p2()+QPointF(d->decorationThickness, d->decorationThickness));
+ line = QTransform(d->shape->absoluteTransformation()*documentToView).map(line);
+ r |= QRectF(line.p1()-QPointF(d->decorationThickness, d->decorationThickness)
+ , line.p2()+QPointF(d->decorationThickness, d->decorationThickness));
QPointF handle(d->handleRadius, d->handleRadius);
r |= QRectF(line.p2() - handle, line.p2() + handle);
@@ -124,3 +132,8 @@ QRectF SvgTextOnPathDecorationHelper::decorationRect(const QTransform documentTo
//qDebug() << "decor rect" << r << line.p2();
return r;
}
+
+void SvgTextOnPathDecorationHelper::setStrategyActive(bool isActive)
+{
+ d->isActive = isActive;
+}
diff --git a/plugins/tools/svgtexttool/SvgTextOnPathDecorationHelper.h b/plugins/tools/svgtexttool/SvgTextOnPathDecorationHelper.h
index 9a9c590770..8b2464746c 100644
--- a/plugins/tools/svgtexttool/SvgTextOnPathDecorationHelper.h
+++ b/plugins/tools/svgtexttool/SvgTextOnPathDecorationHelper.h
@@ -45,7 +45,7 @@ public:
* @brief hitTest
* @return whether the current position is over a handle.
*/
- bool hitTest(QPointF mouseInPts);
+ bool hitTest(QPointF mouseInPts, const QTransform viewToDocument);
/**
* @brief paint
@@ -60,6 +60,8 @@ public:
*/
QRectF decorationRect(const QTransform documentToView) const;
+ void setStrategyActive(bool isActive);
+
private:
struct Private;
QScopedPointer<Private> d;
diff --git a/plugins/tools/svgtexttool/SvgTextPathInfoChangeCommand.cpp b/plugins/tools/svgtexttool/SvgTextPathInfoChangeCommand.cpp
index 295315e12b..f78ea07745 100644
--- a/plugins/tools/svgtexttool/SvgTextPathInfoChangeCommand.cpp
+++ b/plugins/tools/svgtexttool/SvgTextPathInfoChangeCommand.cpp
@@ -13,7 +13,6 @@ SvgTextPathInfoChangeCommand::SvgTextPathInfoChangeCommand(KoSvgTextShape *shape
, m_pos(pos)
, m_newInfo(textPathInfo)
{
- qWarning() << m_shape->topLevelNodeForPos(m_pos).textPath();
setText(kundo2_i18n("Change Text On Path Properties"));
}
@@ -35,7 +34,6 @@ void SvgTextPathInfoChangeCommand::redo()
index.textPathInfo()->method = m_newInfo.method;
index.textPathInfo()->startOffsetIsPercentage = m_newInfo.startOffsetIsPercentage;
index.textPathInfo()->spacing = m_newInfo.spacing;
- qDebug() << "old txtpath info" << index.textPathInfo()->side << index.textPathInfo()->startOffset;
m_shape->relayout();
m_shape->notifyChanged();
diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cpp
index 516c99b825..ff8d56c335 100644
--- a/plugins/tools/svgtexttool/SvgTextTool.cpp
+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp
@@ -518,6 +518,7 @@ void SvgTextTool::requestStrokeEnd()
m_interactionStrategy->cancelInteraction();
m_interactionStrategy = nullptr;
useCursor(Qt::ArrowCursor);
+ m_textOnPathHelper.setStrategyActive(false);
} else if (isInTextMode()) {
canvas()->shapeManager()->selection()->deselectAll();
}
@@ -769,6 +770,7 @@ void SvgTextTool::mousePressEvent(KoPointerEvent *event)
KoToolManager::instance()->switchToolRequested("InteractionTool");
return;
}
+
if (m_highlightItem == HighlightItem::TypeSettingHandle) {
SvgTextCursor::TypeSettingModeHandle handle = m_textCursor.typeSettingHandleAtPos(handleGrabRect(event->point));
if (handle != SvgTextCursor::NoHandle) {
@@ -779,9 +781,10 @@ void SvgTextTool::mousePressEvent(KoPointerEvent *event)
event->accept();
return;
}
- } else if (m_textOnPathHelper.hitTest(event->point)) {
+ } else if (m_textOnPathHelper.hitTest(event->point, canvas()->viewConverter()->viewToDocument())) {
m_interactionStrategy.reset(new SvgChangeTextPathInfoStrategy(this, selectedShape, event->point, m_textCursor.getPos()));
m_dragging = DragMode::TextPathHandle;
+ m_textOnPathHelper.setStrategyActive(true);
event->accept();
return;
} else if (m_highlightItem == HighlightItem::MoveBorder) {
@@ -900,6 +903,7 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)
event->accept();
} else {
m_highlightItem = HighlightItem::None;
+
KoSvgTextShape *const selectedShape = this->selectedShape();
QCursor cursor = m_base_cursor;
if (selectedShape) {
@@ -943,7 +947,9 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)
const KoSvgTextShape *hoveredShape = dynamic_cast<KoSvgTextShape *>(canvas()->shapeManager()->shapeAt(event->point));
const KoPathShape *hoveredFlowShape = dynamic_cast<KoPathShape *>(canvas()->shapeManager()->shapeAt(event->point));
QPainterPath hoverPath = KisToolUtils::shapeHoverInfoCrossLayer(canvas(), event->point, shapeType, &isHorizontal);
- if (selectedShape && selectedShape == hoveredShape && m_highlightItem == HighlightItem::None) {
+ if (m_textOnPathHelper.hitTest(event->point, canvas()->viewConverter()->viewToDocument())) {
+ cursor = Qt::ArrowCursor;
+ } else if (selectedShape && selectedShape == hoveredShape && m_highlightItem == HighlightItem::None) {
if (selectedShape->writingMode() == KoSvgText::HorizontalTB) {
cursor = m_ibeam_horizontal;
} else {
@@ -977,26 +983,6 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)
cursor = m_ibeam_vertical;
}
}
-#if 0
- /// Commenting this out until we have a good idea of how we want to tackle the text and shape to put them on.
- else if(m_highlightItem == HighlightItem::None) {
- KoPathShape *shape = dynamic_cast<KoPathShape *>(canvas()->shapeManager()->shapeAt(event->point));
- if (shape) {
- if (shape->subpathCount() > 0) {
- if (shape->isClosedSubpath(0)) {
- cursor = m_text_in_shape;
- }
- }
- KoPathSegment segment = segmentAtPoint(event->point, shape, handleGrabRect(event->point));
- if (segment.isValid()) {
- cursor = m_text_on_path;
- }
- m_hoveredShapeHighlightRect.addPath(shape->absoluteTransformation().map(shape->outline()));
- } else {
- m_hoveredShapeHighlightRect = QPainterPath();
- }
- }
-#endif
useCursor(cursor);
event->ignore();
}
@@ -1018,6 +1004,7 @@ void SvgTextTool::mouseReleaseEvent(KoPointerEvent *event)
if (m_dragging != DragMode::Select) {
useCursor(m_base_cursor);
}
+ m_textOnPathHelper.setStrategyActive(false);
m_dragging = DragMode::None;
m_textCursor.setDrawTypeSettingHandle(true);
event->accept();