| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * SaveXyzAction.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: May 8, 2010 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include "Helpers/MemDebug.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include "Actions/ParserAction/SaveXyzAction.hpp" | 
|---|
| 23 | #include "Actions/ActionRegistry.hpp" | 
|---|
| 24 | #include "Parser/XyzParser.hpp" | 
|---|
| 25 | #include "atom.hpp" | 
|---|
| 26 | #include "molecule.hpp" | 
|---|
| 27 |  | 
|---|
| 28 | #include <iostream> | 
|---|
| 29 | #include <string> | 
|---|
| 30 |  | 
|---|
| 31 | using namespace std; | 
|---|
| 32 |  | 
|---|
| 33 | #include "UIElements/UIFactory.hpp" | 
|---|
| 34 | #include "UIElements/Dialog.hpp" | 
|---|
| 35 | #include "Actions/ValueStorage.hpp" | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | /****** ParserSaveXyzAction *****/ | 
|---|
| 39 |  | 
|---|
| 40 | //// memento to remember the state when undoing | 
|---|
| 41 | // | 
|---|
| 42 | //class ParserSaveXyzState : public ActionState { | 
|---|
| 43 | //public: | 
|---|
| 44 | //  ParserSaveXyzState(molecule* _mol,std::string _lastName) : | 
|---|
| 45 | //    mol(_mol), | 
|---|
| 46 | //    lastName(_lastName) | 
|---|
| 47 | //  {} | 
|---|
| 48 | //  molecule* mol; | 
|---|
| 49 | //  std::string lastName; | 
|---|
| 50 | //}; | 
|---|
| 51 |  | 
|---|
| 52 | const char ParserSaveXyzAction::NAME[] = "SaveXyz"; | 
|---|
| 53 |  | 
|---|
| 54 | ParserSaveXyzAction::ParserSaveXyzAction() : | 
|---|
| 55 | Action(NAME) | 
|---|
| 56 | {} | 
|---|
| 57 |  | 
|---|
| 58 | ParserSaveXyzAction::~ParserSaveXyzAction() | 
|---|
| 59 | {} | 
|---|
| 60 |  | 
|---|
| 61 | void ParserSaveXyz(std::string &filename) { | 
|---|
| 62 | ValueStorage::getInstance().setCurrentValue(ParserSaveXyzAction::NAME, filename); | 
|---|
| 63 | ActionRegistry::getInstance().getActionByName(ParserSaveXyzAction::NAME)->call(Action::NonInteractive); | 
|---|
| 64 | }; | 
|---|
| 65 |  | 
|---|
| 66 | Dialog* ParserSaveXyzAction::fillDialog(Dialog *dialog) { | 
|---|
| 67 | ASSERT(dialog,"No Dialog given when filling action dialog"); | 
|---|
| 68 |  | 
|---|
| 69 | dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME)); | 
|---|
| 70 |  | 
|---|
| 71 | return dialog; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | Action::state_ptr ParserSaveXyzAction::performCall() { | 
|---|
| 75 | string filename; | 
|---|
| 76 | XyzParser parser; | 
|---|
| 77 |  | 
|---|
| 78 | ValueStorage::getInstance().queryCurrentValue(NAME, filename); | 
|---|
| 79 |  | 
|---|
| 80 | // store xyz file | 
|---|
| 81 | ofstream output; | 
|---|
| 82 | output.open(filename.c_str()); | 
|---|
| 83 | if (!output.fail()) | 
|---|
| 84 | parser.save(&output); | 
|---|
| 85 | output.close(); | 
|---|
| 86 | return Action::failure; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | Action::state_ptr ParserSaveXyzAction::performUndo(Action::state_ptr _state) { | 
|---|
| 90 | //  ParserSaveXyzState *state = assert_cast<ParserSaveXyzState*>(_state.get()); | 
|---|
| 91 |  | 
|---|
| 92 | return Action::failure; | 
|---|
| 93 | //  string newName = state->mol->getName(); | 
|---|
| 94 | //  state->mol->setName(state->lastName); | 
|---|
| 95 | // | 
|---|
| 96 | //  return Action::state_ptr(new ParserSaveXyzState(state->mol,newName)); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | Action::state_ptr ParserSaveXyzAction::performRedo(Action::state_ptr _state){ | 
|---|
| 100 | return Action::failure; | 
|---|
| 101 | //  // Undo and redo have to do the same for this action | 
|---|
| 102 | //  return performUndo(_state); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | bool ParserSaveXyzAction::canUndo() { | 
|---|
| 106 | return false; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | bool ParserSaveXyzAction::shouldUndo() { | 
|---|
| 110 | return false; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | const string ParserSaveXyzAction::getName() { | 
|---|
| 114 | return NAME; | 
|---|
| 115 | } | 
|---|