| 1 | /* | 
|---|
| 2 | * TextDialog.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Jan 5, 2010 | 
|---|
| 5 | *      Author: crueger | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Helpers/MemDebug.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | #include <iostream> | 
|---|
| 11 |  | 
|---|
| 12 | #include <Descriptors/AtomDescriptor.hpp> | 
|---|
| 13 | #include <Descriptors/AtomIdDescriptor.hpp> | 
|---|
| 14 | #include <Descriptors/MoleculeDescriptor.hpp> | 
|---|
| 15 | #include <Descriptors/MoleculeIdDescriptor.hpp> | 
|---|
| 16 | #include "TextUI/TextDialog.hpp" | 
|---|
| 17 |  | 
|---|
| 18 | #include "World.hpp" | 
|---|
| 19 | #include "periodentafel.hpp" | 
|---|
| 20 | #include "log.hpp" | 
|---|
| 21 | #include "verbose.hpp" | 
|---|
| 22 |  | 
|---|
| 23 | #include "atom.hpp" | 
|---|
| 24 | #include "element.hpp" | 
|---|
| 25 | #include "molecule.hpp" | 
|---|
| 26 | #include "vector.hpp" | 
|---|
| 27 |  | 
|---|
| 28 | using namespace std; | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | TextDialog::TextDialog() | 
|---|
| 32 | { | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | TextDialog::~TextDialog() | 
|---|
| 36 | { | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | void TextDialog::queryEmpty(const char* title, string description){ | 
|---|
| 41 | registerQuery(new EmptyTextQuery(title,description)); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | void TextDialog::queryBoolean(const char* title, bool* target, string description){ | 
|---|
| 45 | registerQuery(new BooleanTextQuery(title,target,description)); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | void TextDialog::queryInt(const char* title, int* target, string description){ | 
|---|
| 49 | registerQuery(new IntTextQuery(title,target,description)); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | void TextDialog::queryDouble(const char* title, double* target, string description){ | 
|---|
| 53 | registerQuery(new DoubleTextQuery(title,target,description)); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void TextDialog::queryString(const char* title, string* target, string description){ | 
|---|
| 57 | registerQuery(new StringTextQuery(title,target,description)); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | void TextDialog::queryStrings(const char* title, vector<string>* target, string description){ | 
|---|
| 61 | registerQuery(new StringsTextQuery(title,target,description)); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | void TextDialog::queryAtom(const char* title, atom **target, string description) { | 
|---|
| 65 | registerQuery(new AtomTextQuery(title,target,description)); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | void TextDialog::queryMolecule(const char* title, molecule **target, string description) { | 
|---|
| 69 | registerQuery(new MoleculeTextQuery(title,target,description)); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void TextDialog::queryVector(const char* title, Vector *target,const double *const cellSize, bool check, string description) { | 
|---|
| 73 | registerQuery(new VectorTextQuery(title,target,cellSize,check,description)); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void TextDialog::queryBox(const char* title,double ** const cellSize, string description) { | 
|---|
| 77 | registerQuery(new BoxTextQuery(title,cellSize,description)); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | void TextDialog::queryElement(const char* title, std::vector<element *> *target, string description){ | 
|---|
| 81 | registerQuery(new ElementTextQuery(title,target,description)); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | /************************** Query Infrastructure ************************/ | 
|---|
| 85 |  | 
|---|
| 86 | TextDialog::EmptyTextQuery::EmptyTextQuery(string title, std::string _description) : | 
|---|
| 87 | Dialog::EmptyQuery(title,_description) | 
|---|
| 88 | {} | 
|---|
| 89 |  | 
|---|
| 90 | TextDialog::EmptyTextQuery::~EmptyTextQuery() {} | 
|---|
| 91 |  | 
|---|
| 92 | bool TextDialog::EmptyTextQuery::handle() { | 
|---|
| 93 | cout << "Message of " << getTitle() << ":\n" << getDescription() << "\n"; | 
|---|
| 94 | return true; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | TextDialog::IntTextQuery::IntTextQuery(string title, int * _target, std::string _description) : | 
|---|
| 98 | Dialog::IntQuery(title,_target,_description) | 
|---|
| 99 | {} | 
|---|
| 100 |  | 
|---|
| 101 | TextDialog::IntTextQuery::~IntTextQuery() {} | 
|---|
| 102 |  | 
|---|
| 103 | bool TextDialog::IntTextQuery::handle() { | 
|---|
| 104 | bool badInput = false; | 
|---|
| 105 | do{ | 
|---|
| 106 | badInput = false; | 
|---|
| 107 | Log() << Verbose(0) << getTitle(); | 
|---|
| 108 | cin >> tmp; | 
|---|
| 109 | if(cin.fail()){ | 
|---|
| 110 | badInput=true; | 
|---|
| 111 | cin.clear(); | 
|---|
| 112 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 113 | Log() << Verbose(0) << "Input was not a number!" << endl; | 
|---|
| 114 | } | 
|---|
| 115 | } while(badInput); | 
|---|
| 116 | // clear the input buffer of anything still in the line | 
|---|
| 117 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 118 | return true; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | TextDialog::BooleanTextQuery::BooleanTextQuery(string title, bool * _target, std::string _description) : | 
|---|
| 122 | Dialog::BooleanQuery(title,_target,_description) | 
|---|
| 123 | {} | 
|---|
| 124 |  | 
|---|
| 125 | TextDialog::BooleanTextQuery::~BooleanTextQuery() {} | 
|---|
| 126 |  | 
|---|
| 127 | bool TextDialog::BooleanTextQuery::handle() { | 
|---|
| 128 | bool badInput = false; | 
|---|
| 129 | char input = ' '; | 
|---|
| 130 | do{ | 
|---|
| 131 | badInput = false; | 
|---|
| 132 | Log() << Verbose(0) << getTitle(); | 
|---|
| 133 | cin >> input; | 
|---|
| 134 | if ((input == 'y' ) || (input == 'Y')) { | 
|---|
| 135 | tmp = true; | 
|---|
| 136 | } else if ((input == 'n' ) || (input == 'N')) { | 
|---|
| 137 | tmp = false; | 
|---|
| 138 | } else { | 
|---|
| 139 | badInput=true; | 
|---|
| 140 | cin.clear(); | 
|---|
| 141 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 142 | Log() << Verbose(0) << "Input was not of [yYnN]!" << endl; | 
|---|
| 143 | } | 
|---|
| 144 | } while(badInput); | 
|---|
| 145 | // clear the input buffer of anything still in the line | 
|---|
| 146 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 147 | return true; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | TextDialog::StringTextQuery::StringTextQuery(string title,string *_target, std::string _description) : | 
|---|
| 151 | Dialog::StringQuery(title,_target,_description) | 
|---|
| 152 | {} | 
|---|
| 153 |  | 
|---|
| 154 | TextDialog::StringTextQuery::~StringTextQuery() {} | 
|---|
| 155 |  | 
|---|
| 156 | bool TextDialog::StringTextQuery::handle() { | 
|---|
| 157 | Log() << Verbose(0) << getTitle(); | 
|---|
| 158 | getline(cin,tmp); | 
|---|
| 159 | return true; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | TextDialog::StringsTextQuery::StringsTextQuery(string title,vector<string> *_target, std::string _description) : | 
|---|
| 163 | Dialog::StringsQuery(title,_target,_description) | 
|---|
| 164 | {} | 
|---|
| 165 |  | 
|---|
| 166 | TextDialog::StringsTextQuery::~StringsTextQuery() {} | 
|---|
| 167 |  | 
|---|
| 168 | bool TextDialog::StringsTextQuery::handle() { | 
|---|
| 169 | Log() << Verbose(0) << getTitle(); | 
|---|
| 170 | getline(cin,temp); | 
|---|
| 171 | // dissect by "," | 
|---|
| 172 | string::iterator olditer = temp.begin(); | 
|---|
| 173 | for(string::iterator iter = temp.begin(); iter != temp.end(); ++iter) { | 
|---|
| 174 | if (*iter == ' ') { | 
|---|
| 175 | tmp.push_back(string(iter, olditer)); | 
|---|
| 176 | olditer = iter; | 
|---|
| 177 | } | 
|---|
| 178 | } | 
|---|
| 179 | if (olditer != temp.begin())  // insert last part also | 
|---|
| 180 | tmp.push_back(string(olditer, temp.end())); | 
|---|
| 181 |  | 
|---|
| 182 | return true; | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | TextDialog::DoubleTextQuery::DoubleTextQuery(string title,double *_target, std::string _description) : | 
|---|
| 186 | Dialog::DoubleQuery(title,_target,_description) | 
|---|
| 187 | {} | 
|---|
| 188 |  | 
|---|
| 189 | TextDialog::DoubleTextQuery::~DoubleTextQuery() {} | 
|---|
| 190 |  | 
|---|
| 191 | bool TextDialog::DoubleTextQuery::handle() { | 
|---|
| 192 | bool badInput = false; | 
|---|
| 193 | do{ | 
|---|
| 194 | badInput = false; | 
|---|
| 195 | Log() << Verbose(0) << getTitle(); | 
|---|
| 196 | cin >> tmp; | 
|---|
| 197 | if(cin.fail()){ | 
|---|
| 198 | badInput = true; | 
|---|
| 199 | cin.clear(); | 
|---|
| 200 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 201 | Log() << Verbose(0) << "Input was not a number!" << endl; | 
|---|
| 202 | } | 
|---|
| 203 | }while(badInput); | 
|---|
| 204 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 205 | return true; | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | TextDialog::AtomTextQuery::AtomTextQuery(string title, atom **_target, std::string _description) : | 
|---|
| 209 | Dialog::AtomQuery(title,_target,_description) | 
|---|
| 210 | {} | 
|---|
| 211 |  | 
|---|
| 212 | TextDialog::AtomTextQuery::~AtomTextQuery() {} | 
|---|
| 213 |  | 
|---|
| 214 | bool TextDialog::AtomTextQuery::handle() { | 
|---|
| 215 | int idxOfAtom=0; | 
|---|
| 216 | bool badInput = false; | 
|---|
| 217 | do{ | 
|---|
| 218 | badInput = false; | 
|---|
| 219 | Log() << Verbose(0) << getTitle(); | 
|---|
| 220 | cin >> idxOfAtom; | 
|---|
| 221 | if(cin.fail()){ | 
|---|
| 222 | badInput = true; | 
|---|
| 223 | cin.clear(); | 
|---|
| 224 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 225 | Log() << Verbose(0) << "Input was not a number!" << endl; | 
|---|
| 226 | continue; | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | tmp = World::getInstance().getAtom(AtomById(idxOfAtom)); | 
|---|
| 230 | if(!tmp && idxOfAtom!=-1){ | 
|---|
| 231 | Log() << Verbose(0) << "Invalid Atom Index" << endl; | 
|---|
| 232 | badInput = true; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | } while(badInput); | 
|---|
| 236 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 237 | return (idxOfAtom!=-1); | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | TextDialog::MoleculeTextQuery::MoleculeTextQuery(string title, molecule **_target, std::string _description) : | 
|---|
| 241 | Dialog::MoleculeQuery(title,_target,_description) | 
|---|
| 242 | {} | 
|---|
| 243 |  | 
|---|
| 244 | TextDialog::MoleculeTextQuery::~MoleculeTextQuery() {} | 
|---|
| 245 |  | 
|---|
| 246 | bool TextDialog::MoleculeTextQuery::handle() { | 
|---|
| 247 | int idxOfMol=0; | 
|---|
| 248 | bool badInput = false; | 
|---|
| 249 | do{ | 
|---|
| 250 | badInput = false; | 
|---|
| 251 | Log() << Verbose(0) << getTitle(); | 
|---|
| 252 | cin >> idxOfMol; | 
|---|
| 253 | if(cin.fail()){ | 
|---|
| 254 | badInput = true; | 
|---|
| 255 | cin.clear(); | 
|---|
| 256 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 257 | Log() << Verbose(0) << "Input was not a number!" << endl; | 
|---|
| 258 | continue; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | tmp = World::getInstance().getMolecule(MoleculeById(idxOfMol)); | 
|---|
| 262 | if(!tmp && idxOfMol!=-1){ | 
|---|
| 263 | Log() << Verbose(0) << "Invalid Molecule Index" << endl; | 
|---|
| 264 | badInput = true; | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | } while(badInput); | 
|---|
| 268 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 269 | return (idxOfMol!=-1); | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | TextDialog::VectorTextQuery::VectorTextQuery(std::string title, Vector *_target, const double *const _cellSize, bool _check, std::string _description) : | 
|---|
| 273 | Dialog::VectorQuery(title,_target,_cellSize,_check,_description) | 
|---|
| 274 | {} | 
|---|
| 275 |  | 
|---|
| 276 | TextDialog::VectorTextQuery::~VectorTextQuery() | 
|---|
| 277 | {} | 
|---|
| 278 |  | 
|---|
| 279 | bool TextDialog::VectorTextQuery::handle() { | 
|---|
| 280 | Log() << Verbose(0) << getTitle(); | 
|---|
| 281 |  | 
|---|
| 282 | char coords[3] = {'x','y','z'}; | 
|---|
| 283 | int j = -1; | 
|---|
| 284 | for (int i=0;i<3;i++) { | 
|---|
| 285 | j += i+1; | 
|---|
| 286 | do { | 
|---|
| 287 | Log() << Verbose(0) << coords[i] << "[0.." << cellSize[j] << "]: "; | 
|---|
| 288 | cin >> (*tmp)[i]; | 
|---|
| 289 | } while ((((*tmp)[i] < 0) || ((*tmp)[i] >= cellSize[j])) && (check)); | 
|---|
| 290 | } | 
|---|
| 291 | return true; | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | TextDialog::BoxTextQuery::BoxTextQuery(std::string title, double ** const _cellSize, std::string _description) : | 
|---|
| 295 | Dialog::BoxQuery(title,_cellSize,_description) | 
|---|
| 296 | {} | 
|---|
| 297 |  | 
|---|
| 298 | TextDialog::BoxTextQuery::~BoxTextQuery() | 
|---|
| 299 | {} | 
|---|
| 300 |  | 
|---|
| 301 | bool TextDialog::BoxTextQuery::handle() { | 
|---|
| 302 | Log() << Verbose(0) << getTitle(); | 
|---|
| 303 |  | 
|---|
| 304 | std::string coords[6] = {"xx","xy","xz", "yy", "yz", "zz"}; | 
|---|
| 305 | for (int i=0;i<6;i++) { | 
|---|
| 306 | Log() << Verbose(0) << coords[i] << ": "; | 
|---|
| 307 | cin >> tmp[i]; | 
|---|
| 308 | } | 
|---|
| 309 | return true; | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 | TextDialog::ElementTextQuery::ElementTextQuery(std::string title, std::vector<element *> *_target, std::string _description) : | 
|---|
| 313 | Dialog::ElementQuery(title,_target,_description) | 
|---|
| 314 | {} | 
|---|
| 315 |  | 
|---|
| 316 | TextDialog::ElementTextQuery::~ElementTextQuery() | 
|---|
| 317 | {} | 
|---|
| 318 |  | 
|---|
| 319 | bool TextDialog::ElementTextQuery::handle() { | 
|---|
| 320 | bool badInput=false; | 
|---|
| 321 | bool aborted = false; | 
|---|
| 322 | element * tmp = NULL; | 
|---|
| 323 | do{ | 
|---|
| 324 | badInput = false; | 
|---|
| 325 | Log() << Verbose(0) << getTitle(); | 
|---|
| 326 |  | 
|---|
| 327 | // try to read as Atomic number | 
|---|
| 328 | int Z; | 
|---|
| 329 | cin >> Z; | 
|---|
| 330 | if(!cin.fail()){ | 
|---|
| 331 | if(Z==-1){ | 
|---|
| 332 | aborted = true; | 
|---|
| 333 | } | 
|---|
| 334 | else{ | 
|---|
| 335 | tmp = World::getInstance().getPeriode()->FindElement(Z); | 
|---|
| 336 | if(!tmp){ | 
|---|
| 337 | Log() << Verbose(0) << "No element with this atomic number!" << endl; | 
|---|
| 338 | badInput = true; | 
|---|
| 339 | } else { | 
|---|
| 340 | elements.push_back(tmp); | 
|---|
| 341 | } | 
|---|
| 342 | } | 
|---|
| 343 | continue; | 
|---|
| 344 | } | 
|---|
| 345 | else{ | 
|---|
| 346 | cin.clear(); | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | // Try to read as shorthand | 
|---|
| 350 | // the last buffer content was not removed, so we read the | 
|---|
| 351 | // same thing again, this time as a string | 
|---|
| 352 | string shorthand; | 
|---|
| 353 | cin >> shorthand; | 
|---|
| 354 | if(!cin.fail()){ | 
|---|
| 355 | if(shorthand.empty()){ | 
|---|
| 356 | aborted = true; | 
|---|
| 357 | } | 
|---|
| 358 | else{ | 
|---|
| 359 | tmp = World::getInstance().getPeriode()->FindElement(shorthand.c_str()); | 
|---|
| 360 | if(!tmp){ | 
|---|
| 361 | Log() << Verbose(0) << "No element with this shorthand!" << endl; | 
|---|
| 362 | badInput = true; | 
|---|
| 363 | } else { | 
|---|
| 364 | elements.push_back(tmp); | 
|---|
| 365 | } | 
|---|
| 366 | } | 
|---|
| 367 | } | 
|---|
| 368 | else{ | 
|---|
| 369 | Log() << Verbose(0) << "Could not read input. Try Again." << endl; | 
|---|
| 370 | cin.clear(); | 
|---|
| 371 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 372 | badInput = true; | 
|---|
| 373 | } | 
|---|
| 374 |  | 
|---|
| 375 | }while(badInput); | 
|---|
| 376 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
| 377 | return !aborted; | 
|---|
| 378 | } | 
|---|