| 1 | /*
 | 
|---|
| 2 |  * NotAllAtomsInsideSphereAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Aug 9, 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/NotAllAtomsInsideSphereAction.hpp"
 | 
|---|
| 16 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 17 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 18 | #include "Descriptors/AtomShapeDescriptor.hpp"
 | 
|---|
| 19 | #include "atom.hpp"
 | 
|---|
| 20 | #include "Helpers/Log.hpp"
 | 
|---|
| 21 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 22 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 23 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| 24 | #include "Shapes/Shape.hpp"
 | 
|---|
| 25 | #include "Shapes/ShapeOps.hpp"
 | 
|---|
| 26 | #include "World.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include <iostream>
 | 
|---|
| 29 | #include <string>
 | 
|---|
| 30 | 
 | 
|---|
| 31 | using namespace std;
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 34 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 35 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | 
 | 
|---|
| 38 | // memento to remember the state when undoing
 | 
|---|
| 39 | 
 | 
|---|
| 40 | class SelectionNotAllAtomsInsideSphereState : public ActionState {
 | 
|---|
| 41 | public:
 | 
|---|
| 42 |   SelectionNotAllAtomsInsideSphereState(std::vector<atom*> _selectedAtoms, const Vector &_position, const double _radius) :
 | 
|---|
| 43 |     selectedAtoms(_selectedAtoms),
 | 
|---|
| 44 |     position(_position),
 | 
|---|
| 45 |     radius(_radius)
 | 
|---|
| 46 |   {}
 | 
|---|
| 47 |   std::vector<atom*> selectedAtoms;
 | 
|---|
| 48 |   Vector position;
 | 
|---|
| 49 |   double radius;
 | 
|---|
| 50 | };
 | 
|---|
| 51 | 
 | 
|---|
| 52 | const char SelectionNotAllAtomsInsideSphereAction::NAME[] = "unselect-atoms-inside-sphere";
 | 
|---|
| 53 | 
 | 
|---|
| 54 | SelectionNotAllAtomsInsideSphereAction::SelectionNotAllAtomsInsideSphereAction() :
 | 
|---|
| 55 |   Action(NAME)
 | 
|---|
| 56 | {}
 | 
|---|
| 57 | 
 | 
|---|
| 58 | SelectionNotAllAtomsInsideSphereAction::~SelectionNotAllAtomsInsideSphereAction()
 | 
|---|
| 59 | {}
 | 
|---|
| 60 | 
 | 
|---|
| 61 | void SelectionNotAllAtomsInsideSphere(const Vector &position, const double radius) {
 | 
|---|
| 62 |   ValueStorage::getInstance().setCurrentValue(SelectionNotAllAtomsInsideSphereAction::NAME, radius);
 | 
|---|
| 63 |   ValueStorage::getInstance().setCurrentValue("position", position);
 | 
|---|
| 64 |   ActionRegistry::getInstance().getActionByName(SelectionNotAllAtomsInsideSphereAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 65 | };
 | 
|---|
| 66 | 
 | 
|---|
| 67 | Dialog* SelectionNotAllAtomsInsideSphereAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 68 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   dialog->queryDouble(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 71 |   dialog->queryVector("position", false, ValueStorage::getInstance().getDescription("position"));
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   return dialog;
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | Action::state_ptr SelectionNotAllAtomsInsideSphereAction::performCall() {
 | 
|---|
| 77 |   std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
 | 
|---|
| 78 |   double radius = 0.;
 | 
|---|
| 79 |   Vector position;
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   ValueStorage::getInstance().queryCurrentValue(NAME, radius);
 | 
|---|
| 82 |   ValueStorage::getInstance().queryCurrentValue("position", position);
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   DoLog(1) && (Log() << Verbose(1) << "Unselecting all atoms inside a sphere at " << position << " with radius " << radius << "." << endl);
 | 
|---|
| 85 |   Shape s = translate(resize(Sphere(),radius),position);
 | 
|---|
| 86 |   World::getInstance().unselectAllAtoms(AtomByShape(s));
 | 
|---|
| 87 |   return Action::state_ptr(new SelectionNotAllAtomsInsideSphereState(selectedAtoms, position, radius));
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | Action::state_ptr SelectionNotAllAtomsInsideSphereAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 91 |   SelectionNotAllAtomsInsideSphereState *state = assert_cast<SelectionNotAllAtomsInsideSphereState*>(_state.get());
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   World::getInstance().clearAtomSelection();
 | 
|---|
| 94 |   for(std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter)
 | 
|---|
| 95 |     World::getInstance().selectAtom(*iter);
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   return Action::state_ptr(new SelectionNotAllAtomsInsideSphereState(state->selectedAtoms, state->position, state->radius));
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | Action::state_ptr SelectionNotAllAtomsInsideSphereAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 101 |   SelectionNotAllAtomsInsideSphereState *state = assert_cast<SelectionNotAllAtomsInsideSphereState*>(_state.get());
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   Shape s = translate(resize(Sphere(),state->radius),state->position);
 | 
|---|
| 104 |   World::getInstance().unselectAllAtoms(AtomByShape(s));
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   return Action::state_ptr(new SelectionNotAllAtomsInsideSphereState(state->selectedAtoms, state->position, state->radius));
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | bool SelectionNotAllAtomsInsideSphereAction::canUndo() {
 | 
|---|
| 110 |   return true;
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | bool SelectionNotAllAtomsInsideSphereAction::shouldUndo() {
 | 
|---|
| 114 |   return true;
 | 
|---|
| 115 | }
 | 
|---|
| 116 | 
 | 
|---|
| 117 | const string SelectionNotAllAtomsInsideSphereAction::getName() {
 | 
|---|
| 118 |   return NAME;
 | 
|---|
| 119 | }
 | 
|---|