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