| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 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 "CodePatterns/MemDebug.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include "Atom/atom.hpp" | 
|---|
| 23 | #include "Atom/AtomicInfo.hpp" | 
|---|
| 24 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
| 25 | #include "CodePatterns/Log.hpp" | 
|---|
| 26 | #include "molecule.hpp" | 
|---|
| 27 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 28 | #include "World.hpp" | 
|---|
| 29 |  | 
|---|
| 30 | #include <iostream> | 
|---|
| 31 | #include <string> | 
|---|
| 32 |  | 
|---|
| 33 | #include "Actions/AtomAction/RemoveAction.hpp" | 
|---|
| 34 |  | 
|---|
| 35 | using namespace MoleCuilder; | 
|---|
| 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 | // create undo state | 
|---|
| 43 | std::vector<AtomicInfo> Walkers; | 
|---|
| 44 | for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection(); | 
|---|
| 45 | iter != World::getInstance().endAtomSelection(); | 
|---|
| 46 | ++iter) { | 
|---|
| 47 | Walkers.push_back(AtomicInfo(*(iter->second))); | 
|---|
| 48 | } | 
|---|
| 49 | AtomRemoveState *UndoState = new AtomRemoveState(Walkers, params); | 
|---|
| 50 |  | 
|---|
| 51 | // remove all selected atoms | 
|---|
| 52 | for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection(); | 
|---|
| 53 | iter != World::getInstance().endAtomSelection(); | 
|---|
| 54 | iter = World::getInstance().beginAtomSelection()) { | 
|---|
| 55 | LOG(1, "Removing atom " << (iter->second)->getId() << "."); | 
|---|
| 56 | World::getInstance().destroyAtom((iter->second)); | 
|---|
| 57 | } | 
|---|
| 58 | return Action::state_ptr(UndoState); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | Action::state_ptr AtomRemoveAction::performUndo(Action::state_ptr _state) { | 
|---|
| 62 | AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get()); | 
|---|
| 63 |  | 
|---|
| 64 | size_t i=0; | 
|---|
| 65 | for (; i<state->Walkers.size(); ++i) { | 
|---|
| 66 | // re-create the atom | 
|---|
| 67 | LOG(1, "Re-adding atom " << state->Walkers[i].getId() << "."); | 
|---|
| 68 | atom *Walker = World::getInstance().createAtom(); | 
|---|
| 69 | if (!state->Walkers[i].setAtom(*Walker)) { | 
|---|
| 70 | ELOG(1, "Failed to set id."); | 
|---|
| 71 | World::getInstance().destroyAtom(Walker); | 
|---|
| 72 | break; | 
|---|
| 73 | } | 
|---|
| 74 | } | 
|---|
| 75 | if (i<state->Walkers.size()) { | 
|---|
| 76 | // remove all previous ones, too | 
|---|
| 77 | for (size_t j=0;j<i;++j) | 
|---|
| 78 | World::getInstance().destroyAtom(state->Walkers[j].getId()); | 
|---|
| 79 | // and announce the failure of the undo | 
|---|
| 80 | return Action::failure; | 
|---|
| 81 | } | 
|---|
| 82 | return Action::state_ptr(_state); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | Action::state_ptr AtomRemoveAction::performRedo(Action::state_ptr _state){ | 
|---|
| 86 | AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get()); | 
|---|
| 87 |  | 
|---|
| 88 | // simple remove again all previously added atoms | 
|---|
| 89 | for (size_t i=0; i<state->Walkers.size(); ++i) { | 
|---|
| 90 | LOG(1, "Re-removing atom " << state->Walkers[i].getId() << "."); | 
|---|
| 91 | World::getInstance().destroyAtom(state->Walkers[i].getId()); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | return Action::state_ptr(_state); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | bool AtomRemoveAction::canUndo() { | 
|---|
| 98 | return true; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | bool AtomRemoveAction::shouldUndo() { | 
|---|
| 102 | return true; | 
|---|
| 103 | } | 
|---|
| 104 | /** =========== end of function ====================== */ | 
|---|