| [a2ab15] | 1 | /* | 
|---|
|  | 2 | * CommandLineDialog.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: May 8, 2010 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 |  | 
|---|
| [d90762] | 9 | #include <cassert> | 
|---|
| [a2ab15] | 10 | #include <iostream> | 
|---|
|  | 11 |  | 
|---|
| [97ebf8] | 12 | #include <Descriptors/AtomDescriptor.hpp> | 
|---|
|  | 13 | #include <Descriptors/AtomIdDescriptor.hpp> | 
|---|
| [d90762] | 14 | #include <Descriptors/MoleculeDescriptor.hpp> | 
|---|
|  | 15 | #include <Descriptors/MoleculeIdDescriptor.hpp> | 
|---|
| [5079a0] | 16 | #include "CommandLineUI/CommandLineDialog.hpp" | 
|---|
| [a2ab15] | 17 |  | 
|---|
| [97ebf8] | 18 | #include "element.hpp" | 
|---|
| [a2ab15] | 19 | #include "periodentafel.hpp" | 
|---|
| [d90762] | 20 | #include "CommandLineParser.hpp" | 
|---|
|  | 21 | #include "defs.hpp" | 
|---|
| [a2ab15] | 22 | #include "log.hpp" | 
|---|
| [97ebf8] | 23 | #include "periodentafel.hpp" | 
|---|
| [a2ab15] | 24 | #include "verbose.hpp" | 
|---|
| [d90762] | 25 | #include "World.hpp" | 
|---|
| [a2ab15] | 26 |  | 
|---|
| [97ebf8] | 27 | #include "atom.hpp" | 
|---|
|  | 28 | #include "element.hpp" | 
|---|
|  | 29 | #include "molecule.hpp" | 
|---|
|  | 30 | #include "vector.hpp" | 
|---|
|  | 31 |  | 
|---|
| [a2ab15] | 32 | using namespace std; | 
|---|
|  | 33 |  | 
|---|
|  | 34 |  | 
|---|
|  | 35 | CommandLineDialog::CommandLineDialog() | 
|---|
|  | 36 | { | 
|---|
|  | 37 | } | 
|---|
|  | 38 |  | 
|---|
|  | 39 | CommandLineDialog::~CommandLineDialog() | 
|---|
|  | 40 | { | 
|---|
|  | 41 | } | 
|---|
|  | 42 |  | 
|---|
|  | 43 |  | 
|---|
| [86466e] | 44 | void CommandLineDialog::queryEmpty(const char* title, string _description){ | 
|---|
|  | 45 | registerQuery(new EmptyCommandLineQuery(title, _description)); | 
|---|
| [a2ab15] | 46 | } | 
|---|
|  | 47 |  | 
|---|
| [86466e] | 48 | void CommandLineDialog::queryInt(const char* title, int* target, string _description){ | 
|---|
|  | 49 | registerQuery(new IntCommandLineQuery(title,target, _description)); | 
|---|
| [a2ab15] | 50 | } | 
|---|
|  | 51 |  | 
|---|
| [97ebf8] | 52 | void CommandLineDialog::queryBoolean(const char* title, bool* target, string _description){ | 
|---|
|  | 53 | registerQuery(new BooleanCommandLineQuery(title,target, _description)); | 
|---|
|  | 54 | } | 
|---|
|  | 55 |  | 
|---|
| [86466e] | 56 | void CommandLineDialog::queryDouble(const char* title, double* target, string _description){ | 
|---|
|  | 57 | registerQuery(new DoubleCommandLineQuery(title,target, _description)); | 
|---|
| [a2ab15] | 58 | } | 
|---|
|  | 59 |  | 
|---|
| [86466e] | 60 | void CommandLineDialog::queryString(const char* title, string* target, string _description){ | 
|---|
|  | 61 | registerQuery(new StringCommandLineQuery(title,target, _description)); | 
|---|
| [a2ab15] | 62 | } | 
|---|
|  | 63 |  | 
|---|
| [97ebf8] | 64 | void CommandLineDialog::queryAtom(const char* title, atom **target, string _description) { | 
|---|
|  | 65 | registerQuery(new AtomCommandLineQuery(title,target, _description)); | 
|---|
|  | 66 | } | 
|---|
|  | 67 |  | 
|---|
|  | 68 | void CommandLineDialog::queryMolecule(const char* title, molecule **target, string _description) { | 
|---|
|  | 69 | registerQuery(new MoleculeCommandLineQuery(title,target, _description)); | 
|---|
| [a2ab15] | 70 | } | 
|---|
|  | 71 |  | 
|---|
| [86466e] | 72 | void CommandLineDialog::queryVector(const char* title, Vector *target,const double *const cellSize, bool check, string _description) { | 
|---|
|  | 73 | registerQuery(new VectorCommandLineQuery(title,target,cellSize,check, _description)); | 
|---|
|  | 74 | } | 
|---|
|  | 75 |  | 
|---|
| [97ebf8] | 76 | void CommandLineDialog::queryBox(const char* title, double ** const cellSize, string _description) { | 
|---|
|  | 77 | registerQuery(new BoxCommandLineQuery(title,cellSize,_description)); | 
|---|
|  | 78 | } | 
|---|
|  | 79 |  | 
|---|
| [86466e] | 80 | void CommandLineDialog::queryElement(const char* title, const element **target, string _description){ | 
|---|
|  | 81 | registerQuery(new ElementCommandLineQuery(title,target, _description)); | 
|---|
| [a2ab15] | 82 | } | 
|---|
|  | 83 |  | 
|---|
|  | 84 | /************************** Query Infrastructure ************************/ | 
|---|
|  | 85 |  | 
|---|
| [86466e] | 86 | CommandLineDialog::EmptyCommandLineQuery::EmptyCommandLineQuery(string title, string _description) : | 
|---|
|  | 87 | Dialog::EmptyQuery(title, _description) | 
|---|
|  | 88 | {} | 
|---|
|  | 89 |  | 
|---|
|  | 90 | CommandLineDialog::EmptyCommandLineQuery::~EmptyCommandLineQuery() {} | 
|---|
|  | 91 |  | 
|---|
|  | 92 | bool CommandLineDialog::EmptyCommandLineQuery::handle() { | 
|---|
|  | 93 | cout << "Message of " << getTitle() << ":\n" << getDescription() << "\n"; | 
|---|
|  | 94 | return true; | 
|---|
|  | 95 | } | 
|---|
|  | 96 |  | 
|---|
|  | 97 | CommandLineDialog::IntCommandLineQuery::IntCommandLineQuery(string title,int *_target, string _description) : | 
|---|
|  | 98 | Dialog::IntQuery(title,_target, _description) | 
|---|
| [a2ab15] | 99 | {} | 
|---|
|  | 100 |  | 
|---|
| [86466e] | 101 | CommandLineDialog::IntCommandLineQuery::~IntCommandLineQuery() {} | 
|---|
| [a2ab15] | 102 |  | 
|---|
| [86466e] | 103 | bool CommandLineDialog::IntCommandLineQuery::handle() { | 
|---|
| [d90762] | 104 | if (CommandLineParser::getInstance().vm.count(getTitle())) { | 
|---|
|  | 105 | tmp = CommandLineParser::getInstance().vm[getTitle()].as<int>(); | 
|---|
|  | 106 | return true; | 
|---|
|  | 107 | } else | 
|---|
|  | 108 | return false; | 
|---|
| [a2ab15] | 109 | } | 
|---|
|  | 110 |  | 
|---|
| [97ebf8] | 111 | CommandLineDialog::BooleanCommandLineQuery::BooleanCommandLineQuery(string title,bool *_target, string _description) : | 
|---|
|  | 112 | Dialog::BooleanQuery(title,_target, _description) | 
|---|
|  | 113 | {} | 
|---|
|  | 114 |  | 
|---|
|  | 115 | CommandLineDialog::BooleanCommandLineQuery::~BooleanCommandLineQuery() {} | 
|---|
|  | 116 |  | 
|---|
|  | 117 | bool CommandLineDialog::BooleanCommandLineQuery::handle() { | 
|---|
|  | 118 | bool badInput = false; | 
|---|
|  | 119 | char input = ' '; | 
|---|
|  | 120 | do{ | 
|---|
|  | 121 | badInput = false; | 
|---|
|  | 122 | Log() << Verbose(0) << getTitle(); | 
|---|
|  | 123 | cin >> input; | 
|---|
|  | 124 | if ((input == 'y' ) || (input == 'Y')) { | 
|---|
|  | 125 | tmp = true; | 
|---|
|  | 126 | } else if ((input == 'n' ) || (input == 'N')) { | 
|---|
|  | 127 | tmp = false; | 
|---|
|  | 128 | } else { | 
|---|
|  | 129 | badInput=true; | 
|---|
|  | 130 | cin.clear(); | 
|---|
|  | 131 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
|  | 132 | Log() << Verbose(0) << "Input was not of [yYnN]!" << endl; | 
|---|
|  | 133 | } | 
|---|
|  | 134 | } while(badInput); | 
|---|
|  | 135 | // clear the input buffer of anything still in the line | 
|---|
|  | 136 | cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); | 
|---|
|  | 137 | return true; | 
|---|
|  | 138 | } | 
|---|
|  | 139 |  | 
|---|
| [86466e] | 140 | CommandLineDialog::StringCommandLineQuery::StringCommandLineQuery(string title,string *_target, string _description) : | 
|---|
|  | 141 | Dialog::StringQuery(title,_target, _description) | 
|---|
| [a2ab15] | 142 | {} | 
|---|
|  | 143 |  | 
|---|
| [86466e] | 144 | CommandLineDialog::StringCommandLineQuery::~StringCommandLineQuery() {} | 
|---|
| [a2ab15] | 145 |  | 
|---|
| [86466e] | 146 | bool CommandLineDialog::StringCommandLineQuery::handle() { | 
|---|
| [d90762] | 147 | if (CommandLineParser::getInstance().vm.count(getTitle())) { | 
|---|
| [86466e] | 148 | tmp = CommandLineParser::getInstance().vm[getTitle()].as<string>(); | 
|---|
| [d90762] | 149 | return true; | 
|---|
|  | 150 | } else | 
|---|
|  | 151 | return false; | 
|---|
| [a2ab15] | 152 | } | 
|---|
|  | 153 |  | 
|---|
| [86466e] | 154 | CommandLineDialog::DoubleCommandLineQuery::DoubleCommandLineQuery(string title,double *_target, string _description) : | 
|---|
|  | 155 | Dialog::DoubleQuery(title,_target, _description) | 
|---|
| [a2ab15] | 156 | {} | 
|---|
|  | 157 |  | 
|---|
| [86466e] | 158 | CommandLineDialog::DoubleCommandLineQuery::~DoubleCommandLineQuery() {} | 
|---|
| [a2ab15] | 159 |  | 
|---|
| [86466e] | 160 | bool CommandLineDialog::DoubleCommandLineQuery::handle() { | 
|---|
| [d90762] | 161 | if (CommandLineParser::getInstance().vm.count(getTitle())) { | 
|---|
|  | 162 | tmp = CommandLineParser::getInstance().vm[getTitle()].as<double>(); | 
|---|
|  | 163 | return true; | 
|---|
|  | 164 | } else | 
|---|
|  | 165 | return false; | 
|---|
| [a2ab15] | 166 | } | 
|---|
|  | 167 |  | 
|---|
| [97ebf8] | 168 | CommandLineDialog::AtomCommandLineQuery::AtomCommandLineQuery(string title, atom **_target, string _description) : | 
|---|
|  | 169 | Dialog::AtomQuery(title,_target, _description) | 
|---|
|  | 170 | {} | 
|---|
|  | 171 |  | 
|---|
|  | 172 | CommandLineDialog::AtomCommandLineQuery::~AtomCommandLineQuery() {} | 
|---|
|  | 173 |  | 
|---|
|  | 174 | bool CommandLineDialog::AtomCommandLineQuery::handle() { | 
|---|
|  | 175 | int IdxOfAtom = -1; | 
|---|
|  | 176 | if (CommandLineParser::getInstance().vm.count(getTitle())) { | 
|---|
|  | 177 | IdxOfAtom = CommandLineParser::getInstance().vm[getTitle()].as<int>(); | 
|---|
|  | 178 | tmp = World::getInstance().getAtom(AtomById(IdxOfAtom)); | 
|---|
|  | 179 | return true; | 
|---|
|  | 180 | } else | 
|---|
|  | 181 | return false; | 
|---|
|  | 182 | } | 
|---|
|  | 183 |  | 
|---|
|  | 184 | CommandLineDialog::MoleculeCommandLineQuery::MoleculeCommandLineQuery(string title, molecule **_target, string _description) : | 
|---|
|  | 185 | Dialog::MoleculeQuery(title,_target, _description) | 
|---|
| [a2ab15] | 186 | {} | 
|---|
|  | 187 |  | 
|---|
| [86466e] | 188 | CommandLineDialog::MoleculeCommandLineQuery::~MoleculeCommandLineQuery() {} | 
|---|
| [a2ab15] | 189 |  | 
|---|
| [86466e] | 190 | bool CommandLineDialog::MoleculeCommandLineQuery::handle() { | 
|---|
| [d90762] | 191 | int IdxOfMol = -1; | 
|---|
|  | 192 | if (CommandLineParser::getInstance().vm.count(getTitle())) { | 
|---|
|  | 193 | IdxOfMol = CommandLineParser::getInstance().vm[getTitle()].as<int>(); | 
|---|
| [e30ce8] | 194 | cout << "IdxOfMol " << IdxOfMol << endl; | 
|---|
|  | 195 | if (IdxOfMol >= 0) | 
|---|
|  | 196 | tmp = World::getInstance().getMolecule(MoleculeById(IdxOfMol)); | 
|---|
|  | 197 | else | 
|---|
|  | 198 | tmp = NULL; | 
|---|
| [d90762] | 199 | return true; | 
|---|
|  | 200 | } else | 
|---|
|  | 201 | return false; | 
|---|
| [a2ab15] | 202 | } | 
|---|
|  | 203 |  | 
|---|
| [86466e] | 204 | CommandLineDialog::VectorCommandLineQuery::VectorCommandLineQuery(string title, Vector *_target, const double *const _cellSize, bool _check, string _description) : | 
|---|
|  | 205 | Dialog::VectorQuery(title,_target,_cellSize,_check, _description) | 
|---|
| [a2ab15] | 206 | {} | 
|---|
|  | 207 |  | 
|---|
| [86466e] | 208 | CommandLineDialog::VectorCommandLineQuery::~VectorCommandLineQuery() | 
|---|
| [a2ab15] | 209 | {} | 
|---|
|  | 210 |  | 
|---|
| [86466e] | 211 | bool CommandLineDialog::VectorCommandLineQuery::handle() { | 
|---|
| [d90762] | 212 | vector<double> temp; | 
|---|
|  | 213 | if (CommandLineParser::getInstance().vm.count(getTitle())) { | 
|---|
|  | 214 | temp = CommandLineParser::getInstance().vm[getTitle()].as<vector<double> >(); | 
|---|
|  | 215 | assert((temp.size() == 3) && "Vector from command line does not have three components."); | 
|---|
|  | 216 | for (int i=0;i<NDIM;i++) | 
|---|
|  | 217 | tmp->at(i) = temp[i]; | 
|---|
|  | 218 | return true; | 
|---|
|  | 219 | } else | 
|---|
|  | 220 | return false; | 
|---|
| [a2ab15] | 221 | } | 
|---|
|  | 222 |  | 
|---|
|  | 223 |  | 
|---|
| [97ebf8] | 224 | CommandLineDialog::BoxCommandLineQuery::BoxCommandLineQuery(string title, double ** const _cellSize, string _description) : | 
|---|
|  | 225 | Dialog::BoxQuery(title,_cellSize, _description) | 
|---|
|  | 226 | {} | 
|---|
|  | 227 |  | 
|---|
|  | 228 | CommandLineDialog::BoxCommandLineQuery::~BoxCommandLineQuery() | 
|---|
|  | 229 | {} | 
|---|
|  | 230 |  | 
|---|
|  | 231 | bool CommandLineDialog::BoxCommandLineQuery::handle() { | 
|---|
|  | 232 | vector<double> temp; | 
|---|
|  | 233 | if (CommandLineParser::getInstance().vm.count(getTitle())) { | 
|---|
|  | 234 | temp = CommandLineParser::getInstance().vm[getTitle()].as<vector<double> >(); | 
|---|
|  | 235 | assert((temp.size() == 6) && "Symmetric box matrix from command line does not have six components."); | 
|---|
| [26f75a] | 236 | for (int i=0;i<6;i++) { | 
|---|
| [97ebf8] | 237 | tmp[i] = temp[i]; | 
|---|
| [26f75a] | 238 | } | 
|---|
| [97ebf8] | 239 | return true; | 
|---|
|  | 240 | } else | 
|---|
|  | 241 | return false; | 
|---|
|  | 242 | } | 
|---|
|  | 243 |  | 
|---|
| [86466e] | 244 | CommandLineDialog::ElementCommandLineQuery::ElementCommandLineQuery(string title, const element **target, string _description) : | 
|---|
|  | 245 | Dialog::ElementQuery(title,target, _description) | 
|---|
| [a2ab15] | 246 | {} | 
|---|
|  | 247 |  | 
|---|
| [86466e] | 248 | CommandLineDialog::ElementCommandLineQuery::~ElementCommandLineQuery() | 
|---|
| [a2ab15] | 249 | {} | 
|---|
|  | 250 |  | 
|---|
| [86466e] | 251 | bool CommandLineDialog::ElementCommandLineQuery::handle() { | 
|---|
| [97ebf8] | 252 | // TODO: vector of ints and removing first is not correctly implemented yet. How to remove from a vector? | 
|---|
|  | 253 | int Z; | 
|---|
| [d90762] | 254 | if (CommandLineParser::getInstance().vm.count(getTitle())) { | 
|---|
| [97ebf8] | 255 | vector<int> AllElements = CommandLineParser::getInstance().vm[getTitle()].as< vector<int> >(); | 
|---|
|  | 256 | vector<int>::iterator ElementRunner = AllElements.begin(); | 
|---|
|  | 257 | Z = *ElementRunner; | 
|---|
|  | 258 | // TODO: So far, this does not really erase the element in the parsed list. | 
|---|
|  | 259 | AllElements.erase(ElementRunner); | 
|---|
| [d90762] | 260 | tmp = World::getInstance().getPeriode()->FindElement(Z); | 
|---|
|  | 261 | return true; | 
|---|
|  | 262 | } else | 
|---|
|  | 263 | return false; | 
|---|
| [a2ab15] | 264 | } | 
|---|