- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/UIElements/TextUI/TextDialog.cpp
r8df74d r9ee38b 20 20 #include "Helpers/MemDebug.hpp" 21 21 22 #include <iostream> 23 24 #include <Descriptors/AtomDescriptor.hpp> 25 #include <Descriptors/AtomIdDescriptor.hpp> 26 #include <Descriptors/MoleculeDescriptor.hpp> 27 #include <Descriptors/MoleculeIdDescriptor.hpp> 28 #include <boost/lexical_cast.hpp> 22 29 #include "TextUI/TextDialog.hpp" 30 31 #include "World.hpp" 32 #include "periodentafel.hpp" 33 #include "Helpers/Log.hpp" 34 #include "Helpers/Verbose.hpp" 35 36 #include "atom.hpp" 37 #include "element.hpp" 38 #include "molecule.hpp" 39 #include "LinearAlgebra/Vector.hpp" 40 #include "LinearAlgebra/Matrix.hpp" 41 #include "Box.hpp" 42 43 #include <boost/lexical_cast.hpp> 23 44 24 45 using namespace std; … … 104 125 } 105 126 106 void TextDialog::queryFile(const char* title, std::string description){107 registerQuery(new FileTextQuery(title,description));108 }109 110 127 /************************** Query Infrastructure ************************/ 111 /* ---> shifted to folder Query */ 112 /************************************************************************/ 113 128 129 TextDialog::EmptyTextQuery::EmptyTextQuery(string title, std::string _description) : 130 Dialog::EmptyQuery(title,_description) 131 {} 132 133 TextDialog::EmptyTextQuery::~EmptyTextQuery() {} 134 135 bool TextDialog::EmptyTextQuery::handle() { 136 cout << "Message of " << getTitle() << ":\n" << getDescription() << "\n"; 137 return true; 138 } 139 140 TextDialog::IntTextQuery::IntTextQuery(string title, std::string _description) : 141 Dialog::IntQuery(title,_description) 142 {} 143 144 TextDialog::IntTextQuery::~IntTextQuery() {} 145 146 bool TextDialog::IntTextQuery::handle() { 147 bool badInput = false; 148 do{ 149 badInput = false; 150 Log() << Verbose(0) << getTitle(); 151 cin >> tmp; 152 if(cin.fail()){ 153 badInput=true; 154 cin.clear(); 155 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 156 Log() << Verbose(0) << "Input was not a number!" << endl; 157 } 158 } while(badInput); 159 // clear the input buffer of anything still in the line 160 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 161 return true; 162 } 163 164 TextDialog::IntsTextQuery::IntsTextQuery(string title, std::string _description) : 165 Dialog::IntsQuery(title,_description) 166 {} 167 168 TextDialog::IntsTextQuery::~IntsTextQuery() {} 169 170 bool TextDialog::IntsTextQuery::handle() { 171 Log() << Verbose(0) << getTitle(); 172 std::string line; 173 getline(cin,line); 174 // dissect by " " 175 std::string::iterator olditer = line.begin(); 176 for(string::iterator iter = line.begin(); iter != line.end(); ++iter) { 177 if (*iter == ' ') { 178 std::istringstream stream(string(iter, olditer)); 179 stream >> temp; 180 tmp.push_back(temp); 181 olditer = iter; 182 } 183 } 184 if (olditer != line.begin()) { // insert last part also 185 std::istringstream stream(string(olditer, line.end())); 186 stream >> temp; 187 tmp.push_back(temp); 188 } 189 190 return true; 191 } 192 193 TextDialog::BooleanTextQuery::BooleanTextQuery(string title, std::string _description) : 194 Dialog::BooleanQuery(title,_description) 195 {} 196 197 TextDialog::BooleanTextQuery::~BooleanTextQuery() {} 198 199 bool TextDialog::BooleanTextQuery::handle() { 200 bool badInput = false; 201 char input = ' '; 202 do{ 203 badInput = false; 204 Log() << Verbose(0) << getTitle(); 205 cin >> input; 206 if ((input == 'y' ) || (input == 'Y')) { 207 tmp = true; 208 } else if ((input == 'n' ) || (input == 'N')) { 209 tmp = false; 210 } else { 211 badInput=true; 212 cin.clear(); 213 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 214 Log() << Verbose(0) << "Input was not of [yYnN]!" << endl; 215 } 216 } while(badInput); 217 // clear the input buffer of anything still in the line 218 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 219 return true; 220 } 221 222 TextDialog::StringTextQuery::StringTextQuery(string title, std::string _description) : 223 Dialog::StringQuery(title,_description) 224 {} 225 226 TextDialog::StringTextQuery::~StringTextQuery() {} 227 228 bool TextDialog::StringTextQuery::handle() { 229 Log() << Verbose(0) << getTitle(); 230 getline(cin,tmp); 231 return true; 232 } 233 234 TextDialog::StringsTextQuery::StringsTextQuery(string title, std::string _description) : 235 Dialog::StringsQuery(title,_description) 236 {} 237 238 TextDialog::StringsTextQuery::~StringsTextQuery() {} 239 240 bool TextDialog::StringsTextQuery::handle() { 241 Log() << Verbose(0) << getTitle(); 242 getline(cin,temp); 243 // dissect by " " 244 std::string::iterator olditer = temp.begin(); 245 for(string::iterator iter = temp.begin(); iter != temp.end(); ++iter) { 246 if (*iter == ' ') { 247 tmp.push_back(string(iter, olditer)); 248 olditer = iter; 249 } 250 } 251 if (olditer != temp.begin()) // insert last part also 252 tmp.push_back(string(olditer, temp.end())); 253 254 return true; 255 } 256 257 TextDialog::DoubleTextQuery::DoubleTextQuery(string title, std::string _description) : 258 Dialog::DoubleQuery(title,_description) 259 {} 260 261 TextDialog::DoubleTextQuery::~DoubleTextQuery() {} 262 263 bool TextDialog::DoubleTextQuery::handle() { 264 bool badInput = false; 265 do{ 266 badInput = false; 267 Log() << Verbose(0) << getTitle(); 268 cin >> tmp; 269 if(cin.fail()){ 270 badInput = true; 271 cin.clear(); 272 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 273 Log() << Verbose(0) << "Input was not a number!" << endl; 274 } 275 }while(badInput); 276 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 277 return true; 278 } 279 280 281 TextDialog::DoublesTextQuery::DoublesTextQuery(string title, std::string _description) : 282 Dialog::DoublesQuery(title,_description) 283 {} 284 285 TextDialog::DoublesTextQuery::~DoublesTextQuery() {} 286 287 bool TextDialog::DoublesTextQuery::handle() { 288 Log() << Verbose(0) << getTitle(); 289 std::string line; 290 getline(cin,line); 291 // dissect by " " 292 std::string::iterator olditer = line.begin(); 293 for(string::iterator iter = line.begin(); iter != line.end(); ++iter) { 294 if (*iter == ' ') { 295 std::istringstream stream(string(iter, olditer)); 296 stream >> temp; 297 tmp.push_back(temp); 298 olditer = iter; 299 } 300 } 301 if (olditer != line.begin()) { // insert last part also 302 std::istringstream stream(string(olditer, line.end())); 303 stream >> temp; 304 tmp.push_back(temp); 305 } 306 307 return true; 308 } 309 310 TextDialog::AtomTextQuery::AtomTextQuery(string title, std::string _description) : 311 Dialog::AtomQuery(title,_description) 312 {} 313 314 TextDialog::AtomTextQuery::~AtomTextQuery() {} 315 316 bool TextDialog::AtomTextQuery::handle() { 317 int idxOfAtom=-1; 318 bool badInput = false; 319 do{ 320 badInput = false; 321 Log() << Verbose(0) << getTitle(); 322 cin >> idxOfAtom; 323 if(cin.fail()){ 324 badInput = true; 325 cin.clear(); 326 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 327 Log() << Verbose(0) << "Input was not a number!" << endl; 328 continue; 329 } 330 331 tmp = World::getInstance().getAtom(AtomById(idxOfAtom)); 332 if(!tmp && idxOfAtom!=-1){ 333 Log() << Verbose(0) << "Invalid Atom Index" << idxOfAtom << endl; 334 badInput = true; 335 } 336 337 } while(badInput); 338 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 339 return (idxOfAtom!=-1); 340 } 341 342 343 TextDialog::AtomsTextQuery::AtomsTextQuery(string title, std::string _description) : 344 Dialog::AtomsQuery(title,_description) 345 {} 346 347 TextDialog::AtomsTextQuery::~AtomsTextQuery() {} 348 349 bool TextDialog::AtomsTextQuery::handle() { 350 int idxOfAtom=-1; 351 Log() << Verbose(0) << getTitle(); 352 std::string line; 353 getline(cin,line); 354 // dissect by " " 355 std::string::iterator olditer = line.begin(); 356 for(string::iterator iter = line.begin(); iter != line.end(); ++iter) { 357 if (*iter == ' ') { 358 std::istringstream stream(string(iter, olditer)); 359 stream >> idxOfAtom; 360 temp = World::getInstance().getAtom(AtomById(idxOfAtom)); 361 if(!temp && idxOfAtom!=-1){ 362 Log() << Verbose(0) << "Invalid Atom Index" << idxOfAtom << endl; 363 break; 364 } 365 tmp.push_back(temp); 366 olditer = iter; 367 } 368 } 369 if (olditer != line.begin()) { // insert last part also 370 std::istringstream stream(string(olditer, line.end())); 371 stream >> idxOfAtom; 372 temp = World::getInstance().getAtom(AtomById(idxOfAtom)); 373 if(!temp && idxOfAtom!=-1) { 374 Log() << Verbose(0) << "Invalid Atom Index" << idxOfAtom << endl; 375 tmp.push_back(temp); 376 } 377 } 378 379 return (idxOfAtom!=-1); 380 } 381 382 TextDialog::MoleculeTextQuery::MoleculeTextQuery(string title, std::string _description) : 383 Dialog::MoleculeQuery(title,_description) 384 {} 385 386 TextDialog::MoleculeTextQuery::~MoleculeTextQuery() {} 387 388 bool TextDialog::MoleculeTextQuery::handle() { 389 int idxOfMol=0; 390 bool badInput = false; 391 do{ 392 badInput = false; 393 Log() << Verbose(0) << getTitle(); 394 cin >> idxOfMol; 395 if(cin.fail()){ 396 badInput = true; 397 cin.clear(); 398 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 399 Log() << Verbose(0) << "Input was not a number!" << endl; 400 continue; 401 } 402 403 tmp = World::getInstance().getMolecule(MoleculeById(idxOfMol)); 404 if(!tmp && idxOfMol!=-1){ 405 Log() << Verbose(0) << "Invalid Molecule Index" << endl; 406 badInput = true; 407 } 408 409 } while(badInput); 410 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 411 return (idxOfMol!=-1); 412 } 413 414 415 TextDialog::MoleculesTextQuery::MoleculesTextQuery(string title, std::string _description) : 416 Dialog::MoleculesQuery(title,_description) 417 {} 418 419 TextDialog::MoleculesTextQuery::~MoleculesTextQuery() {} 420 421 bool TextDialog::MoleculesTextQuery::handle() { 422 int idxOfMol=-1; 423 Log() << Verbose(0) << getTitle(); 424 std::string line; 425 getline(cin,line); 426 // dissect by " " 427 std::string::iterator olditer = line.begin(); 428 for(string::iterator iter = line.begin(); iter != line.end(); ++iter) { 429 if (*iter == ' ') { 430 std::istringstream stream(string(iter, olditer)); 431 stream >> idxOfMol; 432 temp = World::getInstance().getMolecule(MoleculeById(idxOfMol)); 433 if(!temp && idxOfMol!=-1){ 434 Log() << Verbose(0) << "Invalid Molecule Index" << idxOfMol << endl; 435 break; 436 } 437 tmp.push_back(temp); 438 olditer = iter; 439 } 440 } 441 if (olditer != line.begin()) { // insert last part also 442 std::istringstream stream(string(olditer, line.end())); 443 stream >> idxOfMol; 444 temp = World::getInstance().getMolecule(MoleculeById(idxOfMol)); 445 if(!temp && idxOfMol!=-1){ 446 Log() << Verbose(0) << "Invalid Molecule Index" << idxOfMol << endl; 447 tmp.push_back(temp); 448 } 449 } 450 451 return (idxOfMol!=-1); 452 } 453 454 TextDialog::VectorTextQuery::VectorTextQuery(std::string title, bool _check, std::string _description) : 455 Dialog::VectorQuery(title,_check,_description) 456 {} 457 458 TextDialog::VectorTextQuery::~VectorTextQuery() 459 {} 460 461 bool TextDialog::VectorTextQuery::handle() { 462 std::cout << getTitle(); 463 const Matrix &M = World::getInstance().getDomain().getM(); 464 char coords[3] = {'x', 'y', 'z'}; 465 for (int i=0;i<3;i++) 466 std::cout << coords[i] << "[0.." << M.at(i,i) << ( (i!=2) ? "], " : "]: "); 467 468 std::string line; 469 getline(cin,line); 470 471 // dissect by "," 472 double coord = 0.; 473 int counter = 0; 474 std::string::iterator olditer = line.begin(); 475 for(string::iterator iter = line.begin(); (iter != line.end()) && (counter != 3); ++iter) { 476 if (*iter == ',') { 477 std::istringstream stream(string(iter, olditer)); 478 stream >> coord; 479 tmp[counter++] = coord; 480 olditer = iter; 481 } 482 } 483 if ((olditer != line.begin()) && (counter != 3)) { // insert last part also 484 std::istringstream stream(string(olditer, line.end())); 485 stream >> coord; 486 tmp[counter++] = coord; 487 } 488 489 // check vector 490 return World::getInstance().getDomain().isInside(tmp); 491 } 492 493 TextDialog::VectorsTextQuery::VectorsTextQuery(std::string title, bool _check, std::string _description) : 494 Dialog::VectorsQuery(title,_check,_description) 495 {} 496 497 TextDialog::VectorsTextQuery::~VectorsTextQuery() 498 {} 499 500 bool TextDialog::VectorsTextQuery::handle() { 501 std::cout << getTitle(); 502 char coords[3] = {'x', 'y', 'z'}; 503 const Matrix &M = World::getInstance().getDomain().getM(); 504 for (int i=0;i<3;i++) 505 std::cout << coords[i] << "[0.." << M.at(i,i) << ( (i!=2) ? "], " : "]: "); 506 507 std::string line; 508 getline(cin,line); 509 510 // dissect by "," 511 double coord = 0.; 512 std::string::iterator olditerspace = line.begin(); 513 std::string::iterator olditercomma = line.begin(); 514 int counter = 0; 515 for(string::iterator vectoriter = line.begin(); vectoriter != line.end(); ++vectoriter) { 516 if (*vectoriter == ',') 517 counter++; 518 if ((*vectoriter == ' ') && (counter == 2)) { 519 counter = 0; 520 for(string::iterator componentiter = olditerspace; (componentiter != vectoriter) && (counter !=3); ++componentiter) { 521 if (*componentiter == ',') { 522 std::istringstream stream(string(componentiter, olditercomma)); 523 stream >> coord; 524 temp[counter++] = coord; 525 olditercomma = componentiter; 526 } 527 } 528 if ((olditercomma != line.begin()) && (counter != 3)) { // insert last part also 529 std::istringstream stream(string(olditercomma, vectoriter)); 530 stream >> coord; 531 temp[counter++] = coord; 532 } 533 if (World::getInstance().getDomain().isInside(temp)) 534 tmp.push_back(temp); 535 olditerspace = vectoriter; 536 } 537 } 538 539 return true; 540 } 541 542 TextDialog::BoxTextQuery::BoxTextQuery(std::string title, std::string _description) : 543 Dialog::BoxQuery(title,_description) 544 {} 545 546 TextDialog::BoxTextQuery::~BoxTextQuery() 547 {} 548 549 bool TextDialog::BoxTextQuery::handle() { 550 Log() << Verbose(0) << getTitle(); 551 552 double temp[6]; 553 std::string coords[6] = {"xx","yx","yy", "zx", "zy", "zz"}; 554 for (int i=0;i<6;i++) { 555 Log() << Verbose(0) << coords[i] << ": "; 556 cin >> temp[i]; 557 } 558 Matrix M; 559 M.set(0,0, temp[0]); 560 M.set(0,1, temp[1]); 561 M.set(0,2, temp[2]); 562 M.set(1,0, temp[1]); 563 M.set(1,1, temp[3]); 564 M.set(1,2, temp[4]); 565 M.set(2,0, temp[2]); 566 M.set(2,1, temp[4]); 567 M.set(2,2, temp[5]); 568 tmp.setM(M); 569 return true; 570 } 571 572 TextDialog::ElementTextQuery::ElementTextQuery(std::string title, std::string _description) : 573 Dialog::ElementQuery(title,_description) 574 {} 575 576 TextDialog::ElementTextQuery::~ElementTextQuery() 577 {} 578 579 bool TextDialog::ElementTextQuery::handle() { 580 bool badInput=false; 581 bool aborted = false; 582 const element * temp = NULL; 583 do{ 584 badInput = false; 585 Log() << Verbose(0) << getTitle(); 586 587 // try to read as Atomic number 588 int Z; 589 cin >> Z; 590 if(!cin.fail()){ 591 if(Z==-1){ 592 aborted = true; 593 } 594 else{ 595 temp = World::getInstance().getPeriode()->FindElement(Z); 596 if(!temp){ 597 Log() << Verbose(0) << "No element with this atomic number!" << endl; 598 badInput = true; 599 } 600 } 601 continue; 602 } 603 else{ 604 cin.clear(); 605 } 606 607 // Try to read as shorthand 608 // the last buffer content was not removed, so we read the 609 // same thing again, this time as a std::string 610 std::string shorthand; 611 cin >> shorthand; 612 if(!cin.fail()){ 613 if(shorthand.empty()){ 614 aborted = true; 615 } 616 else{ 617 temp = World::getInstance().getPeriode()->FindElement(shorthand.c_str()); 618 if(!temp){ 619 Log() << Verbose(0) << "No element with this shorthand!" << endl; 620 badInput = true; 621 } 622 } 623 } 624 else{ 625 Log() << Verbose(0) << "Could not read input. Try Again." << endl; 626 cin.clear(); 627 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 628 badInput = true; 629 } 630 631 }while(badInput); 632 cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 633 return !aborted; 634 } 635 636 TextDialog::ElementsTextQuery::ElementsTextQuery(std::string title, std::string _description) : 637 Dialog::ElementsQuery(title,_description) 638 {} 639 640 TextDialog::ElementsTextQuery::~ElementsTextQuery() 641 {} 642 643 bool TextDialog::ElementsTextQuery::handle() { 644 std::string shorthand; 645 int Z=-1; 646 Log() << Verbose(0) << getTitle(); 647 std::string line; 648 getline(cin,line); 649 // dissect by " " 650 std::string::iterator olditer = line.begin(); 651 for(string::iterator iter = line.begin(); iter != line.end(); ++iter) { 652 if (*iter == ' ') { 653 std::istringstream stream(string(iter, olditer)); 654 stream >> shorthand; 655 try { 656 Z = lexical_cast<int>(shorthand); 657 temp = World::getInstance().getPeriode()->FindElement(Z); 658 } catch (bad_lexical_cast) { 659 temp = World::getInstance().getPeriode()->FindElement(shorthand.c_str()); 660 }; 661 if(!temp && Z!=-1){ 662 Log() << Verbose(0) << "Invalid Element" << shorthand << endl; 663 break; 664 } 665 tmp.push_back(temp); 666 olditer = iter; 667 } 668 } 669 if (olditer != line.begin()) { // insert last part also 670 std::istringstream stream(string(olditer, line.end())); 671 stream >> shorthand; 672 try { 673 Z = lexical_cast<int>(shorthand); 674 temp = World::getInstance().getPeriode()->FindElement(Z); 675 } catch (bad_lexical_cast) { 676 temp = World::getInstance().getPeriode()->FindElement(shorthand.c_str()); 677 }; 678 if(!temp && Z!=-1) { 679 Log() << Verbose(0) << "Invalid Element" << shorthand << endl; 680 tmp.push_back(temp); 681 } 682 } 683 684 return (Z!=-1); 685 }
Note:
See TracChangeset
for help on using the changeset viewer.