libs/flake

KoPathPointMoveStrategy.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006 Jan Hambrecht <jaham@gmx.net>
00003  * Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>
00004  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include "KoPathPointMoveStrategy.h"
00023 
00024 #include "commands/KoPathPointMoveCommand.h"
00025 #include "KoPathTool.h"
00026 #include "KoPathToolSelection.h"
00027 #include "KoSnapGuide.h"
00028 #include <KoCanvasBase.h>
00029 
00030 KoPathPointMoveStrategy::KoPathPointMoveStrategy(KoPathTool *tool, KoCanvasBase *canvas, const QPointF &pos)
00031         : KoInteractionStrategy(tool, canvas)
00032         , m_originalPosition(pos)
00033         , m_tool(tool)
00034 {
00035 }
00036 
00037 KoPathPointMoveStrategy::~KoPathPointMoveStrategy()
00038 {
00039 }
00040 
00041 void KoPathPointMoveStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
00042 {
00043     m_tool->canvas()->updateCanvas(m_tool->canvas()->snapGuide()->boundingRect());
00044     QPointF newPosition = m_tool->canvas()->snapGuide()->snap(mouseLocation, modifiers);
00045     m_tool->canvas()->updateCanvas(m_tool->canvas()->snapGuide()->boundingRect());
00046     QPointF move = newPosition - m_originalPosition;
00047 
00048     if (modifiers & Qt::ControlModifier) { // limit change to one direction only.
00049         if (qAbs(move.x()) > qAbs(move.y()))
00050             move.setY(0);
00051         else
00052             move.setX(0);
00053     }
00054 
00055     KoPathToolSelection * selection = dynamic_cast<KoPathToolSelection*>(m_tool->selection());
00056     if (! selection)
00057         return;
00058 
00059     KoPathPointMoveCommand cmd(selection->selectedPointsData(), move - m_move);
00060     cmd.redo();
00061     m_move = move;
00062 }
00063 
00064 void KoPathPointMoveStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
00065 {
00066     Q_UNUSED(modifiers);
00067 }
00068 
00069 QUndoCommand* KoPathPointMoveStrategy::createCommand()
00070 {
00071     m_tool->canvas()->updateCanvas(m_tool->canvas()->snapGuide()->boundingRect());
00072 
00073     KoPathToolSelection * selection = dynamic_cast<KoPathToolSelection*>(m_tool->selection());
00074     if (! selection)
00075         return 0;
00076 
00077     QUndoCommand *cmd = 0;
00078     if (!m_move.isNull()) {
00079         // as the point is already at the new position we need to undo the change
00080         KoPathPointMoveCommand revert(selection->selectedPointsData(), -m_move);
00081         revert.redo();
00082         cmd = new KoPathPointMoveCommand(selection->selectedPointsData(), m_move);
00083     }
00084     return cmd;
00085 }