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