| [bcf653] | 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 | 
 | 
|---|
| [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 | 
 | 
|---|
| [97ebf8] | 22 | #include "atom.hpp"
 | 
|---|
| [e41c48] | 23 | #include "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 | 
 | 
|---|
 | 33 | using namespace std;
 | 
|---|
 | 34 | 
 | 
|---|
| [1fd675] | 35 | #include "Actions/AtomAction/RemoveAction.hpp"
 | 
|---|
| [5cb3cb] | 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() {
 | 
|---|
 | 42 |   atom *first = NULL;
 | 
|---|
| [97ebf8] | 43 | 
 | 
|---|
| [e41c48] | 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 |   }
 | 
|---|
| [1fd675] | 49 |   AtomRemoveState *UndoState = new AtomRemoveState(Walkers, params);
 | 
|---|
| [e41c48] | 50 | 
 | 
|---|
 | 51 |   // remove all selected atoms
 | 
|---|
 | 52 | //  std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
 | 
|---|
| [5cb3cb] | 53 |   for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) {
 | 
|---|
 | 54 |     first = iter->second;
 | 
|---|
| [97ebf8] | 55 |     DoLog(1) && (Log() << Verbose(1) << "Removing atom " << first->getId() << "." << endl);
 | 
|---|
| [e41c48] | 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 | //    }
 | 
|---|
| [97ebf8] | 61 |     World::getInstance().destroyAtom(first);
 | 
|---|
 | 62 |   }
 | 
|---|
| [e41c48] | 63 |   return Action::state_ptr(UndoState);
 | 
|---|
| [97ebf8] | 64 | }
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 | Action::state_ptr AtomRemoveAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| [e41c48] | 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);
 | 
|---|
| [97ebf8] | 88 | }
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 | Action::state_ptr AtomRemoveAction::performRedo(Action::state_ptr _state){
 | 
|---|
| [e41c48] | 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);
 | 
|---|
| [97ebf8] | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 | bool AtomRemoveAction::canUndo() {
 | 
|---|
| [e41c48] | 103 |   return true;
 | 
|---|
| [97ebf8] | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | bool AtomRemoveAction::shouldUndo() {
 | 
|---|
| [e41c48] | 107 |   return true;
 | 
|---|
| [97ebf8] | 108 | }
 | 
|---|
| [1fd675] | 109 | /** =========== end of function ====================== */
 | 
|---|