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