| [97ebf8] | 1 | /* | 
|---|
|  | 2 | * RemoveAction.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/RemoveAction.hpp" | 
|---|
| [0430e3] | 16 | #include "Actions/ActionRegistry.hpp" | 
|---|
| [97ebf8] | 17 | #include "atom.hpp" | 
|---|
| [e41c48] | 18 | #include "AtomicInfo.hpp" | 
|---|
| [97ebf8] | 19 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
| [952f38] | 20 | #include "Helpers/Log.hpp" | 
|---|
| [d55743e] | 21 | #include "molecule.hpp" | 
|---|
| [952f38] | 22 | #include "Helpers/Verbose.hpp" | 
|---|
| [97ebf8] | 23 | #include "World.hpp" | 
|---|
|  | 24 |  | 
|---|
|  | 25 | #include <iostream> | 
|---|
|  | 26 | #include <string> | 
|---|
|  | 27 |  | 
|---|
|  | 28 | using namespace std; | 
|---|
|  | 29 |  | 
|---|
|  | 30 | #include "UIElements/UIFactory.hpp" | 
|---|
|  | 31 | #include "UIElements/Dialog.hpp" | 
|---|
| [861874] | 32 | #include "Actions/ValueStorage.hpp" | 
|---|
| [97ebf8] | 33 |  | 
|---|
| [e41c48] | 34 | // memento to remember the state when undoing | 
|---|
|  | 35 |  | 
|---|
|  | 36 | class AtomRemoveState : public ActionState { | 
|---|
|  | 37 | public: | 
|---|
|  | 38 | AtomRemoveState(std::vector<AtomicInfo> _Walkers) : | 
|---|
|  | 39 | Walkers(_Walkers) | 
|---|
|  | 40 | {} | 
|---|
|  | 41 | std::vector<AtomicInfo> Walkers; | 
|---|
|  | 42 | }; | 
|---|
|  | 43 |  | 
|---|
| [97ebf8] | 44 | const char AtomRemoveAction::NAME[] = "remove-atom"; | 
|---|
|  | 45 |  | 
|---|
|  | 46 | AtomRemoveAction::AtomRemoveAction() : | 
|---|
|  | 47 | Action(NAME) | 
|---|
|  | 48 | {} | 
|---|
|  | 49 |  | 
|---|
|  | 50 | AtomRemoveAction::~AtomRemoveAction() | 
|---|
|  | 51 | {} | 
|---|
|  | 52 |  | 
|---|
| [1a8fbf6] | 53 | void AtomRemove() { | 
|---|
|  | 54 | ActionRegistry::getInstance().getActionByName(AtomRemoveAction::NAME)->call(Action::NonInteractive); | 
|---|
|  | 55 | }; | 
|---|
|  | 56 |  | 
|---|
| [047878] | 57 | Dialog* AtomRemoveAction::fillDialog(Dialog *dialog) { | 
|---|
|  | 58 | ASSERT(dialog,"No Dialog given when filling action dialog"); | 
|---|
| [97ebf8] | 59 |  | 
|---|
| [5cb3cb] | 60 | dialog->queryEmpty(NAME, ValueStorage::getInstance().getDescription(NAME)); | 
|---|
|  | 61 |  | 
|---|
|  | 62 | return dialog; | 
|---|
|  | 63 | } | 
|---|
|  | 64 |  | 
|---|
|  | 65 | Action::state_ptr AtomRemoveAction::performCall() { | 
|---|
|  | 66 | atom *first = NULL; | 
|---|
| [97ebf8] | 67 |  | 
|---|
| [e41c48] | 68 | // create undo state | 
|---|
|  | 69 | std::vector<AtomicInfo> Walkers; | 
|---|
|  | 70 | for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) { | 
|---|
|  | 71 | Walkers.push_back(AtomicInfo(*(iter->second))); | 
|---|
|  | 72 | } | 
|---|
|  | 73 | AtomRemoveState *UndoState = new AtomRemoveState(Walkers); | 
|---|
|  | 74 |  | 
|---|
|  | 75 | // remove all selected atoms | 
|---|
|  | 76 | //  std::vector<molecule *> molecules = World::getInstance().getAllMolecules(); | 
|---|
| [5cb3cb] | 77 | for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) { | 
|---|
|  | 78 | first = iter->second; | 
|---|
| [97ebf8] | 79 | DoLog(1) && (Log() << Verbose(1) << "Removing atom " << first->getId() << "." << endl); | 
|---|
| [e41c48] | 80 | //    // TODO: this is not necessary when atoms and their storing to file are handled by the World | 
|---|
|  | 81 | //    // simply try to erase in every molecule found | 
|---|
|  | 82 | //    for (std::vector<molecule *>::iterator iter = molecules.begin();iter != molecules.end(); ++iter) { | 
|---|
|  | 83 | //      (*iter)->erase(first); | 
|---|
|  | 84 | //    } | 
|---|
| [97ebf8] | 85 | World::getInstance().destroyAtom(first); | 
|---|
|  | 86 | } | 
|---|
| [e41c48] | 87 | return Action::state_ptr(UndoState); | 
|---|
| [97ebf8] | 88 | } | 
|---|
|  | 89 |  | 
|---|
|  | 90 | Action::state_ptr AtomRemoveAction::performUndo(Action::state_ptr _state) { | 
|---|
| [e41c48] | 91 | AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get()); | 
|---|
|  | 92 |  | 
|---|
|  | 93 | size_t i=0; | 
|---|
|  | 94 | for (; i<state->Walkers.size(); ++i) { | 
|---|
|  | 95 | // re-create the atom | 
|---|
|  | 96 | DoLog(1) && (Log() << Verbose(1) << "Re-adding atom " << state->Walkers[i].getId() << "." << endl); | 
|---|
|  | 97 | atom *Walker = World::getInstance().createAtom(); | 
|---|
|  | 98 | if (!state->Walkers[i].setAtom(*Walker)) { | 
|---|
|  | 99 | DoeLog(1) && (eLog() << Verbose(1) << "Failed to set id." << endl); | 
|---|
|  | 100 | World::getInstance().destroyAtom(Walker); | 
|---|
|  | 101 | break; | 
|---|
|  | 102 | } | 
|---|
|  | 103 | } | 
|---|
|  | 104 | if (i<state->Walkers.size()) { | 
|---|
|  | 105 | // remove all previous ones, too | 
|---|
|  | 106 | for (size_t j=0;j<i;++j) | 
|---|
|  | 107 | World::getInstance().destroyAtom(state->Walkers[j].getId()); | 
|---|
|  | 108 | // and announce the failure of the undo | 
|---|
|  | 109 | return Action::failure; | 
|---|
|  | 110 | } | 
|---|
|  | 111 | return Action::state_ptr(_state); | 
|---|
| [97ebf8] | 112 | } | 
|---|
|  | 113 |  | 
|---|
|  | 114 | Action::state_ptr AtomRemoveAction::performRedo(Action::state_ptr _state){ | 
|---|
| [e41c48] | 115 | AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get()); | 
|---|
|  | 116 |  | 
|---|
|  | 117 | // simple remove again all previously added atoms | 
|---|
|  | 118 | for (size_t i=0; i<state->Walkers.size(); ++i) { | 
|---|
|  | 119 | DoLog(1) && (Log() << Verbose(1) << "Re-removing atom " << state->Walkers[i].getId() << "." << endl); | 
|---|
|  | 120 | World::getInstance().destroyAtom(state->Walkers[i].getId()); | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | return Action::state_ptr(_state); | 
|---|
| [97ebf8] | 124 | } | 
|---|
|  | 125 |  | 
|---|
|  | 126 | bool AtomRemoveAction::canUndo() { | 
|---|
| [e41c48] | 127 | return true; | 
|---|
| [97ebf8] | 128 | } | 
|---|
|  | 129 |  | 
|---|
|  | 130 | bool AtomRemoveAction::shouldUndo() { | 
|---|
| [e41c48] | 131 | return true; | 
|---|
| [97ebf8] | 132 | } | 
|---|
|  | 133 |  | 
|---|
|  | 134 | const string AtomRemoveAction::getName() { | 
|---|
|  | 135 | return NAME; | 
|---|
|  | 136 | } | 
|---|