| 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 "Actions/UndoRedoHelpers.hpp"
 | 
|---|
| 23 | #include "Atom/atom.hpp"
 | 
|---|
| 24 | #include "Atom/AtomicInfo.hpp"
 | 
|---|
| 25 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 26 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 27 | #include "molecule.hpp"
 | 
|---|
| 28 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 29 | #include "World.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #include <iostream>
 | 
|---|
| 32 | #include <string>
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #include "Actions/AtomAction/RemoveAction.hpp"
 | 
|---|
| 35 | 
 | 
|---|
| 36 | using namespace MoleCuilder;
 | 
|---|
| 37 | 
 | 
|---|
| 38 | // and construct the stuff
 | 
|---|
| 39 | #include "RemoveAction.def"
 | 
|---|
| 40 | #include "Action_impl_pre.hpp"
 | 
|---|
| 41 | /** =========== define the function ====================== */
 | 
|---|
| 42 | Action::state_ptr AtomRemoveAction::performCall() {
 | 
|---|
| 43 |   // create undo state
 | 
|---|
| 44 |   std::vector<AtomicInfo> Walkers;
 | 
|---|
| 45 |   for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
| 46 |       iter != World::getInstance().endAtomSelection();
 | 
|---|
| 47 |       ++iter) {
 | 
|---|
| 48 |     Walkers.push_back(AtomicInfo(*(iter->second)));
 | 
|---|
| 49 |   }
 | 
|---|
| 50 |   AtomRemoveState *UndoState = new AtomRemoveState(Walkers, params);
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   // remove all selected atoms
 | 
|---|
| 53 |   for (World::AtomSelectionConstIterator iter = World::getInstance().beginAtomSelection();
 | 
|---|
| 54 |       iter != World::getInstance().endAtomSelection();
 | 
|---|
| 55 |       iter = World::getInstance().beginAtomSelection()) {
 | 
|---|
| 56 |     LOG(1, "Removing atom " << (iter->second)->getId() << ".");
 | 
|---|
| 57 |     World::getInstance().destroyAtom((iter->second));
 | 
|---|
| 58 |   }
 | 
|---|
| 59 |   return Action::state_ptr(UndoState);
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | Action::state_ptr AtomRemoveAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 63 |   AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get());
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   // add all removed atoms again
 | 
|---|
| 66 |   if (AddAtomsFromAtomicInfo(state->Walkers))
 | 
|---|
| 67 |     return Action::state_ptr(_state);
 | 
|---|
| 68 |   else
 | 
|---|
| 69 |     return Action::failure;
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | Action::state_ptr AtomRemoveAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 73 |   AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get());
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   // simple remove again all previously added atoms
 | 
|---|
| 76 |   RemoveAtomsFromAtomicInfo(state->Walkers);
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   return Action::state_ptr(_state);
 | 
|---|
| 79 | }
 | 
|---|
| 80 | 
 | 
|---|
| 81 | bool AtomRemoveAction::canUndo() {
 | 
|---|
| 82 |   return true;
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | bool AtomRemoveAction::shouldUndo() {
 | 
|---|
| 86 |   return true;
 | 
|---|
| 87 | }
 | 
|---|
| 88 | /** =========== end of function ====================== */
 | 
|---|