Krita Source Code Documentation
Loading...
Searching...
No Matches
ShapeRotateStrategy.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2006-2007 Thomas Zander <zander@kde.org>
3 * SPDX-FileCopyrightText: 2007-2008 Jan Hambrecht <jaham@gmx.net>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
10
11#include <KoToolBase.h>
12#include <KoCanvasBase.h>
13#include <KoSelection.h>
14#include <KoPointerEvent.h>
15#include <KoShapeManager.h>
18
19#include <QPointF>
20#include <math.h>
21#include <klocalizedstring.h>
22
23ShapeRotateStrategy::ShapeRotateStrategy(KoToolBase *tool, KoSelection *selection, const QPointF &clicked, Qt::MouseButtons buttons)
25 , m_start(clicked)
26{
33
34 Q_FOREACH (KoShape *shape, m_transformedShapesAndSelection) {
36 }
37
38 KoFlake::AnchorPosition anchor = !(buttons & Qt::RightButton) ?
41
42 m_rotationCenter = selection->absolutePosition(anchor);
43
44 tool->setStatusText(i18n("Press ALT to rotate in 45 degree steps."));
45}
46
47void ShapeRotateStrategy::handleMouseMove(const QPointF &point, Qt::KeyboardModifiers modifiers)
48{
49 qreal angle = atan2(point.y() - m_rotationCenter.y(), point.x() - m_rotationCenter.x()) -
50 atan2(m_start.y() - m_rotationCenter.y(), m_start.x() - m_rotationCenter.x());
51 angle = angle / M_PI * 180; // convert to degrees.
52 if (modifiers & (Qt::AltModifier | Qt::ControlModifier)) {
53 // limit to 45 degree angles
54 qreal modula = qAbs(angle);
55 while (modula > 45.0) {
56 modula -= 45.0;
57 }
58 if (modula > 22.5) {
59 modula -= 45.0;
60 }
61 angle += (angle > 0 ? -1 : 1) * modula;
62 }
63
64 rotateBy(angle);
65}
66
68{
69 QTransform matrix;
70 matrix.translate(m_rotationCenter.x(), m_rotationCenter.y());
71 matrix.rotate(angle);
72 matrix.translate(-m_rotationCenter.x(), -m_rotationCenter.y());
73
74 QTransform applyMatrix = matrix * m_rotationMatrix.inverted();
75 m_rotationMatrix = matrix;
76 Q_FOREACH (KoShape *shape, m_transformedShapesAndSelection) {
77 QRectF dirtyRect = shape->boundingRect();
78 shape->applyAbsoluteTransformation(applyMatrix);
79 dirtyRect |= shape->boundingRect();
80 shape->updateAbsolute(dirtyRect);
81 }
82}
83
84void ShapeRotateStrategy::paint(QPainter &painter, const KoViewConverter &converter)
85{
86 // paint the rotation center
87 painter.setPen(QPen(Qt::red));
88 painter.setBrush(QBrush(Qt::red));
89 painter.setRenderHint(QPainter::Antialiasing, true);
90 QRectF circle(0, 0, handleRadius(), handleRadius());
91 circle.moveCenter(converter.documentToView(m_rotationCenter));
92 painter.drawEllipse(circle);
93}
94
96{
97 QList<QTransform> newTransforms;
98 Q_FOREACH (KoShape *shape, m_transformedShapesAndSelection) {
99 newTransforms << shape->transformation();
100 }
101
103 cmd->setText(kundo2_i18n("Rotate"));
104 return cmd;
105}
void setText(const KUndo2MagicString &text)
QPointer< KoCanvasResourceProvider > resourceManager
uint handleRadius() const
Convenience function to get the global handle radius.
const QList< KoShape * > selectedEditableShapes() const
QPointF absolutePosition(KoFlake::AnchorPosition anchor=KoFlake::Center) const
Definition KoShape.cpp:653
void applyAbsoluteTransformation(const QTransform &matrix)
Definition KoShape.cpp:400
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:335
QTransform transformation() const
Returns the shapes local transformation matrix.
Definition KoShape.cpp:424
virtual void updateAbsolute(const QRectF &rect) const
Definition KoShape.cpp:616
KoCanvasBase * canvas() const
Returns the canvas the tool is working on.
void setStatusText(const QString &statusText)
virtual QPointF documentToView(const QPointF &documentPoint) const
void paint(QPainter &painter, const KoViewConverter &converter) override
ShapeRotateStrategy(KoToolBase *tool, KoSelection *selection, const QPointF &clicked, Qt::MouseButtons buttons)
QList< QTransform > m_oldTransforms
void handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override
void rotateBy(qreal angle)
QList< KoShape * > m_transformedShapesAndSelection
KUndo2Command * createCommand() override
#define M_PI
Definition kis_global.h:111
QString buttons(const T &ev)
KUndo2MagicString kundo2_i18n(const char *text)
AnchorPosition
Definition KoFlake.h:85
@ Center
Definition KoFlake.h:90
@ HotPosition
Definition KoFlake.h:102