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

Add Knife Tool prototype to Krita

Agata Cacko71b6382c9f9a
diff --git a/libs/global/kis_algebra_2d.cpp b/libs/global/kis_algebra_2d.cpp
index 99c2bd9ad8..cbbbe8eb01 100644
--- a/libs/global/kis_algebra_2d.cpp
+++ b/libs/global/kis_algebra_2d.cpp
@@ -1304,6 +1304,180 @@ void cropLineToConvexPolygon(QLineF &line, const QPolygonF polygon, bool extendF
}
}
+int lineSideForPoint(const QLineF &line, const QPointF &point)
+{
+ if (fuzzyPointCompare(point, line.p1())) {
+ return 0;
+ }
+ if (fuzzyPointCompare(point, line.p2())) {
+ return 0;
+ }
+ if (qFuzzyCompare(line.length(), 0)) {
+ return 0;
+ }
+
+ qreal whichSide = KisAlgebra2D::crossProduct(line.p2() - line.p1(), point - line.p1());
+ return whichSide == 0 ? 0 : (whichSide > 0 ? 1 : -1);
+}
+
+QPolygonF combineConvexHullParts(const QPolygonF &leftPolygon, QPolygonF &rightPolygon, bool triangular) {
+ // resulting polygon should start at p1, go through all the points, go to p2, then to p1 again
+ // triangular: whether the last point of right is equal to the first point in left or not
+
+ QPolygonF left = leftPolygon;
+ QPolygonF right = rightPolygon;
+
+ left.removeLast(); // remove p1 from the end (but not the beginning)
+ left.removeLast(); // remove nextPoint as well, since it's present in rightPolygon (twice)
+
+ right.removeLast(); // remove nextPoint from the end (but not the beginning)
+
+
+ QPolygonF result;
+ Q_FOREACH(QPointF point, left) {
+ result << point;
+ }
+
+ Q_FOREACH(QPointF point, right) {
+ result << point;
+ }
+
+ if (triangular) {
+ result << left[0];
+ }
+
+ return result;
+}
+
+QPolygonF calculateConvexHullFromPointsOverTheLine(const QPolygonF &points, const QLineF &line)
+{
+ // all the points should be on the correct side already, no need to check it
+ if (points.count() < 1) {
+ QPolygonF result;
+ result << line.p1() << line.p2() << line.p1();
+ return result;
+ }
+ if (points.count() == 1) {
+ QList<QPointF> list = points.toList();
+ qCritical() << list.length() << list.count() << list.empty() << list.first();
+ //qCritical() << points << points.count() << points.length();
+ //qCritical() << points[0];
+ //QPointF p = points[0];
+
+ QPolygonF result;
+ result << line.p1();
+ result << list[0];
+ result << line.p2();
+ result << line.p1();
+
+ //result << line.p1() << points[0] << line.p2() << line.p1();
+ return result;
+ }
+
+ double maxDistance = 0;
+ QPointF nextPoint = points[0];
+ Q_FOREACH(QPointF point, points) {
+ double distance = kisSquareDistanceToLine(point, line);
+ if (distance > maxDistance) {
+ maxDistance = distance;
+ nextPoint = point;
+ }
+ }
+
+ QPolygonF left;
+ QLineF lineForLeft = QLineF(line.p1(), nextPoint);
+ QPolygonF right;
+ QLineF lineForRight = QLineF(nextPoint, line.p2());
+ QPolygonF triangle;
+ triangle << line.p1() << line.p2() << nextPoint << line.p1();
+ Q_FOREACH(QPointF point, points) {
+ if (triangle.containsPoint(point, Qt::WindingFill)) {
+ continue;
+ }
+ if (lineSideForPoint(lineForLeft, point) > 0) {
+ left << point;
+ } else if (lineSideForPoint(lineForRight, point) < 0) {
+ right << point;
+ }
+ }
+
+ QPolygonF leftPolygon = calculateConvexHullFromPointsOverTheLine(left, lineForLeft);
+ QPolygonF rightPolygon = calculateConvexHullFromPointsOverTheLine(right, lineForRight);
+
+ QPolygonF result = combineConvexHullParts(leftPolygon, rightPolygon, true);
+
+ return result;
+}
+
+
+QPolygonF calculateConvexHull(const QPolygonF &polygon)
+{
+ if (polygon.count() < 4) {
+ return polygon;
+ }
+ QPointF leftPoint = polygon[0];
+ QPointF rightPoint = polygon[1];
+ if (leftPoint.x() > rightPoint.x()) { // swap them around
+ leftPoint = polygon[1];
+ rightPoint = polygon[0];
+ }
+
+ Q_FOREACH(QPointF point, polygon) {
+ if (point.x() < leftPoint.x()) {
+ leftPoint = point;
+ } else if (point.x() > rightPoint.x()) {
+ rightPoint = point;
+ }
+ }
+ QLineF line(leftPoint, rightPoint);
+ QLineF lineOther(rightPoint, leftPoint);
+ QPolygonF left;
+ QPolygonF right;
+
+ Q_FOREACH(QPointF point, polygon) {
+ qCritical() << "Checking point " << point << "and line" << line << ": " << lineSideForPoint(line, point);
+ if (lineSideForPoint(line, point) > 0) {
+ left << point;
+ } else if (lineSideForPoint(line, point) < 0) {
+ right << point;
+ }
+ }
+
+ qCritical() << "Using line: " << line << "for points: " << left;
+ qCritical() << "Using line: " << lineOther << "for points: " << right;
+
+ left = calculateConvexHullFromPointsOverTheLine(left, line);
+ right = calculateConvexHullFromPointsOverTheLine(right, lineOther);
+
+ qCritical() << "Then the result: " << left << right;
+
+ QPolygonF result = combineConvexHullParts(left, right, false);
+
+ return result;
+
+}
+
+QList<QLineF> intersectLineConcavePolygon(const QPolygonF polygon, const QLineF& line, bool extendFirst, bool extendSecond) {
+
+ QPolygonF convexHull = calculateConvexHull(polygon);
+ if (convexHull.count() == polygon.count()) {
+ QLineF resultLine = line;
+ bool result = intersectLineConvexPolygon(resultLine, polygon, extendFirst, extendSecond);
+ if (result) {
+ return QList<QLineF>() << resultLine;
+ } else {
+ return QList<QLineF>();
+ }
+ }
+
+ // it was a concave polygon
+ // intersectLines
+
+ KIS_ASSERT(false && "Not implemented yet");
+
+ return QList<QLineF>();
+}
+
qreal findMinimumGoldenSection(std::function<qreal(qreal)> f, qreal xA, qreal xB, qreal eps, int maxIter = 100)
{
diff --git a/libs/global/kis_algebra_2d.h b/libs/global/kis_algebra_2d.h
index 58a08f6787..bdf82df552 100644
--- a/libs/global/kis_algebra_2d.h
+++ b/libs/global/kis_algebra_2d.h
@@ -417,8 +417,10 @@ bool KRITAGLOBAL_EXPORT intersectLineRect(QLineF &line, const QRect rect, bool e
// the same but with a convex polygon; uses Cyrus-Beck algorithm
bool KRITAGLOBAL_EXPORT intersectLineConvexPolygon(QLineF &line, const QPolygonF polygon, bool extendFirst, bool extendSecond);
+//QList<QLineF> KRITAGLOBAL_EXPORT intersectLineConcavePolygon(const QPolygonF polygon, const QLineF& line, bool extendFirst, bool extendSecond);
+
/**
- * Crop line to rect; if it doesn't intersect, just return an empty line (QLineF()).
+ * @brief Crop line to rect; if it doesn't intersect, just return an empty line (QLineF()).
*
* This is using intersectLineRect, but with the difference that it doesn't require the user to check the return value.
* It's useful for drawing code, since it let the developer not use `if` before drawing.
@@ -439,7 +441,12 @@ void KRITAGLOBAL_EXPORT cropLineToRect(QLineF &line, const QRect rect, bool exte
void KRITAGLOBAL_EXPORT cropLineToConvexPolygon(QLineF &line, const QPolygonF polygon, bool extendFirst, bool extendSecond);
-
+/**
+ * @brief calculateConvexHull Calculate the convex hull of the polygon using the QuickHull
+ * @param polygon to find the convex hull of
+ * @return
+ */
+QPolygonF KRITAGLOBAL_EXPORT calculateConvexHull(const QPolygonF &polygon);
template <class Point>
diff --git a/libs/global/kis_global.h b/libs/global/kis_global.h
index fdad1fe2af..55bf07059b 100644
--- a/libs/global/kis_global.h
+++ b/libs/global/kis_global.h
@@ -253,6 +253,30 @@ inline qreal kisDistanceToLine(const QPointF &m, const QLineF &line)
return distance;
}
+inline qreal kisSquareDistanceToLine(const QPointF &m, const QLineF &line)
+{
+ const QPointF &p1 = line.p1();
+ const QPointF &p2 = line.p2();
+
+ qreal distance = 0;
+
+ if (qFuzzyCompare(p1.x(), p2.x())) {
+ distance = pow2(m.x() - p2.x());
+ } else if (qFuzzyCompare(p1.y(), p2.y())) {
+ distance = pow2(m.y() - p2.y());
+ } else {
+ qreal A = 1;
+ qreal B = - (p1.x() - p2.x()) / (p1.y() - p2.y());
+ qreal C = - p1.x() - B * p1.y();
+
+ distance = pow2(A * m.x() + B * m.y() + C) / (pow2(A) + pow2(B));
+ }
+
+ return distance;
+
+}
+
+
inline QPointF kisProjectOnVector(const QPointF &base, const QPointF &v)
{
const qreal prod = base.x() * v.x() + base.y() * v.y();
diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cpp
index 6d78d82c2a..988e907fc6 100644
--- a/libs/image/tests/kis_algebra_2d_test.cpp
+++ b/libs/image/tests/kis_algebra_2d_test.cpp
@@ -999,6 +999,52 @@ void KisAlgebra2DTest::testLinePolygonIntersectionsManual()
}
+bool fuzzyComparePolygons(const QPolygonF &one, const QPolygonF &other) {
+ if (one.count() != other.count()) {
+ return false;
+ }
+ bool firstWay = true;
+ for (int i = 0; i < one.count(); i++) {
+ if (!KisAlgebra2D::fuzzyPointCompare(one[i], other[i])) {
+ firstWay = false;
+ break;
+ }
+ }
+ if (firstWay) {
+ return true;
+ }
+ for (int i = 0; i < one.count(); i++) {
+ if (!KisAlgebra2D::fuzzyPointCompare(one[one.count() - i - 1], other[i])) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void KisAlgebra2DTest::testCalculateConvexHull()
+{
+ using KisAlgebra2D::calculateConvexHull;
+
+ QPolygonF first;
+ first << QPointF(0, 0) << QPointF(0, 100) << QPointF(100, 100) << QPointF(100, 0) << QPointF(0, 0);
+ QPolygonF firstResult = calculateConvexHull(first);
+ qCritical() << first;
+ qCritical() << firstResult;
+ assert(fuzzyComparePolygons(first, firstResult));
+
+ QPolygonF second;
+ second << QPointF(0, 0) << QPointF(100, 0) << QPointF(100, 10) << QPointF(50, 10) << QPointF(50, 90) << QPointF(100, 90) << QPointF(100, 100) << QPointF(0, 100) << QPointF(0, 0);
+ QPolygonF secondResultExpected;
+ secondResultExpected << QPointF(0, 0) << QPointF(100, 0) << QPointF(100, 100) << QPointF(0, 100) << QPointF(0, 0);
+ QPolygonF secondResult = calculateConvexHull(second);
+
+ qCritical() << "second result was: " << secondResult;
+ qCritical() << "while expected was: " << secondResultExpected;
+ assert(fuzzyComparePolygons(secondResultExpected, secondResult));
+
+
+}
+
void KisAlgebra2DTest::testFindTrianglePoint()
{
using KisAlgebra2D::findTrianglePoint;
diff --git a/libs/image/tests/kis_algebra_2d_test.h b/libs/image/tests/kis_algebra_2d_test.h
index 223bc11f23..e64b887a89 100644
--- a/libs/image/tests/kis_algebra_2d_test.h
+++ b/libs/image/tests/kis_algebra_2d_test.h
@@ -35,6 +35,8 @@ private Q_SLOTS:
void testLinePolygonIntersectionsManual();
+ void testCalculateConvexHull();
+
void testFindTrianglePoint();
void testTriangularMotion();
diff --git a/plugins/tools/CMakeLists.txt b/plugins/tools/CMakeLists.txt
index b52a2b2af6..abf3ba2558 100644
--- a/plugins/tools/CMakeLists.txt
+++ b/plugins/tools/CMakeLists.txt
@@ -11,6 +11,8 @@ add_subdirectory( tool_lazybrush )
add_subdirectory( tool_smart_patch )
add_subdirectory( svgtexttool )
add_subdirectory( tool_enclose_and_fill )
+add_subdirectory( tool_knife )
+
install( FILES
tools.action
diff --git a/plugins/tools/tool_knife/CMakeLists.txt b/plugins/tools/tool_knife/CMakeLists.txt
new file mode 100644
index 0000000000..a7190c2d8b
--- /dev/null
+++ b/plugins/tools/tool_knife/CMakeLists.txt
@@ -0,0 +1,17 @@
+set(kritatoolKnife_SOURCES
+ ToolKnife.cpp
+ KisToolKnife.cpp
+ KisToolKnifeOptionsWidget.cpp
+ CutThroughShapeStrategy.cpp
+ )
+
+ki18n_wrap_ui(kritatoolKnife_SOURCES KisToolKnifeOptionsWidget.ui)
+
+kis_add_library(kritatoolKnife MODULE ${kritatoolKnife_SOURCES})
+
+generate_export_header(kritatoolKnife BASE_NAME kritatoolKnife)
+
+target_link_libraries(kritatoolKnife kritaui)
+
+install(TARGETS kritatoolKnife DESTINATION ${KRITA_PLUGIN_INSTALL_DIR})
+
diff --git a/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp b/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp
new file mode 100644
index 0000000000..c0e9d757e7
--- /dev/null
+++ b/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp
@@ -0,0 +1,437 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "CutThroughShapeStrategy.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>
+
+
+CutThroughShapeStrategy::CutThroughShapeStrategy(KoToolBase *tool, KoSelection *selection, QPointF startPoint, qreal width)
+ : KoInteractionStrategy(tool)
+ , m_startPoint(startPoint)
+ , m_width(width)
+{
+ qCritical() << "Creating a strategy";
+
+ m_selectedShapes = selection->selectedEditableShapes();
+ qCritical() << "Amount of selected shapes: " << m_selectedShapes.length();
+}
+
+CutThroughShapeStrategy::~CutThroughShapeStrategy()
+{
+
+}
+
+KUndo2Command *CutThroughShapeStrategy::createCommand()
+{
+ // TODO: undoing
+ return 0;
+}
+
+void CutThroughShapeStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
+{
+ m_endPoint = mouseLocation;
+ //qCritical() << "Now supposedly drawing a line between " << m_d->startPoint << " to " << m_d->endPoint;
+ QRectF dirtyRect;
+ KisAlgebra2D::accumulateBounds(m_startPoint, &dirtyRect);
+ KisAlgebra2D::accumulateBounds(m_endPoint, &dirtyRect);
+ //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;
+
+}
+
+QList<QLineF> getParallelLines(const QLineF& line, const qreal distance) {
+ if (line.length() == 0) {
+ return QList<QLineF>();
+ }
+ QPointF lineVector = line.p2() - line.p1();
+ // dist(P, l) = |Ax + By + C|/(sqrt(A^2 + B^2))
+ // dist(P, l) = |Vy*x - Vx*y + (p2x*p1y - p2y*p1x)|/(sqrt(line.length()))
+ // |Vy*x - Vx*y + M| = distance*sqrt(line.length())
+ // // that gives us two line equations:
+ // Vy*x - Vx*y + M +/- distance*sqrt(line.length()) = 0
+ // if Vy != 0:
+ // x = (Vx*y + N)/Vy // where N = M +/- distance*sqrt(line.length())
+ // else:
+ // y = (Vy*x + N)/Vx // where N = M +/- distance*sqrt(line.length())
+
+ qreal M = line.p2().x()*line.p1().y() - line.p2().y()*line.p1().x();
+ qreal N1 = M - distance*sqrt(line.length());
+ qreal N2 = M + distance*sqrt(line.length());
+
+ qreal Vx = lineVector.x();
+ qreal Vy = lineVector.y();
+
+
+ auto calculateSecondDimension = [](qreal dim1, qreal N, qreal V1, qreal V2) {
+ //(Vx*y + N)/Vy
+ return (dim1*V2 + N)/V1;
+ };
+
+ QList<QLineF> responses;
+
+ if (lineVector.x() != 0) {
+ // y = (Vx*y + N)/Vx // where N = M +/- distance*sqrt(line.length())
+
+ qreal x1 = line.p1().x();
+ qreal x2 = line.p2().x();
+ //qreal y1 = (lineVector.y()*x1 + N1
+ responses << QLineF(x1, calculateSecondDimension(x1, N1, Vx, Vy), x2, calculateSecondDimension(x2, N1, Vx, Vy));
+ responses << QLineF(x1, calculateSecondDimension(x1, N2, Vx, Vy), x2, calculateSecondDimension(x2, N2, Vx, Vy));
+
+ } else {
+
+ qreal y1 = line.p1().y();
+ qreal y2 = line.p2().y();
+ //qreal y1 = (lineVector.y()*x1 + N1
+ responses << QLineF(calculateSecondDimension(y1, N1, Vy, Vx), y1, calculateSecondDimension(y2, N1, Vy, Vx), y2);
+ responses << QLineF(calculateSecondDimension(y1, N2, Vy, Vx), y1, calculateSecondDimension(y2, N2, Vy, Vx), y2);
+ }
+
+ return responses;
+
+
+}
+
+QPainterPath getOnePathFromRectangleCutThrough(const QList<QPointF> &points, const QLineF &line, bool left) {
+
+ qCritical() << "~~~ ONE PATH at a time ~~~" << left;
+ qCritical() << "line: " << line << "left: " << left << "points:" << points[0] << points[1] << points[2] << points[3];
+
+ auto onTheLine = [](QPointF first, QPointF second) {
+ //qCritical() << "Fuzzy compating " << first << " and " << second << "more precisely: " << first.y() << second.y();
+ //qCritical() << "X: " << qFuzzyCompare(first.x(), second.x()) << " Y: " << qFuzzyCompare(first.y(), second.y());
+ //qCritical() << qAbs(first.x() - second.x()) << qAbs(first.y() - second.y());
+ //return qFuzzyCompare(first.x(), second.x()) || qFuzzyCompare(first.y(), second.y());
+ //return KisAlgebra2D::fuzzyPointCompare(first, second, 0.5f);
+ float delta = 0.1f;
+ return qAbs(first.x() - second.x()) < delta || qAbs(first.y() - second.y()) < delta;
+ };
+
+
+ bool started = false;
+ QPainterPath path;
+
+ path.moveTo(line.p1());
+ path.lineTo(line.p2());
+
+ qCritical() << "path after initialization: " << path;
+
+ auto stopFor = [](int index, int max, bool left) {
+ if (left) {
+ return index <= 0;
+ } else {
+ return index >= max;
+ }
+ };
+
+ int startValue, increment;
+ if (left) {
+ startValue = 2*points.length();
+ increment = -1;
+ } else {
+ startValue = 0;
+ increment = 1;
+ }
+
+ QList<QPointF> availablePoints;
+ Q_FOREACH(QPointF point, points) {
+ qreal whichSide = KisAlgebra2D::crossProduct(line.p2() - line.p1(), line.p2() - point);
+ if (whichSide*(left ? 1 : -1) >= 0) {
+ availablePoints << point;
+ qCritical() << "Available point:" << point;
+ }
+ }
+
+ for (int i = startValue; !stopFor(i, 2*availablePoints.length(), left); i += increment) {
+ int index = KisAlgebra2D::wrapValue(i, 0, availablePoints.length());
+ qCritical() << "Index is: " << index << "started: " << started << "on the line: " << onTheLine(availablePoints[index], path.currentPosition());
+ qCritical() << "...And current position is: " << path.currentPosition() << "while the point at index is: " << availablePoints[index];
+
+ if (onTheLine(availablePoints[index], path.currentPosition())) {
+ qCritical() << "it is on the line";
+ if (!started) {
+ qCritical() << "it Starts now, at" << availablePoints[index] << "but without it";
+ started = true;
+
+ // TODO: if it's not the same point?
+ path.lineTo(availablePoints[index]);
+
+ } else {
+ // usually would add, unless it's the ending
+ qCritical() << "it started already, we're at" << availablePoints[index];
+ if (onTheLine(availablePoints[index], line.p1())) {
+ path.lineTo(availablePoints[index]);
+ qCritical() << "We're on the line with p1, so ending the path: " << path;
+ break;
+ }
+ path.lineTo(availablePoints[index]);
+ qCritical() << "Path after adding a new point with a line:" << path;
+ }
+ }
+ }
+
+
+
+ return path;
+
+ for (int i = startValue; !stopFor(i, 2*points.length(), left); i += increment) {
+ int index = KisAlgebra2D::wrapValue(i, 0, points.length());
+ qCritical() << "Index is: " << index << "started: " << started << "on the line: " << onTheLine(points[index], path.currentPosition());
+ qCritical() << "...And current position is: " << path.currentPosition() << "while the point at index is: " << points[index];
+ qreal whichSide = KisAlgebra2D::dotProduct(line.p2() - line.p1(), line.p2() - points[index]);
+ if (whichSide*(left ? 1 : -1) >= 0) {
+ continue;
+ }
+
+ if (onTheLine(points[index], path.currentPosition())) {
+ qCritical() << "it is on the line";
+ if (!started) {
+ qCritical() << "it Starts now, at" << points[index] << "but without it";
+ started = true;
+
+ // TODO: if it's not the same point?
+ path.lineTo(points[index]);
+
+ } else {
+ // usually would add, unless it's the ending
+ qCritical() << "it started already, we're at" << points[index];
+ if (onTheLine(points[index], line.p1())) {
+ qCritical() << "We're on the line with p1, so ending the path: " << path;
+ break;
+ }
+ path.lineTo(points[index]);
+ qCritical() << "Path after adding a new point with a line:" << path;
+ }
+ }
+ }
+
+ qCritical() << "~~~ ONE PATH ^^^ ~~~" << left;
+ return path;
+}
+
+QList<QPainterPath> getPathsFromRectangleCutThrough(QRectF rect, QLineF line1, QLineF line2) {
+
+ qCritical() << "~~~ BOTH PATHS ~~~";
+
+ QPainterPath left;
+ QPainterPath right;
+
+ left.moveTo(line1.p1());
+ left.lineTo(line1.p2());
+
+ right.moveTo(line2.p1());
+ right.lineTo(line2.p2());
+
+ QList<QPointF> rectPoints;
+ rectPoints << rect.topLeft() << rect.bottomLeft() << rect.bottomRight() << rect.topRight();
+
+ left = getOnePathFromRectangleCutThrough(rectPoints, line1, true);
+ right = getOnePathFromRectangleCutThrough(rectPoints, line2, false);
+
+ QList<QPainterPath> paths;
+ paths << left << right;
+
+ qCritical() << "~~~ BOTH PATHS ^^^ ~~~";
+
+ return paths;
+}
+
+void CutThroughShapeStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
+{
+ tool()->canvas()->updateCanvas(m_previousLineDirtyRect);
+
+
+ KisCanvas2 *kisCanvas = static_cast<KisCanvas2 *>(tool()->canvas());
+ KIS_SAFE_ASSERT_RECOVER_RETURN(kisCanvas);
+ const QTransform booleanWorkaroundTransform = KritaUtils::pathShapeBooleanSpaceWorkaround(kisCanvas->image());
+
+ QList<QPainterPath> srcOutlines;
+ QRectF outlineRect;
+
+ if (m_selectedShapes.length() == 0) {
+ qCritical() << "No shapes are selected";
+ return;
+ }
+
+ Q_FOREACH (KoShape *shape, m_selectedShapes) {
+
+ QPainterPath outlineHere =
+ booleanWorkaroundTransform.map(
+ shape->absoluteTransformation().map(
+ shape->outline()));
+
+ srcOutlines << outlineHere;
+ outlineRect |= outlineHere.boundingRect();//booleanWorkaroundTransform.map(shape->absoluteOutlineRect()).boundingRect();
+ }
+
+ if (outlineRect.isEmpty()) {
+
+ qCritical() << "The outline rect is empty";
+ return;
+ }
+
+ QRectF outlineRectBigger = kisGrowRect(outlineRect, 10);
+ QRect outlineRectBiggerInt = outlineRectBigger.toRect();
+
+ // TODO: two lines, for both sides of the gap
+ QLineF gapLine = QLineF(m_startPoint, m_endPoint);
+
+ QList<QLineF> gapLines = getParallelLines(gapLine, m_width);
+
+ gapLine = booleanWorkaroundTransform.map(gapLine);
+ gapLines[0] = booleanWorkaroundTransform.map(gapLines[0]);
+ gapLines[1] = booleanWorkaroundTransform.map(gapLines[1]);
+
+ QLineF leftLine = gapLines[0];
+ QLineF rightLine = gapLines[1];
+
+
+ KisAlgebra2D::cropLineToRect(leftLine, outlineRectBiggerInt, true, true);
+ KisAlgebra2D::cropLineToRect(rightLine, outlineRectBiggerInt, true, true);
+
+
+ if (leftLine.length() == 0 || rightLine.length() == 0) {
+ qCritical() << "Line is: " << leftLine << rightLine << " and the outline rect is: " << outlineRect << ppVar(outlineRectBigger);
+ return; // there would be no cutting
+ }
+
+
+ QList<QPainterPath> paths = getPathsFromRectangleCutThrough(QRectF(outlineRectBiggerInt), leftLine, rightLine);
+ QPainterPath left = paths[0];
+ QPainterPath right = paths[1];
+
+
+
+
+
+
+
+
+ Q_FOREACH(KoShape* shape, m_selectedShapes) {
+ KoPathShape* pathShape = dynamic_cast<KoPathShape*>(shape);
+ if (pathShape) {
+ QPainterPath path = pathShape->outline();
+ QRectF rect = pathShape->absoluteOutlineRect();
+
+ }
+ }
+
+
+ //QPainterPath dstOutline;
+ //QList<QPainterPath> dstOutlines;
+
+ KUndo2Command *cmd = new KUndo2Command(kundo2_i18n("Knife tool"));
+ QList<KoShape*> newSelectedShapes;
+
+ QList<KoShape*> shapesToRemove;
+
+ for (int i = 0; i < srcOutlines.size(); i++) {
+ QPainterPath leftPath = srcOutlines[i] & left;
+ QPainterPath rightPath = srcOutlines[i] & right;
+
+ // there is a bug in Qt, sometimes it leaves the resulting
+ // outline open, so just close it explicitly.
+
+ leftPath.closeSubpath();
+ rightPath.closeSubpath();
+
+ leftPath = booleanWorkaroundTransform.inverted().map(leftPath);
+ rightPath = booleanWorkaroundTransform.inverted().map(rightPath);
+
+ qCritical() << "Path 1 = " << leftPath << leftPath.length();
+ qCritical() << "Path 2 = " << rightPath << rightPath.length();
+
+
+ KoPathShape* leftShape = KoPathShape::createShapeFromPainterPath(leftPath);
+ KoPathShape* rightShape = KoPathShape::createShapeFromPainterPath(rightPath);
+
+ if (!leftPath.isEmpty() || !rightPath.isEmpty()) {
+ shapesToRemove << m_selectedShapes[i];
+ }
+
+ QList<KoPathShape*> shapes;
+ shapes << leftShape << rightShape;
+ KoShape* referenceShape = m_selectedShapes[i];
+ Q_FOREACH(KoShape* shape, shapes) {
+ shape->setBackground(referenceShape->background());
+ shape->setStroke(referenceShape->stroke());
+ shape->setZIndex(referenceShape->zIndex());
+
+ KoShapeContainer *parent = referenceShape->parent();
+ tool()->canvas()->shapeController()->addShapeDirect(shape, parent, cmd);
+
+ newSelectedShapes << shape;
+
+ }
+
+ }
+
+ tool()->canvas()->shapeController()->removeShapes(shapesToRemove, cmd);
+ new KoKeepShapesSelectedCommand({}, newSelectedShapes, tool()->canvas()->selectedShapesProxy(), true, cmd);
+
+
+ tool()->canvas()->addCommand(cmd);
+
+
+
+
+
+
+
+
+
+}
+
+void CutThroughShapeStrategy::paint(QPainter &painter, const KoViewConverter &converter)
+{
+ qCritical() << "Cut shape strategy :: paint";
+ painter.save();
+
+ painter.setBrush(Qt::darkGray);
+ painter.drawLine(converter.documentToView(m_startPoint), converter.documentToView(m_endPoint));
+ //painter.drawEllipse(converter.documentToView(m_d->mousePoint), 4, 4);
+ //QPointF topLeft = QPointF(min(m_d->startPoint.x(), m_d->endPoint.x()), min(min(m_d->startPoint.x(), m_d->endPoint.x())));
+ //QRectF dirtyRect;
+ //KisAlgebra2D::accumulateBounds(m_startPoint, &dirtyRect);
+ //KisAlgebra2D::accumulateBounds(m_endPoint, &dirtyRect);
+ //KisAlgebra2D::accumulateBounds(m_mousePoint + QPointF(-2, -2), &dirtyRect);
+ //KisAlgebra2D::accumulateBounds(m_mousePoint + QPointF(2, 2), &dirtyRect);
+ //dirtyRect = converter.viewToDocument(dirtyRect);
+
+ //drawCoordsStart(painter, converter);
+
+
+
+ painter.restore();
+}
diff --git a/plugins/tools/tool_knife/CutThroughShapeStrategy.h b/plugins/tools/tool_knife/CutThroughShapeStrategy.h
new file mode 100644
index 0000000000..97e8f55153
--- /dev/null
+++ b/plugins/tools/tool_knife/CutThroughShapeStrategy.h
@@ -0,0 +1,44 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef CUT_THROUGH_SHAPE_STRATEGY_H_
+#define CUT_THROUGH_SHAPE_STRATEGY_H_
+
+#include <QScopedPointer>
+#include <QRectF>
+
+#include <KoInteractionStrategy.h>
+#include <KoShape.h>
+
+class KoSelection;
+
+
+
+class CutThroughShapeStrategy : public KoInteractionStrategy
+{
+public:
+ CutThroughShapeStrategy(KoToolBase *tool, KoSelection *selection, QPointF startPoint, qreal width);
+ ~CutThroughShapeStrategy() 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;
+ qreal m_width {10};
+};
+
+
+
+
+#endif // CUT_THROUGH_SHAPE_STRATEGY_H_
diff --git a/plugins/tools/tool_knife/KisToolKnife.cpp b/plugins/tools/tool_knife/KisToolKnife.cpp
new file mode 100644
index 0000000000..2a442b0147
--- /dev/null
+++ b/plugins/tools/tool_knife/KisToolKnife.cpp
@@ -0,0 +1,329 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "KisToolKnife.h"
+
+#include "QApplication"
+#include "QPainterPath"
+
+#include <klocalizedstring.h>
+#include <KoColor.h>
+#include <KisViewManager.h>
+#include "kis_canvas2.h"
+#include "kis_cursor.h"
+#include "kis_painter.h"
+#include "kis_paintop_preset.h"
+
+#include "kundo2magicstring.h"
+#include "kundo2stack.h"
+#include "commands_new/kis_transaction_based_command.h"
+#include "kis_transaction.h"
+#include "KoPathShape.h"
+
+#include "kis_processing_applicator.h"
+#include "kis_datamanager.h"
+
+#include "KoColorSpaceRegistry.h"
+#include <KisCursorOverrideLock.h>
+
+#include "KisToolKnifeOptionsWidget.h"
+#include "libs/image/kis_paint_device_debug_utils.h"
+
+#include <KoSelectedShapesProxy.h>
+
+#include "CutThroughShapeStrategy.h"
+
+#include "kis_paint_layer.h"
+#include "kis_algebra_2d.h"
+#include "kis_resources_snapshot.h"
+
+
+struct KisToolKnife::Private {
+ // TO REMOVE
+ KisPaintDeviceSP maskDev = nullptr;
+ KisPainter maskDevPainter;
+ float brushRadius = 50.; //initial default. actually read from ui.
+ KisToolKnifeOptionsWidget *optionsWidget = nullptr;
+ QRectF oldOutlineRect;
+ QPainterPath brushOutline;
+
+ // TO KEEP
+ QPointF startPoint = QPointF(0, 0);
+ QPointF endPoint = QPointF(0, 0);
+ QPointF mousePoint = QPointF(0, 0);
+ QRectF previousLineDirtyRect = QRectF();
+ QRectF previousMouseDirtyRect = QRectF();
+
+};
+
+
+KisToolKnife::KisToolKnife(KoCanvasBase * canvas)
+ : KoInteractionTool(canvas),
+ m_d(new Private)
+{
+ //setSupportOutline(true);
+ setObjectName("tool_knife");
+ m_d->maskDev = new KisPaintDevice(KoColorSpaceRegistry::instance()->rgb8());
+ m_d->maskDevPainter.begin( m_d->maskDev );
+
+ m_d->maskDevPainter.setPaintColor(KoColor(Qt::magenta, m_d->maskDev->colorSpace()));
+ m_d->maskDevPainter.setBackgroundColor(KoColor(Qt::white, m_d->maskDev->colorSpace()));
+ m_d->maskDevPainter.setFillStyle( KisPainter::FillStyleForegroundColor );
+}
+
+KisToolKnife::~KisToolKnife()
+{
+ m_d->optionsWidget = nullptr;
+ m_d->maskDevPainter.end();
+}
+
+void KisToolKnife::addMaskPath( KoPointerEvent *event )
+{
+ KisCanvas2 *canvas2 = dynamic_cast<KisCanvas2 *>(canvas());
+ KIS_ASSERT(canvas2);
+ const KisCoordinatesConverter *converter = canvas2->coordinatesConverter();
+
+ //QPointF imagePos = currentImage()->documentToPixel(event->point);
+ //QPainterPath currentBrushOutline = brushOutline().translated(KisAlgebra2D::alignForZoom(imagePos, converter->effectivePhysicalZoom()));
+ //m_d->maskDevPainter.fillPainterPath(currentBrushOutline);
+
+ //canvas()->updateCanvas(currentImage()->pixelToDocument(m_d->maskDev->exactBounds()));
+}
+
+
+QPainterPath KisToolKnife::brushOutline( void )
+{
+ const qreal diameter = m_d->brushRadius;
+ QPainterPath outline;
+ outline.addEllipse(QPointF(0,0), -0.5 * diameter, -0.5 * diameter );
+ return outline;
+}
+
+QPainterPath KisToolKnife::getBrushOutlinePath(const QPointF &documentPos,
+ const KoPointerEvent *event)
+{
+ Q_UNUSED(event);
+
+ //QPointF imagePos = currentImage()->documentToPixel(documentPos);
+ QPainterPath path = brushOutline();
+
+ KisCanvas2 *canvas2 = dynamic_cast<KisCanvas2 *>(canvas());
+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(canvas2, QPainterPath());
+ const KisCoordinatesConverter *converter = canvas2->coordinatesConverter();
+
+ //return path.translated(KisAlgebra2D::alignForZoom(imagePos, converter->effectivePhysicalZoom()));
+ return path;
+}
+
+void drawCoordsStartFrom(QPainter &painter, QTransform transform, QColor color)
+{
+ painter.save();
+ QPen pen = QPen(QBrush(color), 5, Qt::SolidLine);
+ painter.setPen(pen);
+ painter.setTransform(transform * painter.transform());
+ painter.drawLine(QPointF(0, 0), QPointF(100, 0));
+ painter.drawLine(QPointF(0, 0), QPointF(0, 100));
+ painter.drawLine(QPointF(0, 100), QPointF(5, 100));
+ painter.drawLine(QPointF(100, 0), QPointF(100, 5));
+
+ painter.drawArc(QRect(-50, -50, 100, 100), 0, -16*90);
+
+ painter.restore();
+}
+
+
+void drawCoordsStart(QPainter &painter, const KoViewConverter &converter)
+{
+ //painter.drawLine(QPointF(0, 0), QPointF(100, 0));
+ //painter.drawLine(QPointF(0, 0), QPointF(0, 100));
+ //painter.drawArc(QRect(-50, -50, 100, 100), 0, -16*90);
+
+ drawCoordsStartFrom(painter, QTransform(), Qt::red);
+ drawCoordsStartFrom(painter, converter.documentToView(), Qt::blue); // < ten ma sens w painterze
+ drawCoordsStartFrom(painter, converter.viewToDocument(), Qt::green);
+
+
+
+}
+
+void KisToolKnife::paint(QPainter &painter, const KoViewConverter &converter)
+{
+ //qCritical() << "~~~~ paint!!!!";
+ Q_UNUSED(converter);
+
+ painter.save();
+ //QPainterPath path = pixelToView(m_d->brushOutline);
+ //paintToolOutline(&painter, path);
+ painter.restore();
+
+ painter.save();
+ painter.setBrush(Qt::darkGray);
+ QImage img = m_d->maskDev->convertToQImage(0);
+ if( !img.size().isEmpty() ){
+ //painter.drawImage(pixelToView(m_d->maskDev->exactBounds()), img);
+ }
+ //painter.drawLine(converter.documentToView(m_d->startPoint), converter.documentToView(m_d->endPoint));
+ painter.drawEllipse(converter.documentToView(m_d->mousePoint), 4, 4);
+ //QPointF topLeft = QPointF(min(m_d->startPoint.x(), m_d->endPoint.x()), min(min(m_d->startPoint.x(), m_d->endPoint.x())));
+ QRectF dirtyRect;
+ KisAlgebra2D::accumulateBounds(m_d->startPoint, &dirtyRect);
+ KisAlgebra2D::accumulateBounds(m_d->endPoint, &dirtyRect);
+ KisAlgebra2D::accumulateBounds(m_d->mousePoint + QPointF(-2, -2), &dirtyRect);
+ KisAlgebra2D::accumulateBounds(m_d->mousePoint + QPointF(2, 2), &dirtyRect);
+
+
+ //dirtyRect = converter.viewToDocument(dirtyRect);
+
+ drawCoordsStart(painter, converter);
+
+
+
+ painter.restore();
+
+ KoInteractionTool::paint(painter, converter);
+ //currentStrategy()->paint(painter, converter);
+
+ //canvas()->updateCanvas(dirtyRect);
+}
+
+void KisToolKnife::activate(const QSet<KoShape *> &shapes)
+{
+ // Questions to Dmitry or Wolthera:
+ // 1. Which tool base should it be based on?
+ // 2. Which functions should I reimplement: mouse events, or beingPrimaryAction, or a strategy?
+ // 3. How to get shapes from the layer? I assume that it might happen that no shape is selected, in which case
+ // it should use all shapes on the layer as potential input
+ // 4. How to get points from that shape? I noticed I have KoPathShape* sometimes, and that has (protected) subpaths,
+ // but I can access segments I think? And outline() is useful for now for debug. But how to do it properly?
+
+ //canvas()->selectedShapesProxy();
+ //this->set
+
+ KoInteractionTool::activate(shapes);
+ Q_FOREACH(KoShape* shape, shapes) {
+ if (shape) {
+ qCritical() << "We've got a shape!";
+ KoPathShape* pathShape = dynamic_cast<KoPathShape *>(shape);
+ qCritical() << "Is it a path shape? " << pathShape;
+ if (pathShape) {
+ qCritical() << "Let's see the points:";
+ //qCritical() << pathShape->isClosedSubpath(0);
+ //pathShape->subpaths();
+ // ok, subpaths are protected...
+
+ //temporary:
+ QPainterPath pp = pathShape->outline();
+ //qCritical() << pp.;
+ //Q_FOREACH(QPainterPath::Element element, pp.elem)
+ for (int i = 0; i < pp.elementCount(); i++) {
+ qCritical() << "Point at: " << pp.elementAt(i).x << pp.elementAt(i).y;
+ }
+
+ //Q_FOREACH(QPointF& p, pathShape.)
+ }
+
+ }
+ }
+
+}
+
+void KisToolKnife::deactivate()
+{
+ KoInteractionTool::deactivate();
+}
+
+void KisToolKnife::mousePressEventBackup(KoPointerEvent *event)
+{
+ m_d->startPoint = event->point;//canvas()->viewConverter()->viewToDocument(event->pos());
+ m_d->endPoint = event->point;//canvas()->viewConverter()->viewToDocument(event->pos());
+}
+
+void KisToolKnife::mouseMoveEvent(KoPointerEvent *event)
+{
+ KoInteractionTool::mouseMoveEvent(event);
+
+ //qCritical() << "mouse move event button flags: " << event->buttons() << ", " << event->button();
+ if (event->buttons().testFlag(Qt::MouseButton::LeftButton)) {
+
+ //m_d->endPoint = canvas()->viewConverter()->viewToDocument(event->pos());
+ m_d->endPoint = event->point;
+ //qCritical() << "Now supposedly drawing a line between " << m_d->startPoint << " to " << m_d->endPoint;
+ QRectF dirtyRect;
+ KisAlgebra2D::accumulateBounds(m_d->startPoint, &dirtyRect);
+ KisAlgebra2D::accumulateBounds(m_d->endPoint, &dirtyRect);
+ //dirtyRect = canvas()->viewConverter()->viewToDocument(dirtyRect);
+ //qCritical() << "And the dirty rect is: " << dirtyRect;
+
+ QRectF accumulatedWithPrevious = m_d->previousLineDirtyRect;
+ accumulatedWithPrevious |= dirtyRect;
+ //KisAlgebra2D::accumulateBounds(dirtyRect, &accumulatedWithPrevious);
+ //KisAlgebra2D::accumulateBounds(dirtyRect.topLeft(), &accumulatedWithPrevious);
+ //KisAlgebra2D::accumulateBounds(dirtyRect.bottomRight(), &accumulatedWithPrevious);
+
+ //QRectF rect = dirtyRect + m_d->previousLineDirtyRect;
+
+ canvas()->updateCanvas(accumulatedWithPrevious);
+ m_d->previousLineDirtyRect = dirtyRect;
+
+ } else {
+ m_d->mousePoint = event->point;
+ QRectF dirtyRect;
+ KisAlgebra2D::accumulateBounds(m_d->mousePoint + QPointF(-2, -2), &dirtyRect);
+ KisAlgebra2D::accumulateBounds(m_d->mousePoint + QPointF(2, 2), &dirtyRect);
+
+ //canvas()->updateCanvas(dirtyRect);
+ //dirtyRect = canvas()->viewConverter()->viewToD(dirtyRect);
+
+ canvas()->updateCanvas(dirtyRect);
+
+ //qCritical() << "Changing mouse point to " << event->point << " as opposed to " << event->pos();
+ }
+ repaintDecorations();
+}
+
+void KisToolKnife::mouseReleaseEvent(KoPointerEvent *event)
+{
+ KoInteractionTool::mouseReleaseEvent(event);
+
+ m_d->endPoint = event->point;
+
+ //qCritical() << "Now, AFTER release, supposedly drawing a line between " << m_d->startPoint.x() << ", " << m_d->startPoint.y() << " to " << m_d->endPoint.x() << "," << m_d->endPoint.y();
+ QRectF dirtyRect;
+ KisAlgebra2D::accumulateBounds(m_d->startPoint, &dirtyRect);
+ KisAlgebra2D::accumulateBounds(m_d->endPoint, &dirtyRect);
+
+ QRectF accumulatedWithPrevious = m_d->previousLineDirtyRect | dirtyRect;
+ //KisAlgebra2D::accumulateBounds(dirtyRect.topLeft(), &accumulatedWithPrevious);
+ //KisAlgebra2D::accumulateBounds(dirtyRect.bottomRight(), &accumulatedWithPrevious);
+
+ //KisAlgebra2D::accumulateBounds(dirtyRect, &accumulatedWithPrevious);
+
+ canvas()->updateCanvas(accumulatedWithPrevious);
+ m_d->previousLineDirtyRect = dirtyRect;
+ //dirtyRect = canvas()->viewConverter()->viewToDocument(dirtyRect);
+
+ //canvas()->updateCanvas(dirtyRect);
+}
+
+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());
+ //return NULL;
+}
+
+QWidget * KisToolKnife::createOptionWidget()
+{
+ KisCanvas2 * kiscanvas = dynamic_cast<KisCanvas2*>(canvas());
+ KIS_ASSERT(kiscanvas);
+
+ m_d->optionsWidget = new KisToolKnifeOptionsWidget(kiscanvas->viewManager()->canvasResourceProvider(), 0);
+ m_d->optionsWidget->setObjectName(toolId() + "option widget");
+
+ return m_d->optionsWidget;
+}
+
diff --git a/plugins/tools/tool_knife/KisToolKnife.h b/plugins/tools/tool_knife/KisToolKnife.h
new file mode 100644
index 0000000000..b66be93c1c
--- /dev/null
+++ b/plugins/tools/tool_knife/KisToolKnife.h
@@ -0,0 +1,93 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef KIS_TOOL_KNIFE_H_
+#define KIS_TOOL_KNIFE_H_
+
+#include <QScopedPointer>
+#include <QPainterPath>
+
+#include "KoInteractionTool.h"
+
+#include "KisToolPaintFactoryBase.h"
+
+#include <flake/kis_node_shape.h>
+#include <kis_icon.h>
+#include <QKeySequence>
+#include <klocalizedstring.h>
+
+#include <kconfig.h>
+#include <kconfiggroup.h>
+#include <KoIcon.h>
+
+class KisKActionCollection;
+class KoCanvasBase;
+class KisPaintInformation;
+class KisSpacingInformation;
+
+
+class KisToolKnife : public KoInteractionTool
+{
+ Q_OBJECT
+public:
+ KisToolKnife(KoCanvasBase * canvas);
+ ~KisToolKnife() override;
+
+ QWidget * createOptionWidget() override;
+
+ void paint(QPainter &painter, const KoViewConverter &converter) override;
+
+protected Q_SLOTS:
+
+public Q_SLOTS:
+ void activate(const QSet<KoShape*> &shapes) override;
+ void deactivate() override;
+ void mousePressEventBackup(KoPointerEvent *event);
+ void mouseMoveEvent(KoPointerEvent *event) override;
+ void mouseReleaseEvent(KoPointerEvent *event) override;
+
+ KoInteractionStrategy *createStrategy(KoPointerEvent *event) override;
+private:
+ //QRect inpaintImage(KisPaintDeviceSP maskDev, KisPaintDeviceSP imageDev);
+ QPainterPath getBrushOutlinePath(const QPointF &documentPos, const KoPointerEvent *event);
+ QPainterPath brushOutline();
+
+private:
+ struct Private;
+ class InpaintCommand;
+ const QScopedPointer<Private> m_d;
+
+ void addMaskPath(KoPointerEvent *event);
+};
+
+
+class KisToolKnifeFactory : public KisToolPaintFactoryBase
+{
+
+public:
+ KisToolKnifeFactory()
+ : KisToolPaintFactoryBase("KritaShape/KisToolKnife")
+ {
+
+ setToolTip(i18n("Knife Tool"));
+
+ setSection(ToolBoxSection::Main);
+ setIconName(koIconNameCStr("select"));
+ setPriority(7);
+ setActivationShapeId("flake/always,KoPathShape");
+ }
+
+ ~KisToolKnifeFactory() override {}
+
+ KoToolBase * createTool(KoCanvasBase *canvas) override
+ {
+ return new KisToolKnife(canvas);
+ }
+
+};
+
+
+#endif // KIS_TOOL_KNIFE_H_
diff --git a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp
new file mode 100644
index 0000000000..118bc7ef84
--- /dev/null
+++ b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp
@@ -0,0 +1,114 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "KisToolKnifeOptionsWidget.h"
+
+#include "ui_KisToolKnifeOptionsWidget.h"
+
+#include <KoColorSpaceRegistry.h>
+#include "KisPaletteModel.h"
+
+#include "kis_config.h"
+#include <resources/KoColorSet.h>
+#include "kis_canvas_resource_provider.h"
+
+
+struct KisToolKnifeOptionsWidget::Private {
+ Private()
+ {
+ }
+
+ Ui_KisToolKnifeOptionsWidget *ui {nullptr};
+
+ int getThickGapWidth(void)
+ {
+ return ui->thickGapWidth->value();
+ }
+
+ int getThinGapWidth(void)
+ {
+ return ui->thinGapWidth->value();
+ }
+
+ int getSpecialGapWidth(void)
+ {
+ return ui->specialGapWidth->value();
+ }
+
+ KisToolKnifeOptionsWidget::GapWidthType getWidthType()
+ {
+ if (ui->thickGapWidthRadioButton->isChecked()) {
+ return KisToolKnifeOptionsWidget::Thick;
+ } else if (ui->thinGapWidthRadioButton->isChecked()) {
+ return KisToolKnifeOptionsWidget::Thin;
+ } else { // ui->specialGapWidthRadioButton->isChecked()
+ return KisToolKnifeOptionsWidget::Special;
+ }
+ }
+
+ int getWidthForType(GapWidthType type) {
+ switch(type) {
+ case KisToolKnifeOptionsWidget::Thick:
+ return getThickGapWidth();
+ case KisToolKnifeOptionsWidget::Thin:
+ return getThinGapWidth();
+ case KisToolKnifeOptionsWidget::Special:
+ return getSpecialGapWidth();
+ default:
+ return getSpecialGapWidth();
+ }
+ }
+
+};
+
+KisToolKnifeOptionsWidget::KisToolKnifeOptionsWidget(KisCanvasResourceProvider */*provider*/, QWidget *parent)
+ : QWidget(parent),
+ m_d(new Private)
+{
+ m_d->ui = new Ui_KisToolKnifeOptionsWidget();
+ m_d->ui->setupUi(this);
+}
+
+KisToolKnifeOptionsWidget::~KisToolKnifeOptionsWidget()
+{
+ delete m_d->ui;
+ m_d->ui = nullptr;
+}
+
+int KisToolKnifeOptionsWidget::getThickGapWidth()
+{
+ return m_d->getThickGapWidth();
+}
+
+int KisToolKnifeOptionsWidget::getThinGapWidth()
+{
+ return m_d->getThinGapWidth();
+}
+
+int KisToolKnifeOptionsWidget::getSpecialGapWidth()
+{
+ return m_d->getSpecialGapWidth();
+}
+
+KisToolKnifeOptionsWidget::GapWidthType KisToolKnifeOptionsWidget::getWidthType()
+{
+ return m_d->getWidthType();
+}
+
+int KisToolKnifeOptionsWidget::getCurrentWidth()
+{
+ return getWidthForType(getWidthType());
+}
+
+int KisToolKnifeOptionsWidget::getWidthForType(GapWidthType type)
+{
+ return m_d->getWidthForType(type);
+
+}
+
+
+
+
diff --git a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h
new file mode 100644
index 0000000000..190ae450c9
--- /dev/null
+++ b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h
@@ -0,0 +1,46 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef __KIS_TOOL_KNIFE_OPTIONS_WIDGET_H
+#define __KIS_TOOL_KNIFE_OPTIONS_WIDGET_H
+
+#include <QScopedPointer>
+#include <QWidget>
+#include <QModelIndex>
+
+#include "kis_types.h"
+
+class KisCanvasResourceProvider;
+class KoColor;
+
+
+class KisToolKnifeOptionsWidget : public QWidget
+{
+ Q_OBJECT
+public:
+ KisToolKnifeOptionsWidget(KisCanvasResourceProvider *provider, QWidget *parent);
+ ~KisToolKnifeOptionsWidget() override;
+
+ enum GapWidthType {
+ Thick,
+ Thin,
+ Special
+ };
+
+ int getThickGapWidth(void);
+ int getThinGapWidth(void);
+ int getSpecialGapWidth(void);
+ GapWidthType getWidthType();
+ int getCurrentWidth();
+ int getWidthForType(GapWidthType type);
+
+
+private:
+ struct Private;
+ const QScopedPointer<Private> m_d;
+};
+
+#endif /* __KIS_TOOL_KNIFE_OPTIONS_WIDGET_H */
diff --git a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.ui b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.ui
new file mode 100644
index 0000000000..f91b96d62d
--- /dev/null
+++ b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.ui
@@ -0,0 +1,200 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <author>
+ SPDX-FileCopyrightText: none
+ SPDX-License-Identifier: GPL-3.0-or-later
+ </author>
+ <class>KisToolKnifeOptionsWidget</class>
+ <widget class="QWidget" name="KisToolKnifeOptionsWidget">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>250</width>
+ <height>189</height>
+ </rect>
+ </property>
+ <layout class="QVBoxLayout" name="verticalLayout_2">
+ <item>
+ <widget class="QRadioButton" name="thickGapWidthRadioButton">
+ <property name="text">
+ <string>Thin gap:</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_2">
+ <item>
+ <widget class="QLabel" name="gapWidthLabel1">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>Width:</string>
+ </property>
+ <property name="buddy">
+ <cstring>gapWidthLabel1</cstring>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QSpinBox" name="thickGapWidth">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+ </property>
+ <property name="suffix">
+ <string> px</string>
+ </property>
+ <property name="minimum">
+ <number>1</number>
+ </property>
+ <property name="maximum">
+ <number>4000</number>
+ </property>
+ <property name="singleStep">
+ <number>50</number>
+ </property>
+ <property name="value">
+ <number>100</number>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <widget class="QRadioButton" name="thinGapWidthRadioButton">
+ <property name="text">
+ <string>Thick gap:</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_2">
+ <item>
+ <widget class="QLabel" name="gapWidthLabel2">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>Width:</string>
+ </property>
+ <property name="buddy">
+ <cstring>gapWidthLabel2</cstring>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QSpinBox" name="thinGapWidth">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+ </property>
+ <property name="suffix">
+ <string> px</string>
+ </property>
+ <property name="minimum">
+ <number>1</number>
+ </property>
+ <property name="maximum">
+ <number>4000</number>
+ </property>
+ <property name="singleStep">
+ <number>50</number>
+ </property>
+ <property name="value">
+ <number>100</number>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <widget class="QRadioButton" name="customWidthRadioButton">
+ <property name="text">
+ <string>Special gap:</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_2">
+ <item>
+ <widget class="QLabel" name="gapWidthLabel3">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>Width:</string>
+ </property>
+ <property name="buddy">
+ <cstring>gapWidthLabel3</cstring>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QSpinBox" name="specialGapWidth">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+ </property>
+ <property name="suffix">
+ <string> px</string>
+ </property>
+ <property name="minimum">
+ <number>1</number>
+ </property>
+ <property name="maximum">
+ <number>4000</number>
+ </property>
+ <property name="singleStep">
+ <number>50</number>
+ </property>
+ <property name="value">
+ <number>100</number>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <spacer name="verticalSpacer">
+ <property name="orientation">
+ <enum>Qt::Vertical</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>20</width>
+ <height>40</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/plugins/tools/tool_knife/ToolKnife.cpp b/plugins/tools/tool_knife/ToolKnife.cpp
new file mode 100644
index 0000000000..c3ea872b19
--- /dev/null
+++ b/plugins/tools/tool_knife/ToolKnife.cpp
@@ -0,0 +1,33 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "ToolKnife.h"
+#include <QStringList>
+
+#include <kis_debug.h>
+#include <kpluginfactory.h>
+
+#include <kis_tool.h>
+#include <KoToolRegistry.h>
+
+#include "kis_paint_device.h"
+#include "KisToolKnife.h"
+
+
+K_PLUGIN_FACTORY_WITH_JSON(DefaultToolsFactory, "kritatoolknife.json", registerPlugin<ToolKnife>();)
+
+
+ToolKnife::ToolKnife(QObject *parent, const QVariantList &)
+ : QObject(parent)
+{
+ KoToolRegistry::instance()->add(new KisToolKnifeFactory());
+}
+
+ToolKnife::~ToolKnife()
+{
+}
+
+#include "ToolKnife.moc"
diff --git a/plugins/tools/tool_knife/ToolKnife.h b/plugins/tools/tool_knife/ToolKnife.h
new file mode 100644
index 0000000000..17e4a93b04
--- /dev/null
+++ b/plugins/tools/tool_knife/ToolKnife.h
@@ -0,0 +1,22 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Agata Cacko
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TOOL_KNIFE_H_
+#define TOOL_KNIFE_H_
+
+#include <QObject>
+#include <QVariant>
+
+class ToolKnife : public QObject
+{
+ Q_OBJECT
+public:
+ ToolKnife(QObject *parent, const QVariantList &);
+ ~ToolKnife() override;
+
+};
+
+#endif // TOOL_KNIFE_H_
diff --git a/plugins/tools/tool_knife/kritatoolknife.json b/plugins/tools/tool_knife/kritatoolknife.json
new file mode 100644
index 0000000000..e4ac3b9693
--- /dev/null
+++ b/plugins/tools/tool_knife/kritatoolknife.json
@@ -0,0 +1,9 @@
+{
+ "Id": "Knife Tool",
+ "Type": "Service",
+ "X-KDE-Library": "kritatoolknife",
+ "X-KDE-ServiceTypes": [
+ "Krita/Tool"
+ ],
+ "X-Krita-Version": "28"
+}