Krita Source Code Documentation
Loading...
Searching...
No Matches
KoPathSegmentTypeCommand.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2006, 2009 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 <KoPathSegment.h>
11#include <klocalizedstring.h>
12
14: KUndo2Command(parent)
15, m_segmentType(segmentType)
16{
17 QList<KoPathPointData> pointDataList;
18 pointDataList.append(pointData);
19 initialize(pointDataList);
20}
21
23 KUndo2Command *parent)
24 : KUndo2Command(parent)
25 , m_segmentType(segmentType)
26{
27 initialize(pointDataList);
28}
29
33
35{
38 for (; it != m_pointDataList.constEnd(); ++it) {
39 KoPathShape * pathShape = it->pathShape;
40 pathShape->update();
41
42 KoPathSegment segment = pathShape->segmentByIndex(it->pointIndex);
43
44 if (m_segmentType == Curve) {
45 // we change type to curve -> set control point positions
46 QPointF pointDiff = segment.second()->point() - segment.first()->point();
47 segment.first()->setControlPoint2(segment.first()->point() + pointDiff / 3.0);
48 segment.second()->setControlPoint1(segment.first()->point() + pointDiff * 2.0 / 3.0);
49 } else {
50 // we are changing type to line -> remove control points
51 segment.first()->removeControlPoint2();
52 segment.second()->removeControlPoint1();
53 }
54
55 pathShape->normalize();
56 pathShape->update();
57 }
58}
59
61{
63 for (int i = 0; i < m_pointDataList.size(); ++i) {
64 const KoPathPointData & pd = m_pointDataList.at(i);
65 pd.pathShape->update();
67 const SegmentTypeData segmentData(m_segmentData.at(i));
68
69 if (m_segmentType == Line) {
70 // change type back to curve -> reactivate control points and their positions
73 } else {
74 // change back to line -> remove control points
75 segment.first()->removeControlPoint2();
76 segment.second()->removeControlPoint1();
77 }
78
79 segment.first()->setProperties(segmentData.m_properties2);
80 segment.second()->setProperties(segmentData.m_properties1);
81
82 pd.pathShape->normalize();
83 pd.pathShape->update();
84 }
85}
86
88{
89 QList<KoPathPointData>::const_iterator it(pointDataList.begin());
90 for (; it != pointDataList.end(); ++it) {
91 KoPathSegment segment = it->pathShape->segmentByIndex(it->pointIndex);
92 if (segment.isValid()) {
93 if (m_segmentType == Curve) {
94 // don not change segment if already a curve
95 if (segment.first()->activeControlPoint2() || segment.second()->activeControlPoint1())
96 continue;
97 } else {
98 // do not change segment if already a line
99 if (! segment.first()->activeControlPoint2() && ! segment.second()->activeControlPoint1())
100 continue;
101 }
102
103 m_pointDataList.append(*it);
104 SegmentTypeData segmentData;
105
106 KoPathShape * pathShape = segment.first()->parent();
107
108 // we are changing a curve to a line -> save control point positions
109 if (m_segmentType == Line) {
110 segmentData.m_controlPoint2 = pathShape->shapeToDocument(segment.first()->controlPoint2());
111 segmentData.m_controlPoint1 = pathShape->shapeToDocument(segment.second()->controlPoint1());
112 }
113 // save point properties
114 segmentData.m_properties2 = segment.first()->properties();
115 segmentData.m_properties1 = segment.second()->properties();
116 m_segmentData.append(segmentData);
117 }
118 }
119
120 if (m_segmentType == Curve) {
121 setText(kundo2_i18n("Change segments to curves"));
122 } else {
123 setText(kundo2_i18n("Change segments to lines"));
124 }
125}
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
PointProperties properties
void setProperties(PointProperties properties)
Set the properties of a point.
void setControlPoint1(const QPointF &point)
Set the control point 1.
QPointF point
void setControlPoint2(const QPointF &point)
Set the control point 2.
void removeControlPoint1()
Removes the first control point.
QPointF controlPoint1
KoPathShape * parent() const
Get the path shape the point belongs to.
bool activeControlPoint1
void removeControlPoint2()
Removes the second control point.
bool activeControlPoint2
QPointF controlPoint2
QList< KoPathPointData > m_pointDataList
void undo() override
revert the actions done in redo
QList< SegmentTypeData > m_segmentData
KoPathSegmentTypeCommand(const KoPathPointData &pointData, SegmentType segmentType, KUndo2Command *parent=0)
void initialize(const QList< KoPathPointData > &pointDataList)
void redo() override
redo the command
A KoPathSegment consist of two neighboring KoPathPoints.
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
virtual QPointF normalize()
Normalizes the path data.
KoPathSegment segmentByIndex(const KoPathPointIndex &pointIndex) const
Returns the segment specified by a path point index.
void update() const override
reimplemented
QPointF shapeToDocument(const QPointF &point) const
Transforms point from shape coordinates to document coordinates.
Definition KoShape.cpp:1201
QPointF documentToShape(const QPointF &point) const
Transforms point from document coordinates to shape coordinates.
Definition KoShape.cpp:1211
KUndo2MagicString kundo2_i18n(const char *text)