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