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