[97ebf8] | 1 | /*
|
---|
| 2 | * AddAction.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: May 9, 2010
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[112b09] | 13 | #include "Helpers/MemDebug.hpp"
|
---|
| 14 |
|
---|
[97ebf8] | 15 | #include "Actions/AtomAction/AddAction.hpp"
|
---|
[0430e3] | 16 | #include "Actions/ActionRegistry.hpp"
|
---|
[ea2830] | 17 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[97ebf8] | 18 | #include "atom.hpp"
|
---|
| 19 | #include "element.hpp"
|
---|
[952f38] | 20 | #include "Helpers/Log.hpp"
|
---|
[f0a3ec] | 21 | #include "molecule.hpp"
|
---|
[57f243] | 22 | #include "LinearAlgebra/Vector.hpp"
|
---|
[952f38] | 23 | #include "Helpers/Verbose.hpp"
|
---|
[97ebf8] | 24 | #include "World.hpp"
|
---|
| 25 |
|
---|
| 26 | #include <iostream>
|
---|
| 27 | #include <string>
|
---|
| 28 |
|
---|
| 29 | using namespace std;
|
---|
| 30 |
|
---|
| 31 | #include "UIElements/UIFactory.hpp"
|
---|
| 32 | #include "UIElements/Dialog.hpp"
|
---|
[861874] | 33 | #include "Actions/ValueStorage.hpp"
|
---|
[97ebf8] | 34 |
|
---|
[ea2830] | 35 | // memento to remember the state when undoing
|
---|
| 36 |
|
---|
| 37 | class AtomAddState : public ActionState {
|
---|
| 38 | public:
|
---|
| 39 | AtomAddState(const Vector &_position, const element *_elemental, const atomId_t _id) :
|
---|
| 40 | position(_position),
|
---|
| 41 | elemental(_elemental),
|
---|
| 42 | id(_id)
|
---|
| 43 | {}
|
---|
| 44 | Vector position;
|
---|
| 45 | const element *elemental;
|
---|
| 46 | atomId_t id;
|
---|
| 47 | };
|
---|
| 48 |
|
---|
[97ebf8] | 49 | const char AtomAddAction::NAME[] = "add-atom";
|
---|
| 50 |
|
---|
| 51 | AtomAddAction::AtomAddAction() :
|
---|
| 52 | Action(NAME)
|
---|
| 53 | {}
|
---|
| 54 |
|
---|
| 55 | AtomAddAction::~AtomAddAction()
|
---|
| 56 | {}
|
---|
| 57 |
|
---|
[1a8fbf6] | 58 | void AtomAdd(element *elemental, Vector &position) {
|
---|
| 59 | ValueStorage::getInstance().setCurrentValue(AtomAddAction::NAME, elemental);
|
---|
| 60 | ValueStorage::getInstance().setCurrentValue("position", elemental);
|
---|
| 61 | ActionRegistry::getInstance().getActionByName(AtomAddAction::NAME)->call(Action::NonInteractive);
|
---|
| 62 | };
|
---|
| 63 |
|
---|
[047878] | 64 | Dialog * AtomAddAction::fillDialog(Dialog *dialog) {
|
---|
| 65 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
[9d33ba] | 66 |
|
---|
| 67 | dialog->queryElement(NAME, ValueStorage::getInstance().getDescription(NAME));
|
---|
| 68 | dialog->queryVector("position", true, ValueStorage::getInstance().getDescription("position"));
|
---|
| 69 |
|
---|
| 70 | return dialog;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | Action::state_ptr AtomAddAction::performCall() {
|
---|
[e5c0a1] | 74 | const element * elemental = NULL;
|
---|
[97ebf8] | 75 | Vector position;
|
---|
| 76 |
|
---|
[9d33ba] | 77 | // obtain information
|
---|
[85537a] | 78 | ValueStorage::getInstance().queryCurrentValue(NAME, elemental);
|
---|
[9d33ba] | 79 | ValueStorage::getInstance().queryCurrentValue("position", position);
|
---|
| 80 |
|
---|
| 81 | // execute action
|
---|
[85537a] | 82 | atom * first = World::getInstance().createAtom();
|
---|
[d74077] | 83 | first->setType(elemental);
|
---|
| 84 | first->setPosition(position);
|
---|
[b5c53d] | 85 | DoLog(1) && (Log() << Verbose(1) << "Adding new atom with element " << first->getType()->getName() << " at " << (first->getPosition()) << "." << endl);
|
---|
[85537a] | 86 | // TODO: remove when all of World's atoms are stored.
|
---|
| 87 | std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
|
---|
| 88 | if (!molecules.empty()) {
|
---|
| 89 | std::vector<molecule *>::iterator iter = molecules.begin();
|
---|
| 90 | (*iter)->AddAtom(first);
|
---|
[97ebf8] | 91 | }
|
---|
[ea2830] | 92 | return Action::state_ptr(new AtomAddState(position, elemental, first->getId()));
|
---|
[97ebf8] | 93 | }
|
---|
| 94 |
|
---|
| 95 | Action::state_ptr AtomAddAction::performUndo(Action::state_ptr _state) {
|
---|
[ea2830] | 96 | AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
|
---|
[97ebf8] | 97 |
|
---|
[ea2830] | 98 | DoLog(1) && (Log() << Verbose(1) << "Removing atom with id " << state->id << "." << endl);
|
---|
| 99 | World::getInstance().destroyAtom(state->id);
|
---|
| 100 |
|
---|
| 101 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 102 | }
|
---|
| 103 |
|
---|
| 104 | Action::state_ptr AtomAddAction::performRedo(Action::state_ptr _state){
|
---|
[ea2830] | 105 | AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
|
---|
| 106 |
|
---|
| 107 | atom * first = World::getInstance().createAtom();
|
---|
| 108 | first->setType(state->elemental);
|
---|
| 109 | first->setPosition(state->position);
|
---|
| 110 | DoLog(1) && (Log() << Verbose(1) << "Re-adding new atom with element " << state->elemental->getName() << " at " << state->position << "." << endl);
|
---|
| 111 | // TODO: remove when all of World's atoms are stored.
|
---|
| 112 | std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
|
---|
| 113 | if (!molecules.empty()) {
|
---|
| 114 | std::vector<molecule *>::iterator iter = molecules.begin();
|
---|
| 115 | (*iter)->AddAtom(first);
|
---|
| 116 | }
|
---|
| 117 | if (first->getId() != state->id)
|
---|
| 118 | if (!first->changeId(state->id))
|
---|
| 119 | return Action::failure;
|
---|
| 120 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 121 | }
|
---|
| 122 |
|
---|
| 123 | bool AtomAddAction::canUndo() {
|
---|
[ea2830] | 124 | return true;
|
---|
[97ebf8] | 125 | }
|
---|
| 126 |
|
---|
| 127 | bool AtomAddAction::shouldUndo() {
|
---|
[ea2830] | 128 | return true;
|
---|
[97ebf8] | 129 | }
|
---|
| 130 |
|
---|
| 131 | const string AtomAddAction::getName() {
|
---|
| 132 | return NAME;
|
---|
| 133 | }
|
---|