Ignore:
File:
1 edited

Legend:

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

    re5c0a1 rea2830  
    1010#include "Actions/AtomAction/AddAction.hpp"
    1111#include "Actions/ActionRegistry.hpp"
     12#include "Descriptors/AtomIdDescriptor.hpp"
    1213#include "atom.hpp"
    1314#include "element.hpp"
    14 #include "log.hpp"
     15#include "Helpers/Log.hpp"
    1516#include "molecule.hpp"
    16 #include "vector.hpp"
    17 #include "verbose.hpp"
     17#include "LinearAlgebra/Vector.hpp"
     18#include "Helpers/Verbose.hpp"
    1819#include "World.hpp"
    1920
     
    2526#include "UIElements/UIFactory.hpp"
    2627#include "UIElements/Dialog.hpp"
    27 #include "UIElements/ValueStorage.hpp"
     28#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};
    2843
    2944const char AtomAddAction::NAME[] = "add-atom";
     
    6176  // execute action
    6277  atom * first = World::getInstance().createAtom();
    63   first->type = elemental;
    64   first->x = position;
    65   DoLog(1) && (Log() << Verbose(1) << "Adding new atom with element " << *first->type << " at " << (first->x) << "." << endl);
     78  first->setType(elemental);
     79  first->setPosition(position);
     80  DoLog(1) && (Log() << Verbose(1) << "Adding new atom with element " << first->getType()->getName() << " at " << (first->getPosition()) << "." << endl);
    6681  // TODO: remove when all of World's atoms are stored.
    6782  std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
     
    7085    (*iter)->AddAtom(first);
    7186  }
    72   return Action::success;
     87  return Action::state_ptr(new AtomAddState(position, elemental, first->getId()));
    7388}
    7489
    7590Action::state_ptr AtomAddAction::performUndo(Action::state_ptr _state) {
    76 //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
     91  AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
    7792
    78   return Action::failure;
    79 //  string newName = state->mol->getName();
    80 //  state->mol->setName(state->lastName);
    81 //
    82 //  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);
    8397}
    8498
    8599Action::state_ptr AtomAddAction::performRedo(Action::state_ptr _state){
    86   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);
    87116}
    88117
    89118bool AtomAddAction::canUndo() {
    90   return false;
     119  return true;
    91120}
    92121
    93122bool AtomAddAction::shouldUndo() {
    94   return false;
     123  return true;
    95124}
    96125
Note: See TracChangeset for help on using the changeset viewer.