| 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 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * MoleculeByOrderAction.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: May 12, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "molecule.hpp"
 | 
|---|
| 23 | #include "Helpers/Log.hpp"
 | 
|---|
| 24 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 25 | #include "World.hpp"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include <iostream>
 | 
|---|
| 28 | #include <string>
 | 
|---|
| 29 | 
 | 
|---|
| 30 | using namespace std;
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #include "Actions/SelectionAction/MoleculeByOrderAction.hpp"
 | 
|---|
| 33 | 
 | 
|---|
| 34 | // and construct the stuff
 | 
|---|
| 35 | #include "MoleculeByOrderAction.def"
 | 
|---|
| 36 | #include "Action_impl_pre.hpp"
 | 
|---|
| 37 | /** =========== define the function ====================== */
 | 
|---|
| 38 | 
 | 
|---|
| 39 | molecule * SearchMoleculeByOrder(int molindex)
 | 
|---|
| 40 | {
 | 
|---|
| 41 |   molecule *mol = NULL;
 | 
|---|
| 42 |   std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
 | 
|---|
| 43 |   int i=0;
 | 
|---|
| 44 |   if (molindex > 0) {
 | 
|---|
| 45 |     std::vector<molecule *>::const_iterator iter = molecules.begin();
 | 
|---|
| 46 |     for (;iter != molecules.end();++iter) {
 | 
|---|
| 47 |       ++i;
 | 
|---|
| 48 |       if (molindex == i)
 | 
|---|
| 49 |         break;
 | 
|---|
| 50 |     }
 | 
|---|
| 51 |     mol = *iter;
 | 
|---|
| 52 |   } else {
 | 
|---|
| 53 |     std::vector<molecule *>::const_reverse_iterator iter = molecules.rbegin();
 | 
|---|
| 54 |     for (;iter != molecules.rend();--iter) {
 | 
|---|
| 55 |       --i;
 | 
|---|
| 56 |       if (molindex == i)
 | 
|---|
| 57 |         break;
 | 
|---|
| 58 |     }
 | 
|---|
| 59 |     mol = *iter;
 | 
|---|
| 60 |   }
 | 
|---|
| 61 | 
 | 
|---|
| 62 |   return mol;
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | Action::state_ptr SelectionMoleculeByOrderAction::performCall() {
 | 
|---|
| 66 |   // obtain information
 | 
|---|
| 67 |   getParametersfromValueStorage();
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   molecule *mol = SearchMoleculeByOrder(params.molindex);
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   if (mol != NULL) {
 | 
|---|
| 72 |     if (!World::getInstance().isSelected(mol)) {
 | 
|---|
| 73 |       DoLog(1) && (Log() << Verbose(1) << "Selecting molecule " << mol->name << endl);
 | 
|---|
| 74 |       World::getInstance().selectMolecule(mol);
 | 
|---|
| 75 |       return Action::state_ptr(new SelectionMoleculeByOrderState(params));
 | 
|---|
| 76 |     } else {
 | 
|---|
| 77 |       return Action::success;
 | 
|---|
| 78 |     }
 | 
|---|
| 79 |   } else {
 | 
|---|
| 80 |     return Action::failure;
 | 
|---|
| 81 |   }
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | Action::state_ptr SelectionMoleculeByOrderAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 85 |   SelectionMoleculeByOrderState *state = assert_cast<SelectionMoleculeByOrderState*>(_state.get());
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   molecule *mol = SearchMoleculeByOrder(state->params.molindex);
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   if (mol != NULL) {
 | 
|---|
| 90 |     World::getInstance().unselectMolecule(mol);
 | 
|---|
| 91 |     return Action::state_ptr(_state);
 | 
|---|
| 92 |   } else {
 | 
|---|
| 93 |     return Action::failure;
 | 
|---|
| 94 |   }
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | Action::state_ptr SelectionMoleculeByOrderAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 98 |   SelectionMoleculeByOrderState *state = assert_cast<SelectionMoleculeByOrderState*>(_state.get());
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   molecule *mol = SearchMoleculeByOrder(state->params.molindex);
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   if (mol != NULL) {
 | 
|---|
| 103 |     World::getInstance().selectMolecule(mol);
 | 
|---|
| 104 |     return Action::state_ptr(state);
 | 
|---|
| 105 |   } else {
 | 
|---|
| 106 |     return Action::failure;
 | 
|---|
| 107 |   }
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | bool SelectionMoleculeByOrderAction::canUndo() {
 | 
|---|
| 111 |   return true;
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | bool SelectionMoleculeByOrderAction::shouldUndo() {
 | 
|---|
| 115 |   return true;
 | 
|---|
| 116 | }
 | 
|---|
| 117 | /** =========== end of function ====================== */
 | 
|---|