F:/KPlato/koffice/libs/flake/KoShapeRotateStrategy.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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;  // convert to degrees.
00054     if(modifiers & (Qt::AltModifier | Qt::ControlModifier)) {
00055         // limit to 45 degree angles
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 }

Généré le Wed Nov 22 23:41:00 2006 pour KPlato par  doxygen 1.5.1-p1