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