| 1 | /*
 | 
|---|
| 2 |  * MoleculeByIdAction.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/MoleculeByIdAction.hpp"
 | 
|---|
| 16 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 17 | #include "molecule.hpp"
 | 
|---|
| 18 | #include "Helpers/Log.hpp"
 | 
|---|
| 19 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 20 | #include "World.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <iostream>
 | 
|---|
| 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 | 
 | 
|---|
| 32 | // memento to remember the state when undoing
 | 
|---|
| 33 | 
 | 
|---|
| 34 | class SelectionMoleculeByIdState : public ActionState {
 | 
|---|
| 35 | public:
 | 
|---|
| 36 |   SelectionMoleculeByIdState(molecule* _mol) :
 | 
|---|
| 37 |     mol(_mol)
 | 
|---|
| 38 |   {}
 | 
|---|
| 39 |   molecule* mol;
 | 
|---|
| 40 | };
 | 
|---|
| 41 | 
 | 
|---|
| 42 | const char SelectionMoleculeByIdAction::NAME[] = "select-molecule-by-id";
 | 
|---|
| 43 | 
 | 
|---|
| 44 | SelectionMoleculeByIdAction::SelectionMoleculeByIdAction() :
 | 
|---|
| 45 |   Action(NAME)
 | 
|---|
| 46 | {}
 | 
|---|
| 47 | 
 | 
|---|
| 48 | SelectionMoleculeByIdAction::~SelectionMoleculeByIdAction()
 | 
|---|
| 49 | {}
 | 
|---|
| 50 | 
 | 
|---|
| 51 | void SelectionMoleculeById(molecule *_mol) {
 | 
|---|
| 52 |   ValueStorage::getInstance().setCurrentValue(SelectionMoleculeByIdAction::NAME, _mol);
 | 
|---|
| 53 |   ActionRegistry::getInstance().getActionByName(SelectionMoleculeByIdAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 54 | };
 | 
|---|
| 55 | 
 | 
|---|
| 56 | Dialog* SelectionMoleculeByIdAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 57 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   dialog->queryMolecule(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   return dialog;
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | Action::state_ptr SelectionMoleculeByIdAction::performCall() {
 | 
|---|
| 65 |   molecule *mol = NULL;
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   ValueStorage::getInstance().queryCurrentValue(NAME, mol);
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   if (!World::getInstance().isSelected(mol)) {
 | 
|---|
| 70 |     DoLog(1) && (Log() << Verbose(1) << "Selecting molecule " << mol->name << endl);
 | 
|---|
| 71 |     World::getInstance().selectMolecule(mol);
 | 
|---|
| 72 |     return Action::state_ptr(new SelectionMoleculeByIdState(mol));
 | 
|---|
| 73 |   } else {
 | 
|---|
| 74 |     return Action::success;
 | 
|---|
| 75 |   }
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | Action::state_ptr SelectionMoleculeByIdAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 79 |   SelectionMoleculeByIdState *state = assert_cast<SelectionMoleculeByIdState*>(_state.get());
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   if (state->mol != NULL)
 | 
|---|
| 82 |     World::getInstance().unselectMolecule(state->mol);
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   return Action::state_ptr(new SelectionMoleculeByIdState(state->mol));
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | Action::state_ptr SelectionMoleculeByIdAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 88 |   SelectionMoleculeByIdState *state = assert_cast<SelectionMoleculeByIdState*>(_state.get());
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   if (state->mol != NULL)
 | 
|---|
| 91 |     World::getInstance().selectMolecule(state->mol);
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   return Action::state_ptr(new SelectionMoleculeByIdState(state->mol));
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | bool SelectionMoleculeByIdAction::canUndo() {
 | 
|---|
| 97 |   return true;
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | bool SelectionMoleculeByIdAction::shouldUndo() {
 | 
|---|
| 101 |   return true;
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | const string SelectionMoleculeByIdAction::getName() {
 | 
|---|
| 105 |   return NAME;
 | 
|---|
| 106 | }
 | 
|---|