00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KoShapeRotateStrategy.h"
00021 #include "KoInteractionTool.h"
00022 #include "KoCanvasBase.h"
00023 #include "KoPointerEvent.h"
00024 #include "KoShapeManager.h"
00025 #include "KoCommand.h"
00026
00027 #include <QPointF>
00028
00029 #include <math.h>
00030
00031 KoShapeRotateStrategy::KoShapeRotateStrategy( KoTool *tool, KoCanvasBase *canvas, const QPointF &clicked)
00032 : KoInteractionStrategy(tool, canvas)
00033 , m_initialBoundingRect()
00034 , m_start(clicked)
00035 {
00036 KoSelectionSet selectedShapes = canvas->shapeManager()->selection()->selectedShapes(KoFlake::StrippedSelection);
00037 foreach(KoShape *shape, selectedShapes) {
00038 if(shape->isLocked())
00039 continue;
00040 m_selectedShapes << shape;
00041 m_startPositions << shape->position();
00042 m_startAbsolutePositions << shape->absolutePosition();
00043 m_initialAngles << shape->rotation();
00044 m_initialBoundingRect = m_initialBoundingRect.unite( shape->boundingRect() );
00045 }
00046 m_initialSelectionAngle = canvas->shapeManager()->selection()->rotation();
00047 }
00048
00049 void KoShapeRotateStrategy::handleMouseMove(const QPointF &point, Qt::KeyboardModifiers modifiers) {
00050 QPointF center = m_initialBoundingRect.center();
00051 double angle = atan2( point.y() - center.y(), point.x() - center.x() ) -
00052 atan2( m_start.y() - center.y(), m_start.x() - center.x() );
00053 angle = angle / M_PI * 180;
00054 if(modifiers & (Qt::AltModifier | Qt::ControlModifier)) {
00055
00056 double modula = qAbs(angle);
00057 while(modula > 45.0)
00058 modula -= 45.0;
00059 if(modula > 22.5)
00060 modula -= 45.0;
00061 angle += (angle>0?-1:1)*modula;
00062 }
00063
00064 QMatrix matrix;
00065 matrix.translate(center.x(), center.y());
00066 matrix.rotate(angle);
00067 matrix.translate(-center.x(), -center.y());
00068
00069 int counter=0;
00070 foreach(KoShape *shape, m_selectedShapes) {
00071 shape->repaint();
00072 shape->setAbsolutePosition(matrix.map(m_startAbsolutePositions[counter]));
00073 shape->rotate(m_initialAngles[counter] + angle);
00074 shape->repaint();
00075 counter++;
00076 }
00077 m_canvas->shapeManager()->selection()->rotate(m_initialSelectionAngle + angle);
00078
00079 }
00080
00081 void KoShapeRotateStrategy::paint( QPainter &painter, KoViewConverter &converter) {
00082 SelectionDecorator decorator(KoFlake::NoHandle, true, false);
00083 decorator.setSelection(m_canvas->shapeManager()->selection());
00084 decorator.paint(painter, converter);
00085 }
00086
00087 KCommand* KoShapeRotateStrategy::createCommand() {
00088 KMacroCommand *cmd = new KMacroCommand("Rotate");
00089 QList<QPointF> newPositions;
00090 QList<double> newAngles;
00091 foreach(KoShape *shape, m_selectedShapes) {
00092 newPositions << shape->position();
00093 newAngles << shape->rotation();
00094 }
00095 cmd->addCommand(new KoShapeMoveCommand(m_selectedShapes, m_startPositions, newPositions));
00096 cmd->addCommand(new KoShapeRotateCommand(m_selectedShapes, m_initialAngles, newAngles));
00097 return cmd;
00098 }