| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * RemoveAction.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: May 9, 2010 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include "Helpers/MemDebug.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include "atom.hpp" | 
|---|
| 23 | #include "AtomicInfo.hpp" | 
|---|
| 24 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
| 25 | #include "Helpers/Log.hpp" | 
|---|
| 26 | #include "molecule.hpp" | 
|---|
| 27 | #include "Helpers/Verbose.hpp" | 
|---|
| 28 | #include "World.hpp" | 
|---|
| 29 |  | 
|---|
| 30 | #include <iostream> | 
|---|
| 31 | #include <string> | 
|---|
| 32 |  | 
|---|
| 33 | using namespace std; | 
|---|
| 34 |  | 
|---|
| 35 | #include "Actions/AtomAction/RemoveAction.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | // and construct the stuff | 
|---|
| 38 | #include "RemoveAction.def" | 
|---|
| 39 | #include "Action_impl_pre.hpp" | 
|---|
| 40 | /** =========== define the function ====================== */ | 
|---|
| 41 | Action::state_ptr AtomRemoveAction::performCall() { | 
|---|
| 42 | atom *first = NULL; | 
|---|
| 43 |  | 
|---|
| 44 | // create undo state | 
|---|
| 45 | std::vector<AtomicInfo> Walkers; | 
|---|
| 46 | for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) { | 
|---|
| 47 | Walkers.push_back(AtomicInfo(*(iter->second))); | 
|---|
| 48 | } | 
|---|
| 49 | AtomRemoveState *UndoState = new AtomRemoveState(Walkers, params); | 
|---|
| 50 |  | 
|---|
| 51 | // remove all selected atoms | 
|---|
| 52 | //  std::vector<molecule *> molecules = World::getInstance().getAllMolecules(); | 
|---|
| 53 | for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) { | 
|---|
| 54 | first = iter->second; | 
|---|
| 55 | DoLog(1) && (Log() << Verbose(1) << "Removing atom " << first->getId() << "." << endl); | 
|---|
| 56 | //    // TODO: this is not necessary when atoms and their storing to file are handled by the World | 
|---|
| 57 | //    // simply try to erase in every molecule found | 
|---|
| 58 | //    for (std::vector<molecule *>::iterator iter = molecules.begin();iter != molecules.end(); ++iter) { | 
|---|
| 59 | //      (*iter)->erase(first); | 
|---|
| 60 | //    } | 
|---|
| 61 | World::getInstance().destroyAtom(first); | 
|---|
| 62 | } | 
|---|
| 63 | return Action::state_ptr(UndoState); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | Action::state_ptr AtomRemoveAction::performUndo(Action::state_ptr _state) { | 
|---|
| 67 | AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get()); | 
|---|
| 68 |  | 
|---|
| 69 | size_t i=0; | 
|---|
| 70 | for (; i<state->Walkers.size(); ++i) { | 
|---|
| 71 | // re-create the atom | 
|---|
| 72 | DoLog(1) && (Log() << Verbose(1) << "Re-adding atom " << state->Walkers[i].getId() << "." << endl); | 
|---|
| 73 | atom *Walker = World::getInstance().createAtom(); | 
|---|
| 74 | if (!state->Walkers[i].setAtom(*Walker)) { | 
|---|
| 75 | DoeLog(1) && (eLog() << Verbose(1) << "Failed to set id." << endl); | 
|---|
| 76 | World::getInstance().destroyAtom(Walker); | 
|---|
| 77 | break; | 
|---|
| 78 | } | 
|---|
| 79 | } | 
|---|
| 80 | if (i<state->Walkers.size()) { | 
|---|
| 81 | // remove all previous ones, too | 
|---|
| 82 | for (size_t j=0;j<i;++j) | 
|---|
| 83 | World::getInstance().destroyAtom(state->Walkers[j].getId()); | 
|---|
| 84 | // and announce the failure of the undo | 
|---|
| 85 | return Action::failure; | 
|---|
| 86 | } | 
|---|
| 87 | return Action::state_ptr(_state); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | Action::state_ptr AtomRemoveAction::performRedo(Action::state_ptr _state){ | 
|---|
| 91 | AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get()); | 
|---|
| 92 |  | 
|---|
| 93 | // simple remove again all previously added atoms | 
|---|
| 94 | for (size_t i=0; i<state->Walkers.size(); ++i) { | 
|---|
| 95 | DoLog(1) && (Log() << Verbose(1) << "Re-removing atom " << state->Walkers[i].getId() << "." << endl); | 
|---|
| 96 | World::getInstance().destroyAtom(state->Walkers[i].getId()); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | return Action::state_ptr(_state); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | bool AtomRemoveAction::canUndo() { | 
|---|
| 103 | return true; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | bool AtomRemoveAction::shouldUndo() { | 
|---|
| 107 | return true; | 
|---|
| 108 | } | 
|---|
| 109 | /** =========== end of function ====================== */ | 
|---|