Krita Source Code Documentation
Loading...
Searching...
No Matches
KoPathPointRemoveCommand.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
10#include "KoShapeController.h"
11#include "KoPathPoint.h"
12#include <klocalizedstring.h>
13
26
28 const QList<KoPathPointData> &pointDataList,
29 KoShapeController *shapeController,
30 KUndo2Command *parent)
31{
32 /*
33 * We want to decide if we have to:
34 * 1. delete only some points of a path or
35 * 2. delete one or more complete subpath or
36 * 3. delete a complete path
37 */
38
39 QList<KoPathPointData> sortedPointData(pointDataList);
40 std::sort(sortedPointData.begin(), sortedPointData.end());
41
42 KoPathPointData last(0, KoPathPointIndex(-1, -1));
43 // add last at the end so that the point date before last will also be put in
44 // the right places.
45 sortedPointData.append(last);
46
47 QList<KoPathPointData> pointsOfSubpath; // points of current subpath
48 QList<KoPathPointData> subpathsOfPath; // subpaths of current path
49 QList<KoPathPointData> pointsToDelete; // single points to delete
50 QList<KoPathPointData> subpathToDelete; // single subpaths to delete
51 QList<KoShape*> shapesToDelete; // single paths to delete
52
53 last = sortedPointData.first();
54
55 QList<KoPathPointData>::const_iterator it(sortedPointData.constBegin());
56 for (; it != sortedPointData.constEnd(); ++it) {
57 // check if we have come to the next subpath of the same or another path
58 if (last.pathShape != it->pathShape || last.pointIndex.first != it->pointIndex.first) {
59 // check if all points of the last subpath should be deleted
60 if (last.pathShape->subpathPointCount(last.pointIndex.first) == pointsOfSubpath.size()) {
61 // all points of subpath to be deleted -> mark subpath as to be deleted
62 subpathsOfPath.append(pointsOfSubpath.first());
63 } else {
64 // not all points of subpath to be deleted -> add them to the delete point list
65 pointsToDelete += pointsOfSubpath;
66 }
67 // clear the suboath point list
68 pointsOfSubpath.clear();
69 }
70
71 // check if we have come to the next shape
72 if (last.pathShape != it->pathShape) {
73 // check if all subpath of the shape should be deleted
74 if (last.pathShape->subpathCount() == subpathsOfPath.size()) {
75 // all subpaths of path to be deleted -> add shape to delete shape list
76 shapesToDelete.append(last.pathShape);
77 } else {
78 // not all subpaths of path to be deleted -> add them to delete subpath list
79 subpathToDelete += subpathsOfPath;
80 }
81 subpathsOfPath.clear();
82 }
83 if (! it->pathShape)
84 continue;
85 // keep reference to last point
86 last = *it;
87 // add this point to the current subpath point list
88 pointsOfSubpath.append(*it);
89 }
90
91 KUndo2Command *cmd = new KUndo2Command(kundo2_i18n("Remove points"), parent);
92
93 if (pointsToDelete.size() > 0) {
94 new KoPathPointRemoveCommand(pointsToDelete, cmd);
95 }
96 Q_FOREACH (const KoPathPointData & pd, subpathToDelete) {
97 new KoSubpathRemoveCommand(pd.pathShape, pd.pointIndex.first, cmd);
98 }
99 if (shapesToDelete.size() > 0) {
100 shapeController->removeShapes(shapesToDelete, cmd);
101 }
102
103 return cmd;
104}
105
107 KUndo2Command *parent)
108 : KUndo2Command(parent),
110{
111 QList<KoPathPointData>::const_iterator it(pointDataList.begin());
112 for (; it != pointDataList.end(); ++it) {
113 KoPathPoint *point = it->pathShape->pointByIndex(it->pointIndex);
114 if (point) {
115 d->pointDataList.append(*it);
116 d->points.append(0);
117 }
118 }
119 std::sort(d->pointDataList.begin(), d->pointDataList.end());
120 setText(kundo2_i18n("Remove points"));
121}
122
127
129{
131 KoPathShape * lastPathShape = 0;
132 int updateBefore = d->pointDataList.size();
133 for (int i = d->pointDataList.size() - 1; i >= 0; --i) {
134 const KoPathPointData &pd = d->pointDataList.at(i);
135 pd.pathShape->update();
137
138 if (lastPathShape != pd.pathShape) {
139 if (lastPathShape) {
140 QPointF offset = lastPathShape->normalize();
141
142 QTransform matrix;
143 matrix.translate(-offset.x(), -offset.y());
144 for (int j = i + 1; j < updateBefore; ++j) {
145 d->points.at(j)->map(matrix);
146 }
147 lastPathShape->update();
148 updateBefore = i + 1;
149 }
150 lastPathShape = pd.pathShape;
151 }
152 }
153
154 if (lastPathShape) {
155 QPointF offset = lastPathShape->normalize();
156
157 QTransform matrix;
158 matrix.translate(-offset.x(), -offset.y());
159 for (int j = 0; j < updateBefore; ++j) {
160 d->points.at(j)->map(matrix);
161 }
162 lastPathShape->update();
163 }
164
165 d->deletePoints = true;
166}
167
169{
171 KoPathShape * lastPathShape = 0;
172
173 QMap<KoPathShape *, QList<KoPathPointIndex>> pointsMap;
174
175 for (int i = 0; i < d->pointDataList.size(); ++i) {
176 const KoPathPointData &pd = d->pointDataList.at(i);
177 if (lastPathShape && lastPathShape != pd.pathShape) {
178 lastPathShape->normalize();
179 lastPathShape->update();
180 }
182 lastPathShape = pd.pathShape;
183
184 pointsMap[pd.pathShape].append(pd.pointIndex);
185 }
186
187 if (lastPathShape) {
188 lastPathShape->normalize();
189 lastPathShape->update();
190 }
191
192 for (auto it = pointsMap.constBegin(); it != pointsMap.constEnd(); ++it) {
193 it.key()->recommendPointSelectionChange(it.value());
194 }
195
196 d->deletePoints = false;
197}
QPair< int, int > KoPathPointIndex
Definition KoPathShape.h:28
virtual void undo()
void setText(const KUndo2MagicString &text)
virtual void redo()
KUndo2Command(KUndo2Command *parent=0)
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
void undo() override
revert the actions done in redo
void redo() override
redo the command
static KUndo2Command * createCommand(const QList< KoPathPointData > &pointDataList, KoShapeController *shapeController, KUndo2Command *parent=0)
Create command for removing points from path shapes.
KoPathPointRemoveCommandPrivate * d
KoPathPointRemoveCommand(const QList< KoPathPointData > &pointDataList, KUndo2Command *parent=0)
Command to remove a points from path shapes.
A KoPathPoint represents a point in a path.
The position of a path point within a path shape.
Definition KoPathShape.h:63
int subpathPointCount(int subpathIndex) const
Returns the number of points in a subpath.
virtual QPointF normalize()
Normalizes the path data.
KoPathPoint * removePoint(const KoPathPointIndex &pointIndex)
Removes a point from the path.
int subpathCount() const
Returns the number of subpaths in the path.
bool insertPoint(KoPathPoint *point, const KoPathPointIndex &pointIndex)
Inserts a new point into the given subpath at the specified position.
void update() const override
reimplemented
KUndo2Command * removeShapes(const QList< KoShape * > &shapes, KUndo2Command *parent=0)
The undo / redo command for removing a subpath.
KUndo2MagicString kundo2_i18n(const char *text)