| 1 | /*
 | 
|---|
| 2 |  * ActionSequenze.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Dec 17, 2009
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Actions/ActionSequence.hpp"
 | 
|---|
| 11 | #include "Actions/Action.hpp"
 | 
|---|
| 12 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #include "Helpers/Assert.hpp"
 | 
|---|
| 15 | 
 | 
|---|
| 16 | using namespace std;
 | 
|---|
| 17 | 
 | 
|---|
| 18 | ActionSequence::ActionSequence()
 | 
|---|
| 19 | {}
 | 
|---|
| 20 | 
 | 
|---|
| 21 | ActionSequence::~ActionSequence()
 | 
|---|
| 22 | {}
 | 
|---|
| 23 | 
 | 
|---|
| 24 | 
 | 
|---|
| 25 | void ActionSequence::addAction(Action* _action){
 | 
|---|
| 26 |   actions.push_back(_action);
 | 
|---|
| 27 | }
 | 
|---|
| 28 | 
 | 
|---|
| 29 | Action* ActionSequence::removeLastAction(){
 | 
|---|
| 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 |   }
 | 
|---|
| 39 | }
 | 
|---|
| 40 | 
 | 
|---|
| 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
 | 
|---|
| 47 |     Dialog * dialog = (*it)->createDialog();
 | 
|---|
| 48 |     if (dialog != NULL) {
 | 
|---|
| 49 |       dialog->display();
 | 
|---|
| 50 |       delete(dialog);
 | 
|---|
| 51 |     }
 | 
|---|
| 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 | 
 | 
|---|
| 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){
 | 
|---|
| 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
 | 
|---|
| 86 |     Action::state_ptr state = (*it)->performCall();
 | 
|---|
| 87 |     states.push_back(state);
 | 
|---|
| 88 |   }
 | 
|---|
| 89 |   return states;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | ActionSequence::stateSet ActionSequence::undoAll(stateSet states){
 | 
|---|
| 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()){
 | 
|---|
| 100 |       Action::state_ptr newState = (*actionRit)->performUndo(*stateRit);
 | 
|---|
| 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;
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | ActionSequence::stateSet ActionSequence::redoAll(stateSet states){
 | 
|---|
| 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()){
 | 
|---|
| 119 |       Action::state_ptr newState =(*actionIt)->performRedo(*stateIt);
 | 
|---|
| 120 |       res.push_back(newState);
 | 
|---|
| 121 |     }
 | 
|---|
| 122 |     else{
 | 
|---|
| 123 |       res.push_back(Action::success);
 | 
|---|
| 124 |     }
 | 
|---|
| 125 |   }
 | 
|---|
| 126 |   return res;
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | bool ActionSequence::canUndo(){
 | 
|---|
| 130 |   bool canUndo=true;
 | 
|---|
| 131 |   for(deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){
 | 
|---|
| 132 |     if((*it)->shouldUndo()){
 | 
|---|
| 133 |       canUndo &= (*it)->canUndo();
 | 
|---|
| 134 |     }
 | 
|---|
| 135 |   }
 | 
|---|
| 136 |   return canUndo;
 | 
|---|
| 137 | }
 | 
|---|
| 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 | }
 | 
|---|