| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * NotAllAtomsAction.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: May 12, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "Actions/SelectionAction/NotAllAtomsAction.hpp"
 | 
|---|
| 23 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 24 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 25 | #include "atom.hpp"
 | 
|---|
| 26 | #include "Helpers/Log.hpp"
 | 
|---|
| 27 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 28 | #include "World.hpp"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | #include <iostream>
 | 
|---|
| 31 | #include <string>
 | 
|---|
| 32 | 
 | 
|---|
| 33 | using namespace std;
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 36 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 37 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | 
 | 
|---|
| 40 | // memento to remember the state when undoing
 | 
|---|
| 41 | 
 | 
|---|
| 42 | class SelectionNotAllAtomsState : public ActionState {
 | 
|---|
| 43 | public:
 | 
|---|
| 44 |   SelectionNotAllAtomsState(std::vector<atom*> _selectedAtoms) :
 | 
|---|
| 45 |     selectedAtoms(_selectedAtoms)
 | 
|---|
| 46 |   {}
 | 
|---|
| 47 |   std::vector<atom*> selectedAtoms;
 | 
|---|
| 48 | };
 | 
|---|
| 49 | 
 | 
|---|
| 50 | const char SelectionNotAllAtomsAction::NAME[] = "unselect-all-atoms";
 | 
|---|
| 51 | 
 | 
|---|
| 52 | SelectionNotAllAtomsAction::SelectionNotAllAtomsAction() :
 | 
|---|
| 53 |   Action(NAME)
 | 
|---|
| 54 | {}
 | 
|---|
| 55 | 
 | 
|---|
| 56 | SelectionNotAllAtomsAction::~SelectionNotAllAtomsAction()
 | 
|---|
| 57 | {}
 | 
|---|
| 58 | 
 | 
|---|
| 59 | void SelectionNotAllAtoms() {
 | 
|---|
| 60 |   ActionRegistry::getInstance().getActionByName(SelectionNotAllAtomsAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 61 | };
 | 
|---|
| 62 | 
 | 
|---|
| 63 | Dialog* SelectionNotAllAtomsAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 64 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   dialog->queryEmpty(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   return dialog;
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | Action::state_ptr SelectionNotAllAtomsAction::performCall() {
 | 
|---|
| 72 |   std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
 | 
|---|
| 73 |   DoLog(1) && (Log() << Verbose(1) << "Unselecting all atoms." << endl);
 | 
|---|
| 74 |   World::getInstance().clearAtomSelection();
 | 
|---|
| 75 |   return Action::state_ptr(new SelectionNotAllAtomsState(selectedAtoms));
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | Action::state_ptr SelectionNotAllAtomsAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 79 |   SelectionNotAllAtomsState *state = assert_cast<SelectionNotAllAtomsState*>(_state.get());
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   World::getInstance().clearAtomSelection();
 | 
|---|
| 82 |   for(std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter)
 | 
|---|
| 83 |     World::getInstance().selectAtom(*iter);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   return Action::state_ptr(new SelectionNotAllAtomsState(state->selectedAtoms));
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | Action::state_ptr SelectionNotAllAtomsAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 89 |   SelectionNotAllAtomsState *state = assert_cast<SelectionNotAllAtomsState*>(_state.get());
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   World::getInstance().clearAtomSelection();
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   return Action::state_ptr(new SelectionNotAllAtomsState(state->selectedAtoms));
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | bool SelectionNotAllAtomsAction::canUndo() {
 | 
|---|
| 97 |   return true;
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | bool SelectionNotAllAtomsAction::shouldUndo() {
 | 
|---|
| 101 |   return true;
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | const string SelectionNotAllAtomsAction::getName() {
 | 
|---|
| 105 |   return NAME;
 | 
|---|
| 106 | }
 | 
|---|