Commit 6fbdf221 authored by Jan Hambrecht's avatar Jan Hambrecht

added interaction strategy and undo command for moving connection points

parent 8f84005f
......@@ -23,6 +23,7 @@
#include "ConnectionTool.h"
#include "AddConnectionPointCommand.h"
#include "RemoveConnectionPointCommand.h"
#include "MoveConnectionPointStrategy.h"
#include <KoCanvasBase.h>
#include <KoPointerEvent.h>
......@@ -42,8 +43,6 @@
#include <QPointF>
#include <QKeyEvent>
/* TODO: use commands for editing */
ConnectionTool::ConnectionTool(KoCanvasBase * canvas)
: KoPathTool(canvas)
, m_editMode(Idle)
......@@ -136,7 +135,7 @@ void ConnectionTool::repaintDecorations()
void ConnectionTool::mousePressEvent(KoPointerEvent * event)
{
if (m_editMode == Idle) {
// pressing aon a shape in idle mode switches to connection point edit mode
// pressing on a shape in idle mode switches to connection point edit mode
if (m_currentShape && !dynamic_cast<KoConnectionShape*>(m_currentShape)) {
m_editMode = EditConnectionPoint;
m_activeHandle = -1;
......@@ -189,6 +188,10 @@ void ConnectionTool::mousePressEvent(KoPointerEvent * event)
repaintDecorations();
resetEditMode();
}
} else {
if (m_activeHandle >= KoFlake::FirstCustomConnectionPoint) {
m_currentStrategy = new MoveConnectionPointStrategy(m_currentShape, m_activeHandle, this);
}
}
}
}
......@@ -197,7 +200,12 @@ void ConnectionTool::mouseMoveEvent(KoPointerEvent *event)
{
if (m_currentStrategy) {
repaintDecorations();
m_currentStrategy->handleMouseMove(event->point, event->modifiers());
if (m_editMode != EditConnection && m_editMode != CreateConnection) {
QPointF snappedPos = canvas()->snapGuide()->snap(event->point, event->modifiers());
m_currentStrategy->handleMouseMove(snappedPos, event->modifiers());
} else {
m_currentStrategy->handleMouseMove(event->point, event->modifiers());
}
repaintDecorations();
return;
}
......@@ -206,20 +214,11 @@ void ConnectionTool::mouseMoveEvent(KoPointerEvent *event)
if (m_editMode == EditConnectionPoint) {
Q_ASSERT(m_currentShape);
if (event->buttons() & Qt::LeftButton && m_activeHandle >= KoFlake::FirstCustomConnectionPoint) {
QPointF mousePos = canvas()->snapGuide()->snap(event->point, event->modifiers());
QPointF cp = m_currentShape->documentToShape(mousePos);
repaintDecorations();
// TODO: use undo command
m_currentShape->setConnectionPointPosition(m_activeHandle, cp);
// check if we should highlight another connection point
int handleId = handleAtPoint(m_currentShape, event->point);
if(handleId != m_activeHandle) {
m_activeHandle = handleId;
repaintDecorations();
} else {
// check if we should highlight another connection point
int handleId = handleAtPoint(m_currentShape, event->point);
if(handleId != m_activeHandle) {
m_activeHandle = handleId;
repaintDecorations();
}
}
} else {
resetEditMode();
......@@ -284,7 +283,8 @@ void ConnectionTool::mouseReleaseEvent(KoPointerEvent *event)
canvas()->addCommand(command);
delete m_currentStrategy;
m_currentStrategy = 0;
resetEditMode();
if (m_editMode == EditConnection || m_editMode == CreateConnection)
resetEditMode();
}
updateStatusText();
}
......
/* This file is part of the KDE project
*
* Copyright (C) 2011 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "MoveConnectionPointCommand.h"
#include <KoShape.h>
MoveConnectionPointCommand::MoveConnectionPointCommand(KoShape* shape, int connectionPointId, const QPointF &oldPosition, const QPointF &newPosition, QUndoCommand* parent)
: QUndoCommand(parent), m_shape(shape), m_connectionPointId(connectionPointId)
, m_oldPosition(oldPosition), m_newPosition(newPosition)
{
Q_ASSERT(m_shape);
}
MoveConnectionPointCommand::~MoveConnectionPointCommand()
{
}
void MoveConnectionPointCommand::redo()
{
updateRoi(m_oldPosition);
m_shape->setConnectionPointPosition(m_connectionPointId, m_newPosition);
updateRoi(m_newPosition);
QUndoCommand::redo();
}
void MoveConnectionPointCommand::undo()
{
QUndoCommand::undo();
updateRoi(m_oldPosition);
m_shape->setConnectionPointPosition(m_connectionPointId, m_oldPosition);
updateRoi(m_newPosition);
}
void MoveConnectionPointCommand::updateRoi(const QPointF &position)
{
// TODO: is there a way we can get at the correct update size?
QRectF roi(0, 0, 10, 10);
roi.moveCenter(position);
m_shape->update(roi);
}
/* This file is part of the KDE project
*
* Copyright (C) 2011 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef MOVECONNECTIONPOINTCOMMAND_H
#define MOVECONNECTIONPOINTCOMMAND_H
#include <QtGui/QUndoCommand>
#include <QtCore/QPointF>
class KoShape;
class MoveConnectionPointCommand : public QUndoCommand
{
public:
/// Creates new comand to move connection point of shape
MoveConnectionPointCommand(KoShape *shape, int connectionPointId, const QPointF &oldPosition, const QPointF &newPosition, QUndoCommand *parent = 0);
virtual ~MoveConnectionPointCommand();
/// reimplemented from QUndoCommand
virtual void redo();
/// reimplemented from QUndoCommand
virtual void undo();
private:
void updateRoi(const QPointF &position);
KoShape * m_shape;
int m_connectionPointId;
QPointF m_oldPosition;
QPointF m_newPosition;
};
#endif // MOVECONNECTIONPOINTCOMMAND_H
\ No newline at end of file
/* This file is part of the KDE project
*
* Copyright (C) 2011 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "MoveConnectionPointStrategy.h"
#include "MoveConnectionPointCommand.h"
#include <KoShape.h>
#include <KoToolBase.h>
#include <KoCanvasBase.h>
#include <KoResourceManager.h>
MoveConnectionPointStrategy::MoveConnectionPointStrategy(KoShape *shape, int connectionPointId, KoToolBase* parent)
: KoInteractionStrategy(parent), m_shape(shape), m_connectionPointId(connectionPointId)
{
Q_ASSERT(m_shape);
m_oldPosition = m_newPosition = m_shape->connectionPoint(m_connectionPointId);
}
MoveConnectionPointStrategy::~MoveConnectionPointStrategy()
{
}
void MoveConnectionPointStrategy::paint(QPainter& painter, const KoViewConverter& converter)
{
KoInteractionStrategy::paint(painter, converter);
}
void MoveConnectionPointStrategy::handleMouseMove(const QPointF& mouseLocation, Qt::KeyboardModifiers /*modifiers*/)
{
m_newPosition = m_shape->documentToShape(mouseLocation);
m_shape->setConnectionPointPosition(m_connectionPointId, m_newPosition);
}
void MoveConnectionPointStrategy::cancelInteraction()
{
KoInteractionStrategy::cancelInteraction();
m_shape->setConnectionPointPosition(m_connectionPointId, m_oldPosition);
}
void MoveConnectionPointStrategy::finishInteraction(Qt::KeyboardModifiers /*modifiers*/)
{
}
QUndoCommand* MoveConnectionPointStrategy::createCommand()
{
int grabDistance = tool()->canvas()->resourceManager()->grabSensitivity();
const qreal dx = m_newPosition.x()-m_oldPosition.x();
const qreal dy = m_newPosition.y()-m_oldPosition.y();
// check if we have moved the connection point at least a little bit
if(dx*dx+dy*dy < grabDistance*grabDistance)
return 0;
return new MoveConnectionPointCommand(m_shape, m_connectionPointId, m_oldPosition, m_newPosition);
}
/* This file is part of the KDE project
*
* Copyright (C) 2011 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef MOVECONNECTIONPOINTSTRATEGY_H
#define MOVECONNECTIONPOINTSTRATEGY_H
#include <KoInteractionStrategy.h>
#include <QtCore/QPointF>
class KoShape;
class MoveConnectionPointStrategy : public KoInteractionStrategy
{
public:
/// Constructor
MoveConnectionPointStrategy(KoShape *shape, int connectionPointId, KoToolBase *parent);
/// Destructor
virtual ~MoveConnectionPointStrategy();
/// reimplemented from KoInteractionStrategy
virtual void paint(QPainter &painter, const KoViewConverter &converter);
/// reimplemented from KoInteractionStrategy
virtual void handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers);
/// reimplemented from KoInteractionStrategy
virtual QUndoCommand *createCommand();
/// reimplemented from KoInteractionStrategy
virtual void cancelInteraction();
/// reimplemented from KoInteractionStrategy
virtual void finishInteraction(Qt::KeyboardModifiers modifiers);
private:
KoShape *m_shape;
int m_connectionPointId;
QPointF m_oldPosition;
QPointF m_newPosition;
};
#endif // MOVECONNECTIONPOINTSTRATEGY_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment