Undo Command to restore position of vertices moved by mouse or arrow key

This commit is contained in:
baydam
2015-01-19 14:35:19 +00:00
parent 146a7e4a43
commit 078a27f8ce
4 changed files with 53 additions and 6 deletions
+26
View File
@@ -22,3 +22,29 @@ void AddShapesCommand::redo()
m_mainWindow->addMappingItem(currentId);
}
}
MoveVertexCommand::MoveVertexCommand(MapperGLCanvas *mapperGLCanvas, Shape *shape, int activeVertex, const QPointF &point, QUndoCommand *parent) :
QUndoCommand(parent)
{
m_mapperGLCanvas = mapperGLCanvas;
m_shape = shape;
m_activeVertex = activeVertex;
newPosition = point;
oldPosition = m_shape->getVertex(m_activeVertex);
}
void MoveVertexCommand::undo()
{
m_shape->setVertex(m_activeVertex, oldPosition);
m_mapperGLCanvas->update();
emit m_mapperGLCanvas->shapeChanged(m_mapperGLCanvas->getCurrentShape());
}
void MoveVertexCommand::redo()
{
m_shape->setVertex(m_activeVertex, newPosition);
m_mapperGLCanvas->update();
emit m_mapperGLCanvas->shapeChanged(m_mapperGLCanvas->getCurrentShape());
}
+19
View File
@@ -24,6 +24,7 @@
#include <QUndoCommand>
#include "MainWindow.h"
#include "MapperGLCanvas.h"
class AddShapesCommand : public QUndoCommand
{
@@ -39,4 +40,22 @@ private:
};
class MoveVertexCommand : public QUndoCommand
{
public:
MoveVertexCommand(MapperGLCanvas *mapperGLCanvas, Shape *shape, int activeVertex, const QPointF &point, QUndoCommand *parent = 0);
void undo();
void redo();
signals:
void shapeChanged(Shape*);
private:
MapperGLCanvas *m_mapperGLCanvas;
Shape *m_shape;
int m_activeVertex;
QPointF newPosition, oldPosition;
};
#endif /* COMMANDS_H_ */
+3
View File
@@ -374,6 +374,9 @@ public:
void removeCurrentPaint();
void removeCurrentMapping();
// Use the same undoStack for whole program
QUndoStack* getUndoStack() { return undoStack; }
void startFullScreen();
bool setOscPort(QString portNumber);
bool setOscPort(int portNumber);
+5 -6
View File
@@ -21,6 +21,7 @@
#include "MapperGLCanvas.h"
#include "MainWindow.h"
#include "Commands.h"
MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QGLWidget * shareWidget)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent, shareWidget),
@@ -202,10 +203,9 @@ void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event)
// Stick to vertices.
if (stickyVertices())
glueVertex(shape, &p);
shape->setVertex(_activeVertex, p);
update();
emit shapeChanged(getCurrentShape());
// Enable to Undo and Redo when mouse move the position of vertices
getMainWindow()->getUndoStack()->push(new MoveVertexCommand(this, shape, _activeVertex, p));
}
}
else if (_shapeGrabbed)
@@ -268,9 +268,8 @@ void MapperGLCanvas::keyPressEvent(QKeyEvent* event)
break;
}
// TODO: this will always be called even if no arrow key has been pressed (small performance issue).
shape->setVertex(_activeVertex, p);
update();
emit shapeChanged(getCurrentShape());
// Enable to Undo and Redo when arrow keys move the position of vertices
getMainWindow()->getUndoStack()->push(new MoveVertexCommand(this, shape, _activeVertex, p));
}
// Defer unhandled keys to parent.