1 | #include "Actions/small_actions.hpp"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | #include <string>
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | #include "UIElements/UIFactory.hpp"
|
---|
9 | #include "UIElements/Dialog.hpp"
|
---|
10 |
|
---|
11 | #include "atom.hpp"
|
---|
12 | #include "molecule.hpp"
|
---|
13 |
|
---|
14 | /****** ChangeMoleculeNameAction *****/
|
---|
15 |
|
---|
16 | // memento to remember the state when undoing
|
---|
17 |
|
---|
18 | class ChangeMoleculeNameState : public ActionState {
|
---|
19 | public:
|
---|
20 | ChangeMoleculeNameState(molecule* _mol,std::string _lastName) :
|
---|
21 | mol(_mol),
|
---|
22 | lastName(_lastName)
|
---|
23 | {}
|
---|
24 | molecule* mol;
|
---|
25 | std::string lastName;
|
---|
26 | };
|
---|
27 |
|
---|
28 | char ChangeMoleculeNameAction::NAME[] = "Change filename of Molecule";
|
---|
29 |
|
---|
30 | ChangeMoleculeNameAction::ChangeMoleculeNameAction(MoleculeListClass *_molecules) :
|
---|
31 | Action(NAME),
|
---|
32 | molecules(_molecules)
|
---|
33 | {}
|
---|
34 |
|
---|
35 | ChangeMoleculeNameAction::~ChangeMoleculeNameAction()
|
---|
36 | {}
|
---|
37 |
|
---|
38 | ActionState* ChangeMoleculeNameAction::performCall() {
|
---|
39 | string filename;
|
---|
40 | molecule *mol = NULL;
|
---|
41 | Dialog *dialog = UIFactory::getInstance().makeDialog();
|
---|
42 |
|
---|
43 | dialog->queryMolecule("Enter index of molecule: ",&mol,molecules);
|
---|
44 | dialog->queryString("Enter name: ",&filename);
|
---|
45 |
|
---|
46 | if(dialog->display()) {
|
---|
47 | string oldName = mol->getName();
|
---|
48 | mol->setName(filename);
|
---|
49 | delete dialog;
|
---|
50 | return new ChangeMoleculeNameState(mol,oldName);
|
---|
51 | }
|
---|
52 | return 0;
|
---|
53 | }
|
---|
54 |
|
---|
55 | ActionState* ChangeMoleculeNameAction::performUndo(ActionState* _state) {
|
---|
56 | ChangeMoleculeNameState *state = dynamic_cast<ChangeMoleculeNameState*>(_state);
|
---|
57 | ASSERT(state,"State passed to ChangeMoleculeNameAction::performUndo did not have correct type");
|
---|
58 |
|
---|
59 | string newName = state->mol->getName();
|
---|
60 | state->mol->setName(state->lastName);
|
---|
61 |
|
---|
62 | return new ChangeMoleculeNameState(state->mol,newName);
|
---|
63 | }
|
---|
64 |
|
---|
65 | ActionState* ChangeMoleculeNameAction::performRedo(ActionState *_state){
|
---|
66 | // Undo and redo have to do the same for this action
|
---|
67 | return performUndo(_state);
|
---|
68 | }
|
---|
69 |
|
---|
70 | bool ChangeMoleculeNameAction::canUndo() {
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | bool ChangeMoleculeNameAction::shouldUndo() {
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | const string ChangeMoleculeNameAction::getName() {
|
---|
79 | return NAME;
|
---|
80 | }
|
---|