[f5a86a] | 1 | /*
|
---|
| 2 | * TextDialog.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jan 5, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[112b09] | 8 | #include "Helpers/MemDebug.hpp"
|
---|
| 9 |
|
---|
[f5a86a] | 10 | #include <iostream>
|
---|
| 11 |
|
---|
[97ebf8] | 12 | #include <Descriptors/AtomDescriptor.hpp>
|
---|
| 13 | #include <Descriptors/AtomIdDescriptor.hpp>
|
---|
| 14 | #include <Descriptors/MoleculeDescriptor.hpp>
|
---|
| 15 | #include <Descriptors/MoleculeIdDescriptor.hpp>
|
---|
[5079a0] | 16 | #include "TextUI/TextDialog.hpp"
|
---|
[7aa000] | 17 |
|
---|
[5a7243] | 18 | #include "World.hpp"
|
---|
| 19 | #include "periodentafel.hpp"
|
---|
[f5a86a] | 20 | #include "log.hpp"
|
---|
| 21 | #include "verbose.hpp"
|
---|
| 22 |
|
---|
[97ebf8] | 23 | #include "atom.hpp"
|
---|
| 24 | #include "element.hpp"
|
---|
| 25 | #include "molecule.hpp"
|
---|
| 26 | #include "vector.hpp"
|
---|
[84c494] | 27 | #include "Matrix.hpp"
|
---|
| 28 | #include "Box.hpp"
|
---|
[97ebf8] | 29 |
|
---|
[f5a86a] | 30 | using namespace std;
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | TextDialog::TextDialog()
|
---|
| 34 | {
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | TextDialog::~TextDialog()
|
---|
| 38 | {
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[7aa000] | 41 |
|
---|
[86466e] | 42 | void TextDialog::queryEmpty(const char* title, string description){
|
---|
| 43 | registerQuery(new EmptyTextQuery(title,description));
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[97ebf8] | 46 | void TextDialog::queryBoolean(const char* title, bool* target, string description){
|
---|
| 47 | registerQuery(new BooleanTextQuery(title,target,description));
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[a2ab15] | 50 | void TextDialog::queryInt(const char* title, int* target, string description){
|
---|
| 51 | registerQuery(new IntTextQuery(title,target,description));
|
---|
[45f5d6] | 52 | }
|
---|
| 53 |
|
---|
[a2ab15] | 54 | void TextDialog::queryDouble(const char* title, double* target, string description){
|
---|
| 55 | registerQuery(new DoubleTextQuery(title,target,description));
|
---|
[2ededc2] | 56 | }
|
---|
| 57 |
|
---|
[a2ab15] | 58 | void TextDialog::queryString(const char* title, string* target, string description){
|
---|
| 59 | registerQuery(new StringTextQuery(title,target,description));
|
---|
[f5a86a] | 60 | }
|
---|
| 61 |
|
---|
[97ebf8] | 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));
|
---|
[7aa000] | 68 | }
|
---|
| 69 |
|
---|
[84c494] | 70 | void TextDialog::queryVector(const char* title, Vector *target, bool check, string description) {
|
---|
| 71 | registerQuery(new VectorTextQuery(title,target,check,description));
|
---|
[2ededc2] | 72 | }
|
---|
| 73 |
|
---|
[84c494] | 74 | void TextDialog::queryBox(const char* title,Box* cellSize, string description) {
|
---|
[97ebf8] | 75 | registerQuery(new BoxTextQuery(title,cellSize,description));
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[104524] | 78 | void TextDialog::queryElement(const char* title, std::vector<element *> *target, string description){
|
---|
[a2ab15] | 79 | registerQuery(new ElementTextQuery(title,target,description));
|
---|
[5a7243] | 80 | }
|
---|
| 81 |
|
---|
[7aa000] | 82 | /************************** Query Infrastructure ************************/
|
---|
| 83 |
|
---|
[86466e] | 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) :
|
---|
[a2ab15] | 96 | Dialog::IntQuery(title,_target,_description)
|
---|
[45f5d6] | 97 | {}
|
---|
| 98 |
|
---|
| 99 | TextDialog::IntTextQuery::~IntTextQuery() {}
|
---|
| 100 |
|
---|
| 101 | bool TextDialog::IntTextQuery::handle() {
|
---|
[8de8f7] | 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');
|
---|
[45f5d6] | 116 | return true;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[97ebf8] | 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 |
|
---|
[a2ab15] | 148 | TextDialog::StringTextQuery::StringTextQuery(string title,string *_target, std::string _description) :
|
---|
| 149 | Dialog::StringQuery(title,_target,_description)
|
---|
[45f5d6] | 150 | {}
|
---|
| 151 |
|
---|
| 152 | TextDialog::StringTextQuery::~StringTextQuery() {}
|
---|
| 153 |
|
---|
| 154 | bool TextDialog::StringTextQuery::handle() {
|
---|
[7aa000] | 155 | Log() << Verbose(0) << getTitle();
|
---|
[8de8f7] | 156 | getline(cin,tmp);
|
---|
[45f5d6] | 157 | return true;
|
---|
[f5a86a] | 158 | }
|
---|
[7aa000] | 159 |
|
---|
[a2ab15] | 160 | TextDialog::DoubleTextQuery::DoubleTextQuery(string title,double *_target, std::string _description) :
|
---|
| 161 | Dialog::DoubleQuery(title,_target,_description)
|
---|
[2ededc2] | 162 | {}
|
---|
| 163 |
|
---|
| 164 | TextDialog::DoubleTextQuery::~DoubleTextQuery() {}
|
---|
| 165 |
|
---|
| 166 | bool TextDialog::DoubleTextQuery::handle() {
|
---|
[8de8f7] | 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');
|
---|
[2ededc2] | 180 | return true;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[97ebf8] | 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)
|
---|
[7aa000] | 217 | {}
|
---|
| 218 |
|
---|
| 219 | TextDialog::MoleculeTextQuery::~MoleculeTextQuery() {}
|
---|
| 220 |
|
---|
| 221 | bool TextDialog::MoleculeTextQuery::handle() {
|
---|
[8de8f7] | 222 | int idxOfMol=0;
|
---|
| 223 | bool badInput = false;
|
---|
| 224 | do{
|
---|
| 225 | badInput = false;
|
---|
[7aa000] | 226 | Log() << Verbose(0) << getTitle();
|
---|
| 227 | cin >> idxOfMol;
|
---|
[8de8f7] | 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 |
|
---|
[97ebf8] | 236 | tmp = World::getInstance().getMolecule(MoleculeById(idxOfMol));
|
---|
[8de8f7] | 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');
|
---|
[7aa000] | 244 | return (idxOfMol!=-1);
|
---|
| 245 | }
|
---|
[2ededc2] | 246 |
|
---|
[84c494] | 247 | TextDialog::VectorTextQuery::VectorTextQuery(std::string title, Vector *_target, bool _check, std::string _description) :
|
---|
| 248 | Dialog::VectorQuery(title,_target,_check,_description)
|
---|
[2ededc2] | 249 | {}
|
---|
| 250 |
|
---|
| 251 | TextDialog::VectorTextQuery::~VectorTextQuery()
|
---|
| 252 | {}
|
---|
| 253 |
|
---|
| 254 | bool TextDialog::VectorTextQuery::handle() {
|
---|
[0a4f7f] | 255 | Log() << Verbose(0) << getTitle();
|
---|
| 256 |
|
---|
[84c494] | 257 | const Matrix &M = World::getInstance().getDomain().getM();
|
---|
[0a4f7f] | 258 | char coords[3] = {'x','y','z'};
|
---|
| 259 | for (int i=0;i<3;i++) {
|
---|
| 260 | do {
|
---|
[84c494] | 261 | Log() << Verbose(0) << coords[i] << "[0.." << M.at(i,i) << "]: ";
|
---|
[0a4f7f] | 262 | cin >> (*tmp)[i];
|
---|
[84c494] | 263 | } while ((((*tmp)[i] < 0) || ((*tmp)[i] >= M.at(i,i))) && (check));
|
---|
[0a4f7f] | 264 | }
|
---|
| 265 | return true;
|
---|
[5a7243] | 266 | }
|
---|
| 267 |
|
---|
[84c494] | 268 | TextDialog::BoxTextQuery::BoxTextQuery(std::string title, Box* _cellSize, std::string _description) :
|
---|
[97ebf8] | 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 | }
|
---|
[5a7243] | 285 |
|
---|
[104524] | 286 | TextDialog::ElementTextQuery::ElementTextQuery(std::string title, std::vector<element *> *_target, std::string _description) :
|
---|
| 287 | Dialog::ElementQuery(title,_target,_description)
|
---|
[5a7243] | 288 | {}
|
---|
| 289 |
|
---|
| 290 | TextDialog::ElementTextQuery::~ElementTextQuery()
|
---|
| 291 | {}
|
---|
| 292 |
|
---|
| 293 | bool TextDialog::ElementTextQuery::handle() {
|
---|
[8de8f7] | 294 | bool badInput=false;
|
---|
| 295 | bool aborted = false;
|
---|
[104524] | 296 | element * tmp = NULL;
|
---|
[8de8f7] | 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;
|
---|
[104524] | 313 | } else {
|
---|
| 314 | elements.push_back(tmp);
|
---|
[8de8f7] | 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;
|
---|
[104524] | 337 | } else {
|
---|
| 338 | elements.push_back(tmp);
|
---|
[8de8f7] | 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;
|
---|
[2ededc2] | 352 | }
|
---|