1 | /*
|
---|
2 | * ActionSequenze.cpp
|
---|
3 | *
|
---|
4 | * Created on: Dec 17, 2009
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "Helpers/MemDebug.hpp"
|
---|
14 |
|
---|
15 | #include "Actions/ActionSequence.hpp"
|
---|
16 | #include "Actions/Action.hpp"
|
---|
17 | #include "UIElements/Dialog.hpp"
|
---|
18 |
|
---|
19 | #include "Helpers/Assert.hpp"
|
---|
20 |
|
---|
21 | using namespace std;
|
---|
22 |
|
---|
23 | ActionSequence::ActionSequence()
|
---|
24 | {}
|
---|
25 |
|
---|
26 | ActionSequence::~ActionSequence()
|
---|
27 | {}
|
---|
28 |
|
---|
29 |
|
---|
30 | void ActionSequence::addAction(Action* _action){
|
---|
31 | actions.push_back(_action);
|
---|
32 | }
|
---|
33 |
|
---|
34 | Action* ActionSequence::removeLastAction(){
|
---|
35 | if(actions.empty()) {
|
---|
36 | return 0;
|
---|
37 | }
|
---|
38 | else {
|
---|
39 | Action* theAction;
|
---|
40 | theAction = actions.back();
|
---|
41 | actions.pop_back();
|
---|
42 | return theAction;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | // this method is used outside the ActionModule
|
---|
47 | // Each action registers itself with the history
|
---|
48 | Dialog* ActionSequence::fillAllDialogs(Dialog *dialog){
|
---|
49 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){
|
---|
50 | dialog = (*it)->fillDialog(dialog);
|
---|
51 | }
|
---|
52 | return dialog;
|
---|
53 | }
|
---|
54 |
|
---|
55 | // this method is used outside the ActionModule
|
---|
56 | // Each action registers itself with the history
|
---|
57 | void ActionSequence::callAll(){
|
---|
58 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){
|
---|
59 | // we want to have a global bookkeeping for all actions in the sequence, so
|
---|
60 | // we bypass the normal call
|
---|
61 | (*it)->call();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | // This method is used internally when MakroActions are constructed.
|
---|
66 | // In this case only the makro Action should be registered and
|
---|
67 | // handle the states
|
---|
68 | ActionSequence::stateSet ActionSequence::callAll(bool){
|
---|
69 | stateSet states;
|
---|
70 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){
|
---|
71 | // we want to have a global bookkeeping for all actions in the sequence, so
|
---|
72 | // we bypass the normal call
|
---|
73 | Action::state_ptr state = (*it)->performCall();
|
---|
74 | states.push_back(state);
|
---|
75 | }
|
---|
76 | return states;
|
---|
77 | }
|
---|
78 |
|
---|
79 | ActionSequence::stateSet ActionSequence::undoAll(stateSet states){
|
---|
80 | ASSERT(canUndo(),"Trying to undo a sequence that contains methods that can't be undone");
|
---|
81 | stateSet res;
|
---|
82 | actionSet::reverse_iterator actionRit = actions.rbegin();
|
---|
83 | stateSet::reverse_iterator stateRit = states.rbegin();
|
---|
84 | for(;actionRit!=actions.rend();++actionRit,++stateRit){
|
---|
85 | ASSERT(stateRit!=states.rend(),"End of states prematurely reached.");
|
---|
86 | if((*actionRit)->shouldUndo()){
|
---|
87 | Action::state_ptr newState = (*actionRit)->performUndo(*stateRit);
|
---|
88 | // The order of the states has to correspond to the order of the actions
|
---|
89 | // this is why we have to add at the beginning
|
---|
90 | res.push_front(newState);
|
---|
91 | }
|
---|
92 | else{
|
---|
93 | res.push_front(Action::success);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | return res;
|
---|
97 | }
|
---|
98 |
|
---|
99 | ActionSequence::stateSet ActionSequence::redoAll(stateSet states){
|
---|
100 | stateSet res;
|
---|
101 | actionSet::iterator actionIt = actions.begin();
|
---|
102 | stateSet::iterator stateIt = states.begin();
|
---|
103 | for(;actionIt!=actions.end();++actionIt,++stateIt){
|
---|
104 | ASSERT(stateIt!=states.end(),"End of states prematurely reached.");
|
---|
105 | if((*actionIt)->shouldUndo()){
|
---|
106 | Action::state_ptr newState =(*actionIt)->performRedo(*stateIt);
|
---|
107 | res.push_back(newState);
|
---|
108 | }
|
---|
109 | else{
|
---|
110 | res.push_back(Action::success);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | return res;
|
---|
114 | }
|
---|
115 |
|
---|
116 | bool ActionSequence::canUndo(){
|
---|
117 | bool canUndo=true;
|
---|
118 | for(deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){
|
---|
119 | if((*it)->shouldUndo()){
|
---|
120 | canUndo &= (*it)->canUndo();
|
---|
121 | }
|
---|
122 | }
|
---|
123 | return canUndo;
|
---|
124 | }
|
---|
125 |
|
---|
126 | bool ActionSequence::shouldUndo(){
|
---|
127 | bool shouldUndo = false;
|
---|
128 | for(deque<Action*>::iterator it=actions.begin();it!=actions.end();++it){
|
---|
129 | shouldUndo |= (*it)->shouldUndo();
|
---|
130 | }
|
---|
131 | return shouldUndo;
|
---|
132 | }
|
---|