Changeset b31bc4 for src/Actions
- Timestamp:
- Mar 25, 2010, 12:01:58 PM (16 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, Candidate_v1.7.0, 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:
- 9848ba
- Parents:
- 66e95e (diff), d56640 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- src/Actions
- Files:
-
- 2 added
- 19 edited
-
Action.cpp (modified) (3 diffs)
-
Action.hpp (modified) (2 diffs)
-
ActionHistory.cpp (added)
-
ActionHistory.hpp (added)
-
ActionRegistry.cpp (modified) (3 diffs)
-
ActionRegistry.hpp (modified) (2 diffs)
-
ActionSequence.cpp (modified) (2 diffs)
-
ActionSequence.hpp (modified) (3 diffs)
-
AtomsCalculation_impl.hpp (modified) (2 diffs)
-
Calculation.hpp (modified) (2 diffs)
-
Calculation_impl.hpp (modified) (2 diffs)
-
ErrorAction.cpp (modified) (3 diffs)
-
ErrorAction.hpp (modified) (1 diff)
-
MakroAction.cpp (modified) (3 diffs)
-
MakroAction.hpp (modified) (1 diff)
-
ManipulateAtomsProcess.cpp (modified) (3 diffs)
-
ManipulateAtomsProcess.hpp (modified) (2 diffs)
-
MethodAction.cpp (modified) (3 diffs)
-
MethodAction.hpp (modified) (1 diff)
-
small_actions.cpp (modified) (2 diffs)
-
small_actions.hpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Actions/Action.cpp
r66e95e rb31bc4 10 10 #include "Actions/Action.hpp" 11 11 #include "Actions/ActionRegistry.hpp" 12 #include "Actions/ActionHistory.hpp" 12 13 13 14 using namespace std; 15 16 // An empty state to indicate success 17 Action::state_ptr Action::success = Action::state_ptr(new ActionState()); 18 Action::state_ptr Action::failure = Action::state_ptr(new ActionState()); 14 19 15 20 Action::Action(std::string _name,bool _doRegister) : … … 17 22 { 18 23 if(_doRegister){ 19 ActionRegistry::get Registry()->registerAction(this);24 ActionRegistry::getInstance().registerAction(this); 20 25 } 21 26 } … … 27 32 return name; 28 33 } 34 35 void Action::call(){ 36 // forward to private virtual 37 state_ptr state = performCall(); 38 if(shouldUndo() && state != failure){ 39 if(canUndo()){ 40 ActionHistory::getInstance().addElement(this,state); 41 } 42 else{ 43 ActionHistory::getInstance().clear(); 44 } 45 } 46 } 47 Action::state_ptr Action::undo(state_ptr _state) { 48 // forward to private virtual 49 return performUndo(_state); 50 } 51 Action::state_ptr Action::redo(state_ptr _state) { 52 // forward to private virtual 53 return performRedo(_state); 54 } -
src/Actions/Action.hpp
r66e95e rb31bc4 10 10 11 11 #include <string> 12 #include <boost/shared_ptr.hpp> 13 14 // forward declaration 15 16 class ActionState; 17 class ActionSequence; 12 18 13 19 /** … … 21 27 class Action 22 28 { 23 protected: 29 friend class ActionSequence; 24 30 public: 31 32 typedef boost::shared_ptr<ActionState> state_ptr; 33 25 34 Action(std::string _name,bool _doRegister=true); 26 35 virtual ~Action(); 27 36 28 virtual void call()=0; 29 virtual void undo()=0; 37 // this method only handles the infrastructure 38 // actuall action is passed on to a private virtual 39 void call(); 40 state_ptr undo(state_ptr); 41 state_ptr redo(state_ptr); 42 30 43 virtual bool canUndo()=0; 31 //virtual bool shouldUndo()=0;44 virtual bool shouldUndo()=0; 32 45 33 46 virtual const std::string getName(); 34 47 48 protected: 49 static state_ptr success; 50 static state_ptr failure; 51 35 52 private: 53 virtual state_ptr performCall()=0; 54 virtual state_ptr performUndo(state_ptr)=0; 55 virtual state_ptr performRedo(state_ptr)=0; 56 36 57 std::string name; 37 58 }; 38 59 60 /** 61 * This class can be used by actions to save the state. 62 * 63 * It is implementing a memento pattern. The base class is completely empty, 64 * since no general state internals can be given. The Action performing 65 * the Undo should downcast to the apropriate type. 66 */ 67 class ActionState{ 68 public: 69 ActionState(){} 70 virtual ~ActionState(){} 71 }; 72 39 73 #endif /* ACTION_H_ */ -
src/Actions/ActionRegistry.cpp
r66e95e rb31bc4 9 9 #include "Actions/Action.hpp" 10 10 11 #include "Patterns/Singleton_impl.hpp" 12 11 13 #include <string> 12 #include <cassert>14 #include "Helpers/Assert.hpp" 13 15 #include <iostream> 14 16 15 17 using namespace std; 16 17 ActionRegistry *ActionRegistry::theInstance=0;18 18 19 19 ActionRegistry::ActionRegistry() … … 33 33 map<const string,Action*>::iterator iter; 34 34 iter = actionMap.find(name); 35 assert(iter!=actionMap.end() &&"Query for an action not stored in registry");35 ASSERT(iter!=actionMap.end(),"Query for an action not stored in registry"); 36 36 return iter->second; 37 37 } … … 40 40 pair<map<const string,Action*>::iterator,bool> ret; 41 41 ret = actionMap.insert(pair<const string,Action*>(action->getName(),action)); 42 assert(ret.second &&"Two actions with the same name added to registry");42 ASSERT(ret.second,"Two actions with the same name added to registry"); 43 43 } 44 44 45 // singleton stuff 46 ActionRegistry* ActionRegistry::getRegistry(){ 47 if(!theInstance){ 48 theInstance = new ActionRegistry(); 49 } 50 return theInstance; 51 } 52 53 void ActionRegistry::purgeRegistry(){ 54 if(theInstance){ 55 delete theInstance; 56 theInstance = 0; 57 } 58 } 45 CONSTRUCT_SINGLETON(ActionRegistry) -
src/Actions/ActionRegistry.hpp
r66e95e rb31bc4 12 12 #include <map> 13 13 14 #include "Patterns/Singleton.hpp" 15 14 16 class Action; 15 17 16 class ActionRegistry 18 class ActionRegistry : public Singleton<ActionRegistry> 17 19 { 20 friend class Singleton<ActionRegistry>; 18 21 public: 19 22 Action* getActionByName(const std::string); … … 23 26 std::map<const std::string,Action*> actionMap; 24 27 25 // singleton stuff26 public:27 static ActionRegistry* getRegistry();28 static void purgeRegistry();29 28 private: 30 29 ActionRegistry(); 31 30 virtual ~ActionRegistry(); 32 static ActionRegistry *theInstance;33 31 }; 34 32 -
src/Actions/ActionSequence.cpp
r66e95e rb31bc4 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 Action::state_ptr 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(stateSet 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 Action::state_ptr 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(stateSet 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 Action::state_ptr 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
r66e95e rb31bc4 9 9 #define ACTIONSEQUENZE_HPP_ 10 10 11 #include "Actions/Action.hpp" 12 11 13 #include <deque> 12 13 class Action;14 14 15 15 /** … … 19 19 { 20 20 public: 21 typedef std::deque<Action*> actionSet; 22 typedef std::deque<Action::state_ptr> stateSet; 23 21 24 ActionSequence(); 22 25 virtual ~ActionSequence(); … … 25 28 Action* removeLastAction(); 26 29 27 void callAll(); 28 void undoAll(); 30 stateSet callAll(); 31 stateSet undoAll(stateSet); 32 stateSet redoAll(stateSet); 29 33 30 34 bool canUndo(); 35 bool shouldUndo(); 31 36 32 37 private: 33 std::deque<Action*>actions;38 actionSet actions; 34 39 }; 35 40 -
src/Actions/AtomsCalculation_impl.hpp
r66e95e rb31bc4 19 19 AtomsCalculation<T>::AtomsCalculation(boost::function<T(atom*)> _op,std::string name,AtomDescriptor _descr) : 20 20 Calculation<std::vector<T> >(0,name,false), 21 op(_op),22 descr(_descr)21 descr(_descr), 22 op(_op) 23 23 {} 24 24 … … 29 29 template<typename T> 30 30 std::vector<T>* AtomsCalculation<T>::doCalc(){ 31 World* world = World::get ();31 World* world = World::getPointer(); 32 32 int steps = world->numAtoms(); 33 int count = 0;34 33 std::vector<T> *res = new std::vector<T>(); 35 34 res->reserve(steps); 36 35 Process::setMaxSteps(steps); 37 36 Process::start(); 38 World::AtomIterator iter; 39 for(iter=world->getAtomIter(descr);iter!=world->atomEnd();++iter){ 37 for(World::AtomIterator iter=world->getAtomIter(descr);iter!=world->atomEnd();++iter){ 40 38 Process::setCurrStep(iter.getCount()); 41 39 res->push_back(op(*iter)); -
src/Actions/Calculation.hpp
r66e95e rb31bc4 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 Action::state_ptr performCall(); 67 virtual Action::state_ptr performUndo(Action::state_ptr); 68 virtual Action::state_ptr performRedo(Action::state_ptr); 69 66 70 bool done; 67 71 }; -
src/Actions/Calculation_impl.hpp
r66e95e rb31bc4 16 16 Calculation<T>::Calculation(int _maxSteps, std::string _name, bool _doRegister) : 17 17 Process(_maxSteps,_name,_doRegister), 18 done(false),19 result(0)18 result(0), 19 done(false) 20 20 {} 21 21 … … 29 29 30 30 template<typename T> 31 void Calculation<T>::call(){31 Action::state_ptr 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 Action::state_ptr Calculation<T>::performUndo(Action::state_ptr){ 39 ASSERT(0,"Cannot undo a calculation"); 40 return Action::success; 41 } 42 template<typename T> 43 Action::state_ptr Calculation<T>::performRedo(Action::state_ptr){ 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
r66e95e rb31bc4 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 Action::state_ptr ErrorAction::performCall() { 27 28 Log() << Verbose(0) << errorMsg << endl; 29 return Action::success; 28 30 } 29 void ErrorAction::undo() { 31 Action::state_ptr ErrorAction::performUndo(Action::state_ptr) { 32 ASSERT(0,"Undo called for an ErrorAction"); 33 return Action::success; 34 } 35 36 Action::state_ptr ErrorAction::performRedo(Action::state_ptr) { 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
r66e95e rb31bc4 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 Action::state_ptr performCall(); 26 virtual Action::state_ptr performUndo(Action::state_ptr); 27 virtual Action::state_ptr performRedo(Action::state_ptr); 28 25 29 std::string errorMsg; 26 30 }; -
src/Actions/MakroAction.cpp
r66e95e rb31bc4 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 // All contained states are destroyed by the shared ptrs 24 } 25 26 ActionSequence::stateSet states; 27 }; 15 28 16 29 MakroAction::MakroAction(string _name,ActionSequence* _actions,bool _doRegister) : … … 30 43 31 44 32 void MakroAction::call(){ 33 actions->callAll(); 45 Action::state_ptr MakroAction::performCall(){ 46 ActionSequence::stateSet states = actions->callAll(); 47 return Action::state_ptr(new MakroActionState(states)); 34 48 } 35 49 36 void MakroAction::undo() { 37 actions->undoAll(); 50 Action::state_ptr MakroAction::performUndo(Action::state_ptr _state) { 51 MakroActionState *state = dynamic_cast<MakroActionState*>(_state.get()); 52 ASSERT(state,"Type mismatch for the state of the MakroAction"); 53 ActionSequence::stateSet states = actions->undoAll(state->states); 54 return Action::state_ptr(new MakroActionState(states)); 55 } 56 57 Action::state_ptr MakroAction::performRedo(Action::state_ptr _state){ 58 MakroActionState *state = dynamic_cast<MakroActionState*>(_state.get()); 59 ASSERT(state,"Type mismatch for the state of the MakroAction"); 60 ActionSequence::stateSet states = actions->redoAll(state->states); 61 return Action::state_ptr(new MakroActionState(states)); 38 62 } 39 63 … … 41 65 return actions->canUndo(); 42 66 } 67 68 bool MakroAction::shouldUndo() { 69 return actions->shouldUndo(); 70 } -
src/Actions/MakroAction.hpp
r66e95e rb31bc4 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 Action::state_ptr performCall(); 33 virtual Action::state_ptr performUndo(Action::state_ptr); 34 virtual Action::state_ptr performRedo(Action::state_ptr); 35 33 36 ActionSequence *actions; 34 37 }; -
src/Actions/ManipulateAtomsProcess.cpp
r66e95e rb31bc4 9 9 10 10 #include <iostream> 11 12 #include "World.hpp" 13 #include "Helpers/Assert.hpp" 11 14 12 15 using namespace std; … … 22 25 {} 23 26 24 void ManipulateAtomsProcess::call(){ 25 World::get()->doManipulate(this); 27 Action::state_ptr ManipulateAtomsProcess::performCall(){ 28 World::getInstance().doManipulate(this); 29 return Action::success; 26 30 } 27 31 28 void ManipulateAtomsProcess::undo(){ 32 Action::state_ptr ManipulateAtomsProcess::performUndo(Action::state_ptr){ 33 ASSERT(0,"Undo called for a ManipulateAtomsProcess"); 34 return Action::success; 35 } 29 36 37 Action::state_ptr ManipulateAtomsProcess::performRedo(Action::state_ptr){ 38 ASSERT(0,"Redo called for a ManipulateAtomsProcess"); 39 return Action::success; 30 40 } 31 41 … … 34 44 } 35 45 46 bool ManipulateAtomsProcess::shouldUndo(){ 47 return true; 48 } 49 36 50 void ManipulateAtomsProcess::doManipulate(World *world){ 37 51 setMaxSteps(world->numAtoms()); 38 52 start(); 39 World::AtomIterator iter; 40 for(iter=world->getAtomIter(descr);iter!=world->atomEnd();++iter){ 53 for(World::AtomIterator iter=world->getAtomIter(descr);iter!=world->atomEnd();++iter){ 41 54 setCurrStep(iter.getCount()); 42 55 operation(*iter); -
src/Actions/ManipulateAtomsProcess.hpp
r66e95e rb31bc4 10 10 11 11 #include "Actions/Process.hpp" 12 13 #include<boost/function.hpp> 14 12 15 #include "Descriptors/AtomDescriptor.hpp" 16 17 class World; 13 18 14 19 class ManipulateAtomsProcess : public Process … … 18 23 virtual ~ManipulateAtomsProcess(); 19 24 20 virtual void call();21 virtual void undo();22 25 virtual bool canUndo(); 26 virtual bool shouldUndo(); 23 27 24 28 virtual void doManipulate(World *); 25 29 private: 30 31 virtual Action::state_ptr performCall(); 32 virtual Action::state_ptr performUndo(Action::state_ptr); 33 virtual Action::state_ptr performRedo(Action::state_ptr); 34 26 35 AtomDescriptor descr; 27 36 boost::function<void(atom*)> operation; -
src/Actions/MethodAction.cpp
r66e95e rb31bc4 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 Action::state_ptr 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 Action::state_ptr MethodAction::performUndo(Action::state_ptr) { 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 Action::state_ptr MethodAction::performRedo(Action::state_ptr){ 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
r66e95e rb31bc4 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 Action::state_ptr performCall(); 29 virtual Action::state_ptr performUndo(Action::state_ptr); 30 virtual Action::state_ptr performRedo(Action::state_ptr); 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
r66e95e rb31bc4 14 14 /****** ChangeMoleculeNameAction *****/ 15 15 16 char ChangeMoleculeNameAction::NAME[] = "Change filename of Molecule"; 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 28 const char ChangeMoleculeNameAction::NAME[] = "Change filename of Molecule"; 17 29 18 30 ChangeMoleculeNameAction::ChangeMoleculeNameAction(MoleculeListClass *_molecules) : … … 24 36 {} 25 37 26 void ChangeMoleculeNameAction::call() {38 Action::state_ptr ChangeMoleculeNameAction::performCall() { 27 39 string filename; 28 40 molecule *mol = NULL; 29 Dialog *dialog = UIFactory::get ()->makeDialog();41 Dialog *dialog = UIFactory::getInstance().makeDialog(); 30 42 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 Action::state_ptr(new ChangeMoleculeNameState(mol,oldName)); 35 51 } 36 37 52 delete dialog; 53 return Action::failure; 38 54 } 39 55 40 void ChangeMoleculeNameAction::undo() { 56 Action::state_ptr ChangeMoleculeNameAction::performUndo(Action::state_ptr _state) { 57 ChangeMoleculeNameState *state = dynamic_cast<ChangeMoleculeNameState*>(_state.get()); 58 ASSERT(state,"State passed to ChangeMoleculeNameAction::performUndo did not have correct type"); 41 59 60 string newName = state->mol->getName(); 61 state->mol->setName(state->lastName); 62 63 return Action::state_ptr(new ChangeMoleculeNameState(state->mol,newName)); 64 } 65 66 Action::state_ptr ChangeMoleculeNameAction::performRedo(Action::state_ptr _state){ 67 // Undo and redo have to do the same for this action 68 return performUndo(_state); 42 69 } 43 70 44 71 bool ChangeMoleculeNameAction::canUndo() { 45 return false;72 return true; 46 73 } 47 74 -
src/Actions/small_actions.hpp
r66e95e rb31bc4 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 Action::state_ptr performCall(); 22 virtual Action::state_ptr performUndo(Action::state_ptr); 23 virtual Action::state_ptr performRedo(Action::state_ptr); 24 23 25 MoleculeListClass *molecules; 24 static c har NAME[];26 static const char NAME[]; 25 27 }; 26 28
Note:
See TracChangeset
for help on using the changeset viewer.
