Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/AtomAction/AddAction.cpp

    rbf3817 rea2830  
    66 */
    77
    8 // include config.h
    9 #ifdef HAVE_CONFIG_H
    10 #include <config.h>
    11 #endif
    12 
    138#include "Helpers/MemDebug.hpp"
    149
    1510#include "Actions/AtomAction/AddAction.hpp"
    1611#include "Actions/ActionRegistry.hpp"
     12#include "Descriptors/AtomIdDescriptor.hpp"
    1713#include "atom.hpp"
    1814#include "element.hpp"
     
    3127#include "UIElements/Dialog.hpp"
    3228#include "Actions/ValueStorage.hpp"
     29
     30// memento to remember the state when undoing
     31
     32class AtomAddState : public ActionState {
     33public:
     34  AtomAddState(const Vector &_position, const element *_elemental, const atomId_t _id) :
     35    position(_position),
     36    elemental(_elemental),
     37    id(_id)
     38  {}
     39  Vector position;
     40  const element *elemental;
     41  atomId_t id;
     42};
    3343
    3444const char AtomAddAction::NAME[] = "add-atom";
     
    7585    (*iter)->AddAtom(first);
    7686  }
    77   return Action::success;
     87  return Action::state_ptr(new AtomAddState(position, elemental, first->getId()));
    7888}
    7989
    8090Action::state_ptr AtomAddAction::performUndo(Action::state_ptr _state) {
    81 //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
     91  AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
    8292
    83   return Action::failure;
    84 //  string newName = state->mol->getName();
    85 //  state->mol->setName(state->lastName);
    86 //
    87 //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
     93  DoLog(1) && (Log() << Verbose(1) << "Removing atom with id " << state->id << "." << endl);
     94  World::getInstance().destroyAtom(state->id);
     95
     96  return Action::state_ptr(_state);
    8897}
    8998
    9099Action::state_ptr AtomAddAction::performRedo(Action::state_ptr _state){
    91   return Action::failure;
     100  AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
     101
     102  atom * first = World::getInstance().createAtom();
     103  first->setType(state->elemental);
     104  first->setPosition(state->position);
     105  DoLog(1) && (Log() << Verbose(1) << "Re-adding new atom with element " << state->elemental->getName() << " at " << state->position << "." << endl);
     106  // TODO: remove when all of World's atoms are stored.
     107  std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
     108  if (!molecules.empty()) {
     109    std::vector<molecule *>::iterator iter = molecules.begin();
     110    (*iter)->AddAtom(first);
     111  }
     112  if (first->getId() != state->id)
     113    if (!first->changeId(state->id))
     114      return Action::failure;
     115  return Action::state_ptr(_state);
    92116}
    93117
    94118bool AtomAddAction::canUndo() {
    95   return false;
     119  return true;
    96120}
    97121
    98122bool AtomAddAction::shouldUndo() {
    99   return false;
     123  return true;
    100124}
    101125
Note: See TracChangeset for help on using the changeset viewer.