| 1 | /*
 | 
|---|
| 2 |  * AllAtomsAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 12, 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/AllAtomsAction.hpp"
 | 
|---|
| 16 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 17 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 18 | #include "atom.hpp"
 | 
|---|
| 19 | #include "Helpers/Log.hpp"
 | 
|---|
| 20 | #include "Helpers/Verbose.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 SelectionAllAtomsState : public ActionState {
 | 
|---|
| 36 | public:
 | 
|---|
| 37 |   SelectionAllAtomsState(std::vector<atom*> _selectedAtoms) :
 | 
|---|
| 38 |     selectedAtoms(_selectedAtoms)
 | 
|---|
| 39 |   {}
 | 
|---|
| 40 |   std::vector<atom*> selectedAtoms;
 | 
|---|
| 41 | };
 | 
|---|
| 42 | 
 | 
|---|
| 43 | const char SelectionAllAtomsAction::NAME[] = "select-all-atoms";
 | 
|---|
| 44 | 
 | 
|---|
| 45 | SelectionAllAtomsAction::SelectionAllAtomsAction() :
 | 
|---|
| 46 |   Action(NAME)
 | 
|---|
| 47 | {}
 | 
|---|
| 48 | 
 | 
|---|
| 49 | SelectionAllAtomsAction::~SelectionAllAtomsAction()
 | 
|---|
| 50 | {}
 | 
|---|
| 51 | 
 | 
|---|
| 52 | void SelectionAllAtoms() {
 | 
|---|
| 53 |   ActionRegistry::getInstance().getActionByName(SelectionAllAtomsAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 54 | };
 | 
|---|
| 55 | 
 | 
|---|
| 56 | Dialog* SelectionAllAtomsAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 57 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   dialog->queryEmpty(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   return dialog;
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | Action::state_ptr SelectionAllAtomsAction::performCall() {
 | 
|---|
| 65 |   std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
 | 
|---|
| 66 |   DoLog(1) && (Log() << Verbose(1) << "Selecting all atoms." << endl);
 | 
|---|
| 67 |   World::getInstance().selectAllAtoms(AllAtoms());
 | 
|---|
| 68 |   return Action::state_ptr(new SelectionAllAtomsState(selectedAtoms));
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | Action::state_ptr SelectionAllAtomsAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 72 |   SelectionAllAtomsState *state = assert_cast<SelectionAllAtomsState*>(_state.get());
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   World::getInstance().clearAtomSelection();
 | 
|---|
| 75 |   for(std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter)
 | 
|---|
| 76 |     World::getInstance().selectAtom(*iter);
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   return Action::state_ptr(new SelectionAllAtomsState(state->selectedAtoms));
 | 
|---|
| 79 | }
 | 
|---|
| 80 | 
 | 
|---|
| 81 | Action::state_ptr SelectionAllAtomsAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 82 |   SelectionAllAtomsState *state = assert_cast<SelectionAllAtomsState*>(_state.get());
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   World::getInstance().selectAllAtoms(AllAtoms());
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   return Action::state_ptr(new SelectionAllAtomsState(state->selectedAtoms));
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | bool SelectionAllAtomsAction::canUndo() {
 | 
|---|
| 90 |   return true;
 | 
|---|
| 91 | }
 | 
|---|
| 92 | 
 | 
|---|
| 93 | bool SelectionAllAtomsAction::shouldUndo() {
 | 
|---|
| 94 |   return true;
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | const string SelectionAllAtomsAction::getName() {
 | 
|---|
| 98 |   return NAME;
 | 
|---|
| 99 | }
 | 
|---|