22#include <QPainterPath>
33 , m_startPoint(startPoint)
34 , m_endPoint(startPoint)
52QPointF
snapEndPoint(
const QPointF &startPoint,
const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) {
56 if (modifiers & Qt::KeyboardModifier::ShiftModifier) {
58 if (qAbs(mouseLocation.x() - startPoint.x()) >= qAbs(mouseLocation.y() - startPoint.y())) {
60 return QPointF(mouseLocation.x(), startPoint.y());
62 return QPointF(startPoint.x(), mouseLocation.y());
65 QLineF line = QLineF(startPoint, mouseLocation);
66 qreal angle = line.angleTo(QLineF(startPoint, nicePoint));
97 if ((srcOutline.boundingRect() & leftOppositeRect).isEmpty()
98 || (srcOutline.boundingRect() & rightOppositeRect).isEmpty()) {
106 if (checkGapLineRect && (srcOutline.boundingRect() & gapLineRect).isEmpty()) {
116 bool containsGapLinePointStart = srcOutline.contains(gapLine.p1());
117 bool containsGapLinePointEnd = srcOutline.contains(gapLine.p2());
120 bool exactlyOneGapLinePointInside = (containsGapLinePointStart != containsGapLinePointEnd);
121 bool bothGapLinePointsInside = containsGapLinePointStart && containsGapLinePointEnd;
123 if (exactlyOneGapLinePointInside) {
131 bool containsPointWithinGap =
false;
132 Q_FOREACH(QPointF
p, srcOutline.toFillPolygon()) {
133 if (gapLinePolygon.containsPoint(
p, Qt::WindingFill)) {
134 containsPointWithinGap =
true;
139 if (!bothGapLinePointsInside && !crossesGapLine && !containsPointWithinGap) {
158 qCritical() <<
"No shapes are available";
164 QPainterPath outlineHere =
165 booleanWorkaroundTransform.map(
169 srcOutlines << outlineHere;
170 outlineRect |= outlineHere.boundingRect();
173 if (outlineRect.isEmpty()) {
178 QRectF outlineRectBigger =
kisGrowRect(outlineRect, 10);
179 QRect outlineRectBiggerInt = outlineRectBigger.toRect();
182 qreal
eps = 0.0000001;
183 if (gapLine.length() <
eps) {
191 gapLine = booleanWorkaroundTransform.map(gapLine);
192 gapLines[0] = booleanWorkaroundTransform.map(gapLines[0]);
193 gapLines[1] = booleanWorkaroundTransform.map(gapLines[1]);
195 QLineF leftLine = gapLines[0];
196 QLineF rightLine = gapLines[1];
198 QLineF leftLineLong = leftLine;
199 QLineF rightLineLong = rightLine;
206 std::unique_ptr<KUndo2Command> cmd = std::unique_ptr<KUndo2Command>(
new KUndo2Command(
kundo2_i18n(
"Knife tool: cut through shapes")));
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");
223 QPainterPath left = paths[0];
224 QPainterPath right = paths[1];
227 QPainterPath leftOpposite = pathsOpposite[0];
228 QPainterPath rightOpposite = pathsOpposite[1];
234 QTransform booleanWorkaroundTransformInverted = booleanWorkaroundTransform.inverted();
238 QRectF gapLineRect = gapLineLeftRect | gapLineRightRect;
239 bool checkGapLineRect = !gapLineRect.isEmpty();
240 QPolygonF gapLinePolygon = QPolygonF({leftLine.p1(), leftLine.p2(), rightLine.p2(), rightLine.p1(), leftLine.p1()});
242 int affectedShapes = 0;
244 for (
int i = 0; i < srcOutlines.size(); i++) {
249 bool skipThisShape = !
willShapeBeCutGeneral(referenceShape, srcOutlines[i], leftOpposite.boundingRect(), rightOpposite.boundingRect(), checkGapLineRect, gapLineRect);
250 skipThisShape = skipThisShape || !
willShapeBeCutPrecise(srcOutlines[i], gapLine, leftLine, rightLine, gapLinePolygon);
254 newSelectedShapes << referenceShape;
262 QPainterPath leftPath = srcOutlines[i] & left;
263 QPainterPath rightPath = srcOutlines[i] & right;
266 bothSides << leftPath << rightPath;
269 Q_FOREACH(QPainterPath path, bothSides) {
270 if (path.isEmpty()) {
280 path = booleanWorkaroundTransformInverted.map(path);
284 if (shape->boundingRect().isEmpty()) {
288 shape->setBackground(referenceShape->
background());
289 shape->setStroke(referenceShape->
stroke());
290 shape->setZIndex(referenceShape->
zIndex());
295 newSelectedShapes << shape.get();
308 if (affectedShapes > 0) {
325 semitransparentGray.setAlphaF(0.6);
326 QPen pen = QPen(QBrush(semitransparentGray), 2);
329 painter.setRenderHint(QPainter::RenderHint::Antialiasing,
true);
334 gutterCenterLine = converter.
documentToView().map(gutterCenterLine);
335 QLineF gutterWidthHelperLine = QLineF(QPointF(0, 0), QPointF(gutterWidth, 0));
336 gutterWidthHelperLine = converter.
documentToView().map(gutterWidthHelperLine);
338 gutterWidth = gutterWidthHelperLine.length();
342 QLineF gutterLine1 = gutterLines.length() > 0 ? gutterLines[0] : gutterCenterLine;
343 QLineF gutterLine2 = gutterLines.length() > 1 ? gutterLines[1] : gutterCenterLine;
346 painter.drawLine(gutterLine1);
347 painter.drawLine(gutterLine2);
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));
352 int qtAngleFactor = 16;
353 int qtHalfCircle = qtAngleFactor*180;
360 qreal xLengthEllipse = 2*qSqrt(2);
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))});
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))});
371 painter.drawEllipse(gutterCenterLine.p1(), xLengthEllipse, xLengthEllipse);
372 painter.drawEllipse(gutterCenterLine.p2(), xLengthEllipse, xLengthEllipse);
377 semitransparentGray.setAlphaF(0.2);
378 pen.setColor(semitransparentGray);
382 painter.drawLine(gutterCenterLine);
393 return helperGapWidthLineTransformed.length();
398 QPointF vec = end - start;
QPointF snapEndPoint(const QPointF &startPoint, const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
bool willShapeBeCutGeneral(KoShape *referenceShape, const QPainterPath &srcOutline, const QRectF &leftOppositeRect, const QRectF &rightOppositeRect, bool checkGapLineRect, const QRectF &gapLineRect)
~CutThroughShapeStrategy() override
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
QRectF m_previousLineDirtyRect
void paint(QPainter &painter, const KoViewConverter &converter, const KoColorDisplayRendererInterface *displayRendererInterface) override
KUndo2Command * createCommand() override
QList< KoShape * > m_allShapes
CutThroughShapeStrategy(KoToolBase *tool, KoSelection *selection, const QList< KoShape * > &allShapes, QPointF startPoint, const GutterWidthsConfig &width)
qreal gutterWidthInDocumentCoordinates(qreal lineAngle)
GutterWidthsConfig m_width
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.
KoToolBase * tool() const
static KoPathShape * createShapeFromPainterPath(const QPainterPath &path)
Creates path shape from given QPainterPath.
const QList< KoShape * > selectedEditableShapes() const
virtual QPainterPath outline() const
virtual KoShapeStrokeModelSP stroke() const
KoShapeContainer * parent() const
QTransform absoluteTransformation() const
virtual QSharedPointer< KoShapeBackground > background() const
virtual QPointF documentToView(const QPointF &documentPoint) const
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
T kisGrowRect(const T &rect, U offset)
T kisRadiansToDegrees(T radians)
PointType snapToClosestNiceAngle(PointType point, PointType startPoint, qreal angle=(2 *M_PI)/24)
T kisDegreesToRadians(T degrees)
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)