source: molecuilder/src/Actions/small_actions.cpp@ 1561e2

Last change on this file since 1561e2 was 1561e2, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added macros that allow type safe casting using the Assert mechanism

  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[795b4d]1#include "Actions/small_actions.hpp"
2
3#include <iostream>
4#include <string>
5
6using 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
[0012e6]16// memento to remember the state when undoing
17
18class ChangeMoleculeNameState : public ActionState {
19public:
20 ChangeMoleculeNameState(molecule* _mol,std::string _lastName) :
21 mol(_mol),
22 lastName(_lastName)
23 {}
24 molecule* mol;
25 std::string lastName;
26};
27
[8d9984]28const char ChangeMoleculeNameAction::NAME[] = "Change filename of Molecule";
[795b4d]29
30ChangeMoleculeNameAction::ChangeMoleculeNameAction(MoleculeListClass *_molecules) :
31 Action(NAME),
32 molecules(_molecules)
33{}
34
35ChangeMoleculeNameAction::~ChangeMoleculeNameAction()
36{}
37
[521e29]38Action::state_ptr ChangeMoleculeNameAction::performCall() {
[795b4d]39 string filename;
40 molecule *mol = NULL;
[4fc41a]41 Dialog *dialog = UIFactory::getInstance().makeDialog();
[795b4d]42
43 dialog->queryMolecule("Enter index of molecule: ",&mol,molecules);
44 dialog->queryString("Enter name: ",&filename);
[0012e6]45
[795b4d]46 if(dialog->display()) {
[0012e6]47 string oldName = mol->getName();
[adcdf8]48 mol->setName(filename);
[0012e6]49 delete dialog;
[521e29]50 return Action::state_ptr(new ChangeMoleculeNameState(mol,oldName));
[795b4d]51 }
[521e29]52 delete dialog;
53 return Action::failure;
[795b4d]54}
55
[521e29]56Action::state_ptr ChangeMoleculeNameAction::performUndo(Action::state_ptr _state) {
[1561e2]57 ChangeMoleculeNameState *state = assert_cast<ChangeMoleculeNameState*>(_state.get());
[0012e6]58
59 string newName = state->mol->getName();
60 state->mol->setName(state->lastName);
[795b4d]61
[521e29]62 return Action::state_ptr(new ChangeMoleculeNameState(state->mol,newName));
[0012e6]63}
64
[521e29]65Action::state_ptr ChangeMoleculeNameAction::performRedo(Action::state_ptr _state){
[0012e6]66 // Undo and redo have to do the same for this action
67 return performUndo(_state);
[795b4d]68}
69
70bool ChangeMoleculeNameAction::canUndo() {
[0012e6]71 return true;
[795b4d]72}
73
74bool ChangeMoleculeNameAction::shouldUndo() {
75 return true;
76}
77
78const string ChangeMoleculeNameAction::getName() {
79 return NAME;
80}
Note: See TracBrowser for help on using the repository browser.