/* * CommandLineDialog.cpp * * Created on: May 8, 2010 * Author: heber */ #include #include #include #include #include #include #include "CommandLineUI/CommandLineDialog.hpp" #include "element.hpp" #include "periodentafel.hpp" #include "CommandLineParser.hpp" #include "defs.hpp" #include "log.hpp" #include "periodentafel.hpp" #include "verbose.hpp" #include "World.hpp" #include "atom.hpp" #include "element.hpp" #include "molecule.hpp" #include "vector.hpp" using namespace std; CommandLineDialog::CommandLineDialog() { } CommandLineDialog::~CommandLineDialog() { } void CommandLineDialog::queryEmpty(const char* title, string _description){ registerQuery(new EmptyCommandLineQuery(title, _description)); } void CommandLineDialog::queryInt(const char* title, int* target, string _description){ registerQuery(new IntCommandLineQuery(title,target, _description)); } void CommandLineDialog::queryBoolean(const char* title, bool* target, string _description){ registerQuery(new BooleanCommandLineQuery(title,target, _description)); } void CommandLineDialog::queryDouble(const char* title, double* target, string _description){ registerQuery(new DoubleCommandLineQuery(title,target, _description)); } void CommandLineDialog::queryString(const char* title, string* target, string _description){ registerQuery(new StringCommandLineQuery(title,target, _description)); } void CommandLineDialog::queryAtom(const char* title, atom **target, string _description) { registerQuery(new AtomCommandLineQuery(title,target, _description)); } void CommandLineDialog::queryMolecule(const char* title, molecule **target, string _description) { registerQuery(new MoleculeCommandLineQuery(title,target, _description)); } void CommandLineDialog::queryVector(const char* title, Vector *target,const double *const cellSize, bool check, string _description) { registerQuery(new VectorCommandLineQuery(title,target,cellSize,check, _description)); } void CommandLineDialog::queryBox(const char* title, double ** const cellSize, string _description) { registerQuery(new BoxCommandLineQuery(title,cellSize,_description)); } void CommandLineDialog::queryElement(const char* title, const element **target, string _description){ registerQuery(new ElementCommandLineQuery(title,target, _description)); } /************************** Query Infrastructure ************************/ CommandLineDialog::EmptyCommandLineQuery::EmptyCommandLineQuery(string title, string _description) : Dialog::EmptyQuery(title, _description) {} CommandLineDialog::EmptyCommandLineQuery::~EmptyCommandLineQuery() {} bool CommandLineDialog::EmptyCommandLineQuery::handle() { cout << "Message of " << getTitle() << ":\n" << getDescription() << "\n"; return true; } CommandLineDialog::IntCommandLineQuery::IntCommandLineQuery(string title,int *_target, string _description) : Dialog::IntQuery(title,_target, _description) {} CommandLineDialog::IntCommandLineQuery::~IntCommandLineQuery() {} bool CommandLineDialog::IntCommandLineQuery::handle() { if (CommandLineParser::getInstance().vm.count(getTitle())) { tmp = CommandLineParser::getInstance().vm[getTitle()].as(); return true; } else return false; } CommandLineDialog::BooleanCommandLineQuery::BooleanCommandLineQuery(string title,bool *_target, string _description) : Dialog::BooleanQuery(title,_target, _description) {} CommandLineDialog::BooleanCommandLineQuery::~BooleanCommandLineQuery() {} bool CommandLineDialog::BooleanCommandLineQuery::handle() { bool badInput = false; char input = ' '; do{ badInput = false; Log() << Verbose(0) << getTitle(); cin >> input; if ((input == 'y' ) || (input == 'Y')) { tmp = true; } else if ((input == 'n' ) || (input == 'N')) { tmp = false; } else { badInput=true; cin.clear(); cin.ignore(std::numeric_limits::max(),'\n'); Log() << Verbose(0) << "Input was not of [yYnN]!" << endl; } } while(badInput); // clear the input buffer of anything still in the line cin.ignore(std::numeric_limits::max(),'\n'); return true; } CommandLineDialog::StringCommandLineQuery::StringCommandLineQuery(string title,string *_target, string _description) : Dialog::StringQuery(title,_target, _description) {} CommandLineDialog::StringCommandLineQuery::~StringCommandLineQuery() {} bool CommandLineDialog::StringCommandLineQuery::handle() { if (CommandLineParser::getInstance().vm.count(getTitle())) { tmp = CommandLineParser::getInstance().vm[getTitle()].as(); return true; } else return false; } CommandLineDialog::DoubleCommandLineQuery::DoubleCommandLineQuery(string title,double *_target, string _description) : Dialog::DoubleQuery(title,_target, _description) {} CommandLineDialog::DoubleCommandLineQuery::~DoubleCommandLineQuery() {} bool CommandLineDialog::DoubleCommandLineQuery::handle() { if (CommandLineParser::getInstance().vm.count(getTitle())) { tmp = CommandLineParser::getInstance().vm[getTitle()].as(); return true; } else return false; } CommandLineDialog::AtomCommandLineQuery::AtomCommandLineQuery(string title, atom **_target, string _description) : Dialog::AtomQuery(title,_target, _description) {} CommandLineDialog::AtomCommandLineQuery::~AtomCommandLineQuery() {} bool CommandLineDialog::AtomCommandLineQuery::handle() { int IdxOfAtom = -1; if (CommandLineParser::getInstance().vm.count(getTitle())) { IdxOfAtom = CommandLineParser::getInstance().vm[getTitle()].as(); tmp = World::getInstance().getAtom(AtomById(IdxOfAtom)); return true; } else return false; } CommandLineDialog::MoleculeCommandLineQuery::MoleculeCommandLineQuery(string title, molecule **_target, string _description) : Dialog::MoleculeQuery(title,_target, _description) {} CommandLineDialog::MoleculeCommandLineQuery::~MoleculeCommandLineQuery() {} bool CommandLineDialog::MoleculeCommandLineQuery::handle() { int IdxOfMol = -1; if (CommandLineParser::getInstance().vm.count(getTitle())) { IdxOfMol = CommandLineParser::getInstance().vm[getTitle()].as(); tmp = World::getInstance().getMolecule(MoleculeById(IdxOfMol)); return true; } else return false; } CommandLineDialog::VectorCommandLineQuery::VectorCommandLineQuery(string title, Vector *_target, const double *const _cellSize, bool _check, string _description) : Dialog::VectorQuery(title,_target,_cellSize,_check, _description) {} CommandLineDialog::VectorCommandLineQuery::~VectorCommandLineQuery() {} bool CommandLineDialog::VectorCommandLineQuery::handle() { vector temp; if (CommandLineParser::getInstance().vm.count(getTitle())) { temp = CommandLineParser::getInstance().vm[getTitle()].as >(); assert((temp.size() == 3) && "Vector from command line does not have three components."); for (int i=0;iat(i) = temp[i]; return true; } else return false; } CommandLineDialog::BoxCommandLineQuery::BoxCommandLineQuery(string title, double ** const _cellSize, string _description) : Dialog::BoxQuery(title,_cellSize, _description) {} CommandLineDialog::BoxCommandLineQuery::~BoxCommandLineQuery() {} bool CommandLineDialog::BoxCommandLineQuery::handle() { vector temp; if (CommandLineParser::getInstance().vm.count(getTitle())) { temp = CommandLineParser::getInstance().vm[getTitle()].as >(); assert((temp.size() == 6) && "Symmetric box matrix from command line does not have six components."); for (int i=0;i<6;i++) tmp[i] = temp[i]; return true; } else return false; } CommandLineDialog::ElementCommandLineQuery::ElementCommandLineQuery(string title, const element **target, string _description) : Dialog::ElementQuery(title,target, _description) {} CommandLineDialog::ElementCommandLineQuery::~ElementCommandLineQuery() {} bool CommandLineDialog::ElementCommandLineQuery::handle() { // TODO: vector of ints and removing first is not correctly implemented yet. How to remove from a vector? int Z; if (CommandLineParser::getInstance().vm.count(getTitle())) { vector AllElements = CommandLineParser::getInstance().vm[getTitle()].as< vector >(); vector::iterator ElementRunner = AllElements.begin(); Z = *ElementRunner; // TODO: So far, this does not really erase the element in the parsed list. AllElements.erase(ElementRunner); tmp = World::getInstance().getPeriode()->FindElement(Z); return true; } else return false; }