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++
Add strategy to change shape padding or margin by dragging the text areas.
Wolthera van Hövell tot Westerfliere2851d7950af
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex 59c97e8f32..e004641b60 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -1860,7 +1860,14 @@ void KoSvgTextShape::setMemento(const KoSvgTextShapeMementoSP memento)void KoSvgTextShape::setMemento(const KoSvgTextShapeMementoSP memento, int pos, int anchor){+ const bool shapeOffsetBefore = (textProperties().hasProperty(KoSvgTextProperties::ShapeMarginId)+ || textProperties().hasProperty(KoSvgTextProperties::ShapePaddingId));setMementoImpl(memento);+ const bool shapeOffsetAfter = (textProperties().hasProperty(KoSvgTextProperties::ShapeMarginId)+ || textProperties().hasProperty(KoSvgTextProperties::ShapePaddingId));+ if (shapeOffsetBefore || shapeOffsetAfter) {+ d->updateTextWrappingAreas();+ }notifyCursorPosChanged(pos, anchor);notifyMarkupChanged();}diff --git a/plugins/tools/svgtexttool/CMakeLists.txt b/plugins/tools/svgtexttool/CMakeLists.txtindex dd5801f4a8..5d07fd15ac 100644--- a/plugins/tools/svgtexttool/CMakeLists.txt+++ b/plugins/tools/svgtexttool/CMakeLists.txt@@ -32,6 +32,7 @@ set (SvgTextTool_SRCSSvgTextPathInfoChangeCommand.cppSvgChangeTextPathInfoStrategy.cppSvgTextOnPathDecorationHelper.cpp+ SvgChangeTextPaddingMarginStrategy.cppglyphpalette/GlyphPaletteDialog.cppglyphpalette/GlyphPaletteProxyModel.cppdiff --git a/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.cpp b/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.cppnew file mode 100644index 0000000000..4aee0c0e87--- /dev/null+++ b/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.cpp@@ -0,0 +1,164 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-3.0-or-later+ */+#include "SvgChangeTextPaddingMarginStrategy.h"++#include "SvgTextTool.h"+#include <KoPathShape.h>+#include <KoPathSegment.h>++#include <KoSvgTextProperties.h>+#include <KisHandlePainterHelper.h>++#include "SvgTextMergePropertiesRangeCommand.h"++#include <kis_global.h>++SvgChangeTextPaddingMarginStrategy::SvgChangeTextPaddingMarginStrategy(SvgTextTool *tool, KoSvgTextShape *shape, const QPointF &clicked)+ : KoInteractionStrategy(tool)+ , m_shape(shape)+ , m_lastMousePos(clicked)+{+ KoSvgTextProperties props = m_shape->textProperties();+ props.inheritFrom(KoSvgTextProperties::defaultProperties(), true);++ const qreal shapePadding = props.propertyOrDefault(KoSvgTextProperties::ShapePaddingId).value<KoSvgText::CssLengthPercentage>().value;+ const qreal shapeMargin = props.propertyOrDefault(KoSvgTextProperties::ShapeMarginId).value<KoSvgText::CssLengthPercentage>().value;++ const QPointF padding(shapePadding+2, shapePadding+2);+ const QPointF margin(shapeMargin+2, shapeMargin+2);++ qreal minDistance = std::numeric_limits<qreal>::max();+ KoPathShape *candidate = nullptr;+ bool isPadding = false;+ Q_FOREACH(KoShape *shape, m_shape->shapesSubtract()) {+ KoPathShape *path = dynamic_cast<KoPathShape*>(shape);+ if (!path) continue;+ const QPointF mp = shape->documentToShape(clicked);+ const QRectF marginRect(mp - margin, mp + margin);++ Q_FOREACH(KoPathSegment segment, path->segmentsAt(marginRect)) {+ const qreal nearestT = segment.nearestPoint(mp);+ const QPointF nearestP = segment.pointAt(nearestT);+ const qreal distance = kisDistance(mp, nearestP) - shapeMargin;+ if (distance < minDistance) {+ candidate = path;+ minDistance = distance;+ }+ }+ }++ Q_FOREACH(KoShape *shape, m_shape->shapesInside()) {+ KoPathShape *path = dynamic_cast<KoPathShape*>(shape);+ if (!path) continue;+ const QPointF mp = shape->documentToShape(clicked);+ const QRectF paddingRect(mp - padding, mp + padding);++ qDebug() << "segments" << path->segmentsAt(paddingRect).size() << paddingRect;+ Q_FOREACH(KoPathSegment segment, path->segmentsAt(paddingRect)) {+ const qreal nearestT = segment.nearestPoint(mp);+ const QPointF nearestP = segment.pointAt(nearestT);+ const qreal distance = kisDistance(mp, nearestP) - shapePadding;+ if (distance < minDistance) {+ candidate = path;+ minDistance = distance;+ isPadding = true;+ }+ }+ }++ m_referenceShape = candidate;+ m_isPadding = isPadding;+}++SvgChangeTextPaddingMarginStrategy::~SvgChangeTextPaddingMarginStrategy()+{++}++std::optional<QPointF> SvgChangeTextPaddingMarginStrategy::hitTest(KoSvgTextShape *shape, const QPointF &mousePos, const qreal grabSensitivityInPts)+{+ if (!shape) return std::nullopt;+ const QList<QPainterPath> textAreas = shape->textWrappingAreas();+ if (textAreas.isEmpty()) return std::nullopt;++ const QPointF grab(grabSensitivityInPts, grabSensitivityInPts);+ const QRectF grabRect(mousePos-grab, mousePos+grab);++ Q_FOREACH(const QPainterPath area, shape->textWrappingAreas()) {+ KoPathShape *s = KoPathShape::createShapeFromPainterPath(area);+ if (!s) continue;+ s->setTransformation(shape->absoluteTransformation());+ KoPathSegment segment = s->segmentAtPoint(mousePos, grabRect);+ if (segment.isValid()) {+ qreal nearest = segment.nearestPoint(s->documentToShape(mousePos));+ return std::make_optional(segment.angleVectorAtParam(nearest));+ }+ }++ return std::nullopt;+}++QLineF getLine(QPointF mousePos, KoPathShape *referenceShape, bool isPadding) {+ if (!referenceShape) return QLineF();+ const bool hit = referenceShape->hitTest(mousePos);+ if ((!hit && isPadding) || (hit && !isPadding)) return QLineF();++ QPointF pos = referenceShape->documentToShape(mousePos);+ QLineF l(pos, pos);++ qreal minDistance = std::numeric_limits<qreal>::max();++ Q_FOREACH(KoPathSegment segment, referenceShape->segmentsAt(referenceShape->outlineRect().adjusted(-2, -2, 2, 2))) {+ const qreal nearestT = segment.nearestPoint(pos);+ const QPointF nearestP = segment.pointAt(nearestT);+ const qreal distance = kisDistance(pos, nearestP);+ if (distance < minDistance) {+ l.setP1(nearestP);+ minDistance = distance;+ }+ }+ if (l.length() < 0) {+ l.setLength(0);+ }+ return l;+}++void SvgChangeTextPaddingMarginStrategy::paint(QPainter &painter, const KoViewConverter &converter)+{+ if (!(m_referenceShape && m_shape)) return;+ const QTransform originalPainterTransform = painter.transform();+ painter.setTransform(converter.documentToView(), true);+ KisHandlePainterHelper handlePainter(&painter, originalPainterTransform, handleRadius(), decorationThickness());++ const QLineF l = m_referenceShape->absoluteTransformation().map(getLine(m_lastMousePos, m_referenceShape, m_isPadding));+ QPolygonF poly;+ poly << l.p1() << l.p2();+ handlePainter.drawRubberLine(poly);+}++void SvgChangeTextPaddingMarginStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)+{+ Q_UNUSED(modifiers);+ m_lastMousePos = mouseLocation;+}++KUndo2Command *SvgChangeTextPaddingMarginStrategy::createCommand()+{+ if (!(m_referenceShape && m_shape)) return nullptr;+ const QLineF l = getLine(m_lastMousePos, m_referenceShape, m_isPadding);++ KoSvgTextProperties::PropertyId propId = m_isPadding? KoSvgTextProperties::ShapePaddingId: KoSvgTextProperties::ShapeMarginId;+ KoSvgText::CssLengthPercentage length;+ length.value = l.length();+ KoSvgTextProperties props;+ props.setProperty(propId, QVariant::fromValue(length));+ return new SvgTextMergePropertiesRangeCommand(m_shape, props, -1, -1);+}++void SvgChangeTextPaddingMarginStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)+{+ Q_UNUSED(modifiers);+}diff --git a/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.h b/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.hnew file mode 100644index 0000000000..702fa3e4b1--- /dev/null+++ b/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.h@@ -0,0 +1,48 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-3.0-or-later+ */+#ifndef SVGCHANGETEXTPADDINGMARGINSTRATEGY_H+#define SVGCHANGETEXTPADDINGMARGINSTRATEGY_H++#include <KoInteractionStrategy.h>++#include <KoSvgTextShape.h>+#include <optional>++class SvgTextTool;+class KoPathShape;++class SvgChangeTextPaddingMarginStrategy : public KoInteractionStrategy+{+public:+ SvgChangeTextPaddingMarginStrategy(SvgTextTool *tool, KoSvgTextShape *shape, const QPointF &clicked);+ ~SvgChangeTextPaddingMarginStrategy();+++ /**+ * @brief hitTest+ * Tests whether the current mouse position is over a text wrapping area,+ * and if so, will return the angle vector at that point.+ * @param shape -- shape to test for.+ * @param mousePos -- mousePos to test against.+ * @param grabSensitivityInPts -- grabSensitivity in Points+ * @return -- std::optional containing the angle vector.+ */+ static std::optional<QPointF> hitTest(KoSvgTextShape *shape, const QPointF &mousePos, const qreal grabSensitivityInPts);+private:+ KoSvgTextShape *m_shape;+ KoPathShape *m_referenceShape;+ bool m_isPadding;+ QPointF m_lastMousePos;++ // KoInteractionStrategy interface+public:+ void paint(QPainter &painter, const KoViewConverter &converter) override;+ void handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override;+ KUndo2Command *createCommand() override;+ void finishInteraction(Qt::KeyboardModifiers modifiers) override;+};++#endif // SVGCHANGETEXTPADDINGMARGINSTRATEGY_Hdiff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cppindex ff8d56c335..f05ca7365c 100644--- a/plugins/tools/svgtexttool/SvgTextTool.cpp+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp@@ -25,6 +25,7 @@#include "SvgTextTypeSettingStrategy.h"#include "SvgTextChangeTransformsOnRange.h"#include "SvgChangeTextPathInfoStrategy.h"+#include "SvgChangeTextPaddingMarginStrategy.h"#include <QPainterPath>#include <QDesktopServices>@@ -668,7 +669,7 @@ void SvgTextTool::paint(QPainter &gc, const KoViewConverter &converter){if (!isActivated()) return;- if (m_interactionStrategy) {+ if (m_dragging == DragMode::Create || m_dragging == DragMode::InShapeOffset) {m_interactionStrategy->paint(gc, converter);}@@ -781,6 +782,11 @@ void SvgTextTool::mousePressEvent(KoPointerEvent *event)event->accept();return;}+ } else if (SvgChangeTextPaddingMarginStrategy::hitTest(selectedShape, event->point, grabSensitivityInPt())) {+ m_interactionStrategy.reset(new SvgChangeTextPaddingMarginStrategy(this, selectedShape, event->point));+ m_dragging = DragMode::InShapeOffset;+ event->accept();+ return;} 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;@@ -897,7 +903,7 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)} else {useCursor(m_ibeam_vertical);}- } else {+ } else if (m_dragging != DragMode::InShapeOffset) {useCursor(Qt::ArrowCursor);}event->accept();@@ -947,8 +953,10 @@ 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 (m_textOnPathHelper.hitTest(event->point, canvas()->viewConverter()->viewToDocument())) {+ if (m_textOnPathHelper.hitTest(event->point, canvas()->viewConverter()->viewToDocument()) ) {cursor = Qt::ArrowCursor;+ } else if(std::optional<QPointF> offsetVector = SvgChangeTextPaddingMarginStrategy::hitTest(selectedShape, event->point, grabSensitivityInPt())) {+ cursor = lineToCursor(QLineF(QPointF(0, 0), offsetVector.value()).normalVector(), canvas());} else if (selectedShape && selectedShape == hoveredShape && m_highlightItem == HighlightItem::None) {if (selectedShape->writingMode() == KoSvgText::HorizontalTB) {cursor = m_ibeam_horizontal;@@ -1017,8 +1025,8 @@ void SvgTextTool::mouseReleaseEvent(KoPointerEvent *event)void SvgTextTool::keyPressEvent(QKeyEvent *event){if (m_interactionStrategy- && (event->key() == Qt::Key_Control || event->key() == Qt::Key_Alt || event->key() == Qt::Key_Shift- || event->key() == Qt::Key_Meta)) {+ && (event->key() == Qt::Key_Control || event->key() == Qt::Key_Alt || event->key() == Qt::Key_Shift+ || event->key() == Qt::Key_Meta)) {m_interactionStrategy->handleMouseMove(m_lastMousePos, event->modifiers());event->accept();return;@@ -1035,8 +1043,8 @@ void SvgTextTool::keyReleaseEvent(QKeyEvent *event){m_textCursor.updateModifiers(event->modifiers());if (m_interactionStrategy- && (event->key() == Qt::Key_Control || event->key() == Qt::Key_Alt || event->key() == Qt::Key_Shift- || event->key() == Qt::Key_Meta)) {+ && (event->key() == Qt::Key_Control || event->key() == Qt::Key_Alt || event->key() == Qt::Key_Shift+ || event->key() == Qt::Key_Meta)) {m_interactionStrategy->handleMouseMove(m_lastMousePos, event->modifiers());event->accept();} else {diff --git a/plugins/tools/svgtexttool/SvgTextTool.h b/plugins/tools/svgtexttool/SvgTextTool.hindex 8779393821..5acc27f30b 100644--- a/plugins/tools/svgtexttool/SvgTextTool.h+++ b/plugins/tools/svgtexttool/SvgTextTool.h@@ -201,6 +201,7 @@ private:InlineSizeHandle,Move,TextPathHandle,+ InShapeOffset,};enum class HighlightItem {None = 0,