[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 |
|
---|
[9c1324] | 45 | ActionSequence::ActionSequence() :
|
---|
| 46 | loop(1)
|
---|
[1fa107] | 47 | {}
|
---|
[a5041ec] | 48 |
|
---|
| 49 | ActionSequence::~ActionSequence()
|
---|
[1fa107] | 50 | {}
|
---|
[a5041ec] | 51 |
|
---|
| 52 |
|
---|
| 53 | void ActionSequence::addAction(Action* _action){
|
---|
| 54 | actions.push_back(_action);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | Action* ActionSequence::removeLastAction(){
|
---|
[1fa107] | 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 | }
|
---|
[a5041ec] | 67 | }
|
---|
| 68 |
|
---|
[80951de] | 69 | // this method is used outside the ActionModule
|
---|
| 70 | // Each action registers itself with the history
|
---|
[047878] | 71 | Dialog* ActionSequence::fillAllDialogs(Dialog *dialog){
|
---|
[80951de] | 72 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){
|
---|
[047878] | 73 | dialog = (*it)->fillDialog(dialog);
|
---|
[80951de] | 74 | }
|
---|
[047878] | 75 | return dialog;
|
---|
[80951de] | 76 | }
|
---|
| 77 |
|
---|
[2efa90] | 78 | // this method is used outside the ActionModule
|
---|
| 79 | // Each action registers itself with the history
|
---|
| 80 | void ActionSequence::callAll(){
|
---|
[9c1324] | 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 | }
|
---|
[2efa90] | 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){
|
---|
[67e2b3] | 93 | stateSet states;
|
---|
[9c1324] | 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
|
---|
[b5b01e] | 98 | ActionState::ptr state = (*it)->performCall();
|
---|
[9c1324] | 99 | states.push_back(state);
|
---|
| 100 | }
|
---|
[67e2b3] | 101 | return states;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[5b0b98] | 104 | ActionSequence::stateSet ActionSequence::undoAll(stateSet states){
|
---|
[67e2b3] | 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()){
|
---|
[b5b01e] | 112 | ActionState::ptr newState = (*actionRit)->performUndo(*stateRit);
|
---|
[67e2b3] | 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;
|
---|
[a5041ec] | 122 | }
|
---|
| 123 |
|
---|
[5b0b98] | 124 | ActionSequence::stateSet ActionSequence::redoAll(stateSet states){
|
---|
[67e2b3] | 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()){
|
---|
[b5b01e] | 131 | ActionState::ptr newState =(*actionIt)->performRedo(*stateIt);
|
---|
[67e2b3] | 132 | res.push_back(newState);
|
---|
| 133 | }
|
---|
| 134 | else{
|
---|
| 135 | res.push_back(Action::success);
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | return res;
|
---|
[a5041ec] | 139 | }
|
---|
| 140 |
|
---|
| 141 | bool ActionSequence::canUndo(){
|
---|
| 142 | bool canUndo=true;
|
---|
[955b91] | 143 | for(std::deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){
|
---|
[67e2b3] | 144 | if((*it)->shouldUndo()){
|
---|
| 145 | canUndo &= (*it)->canUndo();
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
[a5041ec] | 148 | return canUndo;
|
---|
| 149 | }
|
---|
[67e2b3] | 150 |
|
---|
| 151 | bool ActionSequence::shouldUndo(){
|
---|
| 152 | bool shouldUndo = false;
|
---|
[955b91] | 153 | for(std::deque<Action*>::iterator it=actions.begin();it!=actions.end();++it){
|
---|
[67e2b3] | 154 | shouldUndo |= (*it)->shouldUndo();
|
---|
| 155 | }
|
---|
| 156 | return shouldUndo;
|
---|
| 157 | }
|
---|