Krita Source Code Documentation
Loading...
Searching...
No Matches
KoPathPointInsertCommand.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2006, 2008 Jan Hambrecht <jaham@gmx.net>
3 * SPDX-FileCopyrightText: 2006, 2007 Thorsten Zachmann <zachmann@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
9
10#include "KoPathPoint.h"
11#include <KoPathSegment.h>
12#include <klocalizedstring.h>
13
28
30 : KUndo2Command(parent),
32{
33 if (insertPosition < 0)
34 insertPosition = 0;
35 if (insertPosition > 1)
36 insertPosition = 1;
37
38 //TODO the list needs to be sorted
39
40 QList<KoPathPointData>::const_iterator it(pointDataList.begin());
41 for (; it != pointDataList.end(); ++it) {
42 KoPathShape * pathShape = it->pathShape;
43
44 KoPathSegment segment = pathShape->segmentByIndex(it->pointIndex);
45
46 // should not happen but to be sure
47 if (! segment.isValid())
48 continue;
49
50 d->pointDataList.append(*it);
51
52 QPair<KoPathSegment, KoPathSegment> splitSegments = segment.splitAt(insertPosition);
53
54 KoPathPoint * split1 = splitSegments.first.second();
55 KoPathPoint * split2 = splitSegments.second.first();
56 KoPathPoint * splitPoint = new KoPathPoint(pathShape, split1->point());
57 if(split1->activeControlPoint1())
58 splitPoint->setControlPoint1(split1->controlPoint1());
59 if(split2->activeControlPoint2())
60 splitPoint->setControlPoint2(split2->controlPoint2());
61
62 d->points.append(splitPoint);
63 QPointF cp1 = splitSegments.first.first()->controlPoint2();
64 QPointF cp2 = splitSegments.second.second()->controlPoint1();
65 d->controlPoints.append(QPair<QPointF, QPointF>(cp1, cp2));
66 }
67 setText(kundo2_i18n("Insert points"));
68}
69
74
76{
78 for (int i = d->pointDataList.size() - 1; i >= 0; --i) {
79 KoPathPointData pointData = d->pointDataList.at(i);
80 KoPathShape * pathShape = pointData.pathShape;
81
82 KoPathSegment segment = pathShape->segmentByIndex(pointData.pointIndex);
83
84 ++pointData.pointIndex.second;
85
86 if (segment.first()->activeControlPoint2()) {
87 QPointF controlPoint2 = segment.first()->controlPoint2();
88 std::swap(controlPoint2, d->controlPoints[i].first);
89 segment.first()->setControlPoint2(controlPoint2);
90 }
91
92 if (segment.second()->activeControlPoint1()) {
93 QPointF controlPoint1 = segment.second()->controlPoint1();
94 std::swap(controlPoint1, d->controlPoints[i].second);
95 segment.second()->setControlPoint1(controlPoint1);
96 }
97
98 pathShape->insertPoint(d->points.at(i), pointData.pointIndex);
99 pathShape->recommendPointSelectionChange({pointData.pointIndex});
100 pathShape->update();
101 }
102 d->deletePoints = false;
103}
104
106{
108 for (int i = 0; i < d->pointDataList.size(); ++i) {
109 const KoPathPointData &pdBefore = d->pointDataList.at(i);
110 KoPathShape * pathShape = pdBefore.pathShape;
111 KoPathPointIndex piAfter = pdBefore.pointIndex;
112 ++piAfter.second;
113
114 KoPathPoint * before = pathShape->pointByIndex(pdBefore.pointIndex);
115
116 d->points[i] = pathShape->removePoint(piAfter);
117
118 if (d->points[i]->properties() & KoPathPoint::CloseSubpath) {
119 piAfter.second = 0;
120 }
121
122 KoPathPoint * after = pathShape->pointByIndex(piAfter);
123
124 if (before->activeControlPoint2()) {
125 QPointF controlPoint2 = before->controlPoint2();
126 std::swap(controlPoint2, d->controlPoints[i].first);
127 before->setControlPoint2(controlPoint2);
128 }
129
130 if (after->activeControlPoint1()) {
131 QPointF controlPoint1 = after->controlPoint1();
132 std::swap(controlPoint1, d->controlPoints[i].second);
133 after->setControlPoint1(controlPoint1);
134 }
135
136 QList<KoPathPointIndex> segmentPoints;
137 segmentPoints << pdBefore.pointIndex;
138
139 KoPathPointIndex nextPoint(pdBefore.pointIndex.first, pdBefore.pointIndex.second + 1);
140 if (pathShape->pointByIndex(nextPoint)) {
141 segmentPoints << nextPoint;
142 }
143
144 pathShape->recommendPointSelectionChange(segmentPoints);
145 pathShape->update();
146 }
147 d->deletePoints = true;
148}
149
QPair< int, int > KoPathPointIndex
Definition KoPathShape.h:28
virtual void undo()
void setText(const KUndo2MagicString &text)
virtual void redo()
Describe a KoPathPoint by a KoPathShape and its indices.
KoPathPointIndex pointIndex
position of the point in the path shape
KoPathShape * pathShape
path shape the path point belongs too
QList< QPair< QPointF, QPointF > > controlPoints
KoPathPointInsertCommandPrivate *const d
KoPathPointInsertCommand(const QList< KoPathPointData > &pointDataList, qreal insertPosition, KUndo2Command *parent=0)
void redo() override
redo the command
void undo() override
revert the actions done in redo
QList< KoPathPoint * > insertedPoints() const
Returns list of inserted points.
A KoPathPoint represents a point in a path.
void setControlPoint1(const QPointF &point)
Set the control point 1.
QPointF point
void setControlPoint2(const QPointF &point)
Set the control point 2.
QPointF controlPoint1
@ CloseSubpath
it closes a subpath (only applicable on StartSubpath and StopSubpath)
Definition KoPathPoint.h:40
bool activeControlPoint1
bool activeControlPoint2
QPointF controlPoint2
A KoPathSegment consist of two neighboring KoPathPoints.
QPair< KoPathSegment, KoPathSegment > splitAt(qreal t) const
Splits segment at given position returning the two resulting segments.
KoPathPoint * first
KoPathPoint * second
bool isValid() const
Returns if segment is valid, e.g. has two valid points.
The position of a path point within a path shape.
Definition KoPathShape.h:63
void recommendPointSelectionChange(const QList< KoPathPointIndex > &newSelection)
KoPathPoint * removePoint(const KoPathPointIndex &pointIndex)
Removes a point from the path.
KoPathSegment segmentByIndex(const KoPathPointIndex &pointIndex) const
Returns the segment specified by a path point index.
KoPathPoint * pointByIndex(const KoPathPointIndex &pointIndex) const
Returns the path point specified by a path point index.
bool insertPoint(KoPathPoint *point, const KoPathPointIndex &pointIndex)
Inserts a new point into the given subpath at the specified position.
void update() const override
reimplemented
KUndo2MagicString kundo2_i18n(const char *text)