| [bcf653] | 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 | 
 | 
|---|
| [97ebf8] | 8 | /*
 | 
|---|
 | 9 |  * ChangeElementAction.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: May 9, 2010
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [112b09] | 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
| [97ebf8] | 22 | #include "Actions/AtomAction/ChangeElementAction.hpp"
 | 
|---|
| [0430e3] | 23 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| [f8456c] | 24 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [97ebf8] | 25 | #include "atom.hpp"
 | 
|---|
| [023971] | 26 | #include "element.hpp"
 | 
|---|
| [952f38] | 27 | #include "Helpers/Log.hpp"
 | 
|---|
| [57f243] | 28 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [952f38] | 29 | #include "Helpers/Verbose.hpp"
 | 
|---|
| [dddbfe] | 30 | #include "molecule.hpp"
 | 
|---|
| [97ebf8] | 31 | #include "World.hpp"
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | #include <iostream>
 | 
|---|
| [f8456c] | 34 | #include <map>
 | 
|---|
| [97ebf8] | 35 | #include <string>
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | using namespace std;
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | #include "UIElements/UIFactory.hpp"
 | 
|---|
 | 40 | #include "UIElements/Dialog.hpp"
 | 
|---|
| [861874] | 41 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| [97ebf8] | 42 | 
 | 
|---|
| [f8456c] | 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 | 
 | 
|---|
| [97ebf8] | 57 | const char AtomChangeElementAction::NAME[] = "change-element";
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | AtomChangeElementAction::AtomChangeElementAction() :
 | 
|---|
 | 60 |   Action(NAME)
 | 
|---|
 | 61 | {}
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | AtomChangeElementAction::~AtomChangeElementAction()
 | 
|---|
 | 64 | {}
 | 
|---|
 | 65 | 
 | 
|---|
| [1a8fbf6] | 66 | void AtomChangeElement(element *elemental) {
 | 
|---|
 | 67 |   ValueStorage::getInstance().setCurrentValue(AtomChangeElementAction::NAME, elemental);
 | 
|---|
 | 68 |   ActionRegistry::getInstance().getActionByName(AtomChangeElementAction::NAME)->call(Action::NonInteractive);
 | 
|---|
 | 69 | };
 | 
|---|
 | 70 | 
 | 
|---|
| [047878] | 71 | Dialog* AtomChangeElementAction::fillDialog(Dialog *dialog) {
 | 
|---|
 | 72 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| [454065] | 73 | 
 | 
|---|
 | 74 |   dialog->queryElement(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | return dialog;
 | 
|---|
 | 77 | }
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 | Action::state_ptr AtomChangeElementAction::performCall() {
 | 
|---|
| [97ebf8] | 80 |   atom *first = NULL;
 | 
|---|
| [b5c53d] | 81 |   const element *elemental;
 | 
|---|
| [dddbfe] | 82 |   molecule *mol = NULL;
 | 
|---|
| [97ebf8] | 83 | 
 | 
|---|
| [8d4100] | 84 |   ValueStorage::getInstance().queryCurrentValue(NAME, elemental);
 | 
|---|
| [97ebf8] | 85 | 
 | 
|---|
| [f8456c] | 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 | 
 | 
|---|
| [454065] | 93 |   for (World::AtomSelectionIterator iter = World::getInstance().beginAtomSelection(); iter != World::getInstance().endAtomSelection(); ++iter) {
 | 
|---|
 | 94 |     first = iter->second;
 | 
|---|
| [2fe971] | 95 |     DoLog(1) && (Log() << Verbose(1) << "Changing atom " << *first << " to element " << *elemental << "." << endl);
 | 
|---|
| [dddbfe] | 96 |     mol = first->getMolecule();
 | 
|---|
 | 97 |     first->removeFromMolecule(); // remove atom
 | 
|---|
| [d74077] | 98 |     first->setType(elemental);
 | 
|---|
| [dddbfe] | 99 |     mol->AddAtom(first);  // add atom to ensure correctness of formula
 | 
|---|
| [454065] | 100 |   }
 | 
|---|
| [f8456c] | 101 |   return Action::state_ptr(UndoState);
 | 
|---|
| [97ebf8] | 102 | }
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 | Action::state_ptr AtomChangeElementAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| [f8456c] | 105 |   AtomChangeElementState *state = assert_cast<AtomChangeElementState*>(_state.get());
 | 
|---|
 | 106 |   atom *first = NULL;
 | 
|---|
 | 107 |   molecule *mol = NULL;
 | 
|---|
| [97ebf8] | 108 | 
 | 
|---|
| [f8456c] | 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);
 | 
|---|
| [97ebf8] | 118 | }
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 | Action::state_ptr AtomChangeElementAction::performRedo(Action::state_ptr _state){
 | 
|---|
| [f8456c] | 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);
 | 
|---|
| [97ebf8] | 134 | }
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 | bool AtomChangeElementAction::canUndo() {
 | 
|---|
| [f8456c] | 137 |   return true;
 | 
|---|
| [97ebf8] | 138 | }
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 | bool AtomChangeElementAction::shouldUndo() {
 | 
|---|
| [f8456c] | 141 |   return true;
 | 
|---|
| [97ebf8] | 142 | }
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 | const string AtomChangeElementAction::getName() {
 | 
|---|
 | 145 |   return NAME;
 | 
|---|
 | 146 | }
 | 
|---|