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