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

Move performance checks in Knife Tool to functions

Agata Cackoea6ea49d4272
diff --git a/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp b/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp
index deb4d73328..8416a741ef 100644
--- a/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp
+++ b/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp
@@ -85,6 +85,62 @@ void CutThroughShapeStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::
}
+
+bool CutThroughShapeStrategy::willShapeBeCutGeneral(KoShape* referenceShape, const QPainterPath& srcOutline, const QRectF& leftOppositeRect, const QRectF& rightOppositeRect, bool checkGapLineRect, const QRectF& gapLineRect)
+{
+ if (dynamic_cast<KoSvgTextShape*>(referenceShape)) {
+ // skip all text
+ return false;
+ }
+
+ if ((srcOutline.boundingRect() & leftOppositeRect).isEmpty()
+ || (srcOutline.boundingRect() & rightOppositeRect).isEmpty()) {
+ // there is nothing on one side
+ // everything is on the other, far away from the gap line
+ // it just makes it a bit faster when there is a whole lot of shapes
+
+ return false;
+ }
+
+ if (checkGapLineRect && (srcOutline.boundingRect() & gapLineRect).isEmpty()) {
+ // the gap lines can't cross the shape since their bounding rects don't cross it
+ return false;
+ }
+
+ return true;
+}
+
+bool CutThroughShapeStrategy::willShapeBeCutPrecise(const QPainterPath& srcOutline, const QLineF gapLine, const QLineF& leftLine, const QLineF& rightLine, const QPolygonF& gapLinePolygon)
+{
+ bool containsGapLinePointStart = srcOutline.contains(gapLine.p1());
+ bool containsGapLinePointEnd = srcOutline.contains(gapLine.p2());
+
+ // if should skip if there is exactly one gap line point inside the shape
+ bool exactlyOneGapLinePointInside = (containsGapLinePointStart != containsGapLinePointEnd);
+ bool bothGapLinePointsInside = containsGapLinePointStart && containsGapLinePointEnd;
+
+ if (exactlyOneGapLinePointInside) {
+ return false;
+ }
+
+ bool crossesGapLine = KisAlgebra2D::getLineSegmentCrossingLineIndexes(leftLine, srcOutline).count() > 0
+ || KisAlgebra2D::getLineSegmentCrossingLineIndexes(rightLine, srcOutline).count() > 0;
+
+
+ bool containsPointWithinGap = false;
+ Q_FOREACH(QPointF p, srcOutline.toFillPolygon()) {
+ if (gapLinePolygon.containsPoint(p, Qt::WindingFill)) {
+ containsPointWithinGap = true;
+ break;
+ }
+ }
+
+ if (!bothGapLinePointsInside && !crossesGapLine && !containsPointWithinGap) {
+ return false;
+ }
+ return true;
+}
+
void CutThroughShapeStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
{
tool()->canvas()->updateCanvas(m_previousLineDirtyRect);
@@ -189,71 +245,16 @@ void CutThroughShapeStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
KoShape* referenceShape = m_allShapes[i];
bool wasSelected = m_selectedShapes.contains(referenceShape);
+ bool skipThisShape = !willShapeBeCutGeneral(referenceShape, srcOutlines[i], leftOpposite.boundingRect(), rightOpposite.boundingRect(), checkGapLineRect, gapLineRect);
+ skipThisShape = skipThisShape || !willShapeBeCutPrecise(srcOutlines[i], gapLine, leftLine, rightLine, gapLinePolygon);
- if (dynamic_cast<KoSvgTextShape*>(referenceShape)) {
- // skip all text
- if (wasSelected) {
- newSelectedShapes << referenceShape;
- }
- continue;
- }
-
- if ((srcOutlines[i].boundingRect() & leftOpposite.boundingRect()).isEmpty()
- || (srcOutlines[i].boundingRect() & rightOpposite.boundingRect()).isEmpty()) {
- // there is nothing on one side
- // everything is on the other, far away from the gap line
- // it just makes it a bit faster when there is a whole lot of shapes
-
+ if (skipThisShape) {
if (wasSelected) {
newSelectedShapes << referenceShape;
}
continue;
}
-
- if (checkGapLineRect && (srcOutlines[i].boundingRect() & gapLineRect).isEmpty()) {
- // the gap lines can't cross the shape since their bounding rects don't cross it
- if (wasSelected) {
- newSelectedShapes << referenceShape;
- }
- continue;
- }
-
- bool containsGapLinePointStart = srcOutlines[i].contains(gapLine.p1());
- bool containsGapLinePointEnd = srcOutlines[i].contains(gapLine.p2());
-
- // if should skip if there is exactly one gap line point inside the shape
- bool exactlyOneGapLinePointInside = (containsGapLinePointStart != containsGapLinePointEnd);
- bool bothGapLinePointsInside = containsGapLinePointStart && containsGapLinePointEnd;
-
- if (exactlyOneGapLinePointInside) {
- if (wasSelected) {
- newSelectedShapes << referenceShape;
- }
- continue;
- }
-
- bool crossesGapLine = KisAlgebra2D::getLineSegmentCrossingLineIndexes(leftLine, srcOutlines[i]).count() > 0
- || KisAlgebra2D::getLineSegmentCrossingLineIndexes(rightLine, srcOutlines[i]).count() > 0;
-
- bool containsPointWithinGap = false;
- Q_FOREACH(QPointF p, srcOutlines[i].toFillPolygon()) {
- if (gapLinePolygon.containsPoint(p, Qt::WindingFill)) {
- containsPointWithinGap = true;
- break;
- }
- }
- if (!bothGapLinePointsInside && !crossesGapLine && !containsPointWithinGap) {
-
- //qCritical() << "it doesn't cross the line!";
- if (wasSelected) {
- newSelectedShapes << referenceShape;
- }
- continue;
- }
-
-
-
affectedShapes++;
diff --git a/plugins/tools/tool_knife/CutThroughShapeStrategy.h b/plugins/tools/tool_knife/CutThroughShapeStrategy.h
index a5baea38d3..ff73828e97 100644
--- a/plugins/tools/tool_knife/CutThroughShapeStrategy.h
+++ b/plugins/tools/tool_knife/CutThroughShapeStrategy.h
@@ -39,6 +39,9 @@ private:
qreal gutterWidthInDocumentCoordinates(qreal lineAngle);
qreal calculateLineAngle(QPointF start, QPointF end);
+ bool willShapeBeCutGeneral(KoShape* referenceShape, const QPainterPath &srcOutline, const QRectF &leftOppositeRect, const QRectF &rightOppositeRect, bool checkGapLineRect, const QRectF &gapLineRect);
+ bool willShapeBeCutPrecise(const QPainterPath& srcOutline, const QLineF gapLine, const QLineF& leftLine, const QLineF& rightLine, const QPolygonF& gapLinePolygon);
+
private: