22#include <QPainterPath>
32 , m_startPoint(startPoint)
33 , m_endPoint(startPoint)
51QPointF
snapEndPoint(
const QPointF &startPoint,
const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) {
55 if (modifiers & Qt::KeyboardModifier::ShiftModifier) {
57 if (qAbs(mouseLocation.x() - startPoint.x()) >= qAbs(mouseLocation.y() - startPoint.y())) {
59 return QPointF(mouseLocation.x(), startPoint.y());
61 return QPointF(startPoint.x(), mouseLocation.y());
64 QLineF line = QLineF(startPoint, mouseLocation);
65 qreal angle = line.angleTo(QLineF(startPoint, nicePoint));
96 if ((srcOutline.boundingRect() & leftOppositeRect).isEmpty()
97 || (srcOutline.boundingRect() & rightOppositeRect).isEmpty()) {
105 if (checkGapLineRect && (srcOutline.boundingRect() & gapLineRect).isEmpty()) {
115 bool containsGapLinePointStart = srcOutline.contains(gapLine.p1());
116 bool containsGapLinePointEnd = srcOutline.contains(gapLine.p2());
119 bool exactlyOneGapLinePointInside = (containsGapLinePointStart != containsGapLinePointEnd);
120 bool bothGapLinePointsInside = containsGapLinePointStart && containsGapLinePointEnd;
122 if (exactlyOneGapLinePointInside) {
130 bool containsPointWithinGap =
false;
131 Q_FOREACH(QPointF
p, srcOutline.toFillPolygon()) {
132 if (gapLinePolygon.containsPoint(
p, Qt::WindingFill)) {
133 containsPointWithinGap =
true;
138 if (!bothGapLinePointsInside && !crossesGapLine && !containsPointWithinGap) {
157 qCritical() <<
"No shapes are available";
163 QPainterPath outlineHere =
164 booleanWorkaroundTransform.map(
168 srcOutlines << outlineHere;
169 outlineRect |= outlineHere.boundingRect();
172 if (outlineRect.isEmpty()) {
177 QRectF outlineRectBigger =
kisGrowRect(outlineRect, 10);
178 QRect outlineRectBiggerInt = outlineRectBigger.toRect();
181 qreal
eps = 0.0000001;
182 if (gapLine.length() <
eps) {
190 gapLine = booleanWorkaroundTransform.map(gapLine);
191 gapLines[0] = booleanWorkaroundTransform.map(gapLines[0]);
192 gapLines[1] = booleanWorkaroundTransform.map(gapLines[1]);
194 QLineF leftLine = gapLines[0];
195 QLineF rightLine = gapLines[1];
197 QLineF leftLineLong = leftLine;
198 QLineF rightLineLong = rightLine;
205 QScopedPointer<KUndo2Command> cmd = QScopedPointer<KUndo2Command>(
new KUndo2Command(
kundo2_i18n(
"Knife tool: cut through shapes")));
211 if (leftLine.length() == 0 || rightLine.length() == 0) {
212 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");
222 QPainterPath left = paths[0];
223 QPainterPath right = paths[1];
226 QPainterPath leftOpposite = pathsOpposite[0];
227 QPainterPath rightOpposite = pathsOpposite[1];
233 QTransform booleanWorkaroundTransformInverted = booleanWorkaroundTransform.inverted();
237 QRectF gapLineRect = gapLineLeftRect | gapLineRightRect;
238 bool checkGapLineRect = !gapLineRect.isEmpty();
239 QPolygonF gapLinePolygon = QPolygonF({leftLine.p1(), leftLine.p2(), rightLine.p2(), rightLine.p1(), leftLine.p1()});
241 int affectedShapes = 0;
243 for (
int i = 0; i < srcOutlines.size(); i++) {
248 bool skipThisShape = !
willShapeBeCutGeneral(referenceShape, srcOutlines[i], leftOpposite.boundingRect(), rightOpposite.boundingRect(), checkGapLineRect, gapLineRect);
249 skipThisShape = skipThisShape || !
willShapeBeCutPrecise(srcOutlines[i], gapLine, leftLine, rightLine, gapLinePolygon);
253 newSelectedShapes << referenceShape;
261 QPainterPath leftPath = srcOutlines[i] & left;
262 QPainterPath rightPath = srcOutlines[i] & right;
265 bothSides << leftPath << rightPath;
268 Q_FOREACH(QPainterPath path, bothSides) {
269 if (path.isEmpty()) {
279 path = booleanWorkaroundTransformInverted.map(path);
283 if (shape->boundingRect().isEmpty()) {
287 shape->setBackground(referenceShape->
background());
288 shape->setStroke(referenceShape->
stroke());
289 shape->setZIndex(referenceShape->
zIndex());
294 newSelectedShapes << shape.data();
307 if (affectedShapes > 0) {
321 QColor semitransparentGray = QColor(Qt::darkGray);
322 semitransparentGray.setAlphaF(0.6);
323 QPen pen = QPen(QBrush(semitransparentGray), 2);
326 painter.setRenderHint(QPainter::RenderHint::Antialiasing,
true);
331 gutterCenterLine = converter.
documentToView().map(gutterCenterLine);
332 QLineF gutterWidthHelperLine = QLineF(QPointF(0, 0), QPointF(gutterWidth, 0));
333 gutterWidthHelperLine = converter.
documentToView().map(gutterWidthHelperLine);
335 gutterWidth = gutterWidthHelperLine.length();
339 QLineF gutterLine1 = gutterLines.length() > 0 ? gutterLines[0] : gutterCenterLine;
340 QLineF gutterLine2 = gutterLines.length() > 1 ? gutterLines[1] : gutterCenterLine;
343 painter.drawLine(gutterLine1);
344 painter.drawLine(gutterLine2);
346 QRectF arcRect1 = QRectF(gutterCenterLine.p1() - QPointF(gutterWidth/2, gutterWidth/2), gutterCenterLine.p1() + QPointF(gutterWidth/2, gutterWidth/2));
347 QRectF arcRect2 = QRectF(gutterCenterLine.p2() - QPointF(gutterWidth/2, gutterWidth/2), gutterCenterLine.p2() + QPointF(gutterWidth/2, gutterWidth/2));
349 int qtAngleFactor = 16;
350 int qtHalfCircle = qtAngleFactor*180;
357 qreal xLengthEllipse = 2*qSqrt(2);
360 painter.drawLine({QLineF(gutterCenterLine.p1() - QPointF(xLength, xLength), gutterCenterLine.p1() + QPointF(xLength, xLength))});
361 painter.drawLine({QLineF(gutterCenterLine.p2() - QPointF(xLength, xLength), gutterCenterLine.p2() + QPointF(xLength, xLength))});
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))});
368 painter.drawEllipse(gutterCenterLine.p1(), xLengthEllipse, xLengthEllipse);
369 painter.drawEllipse(gutterCenterLine.p2(), xLengthEllipse, xLengthEllipse);
374 semitransparentGray.setAlphaF(0.2);
375 pen.setColor(semitransparentGray);
379 painter.drawLine(gutterCenterLine);
390 return helperGapWidthLineTransformed.length();
395 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
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
void paint(QPainter &painter, const KoViewConverter &converter) override
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...
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)