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