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