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 
27 #include <qevent.h>
28 
29 // local
30 #include "Controller.h"
31 #include "UndoAction.h"
32 #include "UndoRedoStack.h"
33 
35  undoStack(new std::vector<UndoActionPtr>()), redoStack(new std::vector<UndoActionPtr>())
36 {
37 }
38 
39 bool
41 {
42  return !undoStack->empty();
43 }
44 
45 bool
47 {
48  return !redoStack->empty();
49 }
50 
51 std::shared_ptr<std::vector<controller::OperationPtr>>
53 {
54  std::shared_ptr<std::vector<controller::OperationPtr>> toUndo;
55 
56  if (canUndo())
57  {
58  toUndo = undoStack->back()->undo();
59  redoStack->push_back(undoStack->back());
60  undoStack->pop_back();
61  return toUndo;
62  }
63  else
64  {
65  toUndo.reset(new std::vector<controller::OperationPtr>());
66  }
67 
68  return toUndo;
69 }
70 
71 std::shared_ptr<std::vector<controller::OperationPtr>>
73 {
74  std::shared_ptr<std::vector<controller::OperationPtr>> toRedo;
75 
76  if (canRedo())
77  {
78  toRedo = redoStack->back()->redo();
79  undoStack->push_back(redoStack->back());
80  redoStack->pop_back();
81  }
82  else
83  {
84  toRedo.reset(new std::vector<controller::OperationPtr>());
85  }
86 
87  return toRedo;
88 }
89 
90 void
92  const std::shared_ptr<std::vector<controller::OperationPtr>>& operations)
93 {
94  controller::UndoActionPtr action(new controller::UndoAction(operations));
95  undoStack->push_back(action);
96 
97  if (canRedo())
98  {
99  redoStack.reset(new std::vector<UndoActionPtr>());
100  }
101 }
102 
103 void
104 controller::UndoRedoStack::updateObjectId(std::string oldId, std::string newId)
105 {
106  for (auto it = undoStack->begin(); it != undoStack->end(); it++)
107  {
108  for (auto ito = it->get()->getOperations()->begin();
109  ito != it->get()->getOperations()->end();
110  ito++)
111  {
112  if (ito->get()->getObjectId() == oldId)
113  {
114  ito->get()->setObjectId(newId);
115  }
116  else if (ito->get()->getObjectId() == newId)
117  {
118  ito->get()->setObjectId(oldId);
119  }
120  }
121  }
122 
123  for (auto it = redoStack->begin(); it != redoStack->end(); it++)
124  {
125  for (auto ito = it->get()->getOperations()->begin();
126  ito != it->get()->getOperations()->end();
127  ito++)
128  {
129  if (ito->get()->getObjectId() == oldId)
130  {
131  ito->get()->setObjectId(newId);
132  }
133  else if (ito->get()->getObjectId() == newId)
134  {
135  ito->get()->setObjectId(oldId);
136  }
137  }
138  }
139 }
140 
141 void
143 {
144  redoStack.reset(new std::vector<UndoActionPtr>());
145  undoStack.reset(new std::vector<UndoActionPtr>());
146 }
controller::UndoRedoStack::canUndo
bool canUndo()
Returns the possibility to undo a Action.
Definition: UndoRedoStack.cpp:40
controller::UndoRedoStack::UndoRedoStack
UndoRedoStack()
A constructor.
Definition: UndoRedoStack.cpp:34
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:52
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:104
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:72
controller::UndoRedoStack::push
void push(const std::shared_ptr< std::vector< OperationPtr >> &operations)
Adds a Action to the history.
Definition: UndoRedoStack.cpp:91
controller::UndoRedoStack::canRedo
bool canRedo()
Returns the possibility to redo a Action.
Definition: UndoRedoStack.cpp:46
Controller.h
newId
auto newId
Definition: GraspingManager.cpp:76
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:142
std
Definition: Application.h:66
UndoAction.h