From 078a27f8ceca3f168ab52daae140044ebc06b239 Mon Sep 17 00:00:00 2001 From: baydam Date: Mon, 19 Jan 2015 14:35:19 +0000 Subject: [PATCH] Undo Command to restore position of vertices moved by mouse or arrow key --- Commands.cpp | 26 ++++++++++++++++++++++++++ Commands.h | 19 +++++++++++++++++++ MainWindow.h | 3 +++ MapperGLCanvas.cpp | 11 +++++------ 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/Commands.cpp b/Commands.cpp index 89ca53d..5f09aa9 100644 --- a/Commands.cpp +++ b/Commands.cpp @@ -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()); +} diff --git a/Commands.h b/Commands.h index bec7ebd..1dbeb04 100644 --- a/Commands.h +++ b/Commands.h @@ -24,6 +24,7 @@ #include #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_ */ diff --git a/MainWindow.h b/MainWindow.h index e2b7ee7..07b6f76 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -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); diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 72346e9..fa0ac69 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -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.