| 1 | /* | 
|---|
| 2 | * LoadXyzAction.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: May 8, 2010 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Helpers/MemDebug.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | using namespace std; | 
|---|
| 11 |  | 
|---|
| 12 | #include "Actions/ParserAction/LoadXyzAction.hpp" | 
|---|
| 13 | #include "Actions/ActionRegistry.hpp" | 
|---|
| 14 | #include "Parser/XyzParser.hpp" | 
|---|
| 15 | #include "atom.hpp" | 
|---|
| 16 | #include "Helpers/Log.hpp" | 
|---|
| 17 | #include "molecule.hpp" | 
|---|
| 18 | #include "Helpers/Verbose.hpp" | 
|---|
| 19 | #include "World.hpp" | 
|---|
| 20 |  | 
|---|
| 21 | #include <iostream> | 
|---|
| 22 | #include <set> | 
|---|
| 23 | #include <string> | 
|---|
| 24 | #include <vector> | 
|---|
| 25 |  | 
|---|
| 26 | #include "UIElements/UIFactory.hpp" | 
|---|
| 27 | #include "UIElements/Dialog.hpp" | 
|---|
| 28 | #include "Actions/ValueStorage.hpp" | 
|---|
| 29 |  | 
|---|
| 30 | /****** ParserLoadXyzAction *****/ | 
|---|
| 31 |  | 
|---|
| 32 | //// memento to remember the state when undoing | 
|---|
| 33 | // | 
|---|
| 34 | //class ParserLoadXyzState : public ActionState { | 
|---|
| 35 | //public: | 
|---|
| 36 | //  ParserLoadXyzState(molecule* _mol,std::string _lastName) : | 
|---|
| 37 | //    mol(_mol), | 
|---|
| 38 | //    lastName(_lastName) | 
|---|
| 39 | //  {} | 
|---|
| 40 | //  molecule* mol; | 
|---|
| 41 | //  std::string lastName; | 
|---|
| 42 | //}; | 
|---|
| 43 |  | 
|---|
| 44 | const char ParserLoadXyzAction::NAME[] = "parse-xyz"; | 
|---|
| 45 |  | 
|---|
| 46 | ParserLoadXyzAction::ParserLoadXyzAction() : | 
|---|
| 47 | Action(NAME) | 
|---|
| 48 | {} | 
|---|
| 49 |  | 
|---|
| 50 | ParserLoadXyzAction::~ParserLoadXyzAction() | 
|---|
| 51 | {} | 
|---|
| 52 |  | 
|---|
| 53 | void ParserLoadXyz(std::string &filename) { | 
|---|
| 54 | ValueStorage::getInstance().setCurrentValue(ParserLoadXyzAction::NAME, filename); | 
|---|
| 55 | ActionRegistry::getInstance().getActionByName(ParserLoadXyzAction::NAME)->call(Action::NonInteractive); | 
|---|
| 56 | }; | 
|---|
| 57 |  | 
|---|
| 58 | Dialog* ParserLoadXyzAction::fillDialog(Dialog *dialog) { | 
|---|
| 59 | ASSERT(dialog,"No Dialog given when filling action dialog"); | 
|---|
| 60 |  | 
|---|
| 61 | dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME)); | 
|---|
| 62 |  | 
|---|
| 63 | return dialog; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | Action::state_ptr ParserLoadXyzAction::performCall() { | 
|---|
| 67 | string filename; | 
|---|
| 68 |  | 
|---|
| 69 | ValueStorage::getInstance().queryCurrentValue(NAME, filename); | 
|---|
| 70 |  | 
|---|
| 71 | DoLog(1) && (Log() << Verbose(1) << "Parsing xyz file for new atoms." << endl); | 
|---|
| 72 | // parse xyz file | 
|---|
| 73 | ifstream input; | 
|---|
| 74 | input.open(filename.c_str()); | 
|---|
| 75 | if (!input.fail()) { | 
|---|
| 76 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include | 
|---|
| 77 | set <atom*> UniqueList; | 
|---|
| 78 | { | 
|---|
| 79 | vector<atom *> ListBefore = World::getInstance().getAllAtoms(); | 
|---|
| 80 | for (vector<atom *>::iterator runner = ListBefore.begin();runner != ListBefore.end(); ++runner) | 
|---|
| 81 | UniqueList.insert(*runner); | 
|---|
| 82 | } | 
|---|
| 83 | XyzParser parser; // briefly instantiate a parser which is removed at end of focus | 
|---|
| 84 | parser.load(&input); | 
|---|
| 85 | { | 
|---|
| 86 | vector<atom *> ListAfter = World::getInstance().getAllAtoms(); | 
|---|
| 87 | pair< set<atom *>::iterator, bool > Inserter; | 
|---|
| 88 | if (UniqueList.size() != ListAfter.size()) { // only create if new atoms have been parsed | 
|---|
| 89 | MoleculeListClass *molecules = World::getInstance().getMolecules(); | 
|---|
| 90 | molecule *mol = World::getInstance().createMolecule(); | 
|---|
| 91 | molecules->insert(mol); | 
|---|
| 92 | for (vector<atom *>::iterator runner = ListAfter.begin(); runner != ListAfter.end(); ++runner) { | 
|---|
| 93 | Inserter = UniqueList.insert(*runner); | 
|---|
| 94 | if (Inserter.second) { // if not present, then new (just parsed) atom, add ... | 
|---|
| 95 | cout << "Adding new atom " << **runner << " to new mol." << endl; | 
|---|
| 96 | mol->AddAtom(*runner); | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 | mol->doCountAtoms(); | 
|---|
| 100 | } else { | 
|---|
| 101 | cout << "No atoms parsed?" << endl; | 
|---|
| 102 | } | 
|---|
| 103 | } | 
|---|
| 104 | } else { | 
|---|
| 105 | DoeLog(1) && (eLog() << Verbose(1) << "Could not open file " << filename << "." << endl); | 
|---|
| 106 | } | 
|---|
| 107 | input.close(); | 
|---|
| 108 | return Action::success; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | Action::state_ptr ParserLoadXyzAction::performUndo(Action::state_ptr _state) { | 
|---|
| 112 | //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get()); | 
|---|
| 113 |  | 
|---|
| 114 | return Action::failure; | 
|---|
| 115 | //  string newName = state->mol->getName(); | 
|---|
| 116 | //  state->mol->setName(state->lastName); | 
|---|
| 117 | // | 
|---|
| 118 | //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName)); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | Action::state_ptr ParserLoadXyzAction::performRedo(Action::state_ptr _state){ | 
|---|
| 122 | return Action::failure; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | bool ParserLoadXyzAction::canUndo() { | 
|---|
| 126 | return false; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | bool ParserLoadXyzAction::shouldUndo() { | 
|---|
| 130 | return false; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | const string ParserLoadXyzAction::getName() { | 
|---|
| 134 | return NAME; | 
|---|
| 135 | } | 
|---|