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
39bool
41{
42 return !undoStack->empty();
43}
44
45bool
47{
48 return !redoStack->empty();
49}
50
51std::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
71std::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
90void
92 const std::shared_ptr<std::vector<controller::OperationPtr>>& operations)
93{
95 undoStack->push_back(action);
96
97 if (canRedo())
98 {
99 redoStack.reset(new std::vector<UndoActionPtr>());
100 }
101}
102
103void
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
141void
143{
144 redoStack.reset(new std::vector<UndoActionPtr>());
145 undoStack.reset(new std::vector<UndoActionPtr>());
146}
auto newId
A container class to store multiple Operations and undo/redo them.
Definition UndoAction.h:43
std::shared_ptr< std::vector< OperationPtr > > redo()
Goes one step forward in the history and returns the Operations need to redo.
bool canUndo()
Returns the possibility to undo a Action.
UndoRedoStack()
A constructor.
void push(const std::shared_ptr< std::vector< OperationPtr > > &operations)
Adds a Action to the history.
bool canRedo()
Returns the possibility to redo a Action.
std::shared_ptr< std::vector< OperationPtr > > undo()
Goes one step back in the history and returns the Operations need to redo.
void clear()
Removes all actions from the history.
void updateObjectId(std::string oldId, std::string newId)
Swaps oldId and newId in all Operations saved in this class.
std::shared_ptr< UndoAction > UndoActionPtr