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