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 |
|
---|
8 | /*
|
---|
9 | * ActionSequenze.cpp
|
---|
10 | *
|
---|
11 | * Created on: Dec 17, 2009
|
---|
12 | * Author: crueger
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "Helpers/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "Actions/ActionSequence.hpp"
|
---|
23 | #include "Actions/Action.hpp"
|
---|
24 | #include "UIElements/Dialog.hpp"
|
---|
25 |
|
---|
26 | #include "Helpers/Assert.hpp"
|
---|
27 |
|
---|
28 | using namespace std;
|
---|
29 |
|
---|
30 | ActionSequence::ActionSequence()
|
---|
31 | {}
|
---|
32 |
|
---|
33 | ActionSequence::~ActionSequence()
|
---|
34 | {}
|
---|
35 |
|
---|
36 |
|
---|
37 | void ActionSequence::addAction(Action* _action){
|
---|
38 | actions.push_back(_action);
|
---|
39 | }
|
---|
40 |
|
---|
41 | Action* ActionSequence::removeLastAction(){
|
---|
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 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | // this method is used outside the ActionModule
|
---|
54 | // Each action registers itself with the history
|
---|
55 | Dialog* ActionSequence::fillAllDialogs(Dialog *dialog){
|
---|
56 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){
|
---|
57 | dialog = (*it)->fillDialog(dialog);
|
---|
58 | }
|
---|
59 | return dialog;
|
---|
60 | }
|
---|
61 |
|
---|
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){
|
---|
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
|
---|
80 | Action::state_ptr state = (*it)->performCall();
|
---|
81 | states.push_back(state);
|
---|
82 | }
|
---|
83 | return states;
|
---|
84 | }
|
---|
85 |
|
---|
86 | ActionSequence::stateSet ActionSequence::undoAll(stateSet states){
|
---|
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()){
|
---|
94 | Action::state_ptr newState = (*actionRit)->performUndo(*stateRit);
|
---|
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;
|
---|
104 | }
|
---|
105 |
|
---|
106 | ActionSequence::stateSet ActionSequence::redoAll(stateSet states){
|
---|
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()){
|
---|
113 | Action::state_ptr newState =(*actionIt)->performRedo(*stateIt);
|
---|
114 | res.push_back(newState);
|
---|
115 | }
|
---|
116 | else{
|
---|
117 | res.push_back(Action::success);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | return res;
|
---|
121 | }
|
---|
122 |
|
---|
123 | bool ActionSequence::canUndo(){
|
---|
124 | bool canUndo=true;
|
---|
125 | for(deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){
|
---|
126 | if((*it)->shouldUndo()){
|
---|
127 | canUndo &= (*it)->canUndo();
|
---|
128 | }
|
---|
129 | }
|
---|
130 | return canUndo;
|
---|
131 | }
|
---|
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 | }
|
---|