| 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 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * ChangeElementAction.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 "Helpers/MemDebug.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include "Actions/AtomAction/ChangeElementAction.hpp" | 
|---|
| 23 | #include "Actions/ActionRegistry.hpp" | 
|---|
| 24 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| 25 | #include "atom.hpp" | 
|---|
| 26 | #include "element.hpp" | 
|---|
| 27 | #include "Helpers/Log.hpp" | 
|---|
| 28 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 29 | #include "Helpers/Verbose.hpp" | 
|---|
| 30 | #include "molecule.hpp" | 
|---|
| 31 | #include "World.hpp" | 
|---|
| 32 |  | 
|---|
| 33 | #include <iostream> | 
|---|
| 34 | #include <map> | 
|---|
| 35 | #include <string> | 
|---|
| 36 |  | 
|---|
| 37 | using namespace std; | 
|---|
| 38 |  | 
|---|
| 39 | #include "UIElements/UIFactory.hpp" | 
|---|
| 40 | #include "UIElements/Dialog.hpp" | 
|---|
| 41 | #include "Actions/ValueStorage.hpp" | 
|---|
| 42 |  | 
|---|
| 43 | typedef std::map<int, const element *> ElementMap; | 
|---|
| 44 |  | 
|---|
| 45 | // memento to remember the state when undoing | 
|---|
| 46 |  | 
|---|
| 47 | class AtomChangeElementState : public ActionState { | 
|---|
| 48 | public: | 
|---|
| 49 | AtomChangeElementState(ElementMap _Elements, const element *_elemental) : | 
|---|
| 50 | Elements(_Elements), | 
|---|
| 51 | elemental(_elemental) | 
|---|
| 52 | {} | 
|---|
| 53 | ElementMap Elements; | 
|---|
| 54 | const element *elemental; | 
|---|
| 55 | }; | 
|---|
| 56 |  | 
|---|
| 57 | const char AtomChangeElementAction::NAME[] = "change-element"; | 
|---|
| 58 |  | 
|---|
| 59 | AtomChangeElementAction::AtomChangeElementAction() : | 
|---|
| 60 | Action(NAME) | 
|---|
| 61 | {} | 
|---|
| 62 |  | 
|---|
| 63 | AtomChangeElementAction::~AtomChangeElementAction() | 
|---|
| 64 | {} | 
|---|
| 65 |  | 
|---|
| 66 | void AtomChangeElement(element *elemental) { | 
|---|
| 67 | ValueStorage::getInstance().setCurrentValue(AtomChangeElementAction::NAME, elemental); | 
|---|
| 68 | ActionRegistry::getInstance().getActionByName(AtomChangeElementAction::NAME)->call(Action::NonInteractive); | 
|---|
| 69 | }; | 
|---|
| 70 |  | 
|---|
| 71 | Dialog* AtomChangeElementAction::fillDialog(Dialog *dialog) { | 
|---|
| 72 | ASSERT(dialog,"No Dialog given when filling action dialog"); | 
|---|
| 73 |  | 
|---|
| 74 | dialog->queryElement(NAME, ValueStorage::getInstance().getDescription(NAME)); | 
|---|
| 75 |  | 
|---|
| 76 | return dialog; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | Action::state_ptr AtomChangeElementAction::performCall() { | 
|---|
| 80 | atom *first = NULL; | 
|---|
| 81 | const element *elemental; | 
|---|
| 82 | molecule *mol = NULL; | 
|---|
| 83 |  | 
|---|
| 84 | ValueStorage::getInstance().queryCurrentValue(NAME, elemental); | 
|---|
| 85 |  | 
|---|
| 86 | // create undo state | 
|---|
| 87 | ElementMap Elements; | 
|---|
| 88 | for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) { | 
|---|
| 89 | Elements.insert(std::pair<int, const element *> (iter->second->getId(), iter->second->getType())); | 
|---|
| 90 | } | 
|---|
| 91 | AtomChangeElementState *UndoState = new AtomChangeElementState(Elements, elemental); | 
|---|
| 92 |  | 
|---|
| 93 | for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) { | 
|---|
| 94 | first = iter->second; | 
|---|
| 95 | DoLog(1) && (Log() << Verbose(1) << "Changing atom " << *first << " to element " << *elemental << "." << endl); | 
|---|
| 96 | mol = first->getMolecule(); | 
|---|
| 97 | first->removeFromMolecule(); // remove atom | 
|---|
| 98 | first->setType(elemental); | 
|---|
| 99 | mol->AddAtom(first);  // add atom to ensure correctness of formula | 
|---|
| 100 | } | 
|---|
| 101 | return Action::state_ptr(UndoState); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | Action::state_ptr AtomChangeElementAction::performUndo(Action::state_ptr _state) { | 
|---|
| 105 | AtomChangeElementState *state = assert_cast<AtomChangeElementState*>(_state.get()); | 
|---|
| 106 | atom *first = NULL; | 
|---|
| 107 | molecule *mol = NULL; | 
|---|
| 108 |  | 
|---|
| 109 | for(ElementMap::const_iterator iter = state->Elements.begin(); iter != state->Elements.end(); ++iter) { | 
|---|
| 110 | first = World::getInstance().getAtom(AtomById(iter->first)); | 
|---|
| 111 | mol = first->getMolecule(); | 
|---|
| 112 | first->removeFromMolecule(); // remove atom | 
|---|
| 113 | first->setType(iter->second); | 
|---|
| 114 | mol->AddAtom(first);  // add atom to ensure correctness of formula | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | return Action::state_ptr(_state); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | Action::state_ptr AtomChangeElementAction::performRedo(Action::state_ptr _state){ | 
|---|
| 121 | AtomChangeElementState *state = assert_cast<AtomChangeElementState*>(_state.get()); | 
|---|
| 122 | atom *first = NULL; | 
|---|
| 123 | molecule *mol = NULL; | 
|---|
| 124 |  | 
|---|
| 125 | for(ElementMap::const_iterator iter = state->Elements.begin(); iter != state->Elements.end(); ++iter) { | 
|---|
| 126 | first = World::getInstance().getAtom(AtomById(iter->first)); | 
|---|
| 127 | mol = first->getMolecule(); | 
|---|
| 128 | first->removeFromMolecule(); // remove atom | 
|---|
| 129 | first->setType(state->elemental); | 
|---|
| 130 | mol->AddAtom(first);  // add atom to ensure correctness of formula | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | return Action::state_ptr(_state); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | bool AtomChangeElementAction::canUndo() { | 
|---|
| 137 | return true; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | bool AtomChangeElementAction::shouldUndo() { | 
|---|
| 141 | return true; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | const string AtomChangeElementAction::getName() { | 
|---|
| 145 | return NAME; | 
|---|
| 146 | } | 
|---|