| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [97ebf8] | 8 | /*
 | 
|---|
 | 9 |  * RemoveAction.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: May 9, 2010
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 21 | 
 | 
|---|
| [6f0841] | 22 | #include "Atom/atom.hpp"
 | 
|---|
 | 23 | #include "Atom/AtomicInfo.hpp"
 | 
|---|
| [97ebf8] | 24 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| [ad011c] | 25 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [d55743e] | 26 | #include "molecule.hpp"
 | 
|---|
| [ad011c] | 27 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [97ebf8] | 28 | #include "World.hpp"
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | #include <iostream>
 | 
|---|
 | 31 | #include <string>
 | 
|---|
 | 32 | 
 | 
|---|
| [1fd675] | 33 | #include "Actions/AtomAction/RemoveAction.hpp"
 | 
|---|
| [5cb3cb] | 34 | 
 | 
|---|
| [ce7fdc] | 35 | using namespace MoleCuilder;
 | 
|---|
 | 36 | 
 | 
|---|
| [1fd675] | 37 | // and construct the stuff
 | 
|---|
 | 38 | #include "RemoveAction.def"
 | 
|---|
 | 39 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 40 | /** =========== define the function ====================== */
 | 
|---|
| [5cb3cb] | 41 | Action::state_ptr AtomRemoveAction::performCall() {
 | 
|---|
| [e41c48] | 42 |   // create undo state
 | 
|---|
 | 43 |   std::vector<AtomicInfo> Walkers;
 | 
|---|
| [38f991] | 44 |   for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
 | 45 |       iter != World::getInstance().endAtomSelection();
 | 
|---|
 | 46 |       ++iter) {
 | 
|---|
| [e41c48] | 47 |     Walkers.push_back(AtomicInfo(*(iter->second)));
 | 
|---|
 | 48 |   }
 | 
|---|
| [1fd675] | 49 |   AtomRemoveState *UndoState = new AtomRemoveState(Walkers, params);
 | 
|---|
| [e41c48] | 50 | 
 | 
|---|
 | 51 |   // remove all selected atoms
 | 
|---|
| [38f991] | 52 |   for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
 | 53 |       iter != World::getInstance().endAtomSelection();
 | 
|---|
 | 54 |       iter = World::getInstance().beginAtomSelection()) {
 | 
|---|
| [47d041] | 55 |     LOG(1, "Removing atom " << (iter->second)->getId() << ".");
 | 
|---|
| [38f991] | 56 |     World::getInstance().destroyAtom((iter->second));
 | 
|---|
| [97ebf8] | 57 |   }
 | 
|---|
| [e41c48] | 58 |   return Action::state_ptr(UndoState);
 | 
|---|
| [97ebf8] | 59 | }
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | Action::state_ptr AtomRemoveAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| [e41c48] | 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
 | 
|---|
| [47d041] | 67 |     LOG(1, "Re-adding atom " << state->Walkers[i].getId() << ".");
 | 
|---|
| [e41c48] | 68 |     atom *Walker = World::getInstance().createAtom();
 | 
|---|
 | 69 |     if (!state->Walkers[i].setAtom(*Walker)) {
 | 
|---|
| [47d041] | 70 |       ELOG(1, "Failed to set id.");
 | 
|---|
| [e41c48] | 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);
 | 
|---|
| [97ebf8] | 83 | }
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 | Action::state_ptr AtomRemoveAction::performRedo(Action::state_ptr _state){
 | 
|---|
| [e41c48] | 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) {
 | 
|---|
| [47d041] | 90 |     LOG(1, "Re-removing atom " << state->Walkers[i].getId() << ".");
 | 
|---|
| [e41c48] | 91 |     World::getInstance().destroyAtom(state->Walkers[i].getId());
 | 
|---|
 | 92 |   }
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 |   return Action::state_ptr(_state);
 | 
|---|
| [97ebf8] | 95 | }
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 | bool AtomRemoveAction::canUndo() {
 | 
|---|
| [e41c48] | 98 |   return true;
 | 
|---|
| [97ebf8] | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | bool AtomRemoveAction::shouldUndo() {
 | 
|---|
| [e41c48] | 102 |   return true;
 | 
|---|
| [97ebf8] | 103 | }
 | 
|---|
| [1fd675] | 104 | /** =========== end of function ====================== */
 | 
|---|