| 1 | /*
|
|---|
| 2 | * ChangeNameAction.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Jan 15, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 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/MoleculeAction/ChangeNameAction.hpp"
|
|---|
| 16 | #include "Actions/ActionRegistry.hpp"
|
|---|
| 17 | #include "atom.hpp"
|
|---|
| 18 | #include "molecule.hpp"
|
|---|
| 19 |
|
|---|
| 20 | #include <iostream>
|
|---|
| 21 | #include <string>
|
|---|
| 22 |
|
|---|
| 23 | using namespace std;
|
|---|
| 24 |
|
|---|
| 25 | #include "UIElements/UIFactory.hpp"
|
|---|
| 26 | #include "UIElements/Dialog.hpp"
|
|---|
| 27 | #include "Actions/ValueStorage.hpp"
|
|---|
| 28 |
|
|---|
| 29 | /****** MoleculeChangeNameAction *****/
|
|---|
| 30 |
|
|---|
| 31 | // memento to remember the state when undoing
|
|---|
| 32 |
|
|---|
| 33 | class MoleculeChangeNameState : public ActionState {
|
|---|
| 34 | public:
|
|---|
| 35 | MoleculeChangeNameState(molecule* _mol,std::string _lastName) :
|
|---|
| 36 | mol(_mol),
|
|---|
| 37 | lastName(_lastName)
|
|---|
| 38 | {}
|
|---|
| 39 | molecule* mol;
|
|---|
| 40 | std::string lastName;
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | const char MoleculeChangeNameAction::NAME[] = "change-molname";
|
|---|
| 44 |
|
|---|
| 45 | MoleculeChangeNameAction::MoleculeChangeNameAction() :
|
|---|
| 46 | Action(NAME)
|
|---|
| 47 | {}
|
|---|
| 48 |
|
|---|
| 49 | MoleculeChangeNameAction::~MoleculeChangeNameAction()
|
|---|
| 50 | {}
|
|---|
| 51 |
|
|---|
| 52 | void MoleculeChangeName(std::string &name) {
|
|---|
| 53 | ValueStorage::getInstance().setCurrentValue(MoleculeChangeNameAction::NAME, name);
|
|---|
| 54 | ActionRegistry::getInstance().getActionByName(MoleculeChangeNameAction::NAME)->call(Action::NonInteractive);
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | Dialog* MoleculeChangeNameAction::fillDialog(Dialog *dialog) {
|
|---|
| 58 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
|---|
| 59 |
|
|---|
| 60 | dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME));
|
|---|
| 61 |
|
|---|
| 62 | return dialog;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | Action::state_ptr MoleculeChangeNameAction::performCall() {
|
|---|
| 66 | string filename;
|
|---|
| 67 | molecule *mol = NULL;
|
|---|
| 68 |
|
|---|
| 69 | ValueStorage::getInstance().queryCurrentValue(NAME, filename);
|
|---|
| 70 |
|
|---|
| 71 | if (World::getInstance().countSelectedMolecules() == 1) {
|
|---|
| 72 | mol = World::getInstance().beginMoleculeSelection()->second;
|
|---|
| 73 | string oldName = mol->getName();
|
|---|
| 74 | mol->setName(filename);
|
|---|
| 75 | return Action::state_ptr(new MoleculeChangeNameState(mol,oldName));
|
|---|
| 76 | } else
|
|---|
| 77 | return Action::failure;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | Action::state_ptr MoleculeChangeNameAction::performUndo(Action::state_ptr _state) {
|
|---|
| 81 | MoleculeChangeNameState *state = assert_cast<MoleculeChangeNameState*>(_state.get());
|
|---|
| 82 |
|
|---|
| 83 | string newName = state->mol->getName();
|
|---|
| 84 | state->mol->setName(state->lastName);
|
|---|
| 85 |
|
|---|
| 86 | return Action::state_ptr(new MoleculeChangeNameState(state->mol,newName));
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | Action::state_ptr MoleculeChangeNameAction::performRedo(Action::state_ptr _state){
|
|---|
| 90 | // Undo and redo have to do the same for this action
|
|---|
| 91 | return performUndo(_state);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | bool MoleculeChangeNameAction::canUndo() {
|
|---|
| 95 | return true;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | bool MoleculeChangeNameAction::shouldUndo() {
|
|---|
| 99 | return true;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | const string MoleculeChangeNameAction::getName() {
|
|---|
| 103 | return NAME;
|
|---|
| 104 | }
|
|---|