[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();
|
---|
[ed6dd8] | 80 | molecule *mol = World::getInstance().createMolecule();
|
---|
| 81 | molecules->insert(mol);
|
---|
[a1e929] | 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 | }
|
---|
[03bb99] | 97 | input.close();
|
---|
| 98 | }
|
---|
| 99 | delete dialog;
|
---|
| 100 | return Action::failure;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[808fd3] | 103 | Action::state_ptr ParserLoadXyzAction::performUndo(Action::state_ptr _state) {
|
---|
[03bb99] | 104 | // ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
|
---|
[808fd3] | 105 |
|
---|
| 106 | return Action::failure;
|
---|
[03bb99] | 107 | // string newName = state->mol->getName();
|
---|
| 108 | // state->mol->setName(state->lastName);
|
---|
| 109 | //
|
---|
| 110 | // return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
|
---|
[808fd3] | 111 | }
|
---|
| 112 |
|
---|
| 113 | Action::state_ptr ParserLoadXyzAction::performRedo(Action::state_ptr _state){
|
---|
| 114 | return Action::failure;
|
---|
| 115 | }
|
---|
[03bb99] | 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 | }
|
---|