/* * AllAtomsInsideSphereAction.cpp * * Created on: Aug 9, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Helpers/MemDebug.hpp" #include "Actions/SelectionAction/AllAtomsInsideSphereAction.hpp" #include "Actions/ActionRegistry.hpp" #include "Descriptors/AtomDescriptor.hpp" #include "Descriptors/AtomShapeDescriptor.hpp" #include "atom.hpp" #include "Helpers/Log.hpp" #include "Helpers/Verbose.hpp" #include "LinearAlgebra/Vector.hpp" #include "Shapes/BaseShapes.hpp" #include "Shapes/Shape.hpp" #include "Shapes/ShapeOps.hpp" #include "World.hpp" #include #include using namespace std; #include "UIElements/UIFactory.hpp" #include "UIElements/Dialog.hpp" #include "Actions/ValueStorage.hpp" // memento to remember the state when undoing class SelectionAllAtomsInsideSphereState : public ActionState { public: SelectionAllAtomsInsideSphereState(std::vector _selectedAtoms, const Vector &_position, const double _radius) : selectedAtoms(_selectedAtoms), position(_position), radius(_radius) {} std::vector selectedAtoms; Vector position; double radius; }; const char SelectionAllAtomsInsideSphereAction::NAME[] = "select-atoms-inside-sphere"; SelectionAllAtomsInsideSphereAction::SelectionAllAtomsInsideSphereAction() : Action(NAME) {} SelectionAllAtomsInsideSphereAction::~SelectionAllAtomsInsideSphereAction() {} void SelectionAllAtomsInsideSphere(const Vector &position, const double radius) { ValueStorage::getInstance().setCurrentValue(SelectionAllAtomsInsideSphereAction::NAME, radius); ValueStorage::getInstance().setCurrentValue("position", position); ActionRegistry::getInstance().getActionByName(SelectionAllAtomsInsideSphereAction::NAME)->call(Action::NonInteractive); }; Dialog* SelectionAllAtomsInsideSphereAction::fillDialog(Dialog *dialog) { ASSERT(dialog,"No Dialog given when filling action dialog"); dialog->queryDouble(NAME, ValueStorage::getInstance().getDescription(NAME)); dialog->queryVector("position", false, ValueStorage::getInstance().getDescription("position")); return dialog; } Action::state_ptr SelectionAllAtomsInsideSphereAction::performCall() { std::vector selectedAtoms = World::getInstance().getSelectedAtoms(); double radius = 0.; Vector position; ValueStorage::getInstance().queryCurrentValue(NAME, radius); ValueStorage::getInstance().queryCurrentValue("position", position); DoLog(1) && (Log() << Verbose(1) << "Selecting all atoms inside a sphere at " << position << " with radius " << radius << "." << endl); Shape s = translate(resize(Sphere(),radius),position); World::getInstance().selectAllAtoms(AtomByShape(s)); return Action::state_ptr(new SelectionAllAtomsInsideSphereState(selectedAtoms, position, radius)); } Action::state_ptr SelectionAllAtomsInsideSphereAction::performUndo(Action::state_ptr _state) { SelectionAllAtomsInsideSphereState *state = assert_cast(_state.get()); World::getInstance().clearAtomSelection(); for(std::vector::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) World::getInstance().selectAtom(*iter); return Action::state_ptr(new SelectionAllAtomsInsideSphereState(state->selectedAtoms, state->position, state->radius)); } Action::state_ptr SelectionAllAtomsInsideSphereAction::performRedo(Action::state_ptr _state){ SelectionAllAtomsInsideSphereState *state = assert_cast(_state.get()); Shape s = translate(resize(Sphere(),state->radius),state->position); World::getInstance().selectAllAtoms(AtomByShape(s)); return Action::state_ptr(new SelectionAllAtomsInsideSphereState(state->selectedAtoms, state->position, state->radius)); } bool SelectionAllAtomsInsideSphereAction::canUndo() { return true; } bool SelectionAllAtomsInsideSphereAction::shouldUndo() { return true; } const string SelectionAllAtomsInsideSphereAction::getName() { return NAME; }