UndoRedoStack.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 // std
25 #include <exception>
26 #include <qevent.h>
27 
28 // local
29 #include "UndoRedoStack.h"
30 #include "UndoAction.h"
31 #include "Controller.h"
32 
34  undoStack(new std::vector<UndoActionPtr>()),
35  redoStack(new std::vector<UndoActionPtr>())
36 {
37 }
38 
40 {
41  return !undoStack->empty();
42 }
43 
45 {
46  return !redoStack->empty();
47 }
48 
49 std::shared_ptr<std::vector<controller::OperationPtr> > controller::UndoRedoStack::undo()
50 {
51  std::shared_ptr<std::vector<controller::OperationPtr> > toUndo;
52 
53  if (canUndo())
54  {
55  toUndo = undoStack->back()->undo();
56  redoStack->push_back(undoStack->back());
57  undoStack->pop_back();
58  return toUndo;
59  }
60  else
61  {
62  toUndo.reset(new std::vector<controller::OperationPtr>());
63  }
64 
65  return toUndo;
66 }
67 
68 std::shared_ptr<std::vector<controller::OperationPtr> > controller::UndoRedoStack::redo()
69 {
70  std::shared_ptr<std::vector<controller::OperationPtr> > toRedo;
71 
72  if (canRedo())
73  {
74  toRedo = redoStack->back()->redo();
75  undoStack->push_back(redoStack->back());
76  redoStack->pop_back();
77  }
78  else
79  {
80  toRedo.reset(new std::vector<controller::OperationPtr>());
81  }
82 
83  return toRedo;
84 }
85 
86 void controller::UndoRedoStack::push(const std::shared_ptr<std::vector<controller::OperationPtr> >& operations)
87 {
88  controller::UndoActionPtr action(new controller::UndoAction(operations));
89  undoStack->push_back(action);
90 
91  if (canRedo())
92  {
93  redoStack.reset(new std::vector<UndoActionPtr>());
94  }
95 }
96 
97 void controller::UndoRedoStack::updateObjectId(std::string oldId, std::string newId)
98 {
99  for (auto it = undoStack->begin(); it != undoStack->end(); it++)
100  {
101  for (auto ito = it->get()->getOperations()->begin(); ito != it->get()->getOperations()->end(); ito++)
102  {
103  if (ito->get()->getObjectId() == oldId)
104  {
105  ito->get()->setObjectId(newId);
106  }
107  else if (ito->get()->getObjectId() == newId)
108  {
109  ito->get()->setObjectId(oldId);
110  }
111  }
112  }
113 
114  for (auto it = redoStack->begin(); it != redoStack->end(); it++)
115  {
116  for (auto ito = it->get()->getOperations()->begin(); ito != it->get()->getOperations()->end(); ito++)
117  {
118  if (ito->get()->getObjectId() == oldId)
119  {
120  ito->get()->setObjectId(newId);
121  }
122  else if (ito->get()->getObjectId() == newId)
123  {
124  ito->get()->setObjectId(oldId);
125  }
126  }
127  }
128 }
129 
131 {
132  redoStack.reset(new std::vector<UndoActionPtr>());
133  undoStack.reset(new std::vector<UndoActionPtr>());
134 }
135 
controller::UndoRedoStack::canUndo
bool canUndo()
Returns the possibility to undo a Action.
Definition: UndoRedoStack.cpp:39
controller::UndoRedoStack::UndoRedoStack
UndoRedoStack()
A constructor.
Definition: UndoRedoStack.cpp:33
controller::UndoRedoStack::undo
std::shared_ptr< std::vector< OperationPtr > > undo()
Goes one step back in the history and returns the Operations need to redo.
Definition: UndoRedoStack.cpp:49
controller::UndoRedoStack::updateObjectId
void updateObjectId(std::string oldId, std::string newId)
Swaps oldId and newId in all Operations saved in this class.
Definition: UndoRedoStack.cpp:97
controller::UndoActionPtr
std::shared_ptr< UndoAction > UndoActionPtr
Definition: ClassDefinitions.h:70
UndoRedoStack.h
controller::UndoRedoStack::redo
std::shared_ptr< std::vector< OperationPtr > > redo()
Goes one step forward in the history and returns the Operations need to redo.
Definition: UndoRedoStack.cpp:68
controller::UndoRedoStack::push
void push(const std::shared_ptr< std::vector< OperationPtr > > &operations)
Adds a Action to the history.
Definition: UndoRedoStack.cpp:86
controller::UndoRedoStack::canRedo
bool canRedo()
Returns the possibility to redo a Action.
Definition: UndoRedoStack.cpp:44
Controller.h
newId
auto newId
Definition: GraspingManager.cpp:90
controller::UndoAction
A container class to store multiple Operations and undo/redo them.
Definition: UndoAction.h:42
controller::UndoRedoStack::clear
void clear()
Removes all actions from the history.
Definition: UndoRedoStack.cpp:130
std
Definition: Application.h:66
UndoAction.h