Changeset 67e2b3 for src/Actions
- Timestamp:
- Mar 24, 2010, 4:26:21 PM (15 years ago)
- Branches:
- Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
- Children:
- 5b0b98
- Parents:
- ce1d8c
- Location:
- src/Actions
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Actions/Action.cpp
rce1d8c r67e2b3 12 12 13 13 using namespace std; 14 15 // this object is only ever used as indicator. 16 ActionState success_val; 17 18 ActionState *Action::success = &success_val; 14 19 15 20 Action::Action(std::string _name,bool _doRegister) : … … 27 32 return name; 28 33 } 34 35 void Action::call(){ 36 // forward to private virtual 37 performCall(); 38 } 39 ActionState* Action::undo(ActionState* _state) { 40 // forward to private virtual 41 return performUndo(_state); 42 } 43 ActionState* Action::redo(ActionState* _state) { 44 // forward to private virtual 45 return performRedo(_state); 46 } -
src/Actions/Action.hpp
rce1d8c r67e2b3 11 11 #include <string> 12 12 13 // forward declaration 14 15 class ActionState; 16 class ActionSequence; 17 13 18 /** 14 19 * Base class for all actions. … … 21 26 class Action 22 27 { 23 protected: 28 friend class ActionSequence; 24 29 public: 25 30 Action(std::string _name,bool _doRegister=true); 26 31 virtual ~Action(); 27 32 28 virtual void call()=0; 29 virtual void undo()=0; 33 // this method only handles the infrastructure 34 // actuall action is passed on to a private virtual 35 void call(); 36 ActionState* undo(ActionState*); 37 ActionState* redo(ActionState*); 38 30 39 virtual bool canUndo()=0; 31 //virtual bool shouldUndo()=0;40 virtual bool shouldUndo()=0; 32 41 33 42 virtual const std::string getName(); 34 43 44 protected: 45 static ActionState* success; 46 35 47 private: 48 virtual ActionState* performCall()=0; 49 virtual ActionState* performUndo(ActionState*)=0; 50 virtual ActionState* performRedo(ActionState*)=0; 51 36 52 std::string name; 37 53 }; 38 54 55 /** 56 * This class can be used by actions to save the state. 57 * 58 * It is implementing a memento pattern. The base class is completely empty, 59 * since no general state internals can be given. The Action performing 60 * the Undo should downcast to the apropriate type. 61 */ 62 class ActionState{ 63 public: 64 ActionState(){} 65 virtual ~ActionState(){} 66 }; 67 39 68 #endif /* ACTION_H_ */ -
src/Actions/ActionSequence.cpp
rce1d8c r67e2b3 8 8 #include "Actions/ActionSequence.hpp" 9 9 #include "Actions/Action.hpp" 10 11 #include "Helpers/Assert.hpp" 10 12 11 13 using namespace std; … … 34 36 } 35 37 36 void ActionSequence::callAll(){ 37 deque<Action*>::iterator it; 38 for(it=actions.begin(); it!=actions.end(); it++) 39 (*it)->call(); 38 ActionSequence::stateSet ActionSequence::callAll(){ 39 stateSet states; 40 for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){ 41 // we want to have a global bookkeeping for all actions in the sequence, so 42 // we bypass the normal call 43 ActionState *state = (*it)->performCall(); 44 states.push_back(state); 45 } 46 return states; 40 47 } 41 48 42 void ActionSequence::undoAll(){ 43 deque<Action*>::reverse_iterator rit; 44 for(rit=actions.rbegin(); rit!=actions.rend(); rit++) 45 (*rit)->undo(); 49 ActionSequence::stateSet ActionSequence::undoAll(deque<ActionState*> states){ 50 ASSERT(canUndo(),"Trying to undo a sequence that contains methods that can't be undone"); 51 stateSet res; 52 actionSet::reverse_iterator actionRit = actions.rbegin(); 53 stateSet::reverse_iterator stateRit = states.rbegin(); 54 for(;actionRit!=actions.rend();++actionRit,++stateRit){ 55 ASSERT(stateRit!=states.rend(),"End of states prematurely reached."); 56 if((*actionRit)->shouldUndo()){ 57 ActionState *newState = (*actionRit)->performUndo(*stateRit); 58 // The order of the states has to correspond to the order of the actions 59 // this is why we have to add at the beginning 60 res.push_front(newState); 61 } 62 else{ 63 res.push_front(Action::success); 64 } 65 } 66 return res; 67 } 68 69 ActionSequence::stateSet ActionSequence::redoAll(deque<ActionState*> states){ 70 stateSet res; 71 actionSet::iterator actionIt = actions.begin(); 72 stateSet::iterator stateIt = states.begin(); 73 for(;actionIt!=actions.end();++actionIt,++stateIt){ 74 ASSERT(stateIt!=states.end(),"End of states prematurely reached."); 75 if((*actionIt)->shouldUndo()){ 76 ActionState *newState =(*actionIt)->performRedo(*stateIt); 77 res.push_back(newState); 78 } 79 else{ 80 res.push_back(Action::success); 81 } 82 } 83 return res; 46 84 } 47 85 48 86 bool ActionSequence::canUndo(){ 49 87 bool canUndo=true; 50 deque<Action*>::iterator it; 51 for(it=actions.begin(); it!=actions.end(); it++) 52 canUndo &= (*it)->canUndo(); 88 for(deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){ 89 if((*it)->shouldUndo()){ 90 canUndo &= (*it)->canUndo(); 91 } 92 } 53 93 return canUndo; 54 94 } 95 96 bool ActionSequence::shouldUndo(){ 97 bool shouldUndo = false; 98 for(deque<Action*>::iterator it=actions.begin();it!=actions.end();++it){ 99 shouldUndo |= (*it)->shouldUndo(); 100 } 101 return shouldUndo; 102 } -
src/Actions/ActionSequence.hpp
rce1d8c r67e2b3 12 12 13 13 class Action; 14 class ActionState; 14 15 15 16 /** … … 19 20 { 20 21 public: 22 typedef std::deque<Action*> actionSet; 23 typedef std::deque<ActionState*> stateSet; 24 21 25 ActionSequence(); 22 26 virtual ~ActionSequence(); … … 25 29 Action* removeLastAction(); 26 30 27 void callAll(); 28 void undoAll(); 31 stateSet callAll(); 32 stateSet undoAll(stateSet); 33 stateSet redoAll(stateSet); 29 34 30 35 bool canUndo(); 36 bool shouldUndo(); 31 37 32 38 private: 33 std::deque<Action*>actions;39 actionSet actions; 34 40 }; 35 41 -
src/Actions/Calculation.hpp
rce1d8c r67e2b3 29 29 * from menu Items or other places. 30 30 */ 31 virtual void call();32 virtual void undo();33 31 virtual bool canUndo(); 32 33 virtual bool shouldUndo(); 34 34 35 35 /** … … 64 64 virtual T* doCalc()=0; 65 65 private: 66 virtual ActionState* performCall(); 67 virtual ActionState* performUndo(ActionState*); 68 virtual ActionState* performRedo(ActionState*); 69 66 70 bool done; 67 71 }; -
src/Actions/Calculation_impl.hpp
rce1d8c r67e2b3 29 29 30 30 template<typename T> 31 void Calculation<T>::call(){31 ActionState* Calculation<T>::performCall(){ 32 32 reset(); 33 33 (*this)(); 34 return Action::success; 34 35 } 35 36 36 37 template<typename T> 37 void Calculation<T>::undo(){} 38 ActionState* Calculation<T>::performUndo(ActionState*){ 39 ASSERT(0,"Cannot undo a calculation"); 40 return Action::success; 41 } 42 template<typename T> 43 ActionState* Calculation<T>::performRedo(ActionState*){ 44 ASSERT(0,"Cannot redo a calculation"); 45 return Action::success; 46 } 38 47 39 48 template<typename T> 40 49 bool Calculation<T>::canUndo() 50 { 51 return false; 52 } 53 54 template<typename T> 55 bool Calculation<T>::shouldUndo() 41 56 { 42 57 return false; -
src/Actions/ErrorAction.cpp
rce1d8c r67e2b3 11 11 #include "log.hpp" 12 12 #include "verbose.hpp" 13 #include "Helpers/Assert.hpp" 13 14 14 15 using namespace std; … … 24 25 } 25 26 26 void ErrorAction::call() {27 ActionState* ErrorAction::performCall() { 27 28 Log() << Verbose(0) << errorMsg << endl; 29 return Action::success; 28 30 } 29 void ErrorAction::undo() { 31 ActionState* ErrorAction::performUndo(ActionState*) { 32 ASSERT(0,"Undo called for an ErrorAction"); 33 return Action::success; 34 } 35 36 ActionState* ErrorAction::performRedo(ActionState*) { 37 ASSERT(0,"Redo called for an ErrorAction"); 38 return Action::success; 30 39 } 31 40 … … 33 42 return false; 34 43 } 44 45 bool ErrorAction::shouldUndo(){ 46 return false; 47 } -
src/Actions/ErrorAction.hpp
rce1d8c r67e2b3 18 18 virtual ~ErrorAction(); 19 19 20 virtual void call();21 virtual void undo();22 20 virtual bool canUndo(); 21 virtual bool shouldUndo(); 23 22 24 23 private: 24 25 virtual ActionState* performCall(); 26 virtual ActionState* performUndo(ActionState*); 27 virtual ActionState* performRedo(ActionState*); 28 25 29 std::string errorMsg; 26 30 }; -
src/Actions/MakroAction.cpp
rce1d8c r67e2b3 11 11 #include "Actions/Action.hpp" 12 12 #include "Actions/ActionSequence.hpp" 13 #include "Helpers/Assert.hpp" 13 14 14 15 using namespace std; 16 17 class MakroActionState : public ActionState{ 18 public: 19 MakroActionState(ActionSequence::stateSet _states) : 20 states(_states) 21 {} 22 virtual ~MakroActionState(){ 23 for(ActionSequence::stateSet::iterator iter=states.begin(); 24 iter!=states.end();++iter){ 25 delete *iter; 26 } 27 } 28 29 ActionSequence::stateSet states; 30 }; 15 31 16 32 MakroAction::MakroAction(string _name,ActionSequence* _actions,bool _doRegister) : … … 30 46 31 47 32 void MakroAction::call(){ 33 actions->callAll(); 48 ActionState* MakroAction::performCall(){ 49 ActionSequence::stateSet states = actions->callAll(); 50 return new MakroActionState(states); 34 51 } 35 52 36 void MakroAction::undo() { 37 actions->undoAll(); 53 ActionState* MakroAction::performUndo(ActionState* _state) { 54 MakroActionState *state = dynamic_cast<MakroActionState*>(_state); 55 ASSERT(state,"Type mismatch for the state of the MakroAction"); 56 ActionSequence::stateSet states = actions->undoAll(state->states); 57 return new MakroActionState(states); 38 58 } 39 59 … … 41 61 return actions->canUndo(); 42 62 } 63 64 bool MakroAction::shouldUndo() { 65 return actions->shouldUndo(); 66 } -
src/Actions/MakroAction.hpp
rce1d8c r67e2b3 26 26 virtual ~MakroAction(); 27 27 28 virtual void call(); 29 virtual void undo(); 30 virtual bool canUndo(); 28 bool canUndo(); 29 bool shouldUndo(); 31 30 32 31 private: 32 virtual ActionState* performCall(); 33 virtual ActionState* performUndo(ActionState*); 34 virtual ActionState* performRedo(ActionState*); 35 33 36 ActionSequence *actions; 34 37 }; -
src/Actions/ManipulateAtomsProcess.cpp
rce1d8c r67e2b3 11 11 12 12 #include "World.hpp" 13 #include "Helpers/Assert.hpp" 13 14 14 15 using namespace std; … … 24 25 {} 25 26 26 void ManipulateAtomsProcess::call(){27 ActionState* ManipulateAtomsProcess::performCall(){ 27 28 World::getInstance().doManipulate(this); 29 return Action::success; 28 30 } 29 31 30 void ManipulateAtomsProcess::undo(){ 32 ActionState* ManipulateAtomsProcess::performUndo(ActionState*){ 33 ASSERT(0,"Undo called for a ManipulateAtomsProcess"); 34 return Action::success; 35 } 31 36 37 ActionState* ManipulateAtomsProcess::performRedo(ActionState*){ 38 ASSERT(0,"Redo called for a ManipulateAtomsProcess"); 39 return Action::success; 32 40 } 33 41 34 42 bool ManipulateAtomsProcess::canUndo(){ 35 43 return false; 44 } 45 46 bool ManipulateAtomsProcess::shouldUndo(){ 47 return true; 36 48 } 37 49 -
src/Actions/ManipulateAtomsProcess.hpp
rce1d8c r67e2b3 23 23 virtual ~ManipulateAtomsProcess(); 24 24 25 virtual void call();26 virtual void undo();27 25 virtual bool canUndo(); 26 virtual bool shouldUndo(); 28 27 29 28 virtual void doManipulate(World *); 30 29 private: 30 31 virtual ActionState* performCall(); 32 virtual ActionState* performUndo(ActionState*); 33 virtual ActionState* performRedo(ActionState*); 34 31 35 AtomDescriptor descr; 32 36 boost::function<void(atom*)> operation; -
src/Actions/MethodAction.cpp
rce1d8c r67e2b3 10 10 #include <string> 11 11 12 #include "MethodAction.hpp" 12 #include "Actions/MethodAction.hpp" 13 #include "Helpers/Assert.hpp" 13 14 14 15 using namespace std; … … 21 22 22 23 MethodAction::~MethodAction() 23 { 24 // TODO Auto-generated destructor stub 24 {} 25 26 27 ActionState* MethodAction::performCall() { 28 executeMethod(); 29 // we don't have a state to save so we return Action::success 30 return Action::success; 25 31 } 26 32 33 ActionState* MethodAction::performUndo(ActionState*) { 34 ASSERT(0,"Cannot undo a MethodAction"); 35 return Action::success; 36 } 27 37 28 void MethodAction::call() { 29 executeMethod(); 30 } 31 void MethodAction::undo() { 32 38 ActionState* MethodAction::performRedo(ActionState*){ 39 ASSERT(0,"Cannot redo a MethodAction"); 40 return Action::success; 33 41 } 34 42 … … 36 44 return false; 37 45 } 46 47 bool MethodAction::shouldUndo(){ 48 return true; 49 } -
src/Actions/MethodAction.hpp
rce1d8c r67e2b3 22 22 MethodAction(std::string _name,boost::function<void()> _executeMethod,bool _doRegister=true); 23 23 virtual ~MethodAction(); 24 virtual bool canUndo(); 25 virtual bool shouldUndo(); 24 26 25 virtual void call(); 26 virtual void undo(); 27 virtual bool canUndo(); 27 private: 28 virtual ActionState* performCall(); 29 virtual ActionState* performUndo(ActionState*); 30 virtual ActionState* performRedo(ActionState*); 31 28 32 29 33 boost::function<void()> executeMethod; //!< this stores the method to be called 30 31 32 34 }; 33 35 -
src/Actions/small_actions.cpp
rce1d8c r67e2b3 14 14 /****** ChangeMoleculeNameAction *****/ 15 15 16 // memento to remember the state when undoing 17 18 class ChangeMoleculeNameState : public ActionState { 19 public: 20 ChangeMoleculeNameState(molecule* _mol,std::string _lastName) : 21 mol(_mol), 22 lastName(_lastName) 23 {} 24 molecule* mol; 25 std::string lastName; 26 }; 27 16 28 char ChangeMoleculeNameAction::NAME[] = "Change filename of Molecule"; 17 29 … … 24 36 {} 25 37 26 void ChangeMoleculeNameAction::call() {38 ActionState* ChangeMoleculeNameAction::performCall() { 27 39 string filename; 28 40 molecule *mol = NULL; … … 31 43 dialog->queryMolecule("Enter index of molecule: ",&mol,molecules); 32 44 dialog->queryString("Enter name: ",&filename); 45 33 46 if(dialog->display()) { 47 string oldName = mol->getName(); 34 48 mol->setName(filename); 49 delete dialog; 50 return new ChangeMoleculeNameState(mol,oldName); 35 51 } 36 37 delete dialog; 52 return 0; 38 53 } 39 54 40 void ChangeMoleculeNameAction::undo() { 55 ActionState* ChangeMoleculeNameAction::performUndo(ActionState* _state) { 56 ChangeMoleculeNameState *state = dynamic_cast<ChangeMoleculeNameState*>(_state); 57 ASSERT(state,"State passed to ChangeMoleculeNameAction::performUndo did not have correct type"); 41 58 59 string newName = state->mol->getName(); 60 state->mol->setName(state->lastName); 61 62 return new ChangeMoleculeNameState(state->mol,newName); 63 } 64 65 ActionState* ChangeMoleculeNameAction::performRedo(ActionState *_state){ 66 // Undo and redo have to do the same for this action 67 return performUndo(_state); 42 68 } 43 69 44 70 bool ChangeMoleculeNameAction::canUndo() { 45 return false;71 return true; 46 72 } 47 73 -
src/Actions/small_actions.hpp
rce1d8c r67e2b3 14 14 virtual ~ChangeMoleculeNameAction(); 15 15 16 void call();17 void undo();18 16 bool canUndo(); 19 17 bool shouldUndo(); … … 21 19 virtual const std::string getName(); 22 20 private: 21 virtual ActionState* performCall(); 22 virtual ActionState* performUndo(ActionState*); 23 virtual ActionState* performRedo(ActionState*); 24 23 25 MoleculeListClass *molecules; 24 26 static char NAME[];
Note:
See TracChangeset
for help on using the changeset viewer.