[a5041ec] | 1 | /*
|
---|
| 2 | * ActionSequenze.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Dec 17, 2009
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[112b09] | 13 | #include "Helpers/MemDebug.hpp"
|
---|
| 14 |
|
---|
[a5041ec] | 15 | #include "Actions/ActionSequence.hpp"
|
---|
| 16 | #include "Actions/Action.hpp"
|
---|
[80951de] | 17 | #include "UIElements/Dialog.hpp"
|
---|
[a5041ec] | 18 |
|
---|
[67e2b3] | 19 | #include "Helpers/Assert.hpp"
|
---|
| 20 |
|
---|
[a5041ec] | 21 | using namespace std;
|
---|
| 22 |
|
---|
| 23 | ActionSequence::ActionSequence()
|
---|
[1fa107] | 24 | {}
|
---|
[a5041ec] | 25 |
|
---|
| 26 | ActionSequence::~ActionSequence()
|
---|
[1fa107] | 27 | {}
|
---|
[a5041ec] | 28 |
|
---|
| 29 |
|
---|
| 30 | void ActionSequence::addAction(Action* _action){
|
---|
| 31 | actions.push_back(_action);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | Action* ActionSequence::removeLastAction(){
|
---|
[1fa107] | 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 | }
|
---|
[a5041ec] | 44 | }
|
---|
| 45 |
|
---|
[80951de] | 46 | // this method is used outside the ActionModule
|
---|
| 47 | // Each action registers itself with the history
|
---|
[047878] | 48 | Dialog* ActionSequence::fillAllDialogs(Dialog *dialog){
|
---|
[80951de] | 49 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){
|
---|
[047878] | 50 | dialog = (*it)->fillDialog(dialog);
|
---|
[80951de] | 51 | }
|
---|
[047878] | 52 | return dialog;
|
---|
[80951de] | 53 | }
|
---|
| 54 |
|
---|
[2efa90] | 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){
|
---|
[67e2b3] | 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
|
---|
[5b0b98] | 73 | Action::state_ptr state = (*it)->performCall();
|
---|
[67e2b3] | 74 | states.push_back(state);
|
---|
| 75 | }
|
---|
| 76 | return states;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[5b0b98] | 79 | ActionSequence::stateSet ActionSequence::undoAll(stateSet states){
|
---|
[67e2b3] | 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()){
|
---|
[5b0b98] | 87 | Action::state_ptr newState = (*actionRit)->performUndo(*stateRit);
|
---|
[67e2b3] | 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;
|
---|
[a5041ec] | 97 | }
|
---|
| 98 |
|
---|
[5b0b98] | 99 | ActionSequence::stateSet ActionSequence::redoAll(stateSet states){
|
---|
[67e2b3] | 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()){
|
---|
[5b0b98] | 106 | Action::state_ptr newState =(*actionIt)->performRedo(*stateIt);
|
---|
[67e2b3] | 107 | res.push_back(newState);
|
---|
| 108 | }
|
---|
| 109 | else{
|
---|
| 110 | res.push_back(Action::success);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | return res;
|
---|
[a5041ec] | 114 | }
|
---|
| 115 |
|
---|
| 116 | bool ActionSequence::canUndo(){
|
---|
| 117 | bool canUndo=true;
|
---|
[67e2b3] | 118 | for(deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){
|
---|
| 119 | if((*it)->shouldUndo()){
|
---|
| 120 | canUndo &= (*it)->canUndo();
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
[a5041ec] | 123 | return canUndo;
|
---|
| 124 | }
|
---|
[67e2b3] | 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 | }
|
---|