| 1 | /*
 | 
|---|
| 2 |  * AddAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 9, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Actions/AtomAction/AddAction.hpp"
 | 
|---|
| 11 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 12 | #include "atom.hpp"
 | 
|---|
| 13 | #include "element.hpp"
 | 
|---|
| 14 | #include "Helpers/Log.hpp"
 | 
|---|
| 15 | #include "molecule.hpp"
 | 
|---|
| 16 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 17 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 18 | #include "World.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include <iostream>
 | 
|---|
| 21 | #include <string>
 | 
|---|
| 22 | 
 | 
|---|
| 23 | using namespace std;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 26 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 27 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | const char AtomAddAction::NAME[] = "add-atom";
 | 
|---|
| 30 | 
 | 
|---|
| 31 | AtomAddAction::AtomAddAction() :
 | 
|---|
| 32 |   Action(NAME)
 | 
|---|
| 33 | {}
 | 
|---|
| 34 | 
 | 
|---|
| 35 | AtomAddAction::~AtomAddAction()
 | 
|---|
| 36 | {}
 | 
|---|
| 37 | 
 | 
|---|
| 38 | void AtomAdd(element *elemental, Vector &position) {
 | 
|---|
| 39 |   ValueStorage::getInstance().setCurrentValue(AtomAddAction::NAME, elemental);
 | 
|---|
| 40 |   ValueStorage::getInstance().setCurrentValue("position", elemental);
 | 
|---|
| 41 |   ActionRegistry::getInstance().getActionByName(AtomAddAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 42 | };
 | 
|---|
| 43 | 
 | 
|---|
| 44 | Dialog * AtomAddAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 45 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 46 | 
 | 
|---|
| 47 |   dialog->queryElement(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 48 |   dialog->queryVector("position", true, ValueStorage::getInstance().getDescription("position"));
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   return dialog;
 | 
|---|
| 51 | }
 | 
|---|
| 52 | 
 | 
|---|
| 53 | Action::state_ptr AtomAddAction::performCall() {
 | 
|---|
| 54 |   const element * elemental = NULL;
 | 
|---|
| 55 |   Vector position;
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   // obtain information
 | 
|---|
| 58 |   ValueStorage::getInstance().queryCurrentValue(NAME, elemental);
 | 
|---|
| 59 |   ValueStorage::getInstance().queryCurrentValue("position", position);
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   // execute action
 | 
|---|
| 62 |   atom * first = World::getInstance().createAtom();
 | 
|---|
| 63 |   first->setType(elemental);
 | 
|---|
| 64 |   first->setPosition(position);
 | 
|---|
| 65 |   DoLog(1) && (Log() << Verbose(1) << "Adding new atom with element " << first->getType()->getName() << " at " << (first->getPosition()) << "." << endl);
 | 
|---|
| 66 |   // TODO: remove when all of World's atoms are stored.
 | 
|---|
| 67 |   std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
 | 
|---|
| 68 |   if (!molecules.empty()) {
 | 
|---|
| 69 |     std::vector<molecule *>::iterator iter = molecules.begin();
 | 
|---|
| 70 |     (*iter)->AddAtom(first);
 | 
|---|
| 71 |   }
 | 
|---|
| 72 |   return Action::success;
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | Action::state_ptr AtomAddAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 76 | //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
 | 
|---|
| 77 | 
 | 
|---|
| 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));
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | Action::state_ptr AtomAddAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 86 |   return Action::failure;
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | bool AtomAddAction::canUndo() {
 | 
|---|
| 90 |   return false;
 | 
|---|
| 91 | }
 | 
|---|
| 92 | 
 | 
|---|
| 93 | bool AtomAddAction::shouldUndo() {
 | 
|---|
| 94 |   return false;
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | const string AtomAddAction::getName() {
 | 
|---|
| 98 |   return NAME;
 | 
|---|
| 99 | }
 | 
|---|