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"
12
14 const KoPathPointData &pointData,
15 const QPointF &offset,
16 KoPathPoint::PointType pointType,
17 KUndo2Command *parent)
18 : KUndo2Command(parent)
19 , m_pointData(pointData)
20 , m_pointType(pointType)
21{
22 Q_ASSERT(offset.x() < 1e14 && offset.y() < 1e14);
23 KoPathShape * pathShape = m_pointData.pathShape;
24 KoPathPoint * point = pathShape->pointByIndex(m_pointData.pointIndex);
25 if (point) {
26 m_offset = offset;
27 }
28
29 setText(kundo2_i18n("Move control point"));
30}
31
33{
35 KoPathShape * pathShape = m_pointData.pathShape;
36 KoPathPoint * point = pathShape->pointByIndex(m_pointData.pointIndex);
37 if (point) {
38 const QRectF oldDirtyRect = pathShape->boundingRect();
39
41 point->setControlPoint1(point->controlPoint1() + m_offset);
42 if (point->properties() & KoPathPoint::IsSymmetric) {
43 // set the other control point so that it lies on the line between the moved
44 // control point and the point, with the same distance to the point as the moved point
45 point->setControlPoint2(2.0 * point->point() - point->controlPoint1());
46 } else if (point->properties() & KoPathPoint::IsSmooth) {
47 // move the other control point so that it lies on the line through point and control point
48 // keeping its distance to the point
49 QPointF direction = point->point() - point->controlPoint1();
50 direction /= sqrt(direction.x() * direction.x() + direction.y() * direction.y());
51 QPointF distance = point->point() - point->controlPoint2();
52 qreal length = sqrt(distance.x() * distance.x() + distance.y() * distance.y());
53 point->setControlPoint2(point->point() + length * direction);
54 }
56 point->setControlPoint2(point->controlPoint2() + m_offset);
57 if (point->properties() & KoPathPoint::IsSymmetric) {
58 // set the other control point so that it lies on the line between the moved
59 // control point and the point, with the same distance to the point as the moved point
60 point->setControlPoint1(2.0 * point->point() - point->controlPoint2());
61 } else if (point->properties() & KoPathPoint::IsSmooth) {
62 // move the other control point so that it lies on the line through point and control point
63 // keeping its distance to the point
64 QPointF direction = point->point() - point->controlPoint2();
65 direction /= sqrt(direction.x() * direction.x() + direction.y() * direction.y());
66 QPointF distance = point->point() - point->controlPoint1();
67 qreal length = sqrt(distance.x() * distance.x() + distance.y() * distance.y());
68 point->setControlPoint1(point->point() + length * direction);
69 }
70 }
71
72 pathShape->normalize();
73 pathShape->updateAbsolute(oldDirtyRect | pathShape->boundingRect());
74 }
75}
76
78{
80 m_offset *= -1.0;
81 redo();
82 m_offset *= -1.0;
83}
84
89
91{
92 const KoPathControlPointMoveCommand *other = dynamic_cast<const KoPathControlPointMoveCommand*>(command);
93
94 if (!other ||
95 other->m_pointData != m_pointData ||
96 other->m_pointType != m_pointType) {
97
98 return false;
99 }
100
101 m_offset += other->m_offset;
102
103 return true;
104}
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.
QRectF boundingRect() const override
reimplemented
KoPathPoint * pointByIndex(const KoPathPointIndex &pointIndex) const
Returns the path point specified by a path point index.
virtual void updateAbsolute(const QRectF &rect) const
Definition KoShape.cpp:616
KUndo2MagicString kundo2_i18n(const char *text)