| [03bb99] | 1 | /*
 | 
|---|
 | 2 |  * LoadXyzAction.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: May 8, 2010
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [112b09] | 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 9 | 
 | 
|---|
| [a1e929] | 10 | using namespace std;
 | 
|---|
 | 11 | 
 | 
|---|
| [03bb99] | 12 | #include "Actions/ParserAction/LoadXyzAction.hpp"
 | 
|---|
 | 13 | #include "Parser/XyzParser.hpp"
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | #include <iostream>
 | 
|---|
| [a1e929] | 16 | #include <set>
 | 
|---|
| [03bb99] | 17 | #include <string>
 | 
|---|
| [a1e929] | 18 | #include <vector>
 | 
|---|
| [03bb99] | 19 | 
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | #include "UIElements/UIFactory.hpp"
 | 
|---|
 | 22 | #include "UIElements/Dialog.hpp"
 | 
|---|
| [97ebf8] | 23 | #include "Actions/MapOfActions.hpp"
 | 
|---|
| [03bb99] | 24 | 
 | 
|---|
 | 25 | #include "atom.hpp"
 | 
|---|
| [a1e929] | 26 | #include "log.hpp"
 | 
|---|
| [03bb99] | 27 | #include "molecule.hpp"
 | 
|---|
| [a1e929] | 28 | #include "verbose.hpp"
 | 
|---|
 | 29 | #include "World.hpp"
 | 
|---|
| [03bb99] | 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 | 
 | 
|---|
| [a1e929] | 45 | const char ParserLoadXyzAction::NAME[] = "parse-xyz";
 | 
|---|
| [03bb99] | 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 | 
 | 
|---|
| [a1e929] | 58 |   dialog->queryString(NAME,&filename, NAME);
 | 
|---|
| [03bb99] | 59 | 
 | 
|---|
 | 60 |   if(dialog->display()) {
 | 
|---|
| [a1e929] | 61 |     DoLog(1) && (Log() << Verbose(1) << "Parsing xyz file for new atoms." << endl);
 | 
|---|
| [03bb99] | 62 |     // parse xyz file
 | 
|---|
 | 63 |     ifstream input;
 | 
|---|
 | 64 |     input.open(filename.c_str());
 | 
|---|
| [a1e929] | 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
 | 
|---|
| [03bb99] | 74 |       parser.load(&input);
 | 
|---|
| [a1e929] | 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= NULL;
 | 
|---|
 | 81 |           if (molecules->ListOfMolecules.empty()) {
 | 
|---|
 | 82 |             mol = World::getInstance().createMolecule();
 | 
|---|
 | 83 |             molecules->insert(mol);
 | 
|---|
 | 84 |           } else {
 | 
|---|
 | 85 |             mol = *(molecules->ListOfMolecules.begin());
 | 
|---|
 | 86 |           }
 | 
|---|
 | 87 |           for (vector<atom *>::iterator runner = ListAfter.begin(); runner != ListAfter.end(); ++runner) {
 | 
|---|
 | 88 |             Inserter = UniqueList.insert(*runner);
 | 
|---|
 | 89 |             if (Inserter.second) { // if not present, then new (just parsed) atom, add ...
 | 
|---|
 | 90 |               cout << "Adding new atom " << **runner << " to new mol." << endl;
 | 
|---|
 | 91 |               mol->AddAtom(*runner);
 | 
|---|
 | 92 |             }
 | 
|---|
 | 93 |           }
 | 
|---|
 | 94 |           mol->doCountAtoms();
 | 
|---|
 | 95 |         } else {
 | 
|---|
 | 96 |           cout << "No atoms parsed?" << endl;
 | 
|---|
 | 97 |         }
 | 
|---|
 | 98 |       }
 | 
|---|
 | 99 |     } else {
 | 
|---|
 | 100 |       DoeLog(1) && (eLog() << Verbose(1) << "Could not open file " << filename << "." << endl);
 | 
|---|
 | 101 |     }
 | 
|---|
| [03bb99] | 102 |     input.close();
 | 
|---|
 | 103 |   }
 | 
|---|
 | 104 |   delete dialog;
 | 
|---|
 | 105 |   return Action::failure;
 | 
|---|
 | 106 | }
 | 
|---|
 | 107 | 
 | 
|---|
| [808fd3] | 108 | Action::state_ptr ParserLoadXyzAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| [03bb99] | 109 | //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
 | 
|---|
| [808fd3] | 110 | 
 | 
|---|
 | 111 |   return Action::failure;
 | 
|---|
| [03bb99] | 112 | //  string newName = state->mol->getName();
 | 
|---|
 | 113 | //  state->mol->setName(state->lastName);
 | 
|---|
 | 114 | //
 | 
|---|
 | 115 | //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
 | 
|---|
| [808fd3] | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | Action::state_ptr ParserLoadXyzAction::performRedo(Action::state_ptr _state){
 | 
|---|
 | 119 |   return Action::failure;
 | 
|---|
 | 120 | }
 | 
|---|
| [03bb99] | 121 | 
 | 
|---|
 | 122 | bool ParserLoadXyzAction::canUndo() {
 | 
|---|
 | 123 |   return false;
 | 
|---|
 | 124 | }
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 | bool ParserLoadXyzAction::shouldUndo() {
 | 
|---|
 | 127 |   return false;
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | const string ParserLoadXyzAction::getName() {
 | 
|---|
 | 131 |   return NAME;
 | 
|---|
 | 132 | }
 | 
|---|