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= 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 | }
|
---|
102 | input.close();
|
---|
103 | }
|
---|
104 | delete dialog;
|
---|
105 | return Action::failure;
|
---|
106 | }
|
---|
107 |
|
---|
108 | Action::state_ptr ParserLoadXyzAction::performUndo(Action::state_ptr _state) {
|
---|
109 | // ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
|
---|
110 |
|
---|
111 | return Action::failure;
|
---|
112 | // string newName = state->mol->getName();
|
---|
113 | // state->mol->setName(state->lastName);
|
---|
114 | //
|
---|
115 | // return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
|
---|
116 | }
|
---|
117 |
|
---|
118 | Action::state_ptr ParserLoadXyzAction::performRedo(Action::state_ptr _state){
|
---|
119 | return Action::failure;
|
---|
120 | }
|
---|
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 | }
|
---|