| [97ebf8] | 1 | /*
|
|---|
| 2 | * ChangeElementAction.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: May 9, 2010
|
|---|
| 5 | * Author: heber
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| [bf3817] | 8 | // include config.h
|
|---|
| 9 | #ifdef HAVE_CONFIG_H
|
|---|
| 10 | #include <config.h>
|
|---|
| 11 | #endif
|
|---|
| 12 |
|
|---|
| [112b09] | 13 | #include "Helpers/MemDebug.hpp"
|
|---|
| 14 |
|
|---|
| [97ebf8] | 15 | #include "Actions/AtomAction/ChangeElementAction.hpp"
|
|---|
| [0430e3] | 16 | #include "Actions/ActionRegistry.hpp"
|
|---|
| [f8456c] | 17 | #include "Descriptors/AtomIdDescriptor.hpp"
|
|---|
| [97ebf8] | 18 | #include "atom.hpp"
|
|---|
| [023971] | 19 | #include "element.hpp"
|
|---|
| [952f38] | 20 | #include "Helpers/Log.hpp"
|
|---|
| [57f243] | 21 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| [952f38] | 22 | #include "Helpers/Verbose.hpp"
|
|---|
| [dddbfe] | 23 | #include "molecule.hpp"
|
|---|
| [97ebf8] | 24 | #include "World.hpp"
|
|---|
| 25 |
|
|---|
| 26 | #include <iostream>
|
|---|
| [f8456c] | 27 | #include <map>
|
|---|
| [97ebf8] | 28 | #include <string>
|
|---|
| 29 |
|
|---|
| 30 | using namespace std;
|
|---|
| 31 |
|
|---|
| 32 | #include "UIElements/UIFactory.hpp"
|
|---|
| 33 | #include "UIElements/Dialog.hpp"
|
|---|
| [861874] | 34 | #include "Actions/ValueStorage.hpp"
|
|---|
| [97ebf8] | 35 |
|
|---|
| [f8456c] | 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 |
|
|---|
| [97ebf8] | 50 | const char AtomChangeElementAction::NAME[] = "change-element";
|
|---|
| 51 |
|
|---|
| 52 | AtomChangeElementAction::AtomChangeElementAction() :
|
|---|
| 53 | Action(NAME)
|
|---|
| 54 | {}
|
|---|
| 55 |
|
|---|
| 56 | AtomChangeElementAction::~AtomChangeElementAction()
|
|---|
| 57 | {}
|
|---|
| 58 |
|
|---|
| [1a8fbf6] | 59 | void AtomChangeElement(element *elemental) {
|
|---|
| 60 | ValueStorage::getInstance().setCurrentValue(AtomChangeElementAction::NAME, elemental);
|
|---|
| 61 | ActionRegistry::getInstance().getActionByName(AtomChangeElementAction::NAME)->call(Action::NonInteractive);
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| [047878] | 64 | Dialog* AtomChangeElementAction::fillDialog(Dialog *dialog) {
|
|---|
| 65 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
|---|
| [454065] | 66 |
|
|---|
| 67 | dialog->queryElement(NAME, ValueStorage::getInstance().getDescription(NAME));
|
|---|
| 68 |
|
|---|
| 69 | return dialog;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | Action::state_ptr AtomChangeElementAction::performCall() {
|
|---|
| [97ebf8] | 73 | atom *first = NULL;
|
|---|
| [b5c53d] | 74 | const element *elemental;
|
|---|
| [dddbfe] | 75 | molecule *mol = NULL;
|
|---|
| [97ebf8] | 76 |
|
|---|
| [8d4100] | 77 | ValueStorage::getInstance().queryCurrentValue(NAME, elemental);
|
|---|
| [97ebf8] | 78 |
|
|---|
| [f8456c] | 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 |
|
|---|
| [454065] | 86 | for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) {
|
|---|
| 87 | first = iter->second;
|
|---|
| [2fe971] | 88 | DoLog(1) && (Log() << Verbose(1) << "Changing atom " << *first << " to element " << *elemental << "." << endl);
|
|---|
| [dddbfe] | 89 | mol = first->getMolecule();
|
|---|
| 90 | first->removeFromMolecule(); // remove atom
|
|---|
| [d74077] | 91 | first->setType(elemental);
|
|---|
| [dddbfe] | 92 | mol->AddAtom(first); // add atom to ensure correctness of formula
|
|---|
| [454065] | 93 | }
|
|---|
| [f8456c] | 94 | return Action::state_ptr(UndoState);
|
|---|
| [97ebf8] | 95 | }
|
|---|
| 96 |
|
|---|
| 97 | Action::state_ptr AtomChangeElementAction::performUndo(Action::state_ptr _state) {
|
|---|
| [f8456c] | 98 | AtomChangeElementState *state = assert_cast<AtomChangeElementState*>(_state.get());
|
|---|
| 99 | atom *first = NULL;
|
|---|
| 100 | molecule *mol = NULL;
|
|---|
| [97ebf8] | 101 |
|
|---|
| [f8456c] | 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);
|
|---|
| [97ebf8] | 111 | }
|
|---|
| 112 |
|
|---|
| 113 | Action::state_ptr AtomChangeElementAction::performRedo(Action::state_ptr _state){
|
|---|
| [f8456c] | 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);
|
|---|
| [97ebf8] | 127 | }
|
|---|
| 128 |
|
|---|
| 129 | bool AtomChangeElementAction::canUndo() {
|
|---|
| [f8456c] | 130 | return true;
|
|---|
| [97ebf8] | 131 | }
|
|---|
| 132 |
|
|---|
| 133 | bool AtomChangeElementAction::shouldUndo() {
|
|---|
| [f8456c] | 134 | return true;
|
|---|
| [97ebf8] | 135 | }
|
|---|
| 136 |
|
|---|
| 137 | const string AtomChangeElementAction::getName() {
|
|---|
| 138 | return NAME;
|
|---|
| 139 | }
|
|---|