[97ebf8] | 1 | /*
|
---|
| 2 | * RemoveAction.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: May 9, 2010
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[112b09] | 8 | #include "Helpers/MemDebug.hpp"
|
---|
| 9 |
|
---|
[97ebf8] | 10 | #include "Actions/AtomAction/RemoveAction.hpp"
|
---|
[0430e3] | 11 | #include "Actions/ActionRegistry.hpp"
|
---|
[97ebf8] | 12 | #include "atom.hpp"
|
---|
[e41c48] | 13 | #include "AtomicInfo.hpp"
|
---|
[97ebf8] | 14 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
[952f38] | 15 | #include "Helpers/Log.hpp"
|
---|
[d55743e] | 16 | #include "molecule.hpp"
|
---|
[952f38] | 17 | #include "Helpers/Verbose.hpp"
|
---|
[97ebf8] | 18 | #include "World.hpp"
|
---|
| 19 |
|
---|
| 20 | #include <iostream>
|
---|
| 21 | #include <string>
|
---|
| 22 |
|
---|
| 23 | using namespace std;
|
---|
| 24 |
|
---|
| 25 | #include "UIElements/UIFactory.hpp"
|
---|
| 26 | #include "UIElements/Dialog.hpp"
|
---|
[861874] | 27 | #include "Actions/ValueStorage.hpp"
|
---|
[97ebf8] | 28 |
|
---|
[e41c48] | 29 | // memento to remember the state when undoing
|
---|
| 30 |
|
---|
| 31 | class AtomRemoveState : public ActionState {
|
---|
| 32 | public:
|
---|
| 33 | AtomRemoveState(std::vector<AtomicInfo> _Walkers) :
|
---|
| 34 | Walkers(_Walkers)
|
---|
| 35 | {}
|
---|
| 36 | std::vector<AtomicInfo> Walkers;
|
---|
| 37 | };
|
---|
| 38 |
|
---|
[97ebf8] | 39 | const char AtomRemoveAction::NAME[] = "remove-atom";
|
---|
| 40 |
|
---|
| 41 | AtomRemoveAction::AtomRemoveAction() :
|
---|
| 42 | Action(NAME)
|
---|
| 43 | {}
|
---|
| 44 |
|
---|
| 45 | AtomRemoveAction::~AtomRemoveAction()
|
---|
| 46 | {}
|
---|
| 47 |
|
---|
[1a8fbf6] | 48 | void AtomRemove() {
|
---|
| 49 | ActionRegistry::getInstance().getActionByName(AtomRemoveAction::NAME)->call(Action::NonInteractive);
|
---|
| 50 | };
|
---|
| 51 |
|
---|
[047878] | 52 | Dialog* AtomRemoveAction::fillDialog(Dialog *dialog) {
|
---|
| 53 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
[97ebf8] | 54 |
|
---|
[5cb3cb] | 55 | dialog->queryEmpty(NAME, ValueStorage::getInstance().getDescription(NAME));
|
---|
| 56 |
|
---|
| 57 | return dialog;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | Action::state_ptr AtomRemoveAction::performCall() {
|
---|
| 61 | atom *first = NULL;
|
---|
[97ebf8] | 62 |
|
---|
[e41c48] | 63 | // create undo state
|
---|
| 64 | std::vector<AtomicInfo> Walkers;
|
---|
| 65 | for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) {
|
---|
| 66 | Walkers.push_back(AtomicInfo(*(iter->second)));
|
---|
| 67 | }
|
---|
| 68 | AtomRemoveState *UndoState = new AtomRemoveState(Walkers);
|
---|
| 69 |
|
---|
| 70 | // remove all selected atoms
|
---|
| 71 | // std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
|
---|
[5cb3cb] | 72 | for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) {
|
---|
| 73 | first = iter->second;
|
---|
[97ebf8] | 74 | DoLog(1) && (Log() << Verbose(1) << "Removing atom " << first->getId() << "." << endl);
|
---|
[e41c48] | 75 | // // TODO: this is not necessary when atoms and their storing to file are handled by the World
|
---|
| 76 | // // simply try to erase in every molecule found
|
---|
| 77 | // for (std::vector<molecule *>::iterator iter = molecules.begin();iter != molecules.end(); ++iter) {
|
---|
| 78 | // (*iter)->erase(first);
|
---|
| 79 | // }
|
---|
[97ebf8] | 80 | World::getInstance().destroyAtom(first);
|
---|
| 81 | }
|
---|
[e41c48] | 82 | return Action::state_ptr(UndoState);
|
---|
[97ebf8] | 83 | }
|
---|
| 84 |
|
---|
| 85 | Action::state_ptr AtomRemoveAction::performUndo(Action::state_ptr _state) {
|
---|
[e41c48] | 86 | AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get());
|
---|
| 87 |
|
---|
| 88 | size_t i=0;
|
---|
| 89 | for (; i<state->Walkers.size(); ++i) {
|
---|
| 90 | // re-create the atom
|
---|
| 91 | DoLog(1) && (Log() << Verbose(1) << "Re-adding atom " << state->Walkers[i].getId() << "." << endl);
|
---|
| 92 | atom *Walker = World::getInstance().createAtom();
|
---|
| 93 | if (!state->Walkers[i].setAtom(*Walker)) {
|
---|
| 94 | DoeLog(1) && (eLog() << Verbose(1) << "Failed to set id." << endl);
|
---|
| 95 | World::getInstance().destroyAtom(Walker);
|
---|
| 96 | break;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | if (i<state->Walkers.size()) {
|
---|
| 100 | // remove all previous ones, too
|
---|
| 101 | for (size_t j=0;j<i;++j)
|
---|
| 102 | World::getInstance().destroyAtom(state->Walkers[j].getId());
|
---|
| 103 | // and announce the failure of the undo
|
---|
| 104 | return Action::failure;
|
---|
| 105 | }
|
---|
| 106 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 107 | }
|
---|
| 108 |
|
---|
| 109 | Action::state_ptr AtomRemoveAction::performRedo(Action::state_ptr _state){
|
---|
[e41c48] | 110 | AtomRemoveState *state = assert_cast<AtomRemoveState*>(_state.get());
|
---|
| 111 |
|
---|
| 112 | // simple remove again all previously added atoms
|
---|
| 113 | for (size_t i=0; i<state->Walkers.size(); ++i) {
|
---|
| 114 | DoLog(1) && (Log() << Verbose(1) << "Re-removing atom " << state->Walkers[i].getId() << "." << endl);
|
---|
| 115 | World::getInstance().destroyAtom(state->Walkers[i].getId());
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 119 | }
|
---|
| 120 |
|
---|
| 121 | bool AtomRemoveAction::canUndo() {
|
---|
[e41c48] | 122 | return true;
|
---|
[97ebf8] | 123 | }
|
---|
| 124 |
|
---|
| 125 | bool AtomRemoveAction::shouldUndo() {
|
---|
[e41c48] | 126 | return true;
|
---|
[97ebf8] | 127 | }
|
---|
| 128 |
|
---|
| 129 | const string AtomRemoveAction::getName() {
|
---|
| 130 | return NAME;
|
---|
| 131 | }
|
---|