| [f5a86a] | 1 | /*
 | 
|---|
 | 2 |  * TextDialog.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Jan 5, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #include <iostream>
 | 
|---|
 | 9 | 
 | 
|---|
| [7aa000] | 10 | #include "UIElements/TextDialog.hpp"
 | 
|---|
 | 11 | 
 | 
|---|
| [5a7243] | 12 | #include "World.hpp"
 | 
|---|
 | 13 | #include "periodentafel.hpp"
 | 
|---|
| [7aa000] | 14 | #include "atom.hpp"
 | 
|---|
 | 15 | #include "molecule.hpp"
 | 
|---|
| [f5a86a] | 16 | #include "log.hpp"
 | 
|---|
 | 17 | #include "verbose.hpp"
 | 
|---|
 | 18 | 
 | 
|---|
 | 19 | using namespace std;
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | TextDialog::TextDialog()
 | 
|---|
 | 23 | {
 | 
|---|
 | 24 | }
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | TextDialog::~TextDialog()
 | 
|---|
 | 27 | {
 | 
|---|
 | 28 | }
 | 
|---|
 | 29 | 
 | 
|---|
| [7aa000] | 30 | 
 | 
|---|
| [a2ab15] | 31 | void TextDialog::queryInt(const char* title, int* target, string description){
 | 
|---|
 | 32 |   registerQuery(new IntTextQuery(title,target,description));
 | 
|---|
| [45f5d6] | 33 | }
 | 
|---|
 | 34 | 
 | 
|---|
| [a2ab15] | 35 | void TextDialog::queryDouble(const char* title, double* target, string description){
 | 
|---|
 | 36 |   registerQuery(new DoubleTextQuery(title,target,description));
 | 
|---|
| [2ededc2] | 37 | }
 | 
|---|
 | 38 | 
 | 
|---|
| [a2ab15] | 39 | void TextDialog::queryString(const char* title, string* target, string description){
 | 
|---|
 | 40 |   registerQuery(new StringTextQuery(title,target,description));
 | 
|---|
| [f5a86a] | 41 | }
 | 
|---|
 | 42 | 
 | 
|---|
| [a2ab15] | 43 | void TextDialog::queryMolecule(const char* title, molecule **target, MoleculeListClass *molecules, string description) {
 | 
|---|
 | 44 |   registerQuery(new MoleculeTextQuery(title,target,molecules,description));
 | 
|---|
| [7aa000] | 45 | }
 | 
|---|
 | 46 | 
 | 
|---|
| [a2ab15] | 47 | void TextDialog::queryVector(const char* title, Vector *target,const double *const cellSize, bool check, string description) {
 | 
|---|
 | 48 |   registerQuery(new VectorTextQuery(title,target,cellSize,check,description));
 | 
|---|
| [2ededc2] | 49 | }
 | 
|---|
 | 50 | 
 | 
|---|
| [a2ab15] | 51 | void TextDialog::queryElement(const char* title, const element **target, string description){
 | 
|---|
 | 52 |   registerQuery(new ElementTextQuery(title,target,description));
 | 
|---|
| [5a7243] | 53 | }
 | 
|---|
 | 54 | 
 | 
|---|
| [7aa000] | 55 | /************************** Query Infrastructure ************************/
 | 
|---|
 | 56 | 
 | 
|---|
| [a2ab15] | 57 | TextDialog::IntTextQuery::IntTextQuery(string title,int *_target, std::string _description) :
 | 
|---|
 | 58 |     Dialog::IntQuery(title,_target,_description)
 | 
|---|
| [45f5d6] | 59 | {}
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | TextDialog::IntTextQuery::~IntTextQuery() {}
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | bool TextDialog::IntTextQuery::handle() {
 | 
|---|
| [8de8f7] | 64 |   bool badInput = false;
 | 
|---|
 | 65 |   do{
 | 
|---|
 | 66 |     badInput = false;
 | 
|---|
 | 67 |     Log() << Verbose(0) << getTitle();
 | 
|---|
 | 68 |     cin >> tmp;
 | 
|---|
 | 69 |     if(cin.fail()){
 | 
|---|
 | 70 |       badInput=true;
 | 
|---|
 | 71 |       cin.clear();
 | 
|---|
 | 72 |       cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
 | 
|---|
 | 73 |       Log() << Verbose(0) << "Input was not a number!" << endl;
 | 
|---|
 | 74 |     }
 | 
|---|
 | 75 |   } while(badInput);
 | 
|---|
 | 76 |   // clear the input buffer of anything still in the line
 | 
|---|
 | 77 |   cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
 | 
|---|
| [45f5d6] | 78 |   return true;
 | 
|---|
 | 79 | }
 | 
|---|
 | 80 | 
 | 
|---|
| [a2ab15] | 81 | TextDialog::StringTextQuery::StringTextQuery(string title,string *_target, std::string _description) :
 | 
|---|
 | 82 |     Dialog::StringQuery(title,_target,_description)
 | 
|---|
| [45f5d6] | 83 | {}
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 | TextDialog::StringTextQuery::~StringTextQuery() {}
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 | bool TextDialog::StringTextQuery::handle() {
 | 
|---|
| [7aa000] | 88 |   Log() << Verbose(0) << getTitle();
 | 
|---|
| [8de8f7] | 89 |   getline(cin,tmp);
 | 
|---|
| [45f5d6] | 90 |   return true;
 | 
|---|
| [f5a86a] | 91 | }
 | 
|---|
| [7aa000] | 92 | 
 | 
|---|
| [a2ab15] | 93 | TextDialog::DoubleTextQuery::DoubleTextQuery(string title,double *_target, std::string _description) :
 | 
|---|
 | 94 |     Dialog::DoubleQuery(title,_target,_description)
 | 
|---|
| [2ededc2] | 95 | {}
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 | TextDialog::DoubleTextQuery::~DoubleTextQuery() {}
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 | bool TextDialog::DoubleTextQuery::handle() {
 | 
|---|
| [8de8f7] | 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 |   cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
 | 
|---|
| [2ededc2] | 113 |   return true;
 | 
|---|
 | 114 | }
 | 
|---|
 | 115 | 
 | 
|---|
| [a2ab15] | 116 | TextDialog::MoleculeTextQuery::MoleculeTextQuery(string title, molecule **_target, MoleculeListClass *_molecules, std::string _description) :
 | 
|---|
 | 117 |     Dialog::MoleculeQuery(title,_target,_molecules,_description)
 | 
|---|
| [7aa000] | 118 | {}
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 | TextDialog::MoleculeTextQuery::~MoleculeTextQuery() {}
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | bool TextDialog::MoleculeTextQuery::handle() {
 | 
|---|
| [8de8f7] | 123 |   int idxOfMol=0;
 | 
|---|
 | 124 |   bool badInput = false;
 | 
|---|
 | 125 |   do{
 | 
|---|
 | 126 |     badInput = false;
 | 
|---|
| [7aa000] | 127 |     Log() << Verbose(0) << getTitle();
 | 
|---|
 | 128 |     cin >> idxOfMol;
 | 
|---|
| [8de8f7] | 129 |     if(cin.fail()){
 | 
|---|
 | 130 |       badInput = true;
 | 
|---|
 | 131 |       cin.clear();
 | 
|---|
 | 132 |       cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
 | 
|---|
 | 133 |       Log() << Verbose(0) << "Input was not a number!" << endl;
 | 
|---|
 | 134 |       continue;
 | 
|---|
 | 135 |     }
 | 
|---|
 | 136 | 
 | 
|---|
| [7aa000] | 137 |     tmp = molecules->ReturnIndex(idxOfMol);
 | 
|---|
| [8de8f7] | 138 |     if(!tmp && idxOfMol!=-1){
 | 
|---|
 | 139 |       Log() << Verbose(0) << "Invalid Molecule Index" << endl;
 | 
|---|
 | 140 |       badInput = true;
 | 
|---|
 | 141 |     }
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 |   } while(badInput);
 | 
|---|
 | 144 |   cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
 | 
|---|
| [7aa000] | 145 |   return (idxOfMol!=-1);
 | 
|---|
 | 146 | }
 | 
|---|
| [2ededc2] | 147 | 
 | 
|---|
| [a2ab15] | 148 | TextDialog::VectorTextQuery::VectorTextQuery(std::string title, Vector *_target, const double *const _cellSize, bool _check, std::string _description) :
 | 
|---|
 | 149 |     Dialog::VectorQuery(title,_target,_cellSize,_check,_description)
 | 
|---|
| [2ededc2] | 150 | {}
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 | TextDialog::VectorTextQuery::~VectorTextQuery()
 | 
|---|
 | 153 | {}
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 | bool TextDialog::VectorTextQuery::handle() {
 | 
|---|
| [0a4f7f] | 156 |   Log() << Verbose(0) << getTitle();
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 |   char coords[3] = {'x','y','z'};
 | 
|---|
 | 159 |   int j = -1;
 | 
|---|
 | 160 |   for (int i=0;i<3;i++) {
 | 
|---|
 | 161 |     j += i+1;
 | 
|---|
 | 162 |     do {
 | 
|---|
 | 163 |       Log() << Verbose(0) << coords[i] << "[0.." << cellSize[j] << "]: ";
 | 
|---|
 | 164 |       cin >> (*tmp)[i];
 | 
|---|
 | 165 |     } while ((((*tmp)[i] < 0) || ((*tmp)[i] >= cellSize[j])) && (check));
 | 
|---|
 | 166 |   }
 | 
|---|
 | 167 |   return true;
 | 
|---|
| [5a7243] | 168 | }
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 | 
 | 
|---|
| [a2ab15] | 171 | TextDialog::ElementTextQuery::ElementTextQuery(std::string title, const element **target, std::string _description) :
 | 
|---|
 | 172 |     Dialog::ElementQuery(title,target,_description)
 | 
|---|
| [5a7243] | 173 | {}
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 | TextDialog::ElementTextQuery::~ElementTextQuery()
 | 
|---|
 | 176 | {}
 | 
|---|
 | 177 | 
 | 
|---|
 | 178 | bool TextDialog::ElementTextQuery::handle() {
 | 
|---|
| [8de8f7] | 179 |   bool badInput=false;
 | 
|---|
 | 180 |   bool aborted = false;
 | 
|---|
 | 181 |   do{
 | 
|---|
 | 182 |     badInput = false;
 | 
|---|
 | 183 |     Log() << Verbose(0) << getTitle();
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 |     // try to read as Atomic number
 | 
|---|
 | 186 |     int Z;
 | 
|---|
 | 187 |     cin >> Z;
 | 
|---|
 | 188 |     if(!cin.fail()){
 | 
|---|
 | 189 |       if(Z==-1){
 | 
|---|
 | 190 |         aborted = true;
 | 
|---|
 | 191 |       }
 | 
|---|
 | 192 |       else{
 | 
|---|
 | 193 |         tmp = World::getInstance().getPeriode()->FindElement(Z);
 | 
|---|
 | 194 |         if(!tmp){
 | 
|---|
 | 195 |           Log() << Verbose(0) << "No element with this atomic number!" << endl;
 | 
|---|
 | 196 |           badInput = true;
 | 
|---|
 | 197 |         }
 | 
|---|
 | 198 |       }
 | 
|---|
 | 199 |       continue;
 | 
|---|
 | 200 |     }
 | 
|---|
 | 201 |     else{
 | 
|---|
 | 202 |       cin.clear();
 | 
|---|
 | 203 |     }
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 |     // Try to read as shorthand
 | 
|---|
 | 206 |     // the last buffer content was not removed, so we read the
 | 
|---|
 | 207 |     // same thing again, this time as a string
 | 
|---|
 | 208 |     string shorthand;
 | 
|---|
 | 209 |     cin >> shorthand;
 | 
|---|
 | 210 |     if(!cin.fail()){
 | 
|---|
 | 211 |       if(shorthand.empty()){
 | 
|---|
 | 212 |         aborted = true;
 | 
|---|
 | 213 |       }
 | 
|---|
 | 214 |       else{
 | 
|---|
 | 215 |         tmp = World::getInstance().getPeriode()->FindElement(shorthand.c_str());
 | 
|---|
 | 216 |         if(!tmp){
 | 
|---|
 | 217 |           Log() << Verbose(0) << "No element with this shorthand!" << endl;
 | 
|---|
 | 218 |           badInput = true;
 | 
|---|
 | 219 |         }
 | 
|---|
 | 220 |       }
 | 
|---|
 | 221 |     }
 | 
|---|
 | 222 |     else{
 | 
|---|
 | 223 |       Log() << Verbose(0) << "Could not read input. Try Again." << endl;
 | 
|---|
 | 224 |       cin.clear();
 | 
|---|
 | 225 |       cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
 | 
|---|
 | 226 |       badInput = true;
 | 
|---|
 | 227 |     }
 | 
|---|
 | 228 | 
 | 
|---|
 | 229 |   }while(badInput);
 | 
|---|
 | 230 |   cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
 | 
|---|
 | 231 |   return !aborted;
 | 
|---|
| [2ededc2] | 232 | }
 | 
|---|