| 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 | * LoadXyzAction.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 | using namespace std; | 
|---|
| 23 |  | 
|---|
| 24 | #include "Actions/ParserAction/LoadXyzAction.hpp" | 
|---|
| 25 | #include "Actions/ActionRegistry.hpp" | 
|---|
| 26 | #include "Parser/XyzParser.hpp" | 
|---|
| 27 | #include "atom.hpp" | 
|---|
| 28 | #include "Helpers/Log.hpp" | 
|---|
| 29 | #include "molecule.hpp" | 
|---|
| 30 | #include "Helpers/Verbose.hpp" | 
|---|
| 31 | #include "World.hpp" | 
|---|
| 32 |  | 
|---|
| 33 | #include <iostream> | 
|---|
| 34 | #include <set> | 
|---|
| 35 | #include <string> | 
|---|
| 36 | #include <vector> | 
|---|
| 37 |  | 
|---|
| 38 | #include "UIElements/UIFactory.hpp" | 
|---|
| 39 | #include "UIElements/Dialog.hpp" | 
|---|
| 40 | #include "Actions/ValueStorage.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | /****** ParserLoadXyzAction *****/ | 
|---|
| 43 |  | 
|---|
| 44 | //// memento to remember the state when undoing | 
|---|
| 45 | // | 
|---|
| 46 | //class ParserLoadXyzState : public ActionState { | 
|---|
| 47 | //public: | 
|---|
| 48 | //  ParserLoadXyzState(molecule* _mol,std::string _lastName) : | 
|---|
| 49 | //    mol(_mol), | 
|---|
| 50 | //    lastName(_lastName) | 
|---|
| 51 | //  {} | 
|---|
| 52 | //  molecule* mol; | 
|---|
| 53 | //  std::string lastName; | 
|---|
| 54 | //}; | 
|---|
| 55 |  | 
|---|
| 56 | const char ParserLoadXyzAction::NAME[] = "parse-xyz"; | 
|---|
| 57 |  | 
|---|
| 58 | ParserLoadXyzAction::ParserLoadXyzAction() : | 
|---|
| 59 | Action(NAME) | 
|---|
| 60 | {} | 
|---|
| 61 |  | 
|---|
| 62 | ParserLoadXyzAction::~ParserLoadXyzAction() | 
|---|
| 63 | {} | 
|---|
| 64 |  | 
|---|
| 65 | void ParserLoadXyz(std::string &filename) { | 
|---|
| 66 | ValueStorage::getInstance().setCurrentValue(ParserLoadXyzAction::NAME, filename); | 
|---|
| 67 | ActionRegistry::getInstance().getActionByName(ParserLoadXyzAction::NAME)->call(Action::NonInteractive); | 
|---|
| 68 | }; | 
|---|
| 69 |  | 
|---|
| 70 | Dialog* ParserLoadXyzAction::fillDialog(Dialog *dialog) { | 
|---|
| 71 | ASSERT(dialog,"No Dialog given when filling action dialog"); | 
|---|
| 72 |  | 
|---|
| 73 | dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME)); | 
|---|
| 74 |  | 
|---|
| 75 | return dialog; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | Action::state_ptr ParserLoadXyzAction::performCall() { | 
|---|
| 79 | string filename; | 
|---|
| 80 |  | 
|---|
| 81 | ValueStorage::getInstance().queryCurrentValue(NAME, filename); | 
|---|
| 82 |  | 
|---|
| 83 | DoLog(1) && (Log() << Verbose(1) << "Parsing xyz file for new atoms." << endl); | 
|---|
| 84 | // parse xyz file | 
|---|
| 85 | ifstream input; | 
|---|
| 86 | input.open(filename.c_str()); | 
|---|
| 87 | if (!input.fail()) { | 
|---|
| 88 | XyzParser parser; // briefly instantiate a parser which is removed at end of focus | 
|---|
| 89 | parser.load(&input); | 
|---|
| 90 | } else { | 
|---|
| 91 | DoeLog(1) && (eLog() << Verbose(1) << "Could not open file " << filename << "." << endl); | 
|---|
| 92 | } | 
|---|
| 93 | input.close(); | 
|---|
| 94 | return Action::success; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | Action::state_ptr ParserLoadXyzAction::performUndo(Action::state_ptr _state) { | 
|---|
| 98 | //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get()); | 
|---|
| 99 |  | 
|---|
| 100 | return Action::failure; | 
|---|
| 101 | //  string newName = state->mol->getName(); | 
|---|
| 102 | //  state->mol->setName(state->lastName); | 
|---|
| 103 | // | 
|---|
| 104 | //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName)); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | Action::state_ptr ParserLoadXyzAction::performRedo(Action::state_ptr _state){ | 
|---|
| 108 | return Action::failure; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | bool ParserLoadXyzAction::canUndo() { | 
|---|
| 112 | return false; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | bool ParserLoadXyzAction::shouldUndo() { | 
|---|
| 116 | return false; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | const string ParserLoadXyzAction::getName() { | 
|---|
| 120 | return NAME; | 
|---|
| 121 | } | 
|---|