Krita Source Code Documentation
Loading...
Searching...
No Matches
KoPathControlPointMoveCommand.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2006 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#include <klocalizedstring.h>
10#include <math.h>
11#include "kis_command_ids.h"
13
15 const KoPathPointData &pointData,
16 const QPointF &offset,
17 KoPathPoint::PointType pointType,
18 KUndo2Command *parent)
19 : KUndo2Command(parent)
20 , m_pointData(pointData)
21 , m_pointType(pointType)
22{
23 Q_ASSERT(offset.x() < 1e14 && offset.y() < 1e14);
24 KoPathShape * pathShape = m_pointData.pathShape;
25 KoPathPoint * point = pathShape->pointByIndex(m_pointData.pointIndex);
26 if (point) {
27 m_offset = offset;
28 }
29
30 setText(kundo2_i18n("Move control point"));
31}
32
34{
36 KoPathShape * pathShape = m_pointData.pathShape;
37 KoPathPoint * point = pathShape->pointByIndex(m_pointData.pointIndex);
38 if (point) {
39 KoShapeBulkActionLock lock(pathShape);
40
42 point->setControlPoint1(point->controlPoint1() + m_offset);
43 if (point->properties() & KoPathPoint::IsSymmetric) {
44 // set the other control point so that it lies on the line between the moved
45 // control point and the point, with the same distance to the point as the moved point
46 point->setControlPoint2(2.0 * point->point() - point->controlPoint1());
47 } else if (point->properties() & KoPathPoint::IsSmooth) {
48 // move the other control point so that it lies on the line through point and control point
49 // keeping its distance to the point
50 QPointF direction = point->point() - point->controlPoint1();
51 direction /= sqrt(direction.x() * direction.x() + direction.y() * direction.y());
52 QPointF distance = point->point() - point->controlPoint2();
53 qreal length = sqrt(distance.x() * distance.x() + distance.y() * distance.y());
54 point->setControlPoint2(point->point() + length * direction);
55 }
57 point->setControlPoint2(point->controlPoint2() + m_offset);
58 if (point->properties() & KoPathPoint::IsSymmetric) {
59 // set the other control point so that it lies on the line between the moved
60 // control point and the point, with the same distance to the point as the moved point
61 point->setControlPoint1(2.0 * point->point() - point->controlPoint2());
62 } else if (point->properties() & KoPathPoint::IsSmooth) {
63 // move the other control point so that it lies on the line through point and control point
64 // keeping its distance to the point
65 QPointF direction = point->point() - point->controlPoint2();
66 direction /= sqrt(direction.x() * direction.x() + direction.y() * direction.y());
67 QPointF distance = point->point() - point->controlPoint1();
68 qreal length = sqrt(distance.x() * distance.x() + distance.y() * distance.y());
69 point->setControlPoint1(point->point() + length * direction);
70 }
71 }
72
73 pathShape->normalize();
74
76 }
77}
78
80{
82 m_offset *= -1.0;
83 redo();
84 m_offset *= -1.0;
85}
86
91
93{
94 const KoPathControlPointMoveCommand *other = dynamic_cast<const KoPathControlPointMoveCommand*>(command);
95
96 if (!other ||
97 other->m_pointData != m_pointData ||
98 other->m_pointType != m_pointType) {
99
100 return false;
101 }
102
103 m_offset += other->m_offset;
104
105 return true;
106}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
qreal distance(const QPointF &p1, const QPointF &p2)
virtual void undo()
void setText(const KUndo2MagicString &text)
virtual void redo()
The undo / redo command for path point moving.
KoPathControlPointMoveCommand(const KoPathPointData &pointData, const QPointF &offset, KoPathPoint::PointType pointType, KUndo2Command *parent=0)
bool mergeWith(const KUndo2Command *command) override
void undo() override
revert the actions done in 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
A KoPathPoint represents a point in a path.
PointProperties properties
void setControlPoint1(const QPointF &point)
Set the control point 1.
QPointF point
void setControlPoint2(const QPointF &point)
Set the control point 2.
QPointF controlPoint1
@ IsSmooth
it is smooth, both control points on a line through the point
Definition KoPathPoint.h:41
@ IsSymmetric
it is symmetric, like smooth but control points have same distance to point
Definition KoPathPoint.h:42
PointType
the type for identifying part of a KoPathPoint
Definition KoPathPoint.h:47
@ ControlPoint2
the second control point
Definition KoPathPoint.h:51
@ ControlPoint1
the first control point
Definition KoPathPoint.h:50
QPointF controlPoint2
The position of a path point within a path shape.
Definition KoPathShape.h:63
virtual QPointF normalize()
Normalizes the path data.
KoPathPoint * pointByIndex(const KoPathPointIndex &pointIndex) const
Returns the path point specified by a path point index.
static void bulkShapesUpdate(const UpdatesList &updates)
KUndo2MagicString kundo2_i18n(const char *text)