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