| 1 | /*
 | 
|---|
| 2 |  * NotMoleculeOfAtomAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 12, 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/SelectionAction/NotMoleculeOfAtomAction.hpp"
 | 
|---|
| 16 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 17 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
| 18 | #include "atom.hpp"
 | 
|---|
| 19 | #include "Helpers/Log.hpp"
 | 
|---|
| 20 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 21 | #include "World.hpp"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include <iostream>
 | 
|---|
| 24 | #include <string>
 | 
|---|
| 25 | 
 | 
|---|
| 26 | using namespace std;
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 29 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 30 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 31 | 
 | 
|---|
| 32 | 
 | 
|---|
| 33 | // memento to remember the state when undoing
 | 
|---|
| 34 | 
 | 
|---|
| 35 | class SelectionNotMoleculeOfAtomState : public ActionState {
 | 
|---|
| 36 | public:
 | 
|---|
| 37 |   SelectionNotMoleculeOfAtomState(std::vector<molecule*> selectedMolecules, atom *_Walker) :
 | 
|---|
| 38 |     selectedMolecules(selectedMolecules),
 | 
|---|
| 39 |     Walker(_Walker)
 | 
|---|
| 40 |   {}
 | 
|---|
| 41 |   std::vector<molecule*> selectedMolecules;
 | 
|---|
| 42 |   atom *Walker;
 | 
|---|
| 43 | };
 | 
|---|
| 44 | 
 | 
|---|
| 45 | const char SelectionNotMoleculeOfAtomAction::NAME[] = "unselect-molecule-of-atom";
 | 
|---|
| 46 | 
 | 
|---|
| 47 | SelectionNotMoleculeOfAtomAction::SelectionNotMoleculeOfAtomAction() :
 | 
|---|
| 48 |   Action(NAME)
 | 
|---|
| 49 | {}
 | 
|---|
| 50 | 
 | 
|---|
| 51 | SelectionNotMoleculeOfAtomAction::~SelectionNotMoleculeOfAtomAction()
 | 
|---|
| 52 | {}
 | 
|---|
| 53 | 
 | 
|---|
| 54 | void SelectionNotMoleculeOfAtom() {
 | 
|---|
| 55 |   ActionRegistry::getInstance().getActionByName(SelectionNotMoleculeOfAtomAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 56 | };
 | 
|---|
| 57 | 
 | 
|---|
| 58 | Dialog* SelectionNotMoleculeOfAtomAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 59 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   dialog->queryAtom(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   return dialog;
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | Action::state_ptr SelectionNotMoleculeOfAtomAction::performCall() {
 | 
|---|
| 67 |   atom *Walker = NULL;
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   ValueStorage::getInstance().queryCurrentValue(NAME, Walker);
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   std::vector<molecule *> selectedMolecules = World::getInstance().getSelectedMolecules();
 | 
|---|
| 72 |   DoLog(1) && (Log() << Verbose(1) << "Unselecting molecule to which " << Walker->getName() << " belongs." << endl);
 | 
|---|
| 73 |   World::getInstance().unselectMoleculeOfAtom(Walker);
 | 
|---|
| 74 |   return Action::state_ptr(new SelectionNotMoleculeOfAtomState(selectedMolecules, Walker));
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | Action::state_ptr SelectionNotMoleculeOfAtomAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 78 |   SelectionNotMoleculeOfAtomState *state = assert_cast<SelectionNotMoleculeOfAtomState*>(_state.get());
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   World::getInstance().clearMoleculeSelection();
 | 
|---|
| 81 |   for(std::vector<molecule *>::iterator iter = state->selectedMolecules.begin(); iter != state->selectedMolecules.end(); ++iter)
 | 
|---|
| 82 |     World::getInstance().selectMolecule(*iter);
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   return Action::state_ptr(new SelectionNotMoleculeOfAtomState(state->selectedMolecules, state->Walker));
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | Action::state_ptr SelectionNotMoleculeOfAtomAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 88 |   SelectionNotMoleculeOfAtomState *state = assert_cast<SelectionNotMoleculeOfAtomState*>(_state.get());
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   World::getInstance().unselectMoleculeOfAtom(state->Walker);
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   return Action::state_ptr(new SelectionNotMoleculeOfAtomState(state->selectedMolecules, state->Walker));
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | bool SelectionNotMoleculeOfAtomAction::canUndo() {
 | 
|---|
| 96 |   return true;
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | bool SelectionNotMoleculeOfAtomAction::shouldUndo() {
 | 
|---|
| 100 |   return true;
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | const string SelectionNotMoleculeOfAtomAction::getName() {
 | 
|---|
| 104 |   return NAME;
 | 
|---|
| 105 | }
 | 
|---|