| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * ActionSequenze.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Dec 17, 2009 | 
|---|
| 27 | *      Author: crueger | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include "Actions/ActionSequence.hpp" | 
|---|
| 38 | #include "Actions/Action.hpp" | 
|---|
| 39 | #include "UIElements/Dialog.hpp" | 
|---|
| 40 |  | 
|---|
| 41 | #include "CodePatterns/Assert.hpp" | 
|---|
| 42 |  | 
|---|
| 43 | using namespace MoleCuilder; | 
|---|
| 44 |  | 
|---|
| 45 | ActionSequence::ActionSequence() : | 
|---|
| 46 | loop(1) | 
|---|
| 47 | {} | 
|---|
| 48 |  | 
|---|
| 49 | ActionSequence::~ActionSequence() | 
|---|
| 50 | {} | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | void ActionSequence::addAction(Action* _action){ | 
|---|
| 54 | actions.push_back(_action); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | Action* ActionSequence::removeLastAction(){ | 
|---|
| 58 | if(actions.empty()) { | 
|---|
| 59 | return 0; | 
|---|
| 60 | } | 
|---|
| 61 | else { | 
|---|
| 62 | Action* theAction; | 
|---|
| 63 | theAction = actions.back(); | 
|---|
| 64 | actions.pop_back(); | 
|---|
| 65 | return theAction; | 
|---|
| 66 | } | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | // this method is used outside the ActionModule | 
|---|
| 70 | // Each action registers itself with the history | 
|---|
| 71 | Dialog* ActionSequence::fillAllDialogs(Dialog *dialog){ | 
|---|
| 72 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){ | 
|---|
| 73 | dialog = (*it)->fillDialog(dialog); | 
|---|
| 74 | } | 
|---|
| 75 | return dialog; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | // this method is used outside the ActionModule | 
|---|
| 79 | // Each action registers itself with the history | 
|---|
| 80 | void ActionSequence::callAll(){ | 
|---|
| 81 | for (int i=0;i<loop;++i) | 
|---|
| 82 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){ | 
|---|
| 83 | // we want to have a global bookkeeping for all actions in the sequence, so | 
|---|
| 84 | // we bypass the normal call | 
|---|
| 85 | (*it)->call(); | 
|---|
| 86 | } | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | // This method is used internally when MakroActions are constructed. | 
|---|
| 90 | // In this case only the makro Action should be registered and | 
|---|
| 91 | // handle the states | 
|---|
| 92 | ActionSequence::stateSet ActionSequence::callAll(bool){ | 
|---|
| 93 | stateSet states; | 
|---|
| 94 | for (int i=0;i<loop;++i) | 
|---|
| 95 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){ | 
|---|
| 96 | // we want to have a global bookkeeping for all actions in the sequence, so | 
|---|
| 97 | // we bypass the normal call | 
|---|
| 98 | Action::state_ptr state = (*it)->performCall(); | 
|---|
| 99 | states.push_back(state); | 
|---|
| 100 | } | 
|---|
| 101 | return states; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | ActionSequence::stateSet ActionSequence::undoAll(stateSet states){ | 
|---|
| 105 | ASSERT(canUndo(),"Trying to undo a sequence that contains methods that can't be undone"); | 
|---|
| 106 | stateSet res; | 
|---|
| 107 | actionSet::reverse_iterator actionRit = actions.rbegin(); | 
|---|
| 108 | stateSet::reverse_iterator stateRit = states.rbegin(); | 
|---|
| 109 | for(;actionRit!=actions.rend();++actionRit,++stateRit){ | 
|---|
| 110 | ASSERT(stateRit!=states.rend(),"End of states prematurely reached."); | 
|---|
| 111 | if((*actionRit)->shouldUndo()){ | 
|---|
| 112 | Action::state_ptr newState = (*actionRit)->performUndo(*stateRit); | 
|---|
| 113 | // The order of the states has to correspond to the order of the actions | 
|---|
| 114 | // this is why we have to add at the beginning | 
|---|
| 115 | res.push_front(newState); | 
|---|
| 116 | } | 
|---|
| 117 | else{ | 
|---|
| 118 | res.push_front(Action::success); | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|
| 121 | return res; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | ActionSequence::stateSet ActionSequence::redoAll(stateSet states){ | 
|---|
| 125 | stateSet res; | 
|---|
| 126 | actionSet::iterator actionIt = actions.begin(); | 
|---|
| 127 | stateSet::iterator stateIt = states.begin(); | 
|---|
| 128 | for(;actionIt!=actions.end();++actionIt,++stateIt){ | 
|---|
| 129 | ASSERT(stateIt!=states.end(),"End of states prematurely reached."); | 
|---|
| 130 | if((*actionIt)->shouldUndo()){ | 
|---|
| 131 | Action::state_ptr newState =(*actionIt)->performRedo(*stateIt); | 
|---|
| 132 | res.push_back(newState); | 
|---|
| 133 | } | 
|---|
| 134 | else{ | 
|---|
| 135 | res.push_back(Action::success); | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 | return res; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | bool ActionSequence::canUndo(){ | 
|---|
| 142 | bool canUndo=true; | 
|---|
| 143 | for(std::deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){ | 
|---|
| 144 | if((*it)->shouldUndo()){ | 
|---|
| 145 | canUndo &= (*it)->canUndo(); | 
|---|
| 146 | } | 
|---|
| 147 | } | 
|---|
| 148 | return canUndo; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | bool ActionSequence::shouldUndo(){ | 
|---|
| 152 | bool shouldUndo = false; | 
|---|
| 153 | for(std::deque<Action*>::iterator it=actions.begin();it!=actions.end();++it){ | 
|---|
| 154 | shouldUndo |= (*it)->shouldUndo(); | 
|---|
| 155 | } | 
|---|
| 156 | return shouldUndo; | 
|---|
| 157 | } | 
|---|