| 1 | /*
 | 
|---|
| 2 |  * NotAllAtomsInsideCuboidAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Aug 9, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Actions/SelectionAction/NotAllAtomsInsideCuboidAction.hpp"
 | 
|---|
| 11 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 12 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 13 | #include "Descriptors/AtomShapeDescriptor.hpp"
 | 
|---|
| 14 | #include "atom.hpp"
 | 
|---|
| 15 | #include "Helpers/Log.hpp"
 | 
|---|
| 16 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 17 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 18 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| 19 | #include "Shapes/Shape.hpp"
 | 
|---|
| 20 | #include "Shapes/ShapeOps.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 SelectionNotAllAtomsInsideCuboidState : public ActionState {
 | 
|---|
| 36 | public:
 | 
|---|
| 37 |   SelectionNotAllAtomsInsideCuboidState(std::vector<atom*> _selectedAtoms, const Vector &_position, const Vector &_extension) :
 | 
|---|
| 38 |     selectedAtoms(_selectedAtoms),
 | 
|---|
| 39 |     position(_position),
 | 
|---|
| 40 |     extension(_extension)
 | 
|---|
| 41 |   {}
 | 
|---|
| 42 |   std::vector<atom*> selectedAtoms;
 | 
|---|
| 43 |   Vector position;
 | 
|---|
| 44 |   Vector extension;
 | 
|---|
| 45 | };
 | 
|---|
| 46 | 
 | 
|---|
| 47 | const char SelectionNotAllAtomsInsideCuboidAction::NAME[] = "unselect-atoms-inside-cuboid";
 | 
|---|
| 48 | 
 | 
|---|
| 49 | SelectionNotAllAtomsInsideCuboidAction::SelectionNotAllAtomsInsideCuboidAction() :
 | 
|---|
| 50 |   Action(NAME)
 | 
|---|
| 51 | {}
 | 
|---|
| 52 | 
 | 
|---|
| 53 | SelectionNotAllAtomsInsideCuboidAction::~SelectionNotAllAtomsInsideCuboidAction()
 | 
|---|
| 54 | {}
 | 
|---|
| 55 | 
 | 
|---|
| 56 | void SelectionNotAllAtomsInsideCuboid(const Vector &position, const Vector &extension) {
 | 
|---|
| 57 |   ValueStorage::getInstance().setCurrentValue(SelectionNotAllAtomsInsideCuboidAction::NAME, extension);
 | 
|---|
| 58 |   ValueStorage::getInstance().setCurrentValue("position", position);
 | 
|---|
| 59 |   ActionRegistry::getInstance().getActionByName(SelectionNotAllAtomsInsideCuboidAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 60 | };
 | 
|---|
| 61 | 
 | 
|---|
| 62 | Dialog* SelectionNotAllAtomsInsideCuboidAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 63 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   dialog->queryVector(NAME, false, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 66 |   dialog->queryVector("position", false, ValueStorage::getInstance().getDescription("position"));
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   return dialog;
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | Action::state_ptr SelectionNotAllAtomsInsideCuboidAction::performCall() {
 | 
|---|
| 72 |   std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
 | 
|---|
| 73 |   Vector position;
 | 
|---|
| 74 |   Vector extension;
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   ValueStorage::getInstance().queryCurrentValue("position", position);
 | 
|---|
| 77 |   ValueStorage::getInstance().queryCurrentValue(NAME, extension);
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   DoLog(1) && (Log() << Verbose(1) << "Unselecting all atoms inside a cuboid at " << position << " and extension of " << extension << "." << endl);
 | 
|---|
| 80 |   Shape s = translate(stretch(Cuboid(),extension),position);
 | 
|---|
| 81 |   World::getInstance().unselectAllAtoms(AtomByShape(s));
 | 
|---|
| 82 |   return Action::state_ptr(new SelectionNotAllAtomsInsideCuboidState(selectedAtoms, position, extension));
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | Action::state_ptr SelectionNotAllAtomsInsideCuboidAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 86 |   SelectionNotAllAtomsInsideCuboidState *state = assert_cast<SelectionNotAllAtomsInsideCuboidState*>(_state.get());
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   World::getInstance().clearAtomSelection();
 | 
|---|
| 89 |   for(std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter)
 | 
|---|
| 90 |     World::getInstance().selectAtom(*iter);
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   return Action::state_ptr(new SelectionNotAllAtomsInsideCuboidState(state->selectedAtoms, state->position, state->extension));
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | Action::state_ptr SelectionNotAllAtomsInsideCuboidAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 96 |   SelectionNotAllAtomsInsideCuboidState *state = assert_cast<SelectionNotAllAtomsInsideCuboidState*>(_state.get());
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   Shape s = translate(stretch(Cuboid(),state->extension),state->position);
 | 
|---|
| 99 |   World::getInstance().unselectAllAtoms(AtomByShape(s));
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   return Action::state_ptr(new SelectionNotAllAtomsInsideCuboidState(state->selectedAtoms, state->position, state->extension));
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | bool SelectionNotAllAtomsInsideCuboidAction::canUndo() {
 | 
|---|
| 105 |   return true;
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | bool SelectionNotAllAtomsInsideCuboidAction::shouldUndo() {
 | 
|---|
| 109 |   return true;
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | const string SelectionNotAllAtomsInsideCuboidAction::getName() {
 | 
|---|
| 113 |   return NAME;
 | 
|---|
| 114 | }
 | 
|---|