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