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