1 | /*
|
---|
2 | * AllAtomsInsideSphereAction.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/AllAtomsInsideSphereAction.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 SelectionAllAtomsInsideSphereState : public ActionState {
|
---|
41 | public:
|
---|
42 | SelectionAllAtomsInsideSphereState(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 SelectionAllAtomsInsideSphereAction::NAME[] = "select-atoms-inside-sphere";
|
---|
53 |
|
---|
54 | SelectionAllAtomsInsideSphereAction::SelectionAllAtomsInsideSphereAction() :
|
---|
55 | Action(NAME)
|
---|
56 | {}
|
---|
57 |
|
---|
58 | SelectionAllAtomsInsideSphereAction::~SelectionAllAtomsInsideSphereAction()
|
---|
59 | {}
|
---|
60 |
|
---|
61 | void SelectionAllAtomsInsideSphere(const Vector &position, const double radius) {
|
---|
62 | ValueStorage::getInstance().setCurrentValue(SelectionAllAtomsInsideSphereAction::NAME, radius);
|
---|
63 | ValueStorage::getInstance().setCurrentValue("position", position);
|
---|
64 | ActionRegistry::getInstance().getActionByName(SelectionAllAtomsInsideSphereAction::NAME)->call(Action::NonInteractive);
|
---|
65 | };
|
---|
66 |
|
---|
67 | Dialog* SelectionAllAtomsInsideSphereAction::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 SelectionAllAtomsInsideSphereAction::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) << "Selecting all atoms inside a sphere at " << position << " with radius " << radius << "." << endl);
|
---|
85 | Shape s = translate(resize(Sphere(),radius),position);
|
---|
86 | World::getInstance().selectAllAtoms(AtomByShape(s));
|
---|
87 | return Action::state_ptr(new SelectionAllAtomsInsideSphereState(selectedAtoms, position, radius));
|
---|
88 | }
|
---|
89 |
|
---|
90 | Action::state_ptr SelectionAllAtomsInsideSphereAction::performUndo(Action::state_ptr _state) {
|
---|
91 | SelectionAllAtomsInsideSphereState *state = assert_cast<SelectionAllAtomsInsideSphereState*>(_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 SelectionAllAtomsInsideSphereState(state->selectedAtoms, state->position, state->radius));
|
---|
98 | }
|
---|
99 |
|
---|
100 | Action::state_ptr SelectionAllAtomsInsideSphereAction::performRedo(Action::state_ptr _state){
|
---|
101 | SelectionAllAtomsInsideSphereState *state = assert_cast<SelectionAllAtomsInsideSphereState*>(_state.get());
|
---|
102 |
|
---|
103 | Shape s = translate(resize(Sphere(),state->radius),state->position);
|
---|
104 | World::getInstance().selectAllAtoms(AtomByShape(s));
|
---|
105 |
|
---|
106 | return Action::state_ptr(new SelectionAllAtomsInsideSphereState(state->selectedAtoms, state->position, state->radius));
|
---|
107 | }
|
---|
108 |
|
---|
109 | bool SelectionAllAtomsInsideSphereAction::canUndo() {
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | bool SelectionAllAtomsInsideSphereAction::shouldUndo() {
|
---|
114 | return true;
|
---|
115 | }
|
---|
116 |
|
---|
117 | const string SelectionAllAtomsInsideSphereAction::getName() {
|
---|
118 | return NAME;
|
---|
119 | }
|
---|