1 | /*
|
---|
2 | * TextDialog.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jan 5, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <iostream>
|
---|
9 |
|
---|
10 | #include "UIElements/TextDialog.hpp"
|
---|
11 |
|
---|
12 | #include "World.hpp"
|
---|
13 | #include "periodentafel.hpp"
|
---|
14 | #include "atom.hpp"
|
---|
15 | #include "molecule.hpp"
|
---|
16 | #include "log.hpp"
|
---|
17 | #include "verbose.hpp"
|
---|
18 |
|
---|
19 | using namespace std;
|
---|
20 |
|
---|
21 |
|
---|
22 | TextDialog::TextDialog()
|
---|
23 | {
|
---|
24 | }
|
---|
25 |
|
---|
26 | TextDialog::~TextDialog()
|
---|
27 | {
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | void TextDialog::queryEmpty(const char* title, string description){
|
---|
32 | registerQuery(new EmptyTextQuery(title,description));
|
---|
33 | }
|
---|
34 |
|
---|
35 | void TextDialog::queryInt(const char* title, int* target, string description){
|
---|
36 | registerQuery(new IntTextQuery(title,target,description));
|
---|
37 | }
|
---|
38 |
|
---|
39 | void TextDialog::queryDouble(const char* title, double* target, string description){
|
---|
40 | registerQuery(new DoubleTextQuery(title,target,description));
|
---|
41 | }
|
---|
42 |
|
---|
43 | void TextDialog::queryString(const char* title, string* target, string description){
|
---|
44 | registerQuery(new StringTextQuery(title,target,description));
|
---|
45 | }
|
---|
46 |
|
---|
47 | void TextDialog::queryMolecule(const char* title, molecule **target, MoleculeListClass *molecules, string description) {
|
---|
48 | registerQuery(new MoleculeTextQuery(title,target,molecules,description));
|
---|
49 | }
|
---|
50 |
|
---|
51 | void TextDialog::queryVector(const char* title, Vector *target,const double *const cellSize, bool check, string description) {
|
---|
52 | registerQuery(new VectorTextQuery(title,target,cellSize,check,description));
|
---|
53 | }
|
---|
54 |
|
---|
55 | void TextDialog::queryElement(const char* title, const element **target, string description){
|
---|
56 | registerQuery(new ElementTextQuery(title,target,description));
|
---|
57 | }
|
---|
58 |
|
---|
59 | /************************** Query Infrastructure ************************/
|
---|
60 |
|
---|
61 | TextDialog::EmptyTextQuery::EmptyTextQuery(string title, std::string _description) :
|
---|
62 | Dialog::EmptyQuery(title,_description)
|
---|
63 | {}
|
---|
64 |
|
---|
65 | TextDialog::EmptyTextQuery::~EmptyTextQuery() {}
|
---|
66 |
|
---|
67 | bool TextDialog::EmptyTextQuery::handle() {
|
---|
68 | cout << "Message of " << getTitle() << ":\n" << getDescription() << "\n";
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | TextDialog::IntTextQuery::IntTextQuery(string title, int * _target, std::string _description) :
|
---|
73 | Dialog::IntQuery(title,_target,_description)
|
---|
74 | {}
|
---|
75 |
|
---|
76 | TextDialog::IntTextQuery::~IntTextQuery() {}
|
---|
77 |
|
---|
78 | bool TextDialog::IntTextQuery::handle() {
|
---|
79 | bool badInput = false;
|
---|
80 | do{
|
---|
81 | badInput = false;
|
---|
82 | Log() << Verbose(0) << getTitle();
|
---|
83 | cin >> tmp;
|
---|
84 | if(cin.fail()){
|
---|
85 | badInput=true;
|
---|
86 | cin.clear();
|
---|
87 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
|
---|
88 | Log() << Verbose(0) << "Input was not a number!" << endl;
|
---|
89 | }
|
---|
90 | } while(badInput);
|
---|
91 | // clear the input buffer of anything still in the line
|
---|
92 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | TextDialog::StringTextQuery::StringTextQuery(string title,string *_target, std::string _description) :
|
---|
97 | Dialog::StringQuery(title,_target,_description)
|
---|
98 | {}
|
---|
99 |
|
---|
100 | TextDialog::StringTextQuery::~StringTextQuery() {}
|
---|
101 |
|
---|
102 | bool TextDialog::StringTextQuery::handle() {
|
---|
103 | Log() << Verbose(0) << getTitle();
|
---|
104 | getline(cin,tmp);
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | TextDialog::DoubleTextQuery::DoubleTextQuery(string title,double *_target, std::string _description) :
|
---|
109 | Dialog::DoubleQuery(title,_target,_description)
|
---|
110 | {}
|
---|
111 |
|
---|
112 | TextDialog::DoubleTextQuery::~DoubleTextQuery() {}
|
---|
113 |
|
---|
114 | bool TextDialog::DoubleTextQuery::handle() {
|
---|
115 | bool badInput = false;
|
---|
116 | do{
|
---|
117 | badInput = false;
|
---|
118 | Log() << Verbose(0) << getTitle();
|
---|
119 | cin >> tmp;
|
---|
120 | if(cin.fail()){
|
---|
121 | badInput = true;
|
---|
122 | cin.clear();
|
---|
123 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
|
---|
124 | Log() << Verbose(0) << "Input was not a number!" << endl;
|
---|
125 | }
|
---|
126 | }while(badInput);
|
---|
127 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | TextDialog::MoleculeTextQuery::MoleculeTextQuery(string title, molecule **_target, MoleculeListClass *_molecules, std::string _description) :
|
---|
132 | Dialog::MoleculeQuery(title,_target,_molecules,_description)
|
---|
133 | {}
|
---|
134 |
|
---|
135 | TextDialog::MoleculeTextQuery::~MoleculeTextQuery() {}
|
---|
136 |
|
---|
137 | bool TextDialog::MoleculeTextQuery::handle() {
|
---|
138 | int idxOfMol=0;
|
---|
139 | bool badInput = false;
|
---|
140 | do{
|
---|
141 | badInput = false;
|
---|
142 | Log() << Verbose(0) << getTitle();
|
---|
143 | cin >> idxOfMol;
|
---|
144 | if(cin.fail()){
|
---|
145 | badInput = true;
|
---|
146 | cin.clear();
|
---|
147 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
|
---|
148 | Log() << Verbose(0) << "Input was not a number!" << endl;
|
---|
149 | continue;
|
---|
150 | }
|
---|
151 |
|
---|
152 | tmp = molecules->ReturnIndex(idxOfMol);
|
---|
153 | if(!tmp && idxOfMol!=-1){
|
---|
154 | Log() << Verbose(0) << "Invalid Molecule Index" << endl;
|
---|
155 | badInput = true;
|
---|
156 | }
|
---|
157 |
|
---|
158 | } while(badInput);
|
---|
159 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
|
---|
160 | return (idxOfMol!=-1);
|
---|
161 | }
|
---|
162 |
|
---|
163 | TextDialog::VectorTextQuery::VectorTextQuery(std::string title, Vector *_target, const double *const _cellSize, bool _check, std::string _description) :
|
---|
164 | Dialog::VectorQuery(title,_target,_cellSize,_check,_description)
|
---|
165 | {}
|
---|
166 |
|
---|
167 | TextDialog::VectorTextQuery::~VectorTextQuery()
|
---|
168 | {}
|
---|
169 |
|
---|
170 | bool TextDialog::VectorTextQuery::handle() {
|
---|
171 | Log() << Verbose(0) << getTitle();
|
---|
172 |
|
---|
173 | char coords[3] = {'x','y','z'};
|
---|
174 | int j = -1;
|
---|
175 | for (int i=0;i<3;i++) {
|
---|
176 | j += i+1;
|
---|
177 | do {
|
---|
178 | Log() << Verbose(0) << coords[i] << "[0.." << cellSize[j] << "]: ";
|
---|
179 | cin >> (*tmp)[i];
|
---|
180 | } while ((((*tmp)[i] < 0) || ((*tmp)[i] >= cellSize[j])) && (check));
|
---|
181 | }
|
---|
182 | return true;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | TextDialog::ElementTextQuery::ElementTextQuery(std::string title, const element **target, std::string _description) :
|
---|
187 | Dialog::ElementQuery(title,target,_description)
|
---|
188 | {}
|
---|
189 |
|
---|
190 | TextDialog::ElementTextQuery::~ElementTextQuery()
|
---|
191 | {}
|
---|
192 |
|
---|
193 | bool TextDialog::ElementTextQuery::handle() {
|
---|
194 | bool badInput=false;
|
---|
195 | bool aborted = false;
|
---|
196 | do{
|
---|
197 | badInput = false;
|
---|
198 | Log() << Verbose(0) << getTitle();
|
---|
199 |
|
---|
200 | // try to read as Atomic number
|
---|
201 | int Z;
|
---|
202 | cin >> Z;
|
---|
203 | if(!cin.fail()){
|
---|
204 | if(Z==-1){
|
---|
205 | aborted = true;
|
---|
206 | }
|
---|
207 | else{
|
---|
208 | tmp = World::getInstance().getPeriode()->FindElement(Z);
|
---|
209 | if(!tmp){
|
---|
210 | Log() << Verbose(0) << "No element with this atomic number!" << endl;
|
---|
211 | badInput = true;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | continue;
|
---|
215 | }
|
---|
216 | else{
|
---|
217 | cin.clear();
|
---|
218 | }
|
---|
219 |
|
---|
220 | // Try to read as shorthand
|
---|
221 | // the last buffer content was not removed, so we read the
|
---|
222 | // same thing again, this time as a string
|
---|
223 | string shorthand;
|
---|
224 | cin >> shorthand;
|
---|
225 | if(!cin.fail()){
|
---|
226 | if(shorthand.empty()){
|
---|
227 | aborted = true;
|
---|
228 | }
|
---|
229 | else{
|
---|
230 | tmp = World::getInstance().getPeriode()->FindElement(shorthand.c_str());
|
---|
231 | if(!tmp){
|
---|
232 | Log() << Verbose(0) << "No element with this shorthand!" << endl;
|
---|
233 | badInput = true;
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|
237 | else{
|
---|
238 | Log() << Verbose(0) << "Could not read input. Try Again." << endl;
|
---|
239 | cin.clear();
|
---|
240 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
|
---|
241 | badInput = true;
|
---|
242 | }
|
---|
243 |
|
---|
244 | }while(badInput);
|
---|
245 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
|
---|
246 | return !aborted;
|
---|
247 | }
|
---|