Krita Source Code Documentation
Loading...
Searching...
No Matches
CutThroughShapeStrategy.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Agata Cacko
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QDebug>
10#include <QPainter>
11
12#include <kis_algebra_2d.h>
13#include <KoToolBase.h>
14#include <KoCanvasBase.h>
15#include <KoViewConverter.h>
16#include <KoSelection.h>
17#include <kis_global.h>
18#include "kis_debug.h"
19#include <KoPathShape.h>
20#include <krita_utils.h>
21#include <kis_canvas2.h>
22#include <QPainterPath>
23#include <KoShapeController.h>
24#include <kundo2command.h>
26#include <QtMath>
27#include <KoSvgTextShape.h>
29
30
31CutThroughShapeStrategy::CutThroughShapeStrategy(KoToolBase *tool, KoSelection *selection, const QList<KoShape *> &shapes, QPointF startPoint, const GutterWidthsConfig &width)
33 , m_startPoint(startPoint)
34 , m_endPoint(startPoint)
35 , m_width(width)
36{
38 m_allShapes = shapes;
39}
40
45
47{
48 // TODO: undoing
49 return 0;
50}
51
52QPointF snapEndPoint(const QPointF &startPoint, const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) {
53
54 QPointF nicePoint = snapToClosestNiceAngle(mouseLocation, startPoint); // by default the function gives you 15 degrees increments
55
56 if (modifiers & Qt::KeyboardModifier::ShiftModifier) {
57 return nicePoint;
58 if (qAbs(mouseLocation.x() - startPoint.x()) >= qAbs(mouseLocation.y() - startPoint.y())) {
59 // do horizontal line
60 return QPointF(mouseLocation.x(), startPoint.y());
61 } else {
62 return QPointF(startPoint.x(), mouseLocation.y());
63 }
64 }
65 QLineF line = QLineF(startPoint, mouseLocation);
66 qreal angle = line.angleTo(QLineF(startPoint, nicePoint));
67 qreal eps = kisDegreesToRadians(2.0f);
68 if (angle < eps) {
69 return nicePoint;
70 }
71 return mouseLocation;
72}
73
74void CutThroughShapeStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
75{
76 m_endPoint = snapEndPoint(m_startPoint, mouseLocation, modifiers);
77 QRectF dirtyRect;
80 dirtyRect = kisGrowRect(dirtyRect, gutterWidthInDocumentCoordinates(calculateLineAngle(m_startPoint, m_endPoint))); // twice as much as it should need to account for lines showing the effect
81
82 QRectF accumulatedWithPrevious = m_previousLineDirtyRect | dirtyRect;
83
84 tool()->canvas()->updateCanvas(accumulatedWithPrevious);
85 m_previousLineDirtyRect = dirtyRect;
86
87}
88
89
90bool CutThroughShapeStrategy::willShapeBeCutGeneral(KoShape* referenceShape, const QPainterPath& srcOutline, const QRectF& leftOppositeRect, const QRectF& rightOppositeRect, bool checkGapLineRect, const QRectF& gapLineRect)
91{
92 if (dynamic_cast<KoSvgTextShape*>(referenceShape)) {
93 // skip all text
94 return false;
95 }
96
97 if ((srcOutline.boundingRect() & leftOppositeRect).isEmpty()
98 || (srcOutline.boundingRect() & rightOppositeRect).isEmpty()) {
99 // there is nothing on one side
100 // everything is on the other, far away from the gap line
101 // it just makes it a bit faster when there is a whole lot of shapes
102
103 return false;
104 }
105
106 if (checkGapLineRect && (srcOutline.boundingRect() & gapLineRect).isEmpty()) {
107 // the gap lines can't cross the shape since their bounding rects don't cross it
108 return false;
109 }
110
111 return true;
112}
113
114bool CutThroughShapeStrategy::willShapeBeCutPrecise(const QPainterPath& srcOutline, const QLineF gapLine, const QLineF& leftLine, const QLineF& rightLine, const QPolygonF& gapLinePolygon)
115{
116 bool containsGapLinePointStart = srcOutline.contains(gapLine.p1());
117 bool containsGapLinePointEnd = srcOutline.contains(gapLine.p2());
118
119 // if should skip if there is exactly one gap line point inside the shape
120 bool exactlyOneGapLinePointInside = (containsGapLinePointStart != containsGapLinePointEnd);
121 bool bothGapLinePointsInside = containsGapLinePointStart && containsGapLinePointEnd;
122
123 if (exactlyOneGapLinePointInside) {
124 return false;
125 }
126
127 bool crossesGapLine = KisAlgebra2D::getLineSegmentCrossingLineIndexes(leftLine, srcOutline).count() > 0
128 || KisAlgebra2D::getLineSegmentCrossingLineIndexes(rightLine, srcOutline).count() > 0;
129
130
131 bool containsPointWithinGap = false;
132 Q_FOREACH(QPointF p, srcOutline.toFillPolygon()) {
133 if (gapLinePolygon.containsPoint(p, Qt::WindingFill)) {
134 containsPointWithinGap = true;
135 break;
136 }
137 }
138
139 if (!bothGapLinePointsInside && !crossesGapLine && !containsPointWithinGap) {
140 return false;
141 }
142 return true;
143}
144
145void CutThroughShapeStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
146{
148
149
150 KisCanvas2 *kisCanvas = static_cast<KisCanvas2 *>(tool()->canvas());
152 const QTransform booleanWorkaroundTransform = KritaUtils::pathShapeBooleanSpaceWorkaround(kisCanvas->image());
153
154 QList<QPainterPath> srcOutlines;
155 QRectF outlineRect;
156
157 if (m_allShapes.length() == 0) {
158 qCritical() << "No shapes are available";
159 return;
160 }
161
162 Q_FOREACH (KoShape *shape, m_allShapes) {
163
164 QPainterPath outlineHere =
165 booleanWorkaroundTransform.map(
166 shape->absoluteTransformation().map(
167 shape->outline()));
168
169 srcOutlines << outlineHere;
170 outlineRect |= outlineHere.boundingRect();//booleanWorkaroundTransform.map(shape->absoluteOutlineRect()).boundingRect();
171 }
172
173 if (outlineRect.isEmpty()) {
174 //qCritical() << "The outline rect is empty";
175 return;
176 }
177
178 QRectF outlineRectBigger = kisGrowRect(outlineRect, 10);
179 QRect outlineRectBiggerInt = outlineRectBigger.toRect();
180
181 QLineF gapLine = QLineF(m_startPoint, m_endPoint);
182 qreal eps = 0.0000001;
183 if (gapLine.length() < eps) {
184 return;
185 }
186
188
189 QList<QLineF> gapLines = KisAlgebra2D::getParallelLines(gapLine, gutterWidth/2);
190
191 gapLine = booleanWorkaroundTransform.map(gapLine);
192 gapLines[0] = booleanWorkaroundTransform.map(gapLines[0]);
193 gapLines[1] = booleanWorkaroundTransform.map(gapLines[1]);
194
195 QLineF leftLine = gapLines[0];
196 QLineF rightLine = gapLines[1];
197
198 QLineF leftLineLong = leftLine;
199 QLineF rightLineLong = rightLine;
200
201
202
203 KisAlgebra2D::cropLineToRect(leftLineLong, outlineRectBiggerInt, true, true);
204 KisAlgebra2D::cropLineToRect(rightLineLong, outlineRectBiggerInt, true, true);
205
206 std::unique_ptr<KUndo2Command> cmd = std::unique_ptr<KUndo2Command>(new KUndo2Command(kundo2_i18n("Knife tool: cut through shapes")));
207
208
209 new KoKeepShapesSelectedCommand(m_selectedShapes, {}, kisCanvas->selectedShapesProxy(), false, cmd.get());
210
211
212 if (leftLine.length() == 0 || rightLine.length() == 0) {
213 KIS_SAFE_ASSERT_RECOVER_RETURN(gapLine.length() != 0 && gapLines[0].length() != 0 && gapLines[1].length() != 0 && "Original gap lines shouldn't be empty at this point");
214 // looks like *all* shapes need to be cut out
215
216 tool()->canvas()->shapeController()->removeShapes(m_allShapes, cmd.get());
217 tool()->canvas()->addCommand(cmd.release());
218 return;
219 }
220
221
222 QList<QPainterPath> paths = KisAlgebra2D::getPathsFromRectangleCutThrough(QRectF(outlineRectBiggerInt), leftLineLong, rightLineLong);
223 QPainterPath left = paths[0];
224 QPainterPath right = paths[1];
225
226 QList<QPainterPath> pathsOpposite = KisAlgebra2D::getPathsFromRectangleCutThrough(QRectF(outlineRectBiggerInt), rightLineLong, leftLineLong);
227 QPainterPath leftOpposite = pathsOpposite[0];
228 QPainterPath rightOpposite = pathsOpposite[1];
229
230 QList<KoShape*> newSelectedShapes;
231
232 QList<KoShape*> shapesToRemove;
233
234 QTransform booleanWorkaroundTransformInverted = booleanWorkaroundTransform.inverted();
235
236 QRectF gapLineLeftRect = KisAlgebra2D::createRectFromCorners(leftLine); // warning! can be empty for perfectly horizontal/vertical lines
237 QRectF gapLineRightRect = KisAlgebra2D::createRectFromCorners(rightLine);
238 QRectF gapLineRect = gapLineLeftRect | gapLineRightRect; // will not be empty if the gutterWidth > 0
239 bool checkGapLineRect = !gapLineRect.isEmpty();
240 QPolygonF gapLinePolygon = QPolygonF({leftLine.p1(), leftLine.p2(), rightLine.p2(), rightLine.p1(), leftLine.p1()});
241
242 int affectedShapes = 0;
243
244 for (int i = 0; i < srcOutlines.size(); i++) {
245
246 KoShape* referenceShape = m_allShapes[i];
247 bool wasSelected = m_selectedShapes.contains(referenceShape);
248
249 bool skipThisShape = !willShapeBeCutGeneral(referenceShape, srcOutlines[i], leftOpposite.boundingRect(), rightOpposite.boundingRect(), checkGapLineRect, gapLineRect);
250 skipThisShape = skipThisShape || !willShapeBeCutPrecise(srcOutlines[i], gapLine, leftLine, rightLine, gapLinePolygon);
251
252 if (skipThisShape) {
253 if (wasSelected) {
254 newSelectedShapes << referenceShape;
255 }
256 continue;
257 }
258
259 affectedShapes++;
260
261
262 QPainterPath leftPath = srcOutlines[i] & left;
263 QPainterPath rightPath = srcOutlines[i] & right;
264
265 QList<QPainterPath> bothSides;
266 bothSides << leftPath << rightPath;
267
268
269 Q_FOREACH(QPainterPath path, bothSides) {
270 if (path.isEmpty()) {
271 continue;
272 }
273
274 // comment copied from another place:
275 // there is a bug in Qt, sometimes it leaves the resulting
276 // outline open, so just close it explicitly.
277 path.closeSubpath();
278 // this is needed because Qt linearize curves; this allows for a
279 // "sane" linearization instead of a very blocky appearance
280 path = booleanWorkaroundTransformInverted.map(path);
281 std::unique_ptr<KoPathShape> shape = std::unique_ptr<KoPathShape>(KoPathShape::createShapeFromPainterPath(path));
282 shape->closeMerge();
283
284 if (shape->boundingRect().isEmpty()) {
285 continue;
286 }
287
288 shape->setBackground(referenceShape->background());
289 shape->setStroke(referenceShape->stroke());
290 shape->setZIndex(referenceShape->zIndex());
291
292 KoShapeContainer *parent = referenceShape->parent();
293
294 if (wasSelected) {
295 newSelectedShapes << shape.get();
296 }
297
298 tool()->canvas()->shapeController()->addShapeDirect(shape.release(), parent, cmd.get());
299
300 }
301
302 // that happens no matter if there was any non-empty shape
303 // because if there is none, maybe they just were underneath the gap
304 shapesToRemove << m_allShapes[i];
305
306 }
307
308 if (affectedShapes > 0) {
309 tool()->canvas()->shapeController()->removeShapes(shapesToRemove, cmd.get());
310 new KoKeepShapesSelectedCommand({}, newSelectedShapes, tool()->canvas()->selectedShapesProxy(), true, cmd.get());
311 tool()->canvas()->addCommand(cmd.release());
312 }
313
314
315
316}
317
318void CutThroughShapeStrategy::paint(QPainter &painter, const KoViewConverter &converter, const KoColorDisplayRendererInterface *displayRendererInterface)
319{
320 painter.save();
321
322 KoColor c;
323 c.fromQColor(Qt::darkGray);
324 QColor semitransparentGray = displayRendererInterface->convertColorToDisplayColorSpace(c);
325 semitransparentGray.setAlphaF(0.6);
326 QPen pen = QPen(QBrush(semitransparentGray), 2);
327 painter.setPen(pen);
328
329 painter.setRenderHint(QPainter::RenderHint::Antialiasing, true);
330
332
333 QLineF gutterCenterLine = QLineF(m_startPoint, m_endPoint);
334 gutterCenterLine = converter.documentToView().map(gutterCenterLine);
335 QLineF gutterWidthHelperLine = QLineF(QPointF(0, 0), QPointF(gutterWidth, 0));
336 gutterWidthHelperLine = converter.documentToView().map(gutterWidthHelperLine);
337
338 gutterWidth = gutterWidthHelperLine.length();
339
340 QList<QLineF> gutterLines = KisAlgebra2D::getParallelLines(gutterCenterLine, gutterWidth/2);
341
342 QLineF gutterLine1 = gutterLines.length() > 0 ? gutterLines[0] : gutterCenterLine;
343 QLineF gutterLine2 = gutterLines.length() > 1 ? gutterLines[1] : gutterCenterLine;
344
345
346 painter.drawLine(gutterLine1);
347 painter.drawLine(gutterLine2);
348
349 QRectF arcRect1 = QRectF(gutterCenterLine.p1() - QPointF(gutterWidth/2, gutterWidth/2), gutterCenterLine.p1() + QPointF(gutterWidth/2, gutterWidth/2));
350 QRectF arcRect2 = QRectF(gutterCenterLine.p2() - QPointF(gutterWidth/2, gutterWidth/2), gutterCenterLine.p2() + QPointF(gutterWidth/2, gutterWidth/2));
351
352 int qtAngleFactor = 16;
353 int qtHalfCircle = qtAngleFactor*180;
354
355 painter.drawArc(arcRect1, -qtAngleFactor*kisRadiansToDegrees(KisAlgebra2D::directionBetweenPoints(gutterCenterLine.p1(), gutterLine1.p1(), 0)), qtHalfCircle);
356 painter.drawArc(arcRect2, -qtAngleFactor*kisRadiansToDegrees(KisAlgebra2D::directionBetweenPoints(gutterCenterLine.p2(), gutterLine1.p2(), 0)), -qtHalfCircle);
357
358
359 int xLength = 3;
360 qreal xLengthEllipse = 2*qSqrt(2);
361
362 if (false) { // drawing X
363 painter.drawLine({QLineF(gutterCenterLine.p1() - QPointF(xLength, xLength), gutterCenterLine.p1() + QPointF(xLength, xLength))});
364 painter.drawLine({QLineF(gutterCenterLine.p2() - QPointF(xLength, xLength), gutterCenterLine.p2() + QPointF(xLength, xLength))});
365
366 painter.drawLine({QLineF(gutterCenterLine.p1() - QPointF(xLength, -xLength), gutterCenterLine.p1() + QPointF(xLength, -xLength))});
367 painter.drawLine({QLineF(gutterCenterLine.p2() - QPointF(xLength, -xLength), gutterCenterLine.p2() + QPointF(xLength, -xLength))});
368 }
369
370 // ellipse at the both ends of the gutter center line
371 painter.drawEllipse(gutterCenterLine.p1(), xLengthEllipse, xLengthEllipse);
372 painter.drawEllipse(gutterCenterLine.p2(), xLengthEllipse, xLengthEllipse);
373
374
375
376 pen.setWidth(1);
377 semitransparentGray.setAlphaF(0.2);
378 pen.setColor(semitransparentGray);
379
380 painter.setPen(pen);
381
382 painter.drawLine(gutterCenterLine);
383
384 painter.restore();
385}
386
388{
389 KisCanvas2 *kisCanvas = static_cast<KisCanvas2 *>(tool()->canvas());
391 QLineF helperGapWidthLine = QLineF(QPointF(0, 0), QPointF(0, m_width.widthForAngleInPixels(lineAngle)));
392 QLineF helperGapWidthLineTransformed = kisCanvas->coordinatesConverter()->imageToDocument(helperGapWidthLine);
393 return helperGapWidthLineTransformed.length();
394}
395
396qreal CutThroughShapeStrategy::calculateLineAngle(QPointF start, QPointF end)
397{
398 QPointF vec = end - start;
399 qreal angleDegrees = KisAlgebra2D::wrapValue(kisRadiansToDegrees(std::atan2(vec.y(), vec.x())), 0.0, 360.0);
400 return angleDegrees;
401}
QPointF snapEndPoint(const QPointF &startPoint, const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
const Params2D p
bool willShapeBeCutGeneral(KoShape *referenceShape, const QPainterPath &srcOutline, const QRectF &leftOppositeRect, const QRectF &rightOppositeRect, bool checkGapLineRect, const QRectF &gapLineRect)
void finishInteraction(Qt::KeyboardModifiers modifiers) override
qreal calculateLineAngle(QPointF start, QPointF end)
bool willShapeBeCutPrecise(const QPainterPath &srcOutline, const QLineF gapLine, const QLineF &leftLine, const QLineF &rightLine, const QPolygonF &gapLinePolygon)
QList< KoShape * > m_selectedShapes
void handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override
void paint(QPainter &painter, const KoViewConverter &converter, const KoColorDisplayRendererInterface *displayRendererInterface) override
KUndo2Command * createCommand() override
CutThroughShapeStrategy(KoToolBase *tool, KoSelection *selection, const QList< KoShape * > &allShapes, QPointF startPoint, const GutterWidthsConfig &width)
qreal gutterWidthInDocumentCoordinates(qreal lineAngle)
qreal widthForAngleInPixels(qreal lineAngleDegrees)
KisSelectedShapesProxy selectedShapesProxy
KisCoordinatesConverter * coordinatesConverter
KisImageWSP image() const
_Private::Traits< T >::Result imageToDocument(const T &obj) const
QPointer< KoShapeController > shapeController
virtual void updateCanvas(const QRectF &rc)=0
virtual void addCommand(KUndo2Command *command)=0
virtual KoSelectedShapesProxy * selectedShapesProxy() const =0
selectedShapesProxy() is a special interface for keeping a persistent connections to selectionChanged...
virtual QColor convertColorToDisplayColorSpace(const KoColor color) const =0
convertColorToDisplayColorSpace
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213
static KoPathShape * createShapeFromPainterPath(const QPainterPath &path)
Creates path shape from given QPainterPath.
const QList< KoShape * > selectedEditableShapes() const
virtual QPainterPath outline() const
Definition KoShape.cpp:554
virtual KoShapeStrokeModelSP stroke() const
Definition KoShape.cpp:885
KoShapeContainer * parent() const
Definition KoShape.cpp:857
QTransform absoluteTransformation() const
Definition KoShape.cpp:330
virtual QSharedPointer< KoShapeBackground > background() const
Definition KoShape.cpp:754
qint16 zIndex() const
Definition KoShape.cpp:524
KoCanvasBase * canvas() const
Returns the canvas the tool is working on.
virtual QPointF documentToView(const QPointF &documentPoint) const
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
const qreal eps
T kisGrowRect(const T &rect, U offset)
Definition kis_global.h:186
T kisRadiansToDegrees(T radians)
Definition kis_global.h:181
PointType snapToClosestNiceAngle(PointType point, PointType startPoint, qreal angle=(2 *M_PI)/24)
Definition kis_global.h:209
T kisDegreesToRadians(T degrees)
Definition kis_global.h:176
KUndo2MagicString kundo2_i18n(const char *text)
T wrapValue(T value, T wrapBounds)
QList< QPainterPath > getPathsFromRectangleCutThrough(const QRectF &rect, const QLineF &leftLine, const QLineF &rightLine)
getPathsFromRectangleCutThrough get paths defining both sides of a rectangle cut through using two (s...
void accumulateBounds(const Point &pt, Rect *bounds)
qreal directionBetweenPoints(const QPointF &p1, const QPointF &p2, qreal defaultAngle)
void cropLineToRect(QLineF &line, const QRect rect, bool extendFirst, bool extendSecond)
Crop line to rect; if it doesn't intersect, just return an empty line (QLineF()).
QList< QLineF > getParallelLines(const QLineF &line, const qreal distance)
QList< int > getLineSegmentCrossingLineIndexes(const QLineF &line, const QPainterPath &shape)
PointTypeTraits< Point >::rect_type createRectFromCorners(Point corner1, Point corner2)
QTransform pathShapeBooleanSpaceWorkaround(KisImageSP image)