1 /* This file is part of the KDE project
2 *
3 * Copyright (C) 2011 Jan Hambrecht <jaham@gmx.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "MoveConnectionPointStrategy.h"
22 #include "ChangeConnectionPointCommand.h"
23 #include <KoShape.h>
24 #include <KoToolBase.h>
25 #include <KoCanvasBase.h>
26
MoveConnectionPointStrategy(KoShape * shape,int connectionPointId,KoToolBase * parent)27 MoveConnectionPointStrategy::MoveConnectionPointStrategy(KoShape *shape, int connectionPointId, KoToolBase* parent)
28 : KoInteractionStrategy(parent), m_shape(shape), m_connectionPointId(connectionPointId)
29 {
30 Q_ASSERT(m_shape);
31 m_oldPoint = m_newPoint = m_shape->connectionPoint(m_connectionPointId);
32 }
33
~MoveConnectionPointStrategy()34 MoveConnectionPointStrategy::~MoveConnectionPointStrategy()
35 {
36 }
37
paint(QPainter & painter,const KoViewConverter & converter)38 void MoveConnectionPointStrategy::paint(QPainter& painter, const KoViewConverter& converter)
39 {
40 KoInteractionStrategy::paint(painter, converter);
41 }
42
handleMouseMove(const QPointF & mouseLocation,Qt::KeyboardModifiers)43 void MoveConnectionPointStrategy::handleMouseMove(const QPointF& mouseLocation, Qt::KeyboardModifiers /*modifiers*/)
44 {
45 m_newPoint.position = m_shape->documentToShape(mouseLocation);
46 m_shape->setConnectionPoint(m_connectionPointId, m_newPoint);
47 }
48
cancelInteraction()49 void MoveConnectionPointStrategy::cancelInteraction()
50 {
51 KoInteractionStrategy::cancelInteraction();
52 m_shape->setConnectionPoint(m_connectionPointId, m_oldPoint);
53 }
54
finishInteraction(Qt::KeyboardModifiers)55 void MoveConnectionPointStrategy::finishInteraction(Qt::KeyboardModifiers /*modifiers*/)
56 {
57 }
58
createCommand()59 KUndo2Command* MoveConnectionPointStrategy::createCommand()
60 {
61 int grabDistance = grabSensitivity();
62 const qreal dx = m_newPoint.position.x()-m_oldPoint.position.x();
63 const qreal dy = m_newPoint.position.y()-m_oldPoint.position.y();
64 // check if we have moved the connection point at least a little bit
65 if(dx*dx+dy*dy < grabDistance*grabDistance)
66 return 0;
67
68 return new ChangeConnectionPointCommand(m_shape, m_connectionPointId, m_oldPoint, m_newPoint);
69 }
70