Changeset b31bc4 for src/Actions


Ignore:
Timestamp:
Mar 25, 2010, 12:01:58 PM (16 years ago)
Author:
Tillmann Crueger <crueger@…>
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.
Message:

Merge branch 'StructureRefactoring' into MenuRefactoring

Location:
src/Actions
Files:
2 added
19 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/Action.cpp

    r66e95e rb31bc4  
    1010#include "Actions/Action.hpp"
    1111#include "Actions/ActionRegistry.hpp"
     12#include "Actions/ActionHistory.hpp"
    1213
    1314using namespace std;
     15
     16// An empty state to indicate success
     17Action::state_ptr Action::success = Action::state_ptr(new ActionState());
     18Action::state_ptr Action::failure = Action::state_ptr(new ActionState());
    1419
    1520Action::Action(std::string _name,bool _doRegister) :
     
    1722{
    1823  if(_doRegister){
    19     ActionRegistry::getRegistry()->registerAction(this);
     24    ActionRegistry::getInstance().registerAction(this);
    2025  }
    2126}
     
    2732  return name;
    2833}
     34
     35void 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}
     47Action::state_ptr Action::undo(state_ptr _state) {
     48  // forward to private virtual
     49  return performUndo(_state);
     50}
     51Action::state_ptr Action::redo(state_ptr _state) {
     52  // forward to private virtual
     53  return performRedo(_state);
     54}
  • src/Actions/Action.hpp

    r66e95e rb31bc4  
    1010
    1111#include <string>
     12#include <boost/shared_ptr.hpp>
     13
     14// forward declaration
     15
     16class ActionState;
     17class ActionSequence;
    1218
    1319/**
     
    2127class Action
    2228{
    23 protected:
     29friend class ActionSequence;
    2430public:
     31
     32  typedef boost::shared_ptr<ActionState> state_ptr;
     33
    2534  Action(std::string _name,bool _doRegister=true);
    2635  virtual ~Action();
    2736
    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
    3043  virtual bool canUndo()=0;
    31   //virtual bool shouldUndo()=0;
     44  virtual bool shouldUndo()=0;
    3245
    3346  virtual const std::string getName();
    3447
     48protected:
     49  static state_ptr success;
     50  static state_ptr failure;
     51
    3552private:
     53  virtual state_ptr performCall()=0;
     54  virtual state_ptr performUndo(state_ptr)=0;
     55  virtual state_ptr performRedo(state_ptr)=0;
     56
    3657  std::string name;
    3758};
    3859
     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 */
     67class ActionState{
     68public:
     69  ActionState(){}
     70  virtual ~ActionState(){}
     71};
     72
    3973#endif /* ACTION_H_ */
  • src/Actions/ActionRegistry.cpp

    r66e95e rb31bc4  
    99#include "Actions/Action.hpp"
    1010
     11#include "Patterns/Singleton_impl.hpp"
     12
    1113#include <string>
    12 #include <cassert>
     14#include "Helpers/Assert.hpp"
    1315#include <iostream>
    1416
    1517using namespace std;
    16 
    17 ActionRegistry *ActionRegistry::theInstance=0;
    1818
    1919ActionRegistry::ActionRegistry()
     
    3333  map<const string,Action*>::iterator iter;
    3434  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");
    3636  return iter->second;
    3737}
     
    4040  pair<map<const string,Action*>::iterator,bool> ret;
    4141  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");
    4343}
    4444
    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 }
     45CONSTRUCT_SINGLETON(ActionRegistry)
  • src/Actions/ActionRegistry.hpp

    r66e95e rb31bc4  
    1212#include <map>
    1313
     14#include "Patterns/Singleton.hpp"
     15
    1416class Action;
    1517
    16 class ActionRegistry
     18class ActionRegistry : public Singleton<ActionRegistry>
    1719{
     20  friend class Singleton<ActionRegistry>;
    1821public:
    1922  Action* getActionByName(const std::string);
     
    2326  std::map<const std::string,Action*> actionMap;
    2427
    25 // singleton stuff
    26 public:
    27   static ActionRegistry* getRegistry();
    28   static void purgeRegistry();
    2928private:
    3029  ActionRegistry();
    3130  virtual ~ActionRegistry();
    32   static ActionRegistry *theInstance;
    3331};
    3432
  • src/Actions/ActionSequence.cpp

    r66e95e rb31bc4  
    88#include "Actions/ActionSequence.hpp"
    99#include "Actions/Action.hpp"
     10
     11#include "Helpers/Assert.hpp"
    1012
    1113using namespace std;
     
    3436}
    3537
    36 void ActionSequence::callAll(){
    37   deque<Action*>::iterator it;
    38   for(it=actions.begin(); it!=actions.end(); it++)
    39     (*it)->call();
     38ActionSequence::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;
    4047}
    4148
    42 void ActionSequence::undoAll(){
    43   deque<Action*>::reverse_iterator rit;
    44   for(rit=actions.rbegin(); rit!=actions.rend(); rit++)
    45     (*rit)->undo();
     49ActionSequence::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
     69ActionSequence::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;
    4684}
    4785
    4886bool ActionSequence::canUndo(){
    4987  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  }
    5393  return canUndo;
    5494}
     95
     96bool 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  
    99#define ACTIONSEQUENZE_HPP_
    1010
     11#include "Actions/Action.hpp"
     12
    1113#include <deque>
    12 
    13 class Action;
    1414
    1515/**
     
    1919{
    2020public:
     21  typedef std::deque<Action*> actionSet;
     22  typedef std::deque<Action::state_ptr> stateSet;
     23
    2124  ActionSequence();
    2225  virtual ~ActionSequence();
     
    2528  Action* removeLastAction();
    2629
    27   void callAll();
    28   void undoAll();
     30  stateSet callAll();
     31  stateSet undoAll(stateSet);
     32  stateSet redoAll(stateSet);
    2933
    3034  bool canUndo();
     35  bool shouldUndo();
    3136
    3237private:
    33   std::deque<Action*> actions;
     38  actionSet actions;
    3439};
    3540
  • src/Actions/AtomsCalculation_impl.hpp

    r66e95e rb31bc4  
    1919AtomsCalculation<T>::AtomsCalculation(boost::function<T(atom*)> _op,std::string name,AtomDescriptor _descr) :
    2020  Calculation<std::vector<T> >(0,name,false),
    21   op(_op),
    22   descr(_descr)
     21  descr(_descr),
     22  op(_op)
    2323{}
    2424
     
    2929template<typename T>
    3030std::vector<T>* AtomsCalculation<T>::doCalc(){
    31   World* world = World::get();
     31  World* world = World::getPointer();
    3232  int steps = world->numAtoms();
    33   int count = 0;
    3433  std::vector<T> *res = new std::vector<T>();
    3534  res->reserve(steps);
    3635  Process::setMaxSteps(steps);
    3736  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){
    4038    Process::setCurrStep(iter.getCount());
    4139    res->push_back(op(*iter));
  • src/Actions/Calculation.hpp

    r66e95e rb31bc4  
    2929   * from menu Items or other places.
    3030   */
    31   virtual void call();
    32   virtual void undo();
    3331  virtual bool canUndo();
     32
     33  virtual bool shouldUndo();
    3434
    3535  /**
     
    6464  virtual T* doCalc()=0;
    6565private:
     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
    6670  bool done;
    6771};
  • src/Actions/Calculation_impl.hpp

    r66e95e rb31bc4  
    1616Calculation<T>::Calculation(int _maxSteps, std::string _name, bool _doRegister) :
    1717  Process(_maxSteps,_name,_doRegister),
    18   done(false),
    19   result(0)
     18  result(0),
     19  done(false)
    2020{}
    2121
     
    2929
    3030template<typename T>
    31 void Calculation<T>::call(){
     31Action::state_ptr Calculation<T>::performCall(){
    3232  reset();
    3333  (*this)();
     34  return Action::success;
    3435}
    3536
    3637template<typename T>
    37 void Calculation<T>::undo(){}
     38Action::state_ptr Calculation<T>::performUndo(Action::state_ptr){
     39  ASSERT(0,"Cannot undo a calculation");
     40  return Action::success;
     41}
     42template<typename T>
     43Action::state_ptr Calculation<T>::performRedo(Action::state_ptr){
     44  ASSERT(0,"Cannot redo a calculation");
     45  return Action::success;
     46}
    3847
    3948template<typename T>
    4049bool Calculation<T>::canUndo()
     50{
     51  return false;
     52}
     53
     54template<typename T>
     55bool Calculation<T>::shouldUndo()
    4156{
    4257  return false;
  • src/Actions/ErrorAction.cpp

    r66e95e rb31bc4  
    1111#include "log.hpp"
    1212#include "verbose.hpp"
     13#include "Helpers/Assert.hpp"
    1314
    1415using namespace std;
     
    2425}
    2526
    26 void ErrorAction::call() {
     27Action::state_ptr ErrorAction::performCall() {
    2728  Log() << Verbose(0) << errorMsg << endl;
     29  return Action::success;
    2830}
    29 void ErrorAction::undo() {
     31Action::state_ptr ErrorAction::performUndo(Action::state_ptr) {
     32  ASSERT(0,"Undo called for an ErrorAction");
     33  return Action::success;
     34}
     35
     36Action::state_ptr ErrorAction::performRedo(Action::state_ptr) {
     37  ASSERT(0,"Redo called for an ErrorAction");
     38  return Action::success;
    3039}
    3140
     
    3342  return false;
    3443}
     44
     45bool ErrorAction::shouldUndo(){
     46  return false;
     47}
  • src/Actions/ErrorAction.hpp

    r66e95e rb31bc4  
    1818  virtual ~ErrorAction();
    1919
    20   virtual void call();
    21   virtual void undo();
    2220  virtual bool canUndo();
     21  virtual bool shouldUndo();
    2322
    2423private:
     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
    2529  std::string errorMsg;
    2630};
  • src/Actions/MakroAction.cpp

    r66e95e rb31bc4  
    1111#include "Actions/Action.hpp"
    1212#include "Actions/ActionSequence.hpp"
     13#include "Helpers/Assert.hpp"
    1314
    1415using namespace std;
     16
     17class MakroActionState : public ActionState{
     18public:
     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};
    1528
    1629MakroAction::MakroAction(string _name,ActionSequence* _actions,bool _doRegister) :
     
    3043
    3144
    32 void MakroAction::call(){
    33   actions->callAll();
     45Action::state_ptr MakroAction::performCall(){
     46  ActionSequence::stateSet states = actions->callAll();
     47  return Action::state_ptr(new MakroActionState(states));
    3448}
    3549
    36 void MakroAction::undo() {
    37   actions->undoAll();
     50Action::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
     57Action::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));
    3862}
    3963
     
    4165  return actions->canUndo();
    4266}
     67
     68bool MakroAction::shouldUndo() {
     69  return actions->shouldUndo();
     70}
  • src/Actions/MakroAction.hpp

    r66e95e rb31bc4  
    2626  virtual ~MakroAction();
    2727
    28   virtual void call();
    29   virtual void undo();
    30   virtual bool canUndo();
     28  bool canUndo();
     29  bool shouldUndo();
    3130
    3231private:
     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
    3336  ActionSequence *actions;
    3437};
  • src/Actions/ManipulateAtomsProcess.cpp

    r66e95e rb31bc4  
    99
    1010#include <iostream>
     11
     12#include "World.hpp"
     13#include "Helpers/Assert.hpp"
    1114
    1215using namespace std;
     
    2225{}
    2326
    24 void ManipulateAtomsProcess::call(){
    25   World::get()->doManipulate(this);
     27Action::state_ptr ManipulateAtomsProcess::performCall(){
     28  World::getInstance().doManipulate(this);
     29  return Action::success;
    2630}
    2731
    28 void ManipulateAtomsProcess::undo(){
     32Action::state_ptr ManipulateAtomsProcess::performUndo(Action::state_ptr){
     33  ASSERT(0,"Undo called for a ManipulateAtomsProcess");
     34  return Action::success;
     35}
    2936
     37Action::state_ptr ManipulateAtomsProcess::performRedo(Action::state_ptr){
     38  ASSERT(0,"Redo called for a ManipulateAtomsProcess");
     39  return Action::success;
    3040}
    3141
     
    3444}
    3545
     46bool ManipulateAtomsProcess::shouldUndo(){
     47  return true;
     48}
     49
    3650void ManipulateAtomsProcess::doManipulate(World *world){
    3751  setMaxSteps(world->numAtoms());
    3852  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){
    4154    setCurrStep(iter.getCount());
    4255    operation(*iter);
  • src/Actions/ManipulateAtomsProcess.hpp

    r66e95e rb31bc4  
    1010
    1111#include "Actions/Process.hpp"
     12
     13#include<boost/function.hpp>
     14
    1215#include "Descriptors/AtomDescriptor.hpp"
     16
     17class World;
    1318
    1419class ManipulateAtomsProcess : public Process
     
    1823  virtual ~ManipulateAtomsProcess();
    1924
    20   virtual void call();
    21   virtual void undo();
    2225  virtual bool canUndo();
     26  virtual bool shouldUndo();
    2327
    2428  virtual void doManipulate(World *);
    2529private:
     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
    2635  AtomDescriptor descr;
    2736  boost::function<void(atom*)> operation;
  • src/Actions/MethodAction.cpp

    r66e95e rb31bc4  
    1010#include <string>
    1111
    12 #include "MethodAction.hpp"
     12#include "Actions/MethodAction.hpp"
     13#include "Helpers/Assert.hpp"
    1314
    1415using namespace std;
     
    2122
    2223MethodAction::~MethodAction()
    23 {
    24   // TODO Auto-generated destructor stub
     24{}
     25
     26
     27Action::state_ptr MethodAction::performCall() {
     28  executeMethod();
     29  // we don't have a state to save so we return Action::success
     30  return Action::success;
    2531}
    2632
     33Action::state_ptr MethodAction::performUndo(Action::state_ptr) {
     34  ASSERT(0,"Cannot undo a MethodAction");
     35  return Action::success;
     36}
    2737
    28 void MethodAction::call() {
    29   executeMethod();
    30 }
    31 void MethodAction::undo() {
    32 
     38Action::state_ptr MethodAction::performRedo(Action::state_ptr){
     39  ASSERT(0,"Cannot redo a MethodAction");
     40  return Action::success;
    3341}
    3442
     
    3644  return false;
    3745}
     46
     47bool MethodAction::shouldUndo(){
     48  return true;
     49}
  • src/Actions/MethodAction.hpp

    r66e95e rb31bc4  
    2222  MethodAction(std::string _name,boost::function<void()> _executeMethod,bool _doRegister=true);
    2323  virtual ~MethodAction();
     24  virtual bool canUndo();
     25  virtual bool shouldUndo();
    2426
    25   virtual void call();
    26   virtual void undo();
    27   virtual bool canUndo();
     27private:
     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
    2832
    2933  boost::function<void()> executeMethod; //!< this stores the method to be called
    30 
    31 
    3234};
    3335
  • src/Actions/small_actions.cpp

    r66e95e rb31bc4  
    1414/****** ChangeMoleculeNameAction *****/
    1515
    16 char ChangeMoleculeNameAction::NAME[] = "Change filename of Molecule";
     16// memento to remember the state when undoing
     17
     18class ChangeMoleculeNameState : public ActionState {
     19public:
     20  ChangeMoleculeNameState(molecule* _mol,std::string _lastName) :
     21    mol(_mol),
     22    lastName(_lastName)
     23  {}
     24  molecule* mol;
     25  std::string lastName;
     26};
     27
     28const char ChangeMoleculeNameAction::NAME[] = "Change filename of Molecule";
    1729
    1830ChangeMoleculeNameAction::ChangeMoleculeNameAction(MoleculeListClass *_molecules) :
     
    2436{}
    2537
    26 void ChangeMoleculeNameAction::call() {
     38Action::state_ptr ChangeMoleculeNameAction::performCall() {
    2739  string filename;
    2840  molecule *mol = NULL;
    29   Dialog *dialog = UIFactory::get()->makeDialog();
     41  Dialog *dialog = UIFactory::getInstance().makeDialog();
    3042
    3143  dialog->queryMolecule("Enter index of molecule: ",&mol,molecules);
    3244  dialog->queryString("Enter name: ",&filename);
     45
    3346  if(dialog->display()) {
     47    string oldName = mol->getName();
    3448    mol->setName(filename);
     49    delete dialog;
     50    return Action::state_ptr(new ChangeMoleculeNameState(mol,oldName));
    3551  }
    36 
    3752  delete dialog;
     53  return Action::failure;
    3854}
    3955
    40 void ChangeMoleculeNameAction::undo() {
     56Action::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");
    4159
     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
     66Action::state_ptr ChangeMoleculeNameAction::performRedo(Action::state_ptr _state){
     67  // Undo and redo have to do the same for this action
     68  return performUndo(_state);
    4269}
    4370
    4471bool ChangeMoleculeNameAction::canUndo() {
    45   return false;
     72  return true;
    4673}
    4774
  • src/Actions/small_actions.hpp

    r66e95e rb31bc4  
    1414  virtual ~ChangeMoleculeNameAction();
    1515
    16   void call();
    17   void undo();
    1816  bool canUndo();
    1917  bool shouldUndo();
     
    2119  virtual const std::string getName();
    2220private:
     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
    2325  MoleculeListClass *molecules;
    24   static char NAME[];
     26  static const char NAME[];
    2527};
    2628
Note: See TracChangeset for help on using the changeset viewer.