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 | {}
|
---|
47 |
|
---|
48 | ActionSequence::~ActionSequence()
|
---|
49 | {}
|
---|
50 |
|
---|
51 |
|
---|
52 | void ActionSequence::addAction(Action* _action){
|
---|
53 | actions.push_back(_action);
|
---|
54 | }
|
---|
55 |
|
---|
56 | Action* ActionSequence::removeLastAction(){
|
---|
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 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | // this method is used outside the ActionModule
|
---|
69 | // Each action registers itself with the history
|
---|
70 | Dialog* ActionSequence::fillAllDialogs(Dialog *dialog){
|
---|
71 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){
|
---|
72 | dialog = (*it)->fillDialog(dialog);
|
---|
73 | }
|
---|
74 | return dialog;
|
---|
75 | }
|
---|
76 |
|
---|
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){
|
---|
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
|
---|
95 | Action::state_ptr state = (*it)->performCall();
|
---|
96 | states.push_back(state);
|
---|
97 | }
|
---|
98 | return states;
|
---|
99 | }
|
---|
100 |
|
---|
101 | ActionSequence::stateSet ActionSequence::undoAll(stateSet states){
|
---|
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()){
|
---|
109 | Action::state_ptr newState = (*actionRit)->performUndo(*stateRit);
|
---|
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;
|
---|
119 | }
|
---|
120 |
|
---|
121 | ActionSequence::stateSet ActionSequence::redoAll(stateSet states){
|
---|
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()){
|
---|
128 | Action::state_ptr newState =(*actionIt)->performRedo(*stateIt);
|
---|
129 | res.push_back(newState);
|
---|
130 | }
|
---|
131 | else{
|
---|
132 | res.push_back(Action::success);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | return res;
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool ActionSequence::canUndo(){
|
---|
139 | bool canUndo=true;
|
---|
140 | for(std::deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){
|
---|
141 | if((*it)->shouldUndo()){
|
---|
142 | canUndo &= (*it)->canUndo();
|
---|
143 | }
|
---|
144 | }
|
---|
145 | return canUndo;
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool ActionSequence::shouldUndo(){
|
---|
149 | bool shouldUndo = false;
|
---|
150 | for(std::deque<Action*>::iterator it=actions.begin();it!=actions.end();++it){
|
---|
151 | shouldUndo |= (*it)->shouldUndo();
|
---|
152 | }
|
---|
153 | return shouldUndo;
|
---|
154 | }
|
---|