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>
19
20#include <QPointF>
21#include <math.h>
22#include <klocalizedstring.h>
23
24ShapeRotateStrategy::ShapeRotateStrategy(KoToolBase *tool, KoSelection *selection, const QPointF &clicked, Qt::MouseButtons buttons)
26 , m_start(clicked)
27{
34
35 Q_FOREACH (KoShape *shape, m_transformedShapesAndSelection) {
37 }
38
39 KoFlake::AnchorPosition anchor = !(buttons & Qt::RightButton) ?
42
43 m_rotationCenter = selection->absolutePosition(anchor);
44
45 tool->setStatusText(i18n("Press ALT to rotate in 45 degree steps."));
46}
47
48void ShapeRotateStrategy::handleMouseMove(const QPointF &point, Qt::KeyboardModifiers modifiers)
49{
50 qreal angle = atan2(point.y() - m_rotationCenter.y(), point.x() - m_rotationCenter.x()) -
51 atan2(m_start.y() - m_rotationCenter.y(), m_start.x() - m_rotationCenter.x());
52 angle = angle / M_PI * 180; // convert to degrees.
53 if (modifiers & (Qt::AltModifier | Qt::ControlModifier)) {
54 // limit to 45 degree angles
55 qreal modula = qAbs(angle);
56 while (modula > 45.0) {
57 modula -= 45.0;
58 }
59 if (modula > 22.5) {
60 modula -= 45.0;
61 }
62 angle += (angle > 0 ? -1 : 1) * modula;
63 }
64
65 rotateBy(angle);
66}
67
69{
70 QTransform matrix;
71 matrix.translate(m_rotationCenter.x(), m_rotationCenter.y());
72 matrix.rotate(angle);
73 matrix.translate(-m_rotationCenter.x(), -m_rotationCenter.y());
74
75 QTransform applyMatrix = matrix * m_rotationMatrix.inverted();
76 m_rotationMatrix = matrix;
77
79
80 Q_FOREACH (KoShape *shape, m_transformedShapesAndSelection) {
81 shape->applyAbsoluteTransformation(applyMatrix);
82 }
83
85}
86
87void ShapeRotateStrategy::paint(QPainter &painter, const KoViewConverter &converter)
88{
89 // paint the rotation center
90 painter.setPen(QPen(Qt::red));
91 painter.setBrush(QBrush(Qt::red));
92 painter.setRenderHint(QPainter::Antialiasing, true);
93 QRectF circle(0, 0, handleRadius(), handleRadius());
94 circle.moveCenter(converter.documentToView(m_rotationCenter));
95 painter.drawEllipse(circle);
96}
97
99{
100 QList<QTransform> newTransforms;
101 Q_FOREACH (KoShape *shape, m_transformedShapesAndSelection) {
102 newTransforms << shape->transformation();
103 }
104
106 cmd->setText(kundo2_i18n("Rotate"));
107 return cmd;
108}
void setText(const KUndo2MagicString &text)
QPointer< KoCanvasResourceProvider > resourceManager
uint handleRadius() const
Convenience function to get the global handle radius.
const QList< KoShape * > selectedEditableShapes() const
static void bulkShapesUpdate(const UpdatesList &updates)
QPointF absolutePosition(KoFlake::AnchorPosition anchor=KoFlake::Center) const
Definition KoShape.cpp:573
void applyAbsoluteTransformation(const QTransform &matrix)
Definition KoShape.cpp:353
QTransform transformation() const
Returns the shapes local transformation matrix.
Definition KoShape.cpp:383
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