[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"
|
---|
[0430e3] | 13 | #include "Actions/ActionRegistry.hpp"
|
---|
[03bb99] | 14 | #include "Parser/XyzParser.hpp"
|
---|
[215b89] | 15 | #include "atom.hpp"
|
---|
| 16 | #include "log.hpp"
|
---|
| 17 | #include "molecule.hpp"
|
---|
| 18 | #include "verbose.hpp"
|
---|
| 19 | #include "World.hpp"
|
---|
[03bb99] | 20 |
|
---|
| 21 | #include <iostream>
|
---|
[a1e929] | 22 | #include <set>
|
---|
[03bb99] | 23 | #include <string>
|
---|
[a1e929] | 24 | #include <vector>
|
---|
[03bb99] | 25 |
|
---|
| 26 | #include "UIElements/UIFactory.hpp"
|
---|
| 27 | #include "UIElements/Dialog.hpp"
|
---|
[0454de] | 28 | #include "UIElements/ValueStorage.hpp"
|
---|
[03bb99] | 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 |
|
---|
[a1e929] | 44 | const char ParserLoadXyzAction::NAME[] = "parse-xyz";
|
---|
[03bb99] | 45 |
|
---|
| 46 | ParserLoadXyzAction::ParserLoadXyzAction() :
|
---|
| 47 | Action(NAME)
|
---|
| 48 | {}
|
---|
| 49 |
|
---|
| 50 | ParserLoadXyzAction::~ParserLoadXyzAction()
|
---|
| 51 | {}
|
---|
| 52 |
|
---|
[215b89] | 53 | void ParserLoadXyz(std::string &filename) {
|
---|
| 54 | ValueStorage::getInstance().setCurrentValue(ParserLoadXyzAction::NAME, filename);
|
---|
| 55 | ActionRegistry::getInstance().getActionByName(ParserLoadXyzAction::NAME)->call(Action::NonInteractive);
|
---|
| 56 | };
|
---|
| 57 |
|
---|
[0454de] | 58 | Dialog* ParserLoadXyzAction::createDialog() {
|
---|
| 59 | Dialog *dialog = UIFactory::getInstance().makeDialog();
|
---|
| 60 |
|
---|
| 61 | dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME));
|
---|
| 62 |
|
---|
| 63 | return dialog;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[03bb99] | 66 | Action::state_ptr ParserLoadXyzAction::performCall() {
|
---|
| 67 | string filename;
|
---|
| 68 |
|
---|
[0454de] | 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);
|
---|
[a1e929] | 97 | }
|
---|
| 98 | }
|
---|
[0454de] | 99 | mol->doCountAtoms();
|
---|
| 100 | } else {
|
---|
| 101 | cout << "No atoms parsed?" << endl;
|
---|
[a1e929] | 102 | }
|
---|
| 103 | }
|
---|
[0454de] | 104 | } else {
|
---|
| 105 | DoeLog(1) && (eLog() << Verbose(1) << "Could not open file " << filename << "." << endl);
|
---|
[03bb99] | 106 | }
|
---|
[0454de] | 107 | input.close();
|
---|
| 108 | return Action::success;
|
---|
[03bb99] | 109 | }
|
---|
| 110 |
|
---|
[808fd3] | 111 | Action::state_ptr ParserLoadXyzAction::performUndo(Action::state_ptr _state) {
|
---|
[03bb99] | 112 | // ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
|
---|
[808fd3] | 113 |
|
---|
| 114 | return Action::failure;
|
---|
[03bb99] | 115 | // string newName = state->mol->getName();
|
---|
| 116 | // state->mol->setName(state->lastName);
|
---|
| 117 | //
|
---|
| 118 | // return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
|
---|
[808fd3] | 119 | }
|
---|
| 120 |
|
---|
| 121 | Action::state_ptr ParserLoadXyzAction::performRedo(Action::state_ptr _state){
|
---|
| 122 | return Action::failure;
|
---|
| 123 | }
|
---|
[03bb99] | 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 | }
|
---|