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++

First version of remove gutter in the Knife Tool

Agata Cackobcfb5e79456f
diff --git a/libs/global/kis_algebra_2d.cpp b/libs/global/kis_algebra_2d.cpp
index 994fa05447..6b6b8f4427 100644
--- a/libs/global/kis_algebra_2d.cpp
+++ b/libs/global/kis_algebra_2d.cpp
@@ -1845,4 +1845,435 @@ QPointF movePointAlongTheLine(const QPointF &point, const QLineF &direction, qre
+QLineF getLineFromElements(const QPainterPath &shape, int index)
+{
+ QPainterPath::Element element1 = shape.elementAt(wrapValue(index, 0, shape.elementCount() - 1));
+ QPainterPath::Element element2 = shape.elementAt(wrapValue(index + 1, 0, shape.elementCount() - 1));
+ // it doesn't handle isCurveTo, just assumes it's a line... sorry!
+
+ return QLineF(element1, element2);
+}
+
+// local
+QLineF reverseDirection(const QLineF& line)
+{
+ return QLineF(line.p2(), line.p1());
+}
+
+// local
+QPainterPath removeGutterOneEndSmart(const QPainterPath &shape1, int index1, const QPainterPath &shape2, int index2, QPointF middleLineVector, bool reverseFirstPoly, bool reverseSecondPoly)
+{
+ // moving from shape1.index1.p1 to shape1.index2.p2
+
+ auto reverseIfNeeded = [] (const QLineF &line, bool reverse) {
+ if (reverse) {
+ return reverseDirection(line);
+ }
+ return line;
+ };
+
+ QLineF line1 = reverseIfNeeded(getLineFromElements(shape1, index1), reverseFirstPoly);
+ QLineF line2 = reverseIfNeeded(getLineFromElements(shape2, index2), reverseSecondPoly);
+
+ qCritical() << ppVar(line1) << ppVar(line2) << ppVar(reverseFirstPoly) << ppVar(reverseSecondPoly) << ppVar(index1) << ppVar(index2);
+
+ QLineF leftLine;
+ QLineF rightLine;
+
+ int indexMultiplierFirst = reverseFirstPoly ? -1 : 1;
+ int indexMultiplierSecond = reverseSecondPoly ? -1 : 1;
+
+ auto fixIndex = [] (int index, const QPainterPath& shape) {
+ //if (index == shape.)
+ };
+
+
+ leftLine = reverseIfNeeded(getLineFromElements(shape1, index1 - indexMultiplierFirst*1), reverseFirstPoly);
+ rightLine = reverseIfNeeded(getLineFromElements(shape2, index2 + indexMultiplierSecond*1), reverseSecondPoly);
+
+ qCritical() << "Lines from the polygons next to the points to figure out correct behaviour: ";
+ qCritical() << ppVar(leftLine);
+ qCritical() << ppVar(rightLine);
+
+ qreal leftPointDistanceFromMiddle;
+ qreal rightPointDistanceFromMiddle;
+
+ QLineF middleLine = QLineF(QPointF(), middleLineVector);
+
+ QPointF leftPoint = line1.p1();
+ QPointF rightPoint = line2.p2();
+
+ qCritical() << ppVar(leftPoint) << ppVar(rightPoint);
+
+
+ leftPointDistanceFromMiddle = kisDistanceToLine(leftPoint, middleLine);
+ rightPointDistanceFromMiddle = kisDistanceToLine(rightPoint, middleLine);
+
+ QPainterPath simpleResult;
+ simpleResult.moveTo(leftPoint);
+ simpleResult.lineTo(rightPoint);
+
+ auto makeTrianglePath = [leftPoint, rightPoint] (QPointF middle) {
+ QPainterPath result;
+ result.moveTo(leftPoint);
+ result.lineTo(middle);
+ result.lineTo(rightPoint);
+ return result;
+ };
+
+ QPointF intersectionPoint;
+ QLineF::IntersectionType intersectionType = leftLine.intersects(rightLine, &intersectionPoint);
+
+ qCritical() << "Checking intersection between lines: leftLine & rightLine, shown above, result: " << ppVar(intersectionType) << ppVar(intersectionPoint);
+ if (intersectionType == QLineF::NoIntersection) {
+
+ // first arg: bounded line, second: unbounded
+ boost::optional<QPointF> leftIntersection = intersectLines(line2, leftLine);
+ boost::optional<QPointF> rightIntersection = intersectLines(line1, rightLine);
+
+ qCritical() << "We're in the if. Checking two cases: ";
+ qCritical() << "Intersection between bounded: " << ppVar(line2) << "and unbounded: " << ppVar(leftLine) << "result: " << ppVar(leftIntersection.get_value_or(QPointF()));
+ qCritical() << "Intersection between bounded: " << ppVar(line1) << "and unbounded: " << ppVar(rightLine) << "result:" << ppVar(rightIntersection.get_value_or(QPointF()));
+
+
+ if (leftIntersection.has_value()) {
+ return makeTrianglePath(leftIntersection.value());
+ } else if (rightIntersection.has_value()) {
+ return makeTrianglePath(rightIntersection.value());
+ } else {
+ return simpleResult;
+ }
+
+ }
+
+ qreal distanceToMiddle = kisDistanceToLine(intersectionPoint, middleLine);
+ if (distanceToMiddle > qMax(leftPointDistanceFromMiddle, rightPointDistanceFromMiddle)) {
+ return simpleResult;
+ }
+
+ QPainterPath betterResult = makeTrianglePath(intersectionPoint);
+
+ return betterResult;
+
+}
+
+// local
+QPainterPath removeGutterOneEndOld(const QPainterPath &shape1, int index1, const QPainterPath &shape2, int index2, QPointF middleLineVector, bool whichEnd, bool reverseSecondPoly)
+{
+ auto reverseIfNeeded = [] (const QLineF &line, bool reverse) {
+ if (reverse) {
+ return reverseDirection(line);
+ }
+ return line;
+ };
+
+ QLineF line1 = getLineFromElements(shape1, index1);
+ QLineF line2 = reverseIfNeeded(getLineFromElements(shape1, index1), reverseSecondPoly);
+
+ QLineF leftLine;
+ QLineF rightLine;
+
+ int indexMultiplier = reverseSecondPoly ? -1 : 1;
+
+ if (whichEnd) {
+ leftLine = getLineFromElements(shape1, index1 - 1);
+ rightLine = reverseIfNeeded(getLineFromElements(shape1, index2 + indexMultiplier*1), reverseSecondPoly);
+ } else {
+ leftLine = getLineFromElements(shape1, index1 + 1);
+ rightLine = reverseIfNeeded(getLineFromElements(shape1, index2 - indexMultiplier*1), reverseSecondPoly);
+ }
+
+ qreal leftPointDistanceFromMiddle;
+ qreal rightPointDistanceFromMiddle;
+
+ QLineF middleLine = QLineF(QPointF(), middleLineVector);
+
+ if (whichEnd) {
+ leftPointDistanceFromMiddle = kisDistanceToLine(line1.p1(), middleLine);
+ rightPointDistanceFromMiddle = kisDistanceToLine(line2.p2(), middleLine);
+ } else {
+ leftPointDistanceFromMiddle = kisDistanceToLine(line1.p2(), middleLine);
+ rightPointDistanceFromMiddle = kisDistanceToLine(line2.p1(), middleLine);
+ }
+
+ QPainterPath simpleResult;
+ if (whichEnd) {
+ simpleResult.moveTo(line1.p1());
+ simpleResult.lineTo(line2.p2());
+ } else {
+ simpleResult.moveTo(line1.p2());
+ simpleResult.lineTo(line2.p1());
+ }
+
+ QPointF intersectionPoint;
+ QLineF::IntersectionType intersectionType = leftLine.intersects(rightLine, &intersectionPoint);
+ if (intersectionType == QLineF::NoIntersection) {
+ return simpleResult;
+ }
+ qreal distanceToMiddle = kisDistanceToLine(intersectionPoint, middleLine);
+ if (distanceToMiddle > leftPointDistanceFromMiddle + rightPointDistanceFromMiddle) {
+ return simpleResult;
+ }
+
+
+
+ return QPainterPath();
+}
+
+QPainterPath simplifyShape(const QPainterPath& path) {
+
+ QPainterPath::Element buffor;
+ QPainterPath result;
+ QPointF lastPosition = QPointF(0, 0);
+ for (int i = 0; i < path.elementCount() - 1; i++) {
+ if (path.elementAt(i).isMoveTo()) {
+ lastPosition = path.elementAt(i);
+ }
+ if (path.elementAt(i).isLineTo()) {
+ if (path.elementAt(i + 1).isLineTo()) {
+ if (qFuzzyIsNull(dotProduct(path.elementAt(i + 1) - lastPosition, path.elementAt(i) - lastPosition))) {
+ // they are parallel
+ continue;
+ }
+ }
+
+ }
+ }
+
+ return path;
+
+}
+
+
+QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPainterPath &shape2, int index2)
+{
+ qCritical() << ppVar(index1) << ppVar(shape1.elementCount()) << ppVar(index2) << ppVar(shape2.elementCount());
+ if (index1 + 1 >= shape1.elementCount() || index2 + 1 >= shape2.elementCount()) {
+ return QPainterPath();
+ }
+
+ // shape1.elementAt(index1)
+ QLineF line1 = getLineFromElements(shape1, index1);
+ QLineF line2 = getLineFromElements(shape2, index2);
+
+ QPointF middleLineVector = QPointF((line1.dx() + line2.dx())/2, (line1.dy() + line2.dy()));
+
+ QPainterPath gutterShape = QPainterPath();
+ QVector<QPointF> poly1 = QVector<QPointF>() << line1.p1() << line1.p2() << line2.p1();
+ QVector<QPointF> poly2 = QVector<QPointF>() << line1.p2() << line2.p1() << line2.p2();
+ bool reverseSecondPoly = false;
+ if (polygonDirection(poly1) != polygonDirection(poly2)) {
+ line2 = QLineF(line2.p2(), line2.p1()); // quick fix to ensure that the shape will be correct (convex)
+ reverseSecondPoly = true;
+ }
+
+ // more difficult version:
+ QPainterPath oneEnd = removeGutterOneEndSmart(shape1, index1, shape2, index2, middleLineVector, false, reverseSecondPoly);
+ int secondShapeIndexInOtherEnd = reverseSecondPoly ? -index2 + 1 : -index2 + 1;
+ //QPainterPath otherEnd = removeGutterOneEndSmart(shape2, secondShapeIndexInOtherEnd, shape1, -index1 + 2, middleLineVector, reverseSecondPoly);
+ //QPainterPath otherEnd = removeGutterOneEndSmart(shape2, index2, shape1, index1, middleLineVector, true, !reverseSecondPoly);
+ QPainterPath otherEnd = removeGutterOneEndSmart(shape2, index2, shape1, index1, middleLineVector, reverseSecondPoly, false);
+
+
+
+ gutterShape.moveTo(oneEnd.elementAt(0));
+ gutterShape.connectPath(oneEnd);
+ gutterShape.connectPath(otherEnd);
+ gutterShape.closeSubpath();
+
+ qCritical() << ppVar(gutterShape);
+ qCritical() << ppVar(oneEnd);
+ qCritical() << ppVar(otherEnd);
+
+
+ QPainterPath result = shape1 | shape2 | gutterShape;
+
+
+ return result.simplified();
+
+
+
+ // now, easy version:
+
+
+ gutterShape.moveTo(line1.p1());
+ gutterShape.lineTo(line1.p2());
+ gutterShape.lineTo(line2.p1());
+ gutterShape.lineTo(line2.p2());
+
+ //QPainterPath result = shape1 | shape2 | gutterShape;
+ //result.closeSubpath();
+ //return result.simplified();
+
+ //return result;
+
+
+
+}
+
+
+// isAlgebra2D::getLineSegmentCrossingLineIndexes(QLineF const&, QPainterPath const&)
+QList<int> getLineSegmentCrossingLineIndexes(const QLineF &line, const QPainterPath &shape)
+{
+ QList<int> indexes;
+ qCritical() << "Searching for line: " << line;
+ for (int i = 0; i < shape.elementCount() - 1; i++) { // last element would wrap around to be the first segment again
+ QLineF lineSegment = getLineFromElements(shape, i);
+ QPointF intersection;
+ QLineF::IntersectionType type = lineSegment.intersects(line, &intersection);
+ qCritical() << ppVar(i) << ppVar(lineSegment);
+ if (type == QLineF::IntersectionType::BoundedIntersection) {
+ qCritical() << "Found an intersection: " << ppVar(line) << ppVar(lineSegment) << ppVar(intersection) << "At: " << ppVar(i);
+ indexes << i;
+ }
+ }
+ return indexes;
+}
+
+VectorPath::VectorPath(const QPainterPath &path)
+{
+ using VPoint = VectorPath::VectorPathPoint;
+ qCritical() << "Start, for the path that has elements: " << path.elementCount();
+
+ m_points = QList<VectorPath::VectorPathPoint>();
+ for (int i = 0; i < path.elementCount(); i++) {
+ QPainterPath::Element el = path.elementAt(i);
+ switch(el.type) {
+ case QPainterPath::MoveToElement:
+ m_points.append(VPoint(VPoint::MoveTo, el));
+ break;
+ case QPainterPath::LineToElement:
+ m_points.append(VPoint(VPoint::LineTo, el));
+ break;
+ case QPainterPath::CurveToElement:
+ //m_points.append(VPoint(VPoint::CuTo, el));
+ KIS_SAFE_ASSERT_RECOVER(i + 2 < path.elementCount()) { continue; }
+ KIS_SAFE_ASSERT_RECOVER(path.elementAt(i + 1).type == QPainterPath::CurveToDataElement) { continue; }
+ KIS_SAFE_ASSERT_RECOVER(path.elementAt(i + 2).type == QPainterPath::CurveToDataElement) { continue; }
+ // Qt saves the data this way: [control point 1, CurveToElement] [control point 2, CurveToDataElement] [end point, CurveToDataElement]
+ m_points.append(VPoint(VPoint::BezierTo, path.elementAt(i + 2), el, path.elementAt(i + 1)));
+ i += 2; // skip next two points
+ break;
+ }
+ qCritical() << "Added point: " << m_points[m_points.length() - 1].endPoint;
+ }
+ qCritical() << "End.";
+}
+
+VectorPath::VectorPath(const QList<VectorPathPoint> path)
+{
+ m_points = path;
+}
+
+int VectorPath::pointsCount()
+{
+ return m_points.length();
+}
+
+VectorPath::VectorPathPoint VectorPath::pointAt(int i)
+{
+ return m_points[i];
+}
+
+int VectorPath::segmentsCount()
+{
+ return m_points.length() - 1 >= 0 ? m_points.length() - 1 : 0;
+}
+
+QList<VectorPath::VectorPathPoint> VectorPath::segmentAt(int i)
+{
+ QList<VectorPath::VectorPathPoint> response;
+ if (i < 0 || i >= segmentsCount()) {
+ return response;
+ }
+ response << m_points[i] << m_points[i + 1];
+ return response;
+}
+
+QLineF VectorPath::segmentAtAsLine(int i)
+{
+ QList<VectorPath::VectorPathPoint> points = segmentAt(i);
+ if (points.length() < 1) {
+ return QLineF();
+ }
+ return QLineF(points[0].endPoint, points[1].endPoint);
+}
+
+VectorPath VectorPath::trulySimplified()
+{
+ using VPoint = VectorPath::VectorPathPoint;
+
+ qCritical() << "Start simplifying path that has segments: " << segmentsCount();
+
+ QList<VPoint> response;
+ VPoint lineBeginPoint = VPoint(VPoint::MoveTo, QPointF(0, 0));
+ VPoint previousPoint = m_points[0];
+ for (int i = 1; i < m_points.length(); i++) {
+ qCritical() << "Point is: " << m_points[i].endPoint << m_points[i].type;
+ if (m_points[i].type == VPoint::MoveTo) {
+ if (previousPoint.type == VPoint::MoveTo) {
+ previousPoint = m_points[i]; // let's skip the previous point since they're both just moving
+ continue;
+ } else { // LineTo or BezierTo, both need to be added
+ response << previousPoint;
+ previousPoint = m_points[i];
+ continue;
+ }
+ } else if (m_points[i].type == VPoint::LineTo) {
+ if (previousPoint.type == VPoint::LineTo) {
+ // check whether they're parallel
+ qCritical() << "Check whether they're parallel";
+ qreal eps = 1e-05;
+ if (qAbs(crossProduct(m_points[i].endPoint - lineBeginPoint.endPoint, previousPoint.endPoint - lineBeginPoint.endPoint)) >= eps) {
+ qCritical() << "They are not parallel: " << m_points[i].endPoint - lineBeginPoint.endPoint << previousPoint.endPoint - lineBeginPoint.endPoint
+ << crossProduct(m_points[i].endPoint - lineBeginPoint.endPoint, previousPoint.endPoint - lineBeginPoint.endPoint)
+ << qAbs(crossProduct(m_points[i].endPoint - lineBeginPoint.endPoint, previousPoint.endPoint - lineBeginPoint.endPoint))
+ << eps;
+ response << previousPoint;
+ lineBeginPoint = previousPoint;
+ }
+ previousPoint = m_points[i]; // let's skip the previous point since they're parallel
+ continue;
+ } else { // MoveTo or BezierTo, both need to be added
+ response << previousPoint;
+ lineBeginPoint = previousPoint;
+ previousPoint = m_points[i];
+ continue;
+ }
+ } else { // currently in BezierTo
+ response << previousPoint;
+ previousPoint = m_points[i];
+ continue;
+ }
+ }
+ response << previousPoint;
+ qCritical() << "After simplification, we've got points: " << response.length();
+ return VectorPath(response);
}
+
+QPainterPath VectorPath::asPainterPath()
+{
+ using VPoint = VectorPath::VectorPathPoint;
+ if (m_originalPath.elementCount() > 0) {
+ return m_originalPath;
+ }
+ QPainterPath response;
+ for (int i = 0; i < pointsCount(); i++) {
+ switch(m_points[i].type) {
+ case VPoint::MoveTo:
+ response.moveTo(m_points[i].endPoint);
+ break;
+ case VPoint::LineTo:
+ response.lineTo(m_points[i].endPoint);
+ break;
+ case VPoint::BezierTo:
+ response.cubicTo(m_points[i].controlPoint1, m_points[i].controlPoint2, m_points[i].endPoint);
+ break;
+ }
+ }
+ return response;
+}
+
+
+
+} // namespace
diff --git a/libs/global/kis_algebra_2d.h b/libs/global/kis_algebra_2d.h
index ed97b80c10..d9d62e9a19 100644
--- a/libs/global/kis_algebra_2d.h
+++ b/libs/global/kis_algebra_2d.h
@@ -11,6 +11,7 @@
#include <QPointF>
#include <QVector>
#include <QPolygonF>
+#include <QPainterPath>
#include <QTransform>
#include <cmath>
#include <kis_global.h>
@@ -310,6 +311,13 @@ inline Point clampPoint(Point pt, const Rect &bounds)
return pt;
}
+template <class Point>
+inline typename PointTypeTraits<Point>::rect_type
+createRectFromCorners(Point corner1, Point corner2)
+{
+ return typename PointTypeTraits<Point>::rect_type(qMin(corner1.x(), corner2.x()), qMin(corner1.y(), corner2.y()), qAbs(corner1.x() - corner2.x()), qAbs(corner1.y() - corner2.y()));
+}
+
template <class Size>
auto maxDimension(Size size) -> decltype(size.width()) {
return qMax(size.width(), size.height());
@@ -557,6 +565,58 @@ private:
const qreal m_fadeCoeff;
};
+
+// wrapper for QPainterPath providing sane line segment indexing
+class KRITAGLOBAL_EXPORT VectorPath
+{
+public:
+ struct VectorPathPoint
+ {
+ typedef enum Type {
+ MoveTo,
+ LineTo,
+ BezierTo
+ } Type;
+
+
+ QPointF endPoint {QPointF()};
+ QPointF controlPoint1 {QPointF()};
+ QPointF controlPoint2 {QPointF()};
+
+ Type type {MoveTo};
+
+
+
+ VectorPathPoint(Type _type, QPointF _endPoint, QPointF c1 = QPointF(), QPointF c2 = QPointF()) {
+ type = _type;
+ endPoint = _endPoint;
+ controlPoint1 = c1;
+ controlPoint1 = c2;
+ }
+ };
+
+public:
+ VectorPath(const QPainterPath& path);
+ VectorPath(const QList<VectorPathPoint> path);
+
+ int pointsCount();
+ VectorPathPoint pointAt(int i);
+ int segmentsCount();
+ QList<VectorPathPoint> segmentAt(int i);
+ QLineF segmentAtAsLine(int i);
+
+ VectorPath trulySimplified();
+
+ QPainterPath asPainterPath();
+
+private:
+ QPainterPath m_originalPath;
+ QList<VectorPathPoint> m_points;
+
+
+
+};
+
QVector<QPoint> KRITAGLOBAL_EXPORT sampleRectWithPoints(const QRect &rect);
QVector<QPointF> KRITAGLOBAL_EXPORT sampleRectWithPoints(const QRectF &rect);
@@ -974,6 +1034,19 @@ QPainterPath KRITAGLOBAL_EXPORT getOnePathFromRectangleCutThrough(const QList<QP
///
QList<QPainterPath> KRITAGLOBAL_EXPORT getPathsFromRectangleCutThrough(QRectF rect, QLineF leftLine, QLineF rightLine);
+// isAlgebra2D::getLineSegmentCrossingLineIndexes(QLineF const&, QPainterPath const&)
+QList<int> KRITAGLOBAL_EXPORT getLineSegmentCrossingLineIndexes(const QLineF &line, const QPainterPath& shape);
+
+///
+/// \brief removeGutterSmart
+/// \param shape1
+/// \param index1
+/// \param shape2
+/// \param index2
+/// \return
+///
+QPainterPath KRITAGLOBAL_EXPORT removeGutterSmart(const QPainterPath& shape1, int index1, const QPainterPath& shape2, int index2);
+
}
diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cpp
index e2b6196918..56b8986974 100644
--- a/libs/image/tests/kis_algebra_2d_test.cpp
+++ b/libs/image/tests/kis_algebra_2d_test.cpp
@@ -999,14 +999,24 @@ void KisAlgebra2DTest::testLinePolygonIntersectionsManual()
}
-bool fuzzyComparePolygons(const QPolygonF &one, const QPolygonF &other) {
+bool fuzzyComparePolygons(const QPolygonF &one, const QPolygonF &other, qreal eps = 1e-05) {
if (one.count() != other.count()) {
+ qCritical() << "Counts don't match: " << one.count() << other.count();
return false;
}
+
+ auto compare = [eps] (const QPointF& a, const QPointF& b) {
+ if (KisAlgebra2D::fuzzyPointCompare(a, b)) {
+ return true;
+ }
+ return qAbs(a.x() - b.x()) < eps && qAbs(a.y() - b.y()) < eps;
+ };
+
bool firstWay = true;
for (int i = 0; i < one.count(); i++) {
- if (!KisAlgebra2D::fuzzyPointCompare(one[i], other[i])) {
+ if (!compare(one[i], other[i])) {
firstWay = false;
+ qCritical() << "Mismatch on: " << one[i] << other[i] << "difference: " << one[i] - other[i];
break;
}
}
@@ -1014,7 +1024,8 @@ bool fuzzyComparePolygons(const QPolygonF &one, const QPolygonF &other) {
return true;
}
for (int i = 0; i < one.count(); i++) {
- if (!KisAlgebra2D::fuzzyPointCompare(one[one.count() - i - 1], other[i])) {
+ if (!compare(one[one.count() - i - 1], other[i])) {
+ qCritical() << "Mismatch on (second way): " << one[one.count() - i - 1] << other[i];
return false;
}
}
@@ -1106,6 +1117,176 @@ void KisAlgebra2DTest::testGetParallelLines2()
qCritical() << line1 << line2;
+}
+
+QPainterPath makeSpecialShape(bool left)
+{
+ // left has teeth on the left
+ int m = left ? 1 : -1; // multiplier
+ QPainterPath path;
+ path.moveTo(m*100, 100);
+ path.lineTo(m*400, 100);
+
+ path.lineTo(m*400, 200);
+ path.lineTo(m*100, 200);
+
+ path.lineTo(m*150, 175);
+ path.lineTo(m*100, 150);
+ path.lineTo(m*150, 125);
+ path.lineTo(m*100, 100);
+
+
+ if (!left) {
+ path.translate(QPointF(1000, 0));
+ }
+
+ return path;
+
+}
+
+//QPainterPath fromPolygon(const QVector<QPointF>& poly) {
+// return fromPolygon(poly);
+//}
+
+QPainterPath fromPolygon(const QPolygonF& poly) {
+ QPainterPath path;
+ path.addPolygon(poly);
+ path.closeSubpath();
+ return path;
+}
+
+QPolygonF toPolygon(const QPainterPath& path) {
+ return path.toFillPolygon();
+}
+
+
+void testRemoveGutterOneCase(const QPainterPath& shape1, const QPainterPath& shape2, const QLineF& mouseLine, int expectedSize = -1, const QPainterPath &expectedPath = QPainterPath())
+{
+
+ QList<int> indexes1 = KisAlgebra2D::getLineSegmentCrossingLineIndexes(mouseLine, shape1);
+ QList<int> indexes2 = KisAlgebra2D::getLineSegmentCrossingLineIndexes(mouseLine, shape2);
+
+ assert(indexes1.length() == 1);
+ assert(indexes2.length() == 1);
+
+ QPainterPath result = KisAlgebra2D::removeGutterSmart(shape1, indexes1[0], shape2, indexes2[0]);
+ qCritical() << result;
+
+ if (expectedSize >= 0) {
+ qCritical() << ppVar(result.elementCount()) << ppVar(expectedSize);
+ assert(result.elementCount() == expectedSize);
+ }
+
+ if (expectedPath.elementCount() > 0) {
+ //assert(expectedPath.elementCount() == result.elementCount());
+ bool compare = fuzzyComparePolygons(expectedPath.toFillPolygon(), result.toFillPolygon());
+ if (!compare) {
+ qCritical() << "Expected:" << expectedPath.toFillPolygon();
+ qCritical() << "Actual:" << result.toFillPolygon();
+ qCritical() << "Simplified, really? " << result.simplified();
+
+ KisAlgebra2D::VectorPath expectedVector(expectedPath);
+ KisAlgebra2D::VectorPath actualVector(result);
+
+ actualVector = actualVector.trulySimplified();
+ expectedVector = expectedVector.trulySimplified();
+
+ qCritical() << "After my simplification: " << actualVector.asPainterPath();
+ qCritical() << "After my simplification, expected: " << expectedVector.asPainterPath();
+
+ compare = fuzzyComparePolygons(actualVector.asPainterPath().toFillPolygon(), expectedVector.asPainterPath().toFillPolygon());
+ qCritical() << ppVar(compare);
+ qCritical() << ppVar(actualVector.asPainterPath().toFillPolygon());
+ qCritical() << ppVar(expectedVector.asPainterPath().toFillPolygon());
+ }
+ assert(compare);
+ /*
+ for(int i = 0; i < expectedPath.elementCount(); i++) {
+ assert(expectedPath.elementAt(i).type == result.elementAt(i).type);
+ assert(KisAlgebra2D::fuzzyPointCompare(expectedPath.elementAt(i), result.elementAt(i)));
+ }
+ */
+ }
+
+}
+
+void convertShapeToDebugArray(const QPainterPath& shape) {
+ // useful to use with Geogebra
+ // TODO: to remove later
+ for (int i = 0; i < shape.elementCount(); i++) {
+ QPainterPath::Element el = shape.elementAt(i);
+ qCritical() << el.x << "\t" << el.y << "\t" << el.type;
+ }
+
+}
+
+void KisAlgebra2DTest::testRemoveGutter()
+{
+ // simple shape: a rectangle with a middle very easy gutter
+ QPolygonF poly;
+ poly << QPointF(0, 0) << QPointF(100, 0) << QPointF(100, 100) << QPointF(0, 100);
+ QPainterPath polyPath;
+ polyPath.addPolygon(poly);
+ polyPath.closeSubpath();
+
+ QPolygonF poly2;
+
+ poly2 << QPointF(200, 0) << QPointF(300, 0) << QPointF(300, 100) << QPointF(200, 100);
+ QPainterPath polyPath2;
+ polyPath2.addPolygon(poly2);
+ polyPath2.closeSubpath();
+
+ QLineF mouseLine = QLineF(QPointF(50, 50), QPointF(250, 50));
+
+ testRemoveGutterOneCase(polyPath, polyPath2, mouseLine, 5, fromPolygon(QRectF(0, 0, 300, 100)));
+ testRemoveGutterOneCase(polyPath, polyPath2.toReversed(), mouseLine, 5, fromPolygon(QRectF(0, 0, 300, 100)));
+
+
+ //qCritical() << result2;
+
+ QPainterPath special1 = makeSpecialShape(true);
+ QPainterPath special2 = makeSpecialShape(false);
+ QPainterPath specialResult = fromPolygon(QVector({QPointF(100, 100), QPointF(900, 100), QPointF(850, 125), QPointF(900, 150), QPointF(850, 175), QPointF(900, 200), QPointF(100, 200), QPointF(150, 175),
+ QPointF(100, 150), QPointF(150, 125), QPointF(100, 100)}));
+
+ testRemoveGutterOneCase(special1, special2, QLineF(QPointF(300, 150), QPointF(700, 150)), 11, specialResult);
+ testRemoveGutterOneCase(special1, special2.toReversed(), QLineF(QPointF(300, 150), QPointF(700, 150)), 11, specialResult);
+
+ //QPainterPath bigOnTop = fromPolygon(QPolygonF(QVector({QPointF(0, 0), QPointF(100, 0)})));
+ QPainterPath bigOnTop = fromPolygon(QRectF(0, 3, 10, 3));
+ QPainterPath smallRight = fromPolygon(QRectF(7, 0, 3, 2));
+ QPainterPath smallMiddle = fromPolygon(QRectF(3, 0, 3, 2));
+ QPainterPath smallLeft = fromPolygon(QRectF(0, 0, 2, 2));
+
+
+ testRemoveGutterOneCase(bigOnTop, smallRight, QLineF(QPointF(8, 1), QPointF(8, 5)), -1, fromPolygon(QVector({QPointF(0, 3), QPointF(7, 3), QPointF(7, 0), QPointF(10, 0), QPointF(10, 6), QPointF(0, 6), QPointF(0, 3)})));
+ testRemoveGutterOneCase(bigOnTop, smallMiddle, QLineF(QPointF(5, 1), QPointF(5, 5)), -1, fromPolygon(QVector({QPointF(0,3), QPointF(3,3), QPointF(3,0), QPointF(6,0), QPointF(6,3), QPointF(10,3), QPointF(10,6), QPointF(0,6), QPointF(0,3)})));
+ testRemoveGutterOneCase(bigOnTop, smallLeft, QLineF(QPointF(1, 1), QPointF(1, 5)), -1, fromPolygon(QVector({QPointF(2,3), QPointF(10,3), QPointF(10,6), QPointF(0,6), QPointF(0,0), QPointF(2,0), QPointF(2,3)})));
+
+
+ //testRemoveGutterOneCase(fromPolygon({QPointF(1134.24,823.696), QPointF(1777.95,819.542), QPointF(2109.36,1598.84), QPointF(1143.4,1625.02)}));
+ QPainterPath onePoly = fromPolygon(QVector({QPointF(2275.28,763.721), QPointF(3270,757.3), QPointF(3270,210), QPointF(1127.23,210),
+ QPointF(1133.64,771.092), QPointF(2236.91,763.971), QPointF(2275.28,763.721)}));
+
+ QPainterPath otherPoly = fromPolygon(QVector({QPointF(1794.87,819.433), QPointF(2264.27,816.404), QPointF(2664.69,1583.79), QPointF(2126.14,1598.38),
+ QPointF(1794.87,819.433)}));
+
+
+ qCritical() << "First poly:";
+ convertShapeToDebugArray(onePoly);
+ qCritical() << "Second poly:";
+ convertShapeToDebugArray(otherPoly);
+
+
+
+
+ //testRemoveGutterOneCase(onePoly, otherPoly, QLineF(QPointF(2010,662),QPointF(2006,938)));
+
+
+
+
+
+
}
void KisAlgebra2DTest::testFindTrianglePoint()
diff --git a/libs/image/tests/kis_algebra_2d_test.h b/libs/image/tests/kis_algebra_2d_test.h
index 35caff69fc..b4a805c55c 100644
--- a/libs/image/tests/kis_algebra_2d_test.h
+++ b/libs/image/tests/kis_algebra_2d_test.h
@@ -40,6 +40,8 @@ private Q_SLOTS:
void testGetParallelLines();
void testGetParallelLines2();
+ void testRemoveGutter();
+
void testFindTrianglePoint();
diff --git a/plugins/tools/tool_knife/CMakeLists.txt b/plugins/tools/tool_knife/CMakeLists.txt
index a7190c2d8b..d59e72b94b 100644
--- a/plugins/tools/tool_knife/CMakeLists.txt
+++ b/plugins/tools/tool_knife/CMakeLists.txt
@@ -3,6 +3,7 @@ set(kritatoolKnife_SOURCES
KisToolKnife.cpp
KisToolKnifeOptionsWidget.cpp
CutThroughShapeStrategy.cpp
+ RemoveGutterStrategy.cpp
)
ki18n_wrap_ui(kritatoolKnife_SOURCES KisToolKnifeOptionsWidget.ui)
diff --git a/plugins/tools/tool_knife/KisToolKnife.cpp b/plugins/tools/tool_knife/KisToolKnife.cpp
index 2a442b0147..3f3708aa9e 100644
--- a/plugins/tools/tool_knife/KisToolKnife.cpp
+++ b/plugins/tools/tool_knife/KisToolKnife.cpp
@@ -35,6 +35,7 @@
#include <KoSelectedShapesProxy.h>
#include "CutThroughShapeStrategy.h"
+#include "RemoveGutterStrategy.h"
#include "kis_paint_layer.h"
#include "kis_algebra_2d.h"
@@ -312,7 +313,13 @@ KoInteractionStrategy *KisToolKnife::createStrategy(KoPointerEvent *event)
{
qCritical() << "Creating a strategy for " << event->point << event->buttons();
KoSelection *selection = canvas()->selectedShapesProxy()->selection();
- return new CutThroughShapeStrategy(this, selection, event->point, m_d->optionsWidget->getCurrentWidth());
+
+ if (m_d->optionsWidget->getToolMode() == KisToolKnifeOptionsWidget::ToolMode::AddGutter) {
+ return new CutThroughShapeStrategy(this, selection, event->point, m_d->optionsWidget->getCurrentWidth());
+ } else {
+ return new RemoveGutterStrategy(this, selection, event->point);
+ }
+
//return NULL;
}
diff --git a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp
index 118bc7ef84..95ae66e85a 100644
--- a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp
+++ b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp
@@ -62,6 +62,16 @@ struct KisToolKnifeOptionsWidget::Private {
}
}
+ KisToolKnifeOptionsWidget::ToolMode getToolMode() {
+ // TODO: obviously gonna be removed later,
+ // replaced with buttons with icons like the Transform Tool has
+ if (ui->comboBox->currentText() == "Add a gutter") {
+ return KisToolKnifeOptionsWidget::AddGutter;
+ } else {
+ return KisToolKnifeOptionsWidget::RemoveGutter;
+ }
+ }
+
};
KisToolKnifeOptionsWidget::KisToolKnifeOptionsWidget(KisCanvasResourceProvider */*provider*/, QWidget *parent)
@@ -109,6 +119,11 @@ int KisToolKnifeOptionsWidget::getWidthForType(GapWidthType type)
}
+KisToolKnifeOptionsWidget::ToolMode KisToolKnifeOptionsWidget::getToolMode()
+{
+ return m_d->getToolMode();
+}
+
diff --git a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h
index 190ae450c9..98f2dab0e1 100644
--- a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h
+++ b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h
@@ -30,6 +30,11 @@ public:
Special
};
+ enum ToolMode {
+ AddGutter,
+ RemoveGutter
+ };
+
int getThickGapWidth(void);
int getThinGapWidth(void);
int getSpecialGapWidth(void);
@@ -37,8 +42,12 @@ public:
int getCurrentWidth();
int getWidthForType(GapWidthType type);
+ ToolMode getToolMode();
+
private:
+ friend class KisToolKnife;
+
struct Private;
const QScopedPointer<Private> m_d;
};
diff --git a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.ui b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.ui
index c6ce70494a..b1a4efd1c1 100644
--- a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.ui
+++ b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.ui
@@ -11,10 +11,27 @@
<x>0</x>
<y>0</y>
<width>250</width>
- <height>189</height>
+ <height>230</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
+ <item>
+ <widget class="QComboBox" name="comboBox">
+ <property name="modelColumn">
+ <number>0</number>
+ </property>
+ <item>
+ <property name="text">
+ <string>Add a gutter</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>Remove a gutter</string>
+ </property>
+ </item>
+ </widget>
+ </item>
<item>
<widget class="QRadioButton" name="thickGapWidthRadioButton">
<property name="text">
diff --git a/plugins/tools/tool_knife/RemoveGutterStrategy.cpp b/plugins/tools/tool_knife/RemoveGutterStrategy.cpp
new file mode 100644
index 0000000000..ef88967241
--- /dev/null
+++ b/plugins/tools/tool_knife/RemoveGutterStrategy.cpp
@@ -0,0 +1,343 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "RemoveGutterStrategy.h"
+
+
+#include <QDebug>
+#include <QPainter>
+
+#include <kis_algebra_2d.h>
+#include <KoToolBase.h>
+#include <KoCanvasBase.h>
+#include <KoViewConverter.h>
+#include <KoSelection.h>
+#include <kis_global.h>
+#include "kis_debug.h"
+#include <KoPathShape.h>
+#include <krita_utils.h>
+#include <kis_canvas2.h>
+#include <QPainterPath>
+#include <KoShapeController.h>
+#include <kundo2command.h>
+#include <KoKeepShapesSelectedCommand.h>
+
+
+
+
+RemoveGutterStrategy::RemoveGutterStrategy(KoToolBase *tool, KoSelection *selection, QPointF startPoint)
+ : KoInteractionStrategy(tool)
+ , m_startPoint(startPoint)
+ , m_endPoint(startPoint)
+{
+ m_selectedShapes = selection->selectedEditableShapes();
+}
+
+RemoveGutterStrategy::~RemoveGutterStrategy()
+{
+
+}
+
+KUndo2Command *RemoveGutterStrategy::createCommand()
+{
+ return 0;
+}
+
+void RemoveGutterStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
+{
+ m_endPoint = mouseLocation;
+ QRectF dirtyRect;
+ KisAlgebra2D::accumulateBounds(m_startPoint, &dirtyRect);
+ KisAlgebra2D::accumulateBounds(m_endPoint, &dirtyRect);
+ dirtyRect = kisGrowRect(dirtyRect, 10); // twice as much as it should need to account for lines showing the effect
+
+ KisCanvas2* kisCanvas = dynamic_cast<KisCanvas2*>(tool()->canvas());
+ //dirtyRect = kisCanvas->viewManager()->
+ //dirtyRect = canvas()->viewConverter()->viewToDocument(dirtyRect);
+ //qCritical() << "And the dirty rect is: " << dirtyRect;
+
+ QRectF accumulatedWithPrevious = m_previousLineDirtyRect | dirtyRect;
+
+ //KisAlgebra2D::accumulateBounds(dirtyRect, &accumulatedWithPrevious);
+ //KisAlgebra2D::accumulateBounds(dirtyRect.topLeft(), &accumulatedWithPrevious);
+ //KisAlgebra2D::accumulateBounds(dirtyRect.bottomRight(), &accumulatedWithPrevious);
+
+ //QRectF rect = dirtyRect + m_d->previousLineDirtyRect;
+
+ tool()->canvas()->updateCanvas(accumulatedWithPrevious);
+ m_previousLineDirtyRect = dirtyRect;
+}
+
+
+void convertShapeToDebugArray(const QPainterPath& shape) {
+ // useful to use with Geogebra
+ // TODO: to remove later
+ for (int i = 0; i < shape.elementCount(); i++) {
+ QPainterPath::Element el = shape.elementAt(i);
+ qCritical() << el.x << "\t" << el.y << "\t" << el.type;
+ }
+
+}
+
+void convertShapeToDebugArray(const QRectF& rect) {
+ // useful to use with Geogebra
+ // TODO: to remove later
+ QPolygonF poly = QPolygonF(rect);
+ for (int i = 0; i < poly.length(); i++) {
+ QPointF p = poly[i];
+ qCritical() << p.x() << "\t" << p.y();
+ }
+
+}
+
+void convertShapeToDebugArray(const QLineF& line) {
+ // useful to use with Geogebra
+ // TODO: to remove later
+ qCritical() << line.p1().x() << "\t" << line.p1().y();
+ qCritical() << line.p2().x() << "\t" << line.p2().y();
+
+}
+
+void RemoveGutterStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
+{
+
+ // so, first find all the shapes whose bounding rect is nonempty when intersected with the line bounding rect
+ // then for every one of them find a path that's crossing the mouse line
+ // save info for later
+ // ensure you have only two paths belonging to two different shapes
+ // WAIT
+ // what about the situation o T
+ // There are three (four?) cases. Gotta look a the picture to understand it!
+ // write it down later, after implementing
+
+
+ // also: when moving the gutter or gutter end, remember about keeping the gutter of the same
+
+
+ qCritical() << "vvvvvvvvvvvvvvvvvvvvvvvvv REMOVING A GUTTER vvvvvvvvvvvvvvvvvvvvvvvvv";
+
+
+ KisCanvas2 *kisCanvas = static_cast<KisCanvas2 *>(tool()->canvas());
+ KIS_SAFE_ASSERT_RECOVER_RETURN(kisCanvas);
+ const QTransform booleanWorkaroundTransform = KritaUtils::pathShapeBooleanSpaceWorkaround(kisCanvas->image());
+
+ QList<QPainterPath> srcOutlines;
+ QList<QPainterPath> srcOutlinesOutside;
+
+ //QRectF outlineRect;
+
+ if (m_selectedShapes.length() == 0) {
+ qCritical() << "No shapes are selected";
+ return;
+ }
+
+ QLineF mouseLine = QLineF(m_startPoint, m_endPoint);
+ QRectF lineRect = KisAlgebra2D::createRectFromCorners(m_startPoint, m_endPoint);
+
+ qCritical() << "Line and line rect before boolean trans: " << mouseLine << lineRect;
+ mouseLine = booleanWorkaroundTransform.map(mouseLine);
+ lineRect = KisAlgebra2D::createRectFromCorners(mouseLine.p1(), mouseLine.p2());
+
+
+ qCritical() << "Line and line rect after boolean trans: " << mouseLine << lineRect;
+ qCritical() << "Line:";
+ convertShapeToDebugArray(mouseLine);
+ qCritical() << "Rect:";
+ convertShapeToDebugArray(lineRect);
+ qCritical() << "end.";
+
+
+ QList<int> indexes;
+ QList<int> indexesOutside;
+
+ for (int i = 0; i < m_selectedShapes.length(); i++) {
+ KoShape* shape = m_selectedShapes[i];
+ QPainterPath outlineHere =
+ booleanWorkaroundTransform.map(
+ shape->absoluteTransformation().map(
+ shape->outline()));
+
+ qCritical() << "> Selected path (in boolean): ";
+ convertShapeToDebugArray(outlineHere);
+
+ if (outlineHere.boundingRect().intersects(lineRect)) {
+ srcOutlines << outlineHere;
+ indexes << i;
+ //outlineRect |= outlineHere.boundingRect();
+ } else {
+ srcOutlinesOutside << outlineHere;
+ indexesOutside << i;
+ }
+ }
+
+ if (srcOutlines.isEmpty()) {
+ qCritical() << "The list of possible shapes is empty";
+ return;
+ }
+
+
+ QList<int> shapeNewIndexes;
+ QList<int> shapeOrigIndexes;
+ QList<int> lineSegmentIndexes;
+
+
+ for (int i = 0; i < srcOutlines.length(); i++) {
+ QList<int> lineIndexes = KisAlgebra2D::getLineSegmentCrossingLineIndexes(mouseLine, srcOutlines[i]);
+ int shapeOrigIndex = indexes[i];
+
+ if (lineIndexes.length() > 0) {
+ Q_FOREACH(int lineIndex, lineIndexes) {
+ shapeNewIndexes << i;
+ lineSegmentIndexes << lineIndex;
+ shapeOrigIndexes << shapeOrigIndex;
+ }
+ } else {
+ srcOutlinesOutside << srcOutlines[i];
+ indexesOutside << shapeOrigIndex;
+ }
+ }
+
+ if (shapeNewIndexes.length() != 2) {
+
+ qCritical() << "Shape indexes count isn't correct: " << ppVar(shapeNewIndexes.length()) << ppVar(lineSegmentIndexes.length());
+ qCritical() << "Mouse line in used coordinates: " << mouseLine;
+ qCritical() << "Number of shapes even considered: " << srcOutlines.length();
+ Q_FOREACH(QPainterPath path, srcOutlines) {
+ qCritical() << "> A path: ";
+ convertShapeToDebugArray(path);
+ }
+ qCritical() << "That's the end of shapes considered.";
+ qCritical() << "Shapes not considered: " << srcOutlinesOutside.length();
+ Q_FOREACH(QPainterPath path, srcOutlinesOutside) {
+ qCritical() << "> A path: ";
+ convertShapeToDebugArray(path);
+ }
+ qCritical() << "That's the end of shapes not considered.";
+ for(int i = 0; i < shapeNewIndexes.length(); i++) {
+ int index = shapeNewIndexes[i];
+ qCritical() << "Shape: ";
+ convertShapeToDebugArray(srcOutlines[index]);
+ qCritical() << "Line index: " << lineSegmentIndexes[i] << " meaning it's: " << srcOutlines[index].elementAt(KisAlgebra2D::wrapValue(lineSegmentIndexes[i], 0, srcOutlines[index].elementCount()))
+ << srcOutlines[index].elementAt(KisAlgebra2D::wrapValue(lineSegmentIndexes[i] + 1, 0, srcOutlines[index].elementCount()));
+ }
+
+ qCritical() << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF REMOVING A GUTTER, UNSUCCESSFUL";
+
+ // TODO: this if should end here; code below adds a debug line showing the mouse line
+
+ QPainterPath newLineShape = QPainterPath();
+ newLineShape.moveTo(mouseLine.p1());
+ newLineShape.lineTo(mouseLine.p2());
+
+ newLineShape = booleanWorkaroundTransform.inverted().map(newLineShape);
+ KoPathShape* newLineShapeToAdd = KoPathShape::createShapeFromPainterPath(newLineShape);
+
+ newLineShapeToAdd->setBackground(m_selectedShapes[0]->background());
+ newLineShapeToAdd->setStroke(m_selectedShapes[0]->stroke());
+ newLineShapeToAdd->setZIndex(m_selectedShapes[0]->zIndex() + 100);
+
+
+
+ KUndo2Command *cmd = new KUndo2Command(kundo2_i18n("Knife tool: cut through shapes"));
+ tool()->canvas()->shapeController()->addShapeDirect(newLineShapeToAdd, m_selectedShapes[0]->parent(), cmd);
+ tool()->canvas()->addCommand(cmd);
+
+ return;
+ }
+
+ // KisAlgebra2D::getLineSegmentCrossingLineIndexes(QLineF const&, QPainterPath const&)
+
+ qCritical() << "Found two shapes.";
+ qCritical() << "Shape 1:";
+ convertShapeToDebugArray(srcOutlines[shapeNewIndexes[0]]);
+ qCritical() << "Shape 2:";
+ convertShapeToDebugArray(srcOutlines[shapeNewIndexes[1]]);
+
+
+
+ QPainterPath result = KisAlgebra2D::removeGutterSmart(srcOutlines[shapeNewIndexes[0]], lineSegmentIndexes[0], srcOutlines[shapeNewIndexes[1]], lineSegmentIndexes[1]);
+
+ QList<KoShape*> resultShapes;
+
+ qCritical() << "Indexes that are supposed to be left:";
+ Q_FOREACH(int index, indexesOutside) {
+ qCritical() << index;
+ }
+ qCritical() << "End.";
+ qCritical() << "Indexes that compose the result shape: " << shapeOrigIndexes[0] << shapeOrigIndexes[1];
+ qCritical() << "All indexes are up to: " << m_selectedShapes.length();
+
+
+ Q_FOREACH(int index, indexesOutside) {
+ resultShapes << m_selectedShapes[index];
+ }
+
+ // since we can't really decide which style to use, we're gonna use the style of the first found shape.
+ // if the user doesn't like it, they can change it.
+
+
+ result = booleanWorkaroundTransform.inverted().map(result);
+ KoPathShape* resultShape = KoPathShape::createShapeFromPainterPath(result);
+
+ if (resultShape->boundingRect().isEmpty()) {
+ return;
+ }
+
+ KUndo2Command *cmd = new KUndo2Command(kundo2_i18n("Knife tool: remove a gutter"));
+
+
+
+ new KoKeepShapesSelectedCommand(m_selectedShapes, {}, tool()->canvas()->selectedShapesProxy(), false, cmd);
+
+
+ KoShape* referenceShape = m_selectedShapes[shapeOrigIndexes[0]];
+ KoPathShape* koPathReferenceShape = dynamic_cast<KoPathShape*>(referenceShape);
+ resultShape->setBackground(referenceShape->background());
+ resultShape->setStroke(referenceShape->stroke());
+ resultShape->setZIndex(referenceShape->zIndex());
+ if (koPathReferenceShape) {
+ resultShape->setFillRule(koPathReferenceShape->fillRule());
+ }
+
+
+ KoShapeContainer *parent = referenceShape->parent();
+ tool()->canvas()->shapeController()->addShapeDirect(resultShape, parent, cmd);
+
+ resultShapes << resultShape;
+
+ QList<KoShape*> shapesToRemove;
+ shapesToRemove << m_selectedShapes[shapeOrigIndexes[0]];
+ if (shapeOrigIndexes[0] != shapeOrigIndexes[1]) { // there is a good reason in the workflow to allow doing this operation on the same shape
+ shapesToRemove << m_selectedShapes[shapeOrigIndexes[1]];
+ }
+
+
+
+
+ tool()->canvas()->shapeController()->removeShapes(shapesToRemove, cmd);
+
+ new KoKeepShapesSelectedCommand({}, resultShapes, tool()->canvas()->selectedShapesProxy(), true, cmd);
+
+
+ // this line sometimes causes a segfault for some reason???
+ tool()->canvas()->addCommand(cmd);
+
+
+
+ qCritical() << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF REMOVING A GUTTER, SUCCESSFUL, yay! :) ";
+
+
+
+
+}
+
+void RemoveGutterStrategy::paint(QPainter &painter, const KoViewConverter &converter)
+{
+ painter.save();
+ painter.setPen(QPen(QBrush(Qt::black), 5));
+ painter.drawLine(converter.documentToView().map(QLineF(m_startPoint, m_endPoint)));
+ painter.restore();
+
+}
diff --git a/plugins/tools/tool_knife/RemoveGutterStrategy.h b/plugins/tools/tool_knife/RemoveGutterStrategy.h
new file mode 100644
index 0000000000..3a24476520
--- /dev/null
+++ b/plugins/tools/tool_knife/RemoveGutterStrategy.h
@@ -0,0 +1,37 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef REMOVEGUTTERSTRATEGY_H
+#define REMOVEGUTTERSTRATEGY_H
+
+#include <QScopedPointer>
+#include <QRectF>
+
+#include <KoInteractionStrategy.h>
+#include <KoShape.h>
+
+class KoSelection;
+
+
+class RemoveGutterStrategy : public KoInteractionStrategy
+{
+public:
+ RemoveGutterStrategy(KoToolBase *tool, KoSelection *selection, QPointF startPoint);
+ ~RemoveGutterStrategy() override;
+
+ KUndo2Command *createCommand() override;
+
+ void handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override;
+ void finishInteraction(Qt::KeyboardModifiers modifiers) override;
+ void paint(QPainter &painter, const KoViewConverter &converter) override;
+
+private:
+ QPointF m_startPoint = QPointF();
+ QPointF m_endPoint = QPointF();
+ QRectF m_previousLineDirtyRect = QRectF();
+ QList<KoShape *> m_selectedShapes;
+};
+
+#endif // REMOVEGUTTERSTRATEGY_H