source: molecuilder/src/UIElements/CommandLineUI/CommandLineDialog.cpp@ aa36cc

Last change on this file since aa36cc was 7a6fd9, checked in by Frederik Heber <heber@…>, 16 years ago

Actions put into libMolecuilderActions, UIElements into libMolecuilderUI

  • due to great number of Actions it is sensible to put them into their own library
  • this is advantageous overall, but right now especially as all the unit tests do not depend on this lib.
  • it was also needed as Actions depend on UIElements and to resolve to correct ordering, UIElements have to be available before the libMolecuilderActions is aded (if compiled into libmolecuilder it is appended after!)
  • Property mode set to 100644
File size: 8.6 KB
Line 
1/*
2 * CommandLineDialog.cpp
3 *
4 * Created on: May 8, 2010
5 * Author: heber
6 */
7
8
9#include <cassert>
10#include <iostream>
11
12#include <Descriptors/AtomDescriptor.hpp>
13#include <Descriptors/AtomIdDescriptor.hpp>
14#include <Descriptors/MoleculeDescriptor.hpp>
15#include <Descriptors/MoleculeIdDescriptor.hpp>
16#include "CommandLineUI/CommandLineDialog.hpp"
17
18#include "element.hpp"
19#include "periodentafel.hpp"
20#include "CommandLineParser.hpp"
21#include "defs.hpp"
22#include "log.hpp"
23#include "periodentafel.hpp"
24#include "verbose.hpp"
25#include "World.hpp"
26
27#include "atom.hpp"
28#include "element.hpp"
29#include "molecule.hpp"
30#include "vector.hpp"
31
32using namespace std;
33
34
35CommandLineDialog::CommandLineDialog()
36{
37}
38
39CommandLineDialog::~CommandLineDialog()
40{
41}
42
43
44void CommandLineDialog::queryEmpty(const char* title, string _description){
45 registerQuery(new EmptyCommandLineQuery(title, _description));
46}
47
48void CommandLineDialog::queryInt(const char* title, int* target, string _description){
49 registerQuery(new IntCommandLineQuery(title,target, _description));
50}
51
52void CommandLineDialog::queryBoolean(const char* title, bool* target, string _description){
53 registerQuery(new BooleanCommandLineQuery(title,target, _description));
54}
55
56void CommandLineDialog::queryDouble(const char* title, double* target, string _description){
57 registerQuery(new DoubleCommandLineQuery(title,target, _description));
58}
59
60void CommandLineDialog::queryString(const char* title, string* target, string _description){
61 registerQuery(new StringCommandLineQuery(title,target, _description));
62}
63
64void CommandLineDialog::queryAtom(const char* title, atom **target, string _description) {
65 registerQuery(new AtomCommandLineQuery(title,target, _description));
66}
67
68void CommandLineDialog::queryMolecule(const char* title, molecule **target, string _description) {
69 registerQuery(new MoleculeCommandLineQuery(title,target, _description));
70}
71
72void 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
76void CommandLineDialog::queryBox(const char* title, double ** const cellSize, string _description) {
77 registerQuery(new BoxCommandLineQuery(title,cellSize,_description));
78}
79
80void CommandLineDialog::queryElement(const char* title, const element **target, string _description){
81 registerQuery(new ElementCommandLineQuery(title,target, _description));
82}
83
84/************************** Query Infrastructure ************************/
85
86CommandLineDialog::EmptyCommandLineQuery::EmptyCommandLineQuery(string title, string _description) :
87 Dialog::EmptyQuery(title, _description)
88{}
89
90CommandLineDialog::EmptyCommandLineQuery::~EmptyCommandLineQuery() {}
91
92bool CommandLineDialog::EmptyCommandLineQuery::handle() {
93 cout << "Message of " << getTitle() << ":\n" << getDescription() << "\n";
94 return true;
95}
96
97CommandLineDialog::IntCommandLineQuery::IntCommandLineQuery(string title,int *_target, string _description) :
98 Dialog::IntQuery(title,_target, _description)
99{}
100
101CommandLineDialog::IntCommandLineQuery::~IntCommandLineQuery() {}
102
103bool CommandLineDialog::IntCommandLineQuery::handle() {
104 if (CommandLineParser::getInstance().vm.count(getTitle())) {
105 tmp = CommandLineParser::getInstance().vm[getTitle()].as<int>();
106 return true;
107 } else
108 return false;
109}
110
111CommandLineDialog::BooleanCommandLineQuery::BooleanCommandLineQuery(string title,bool *_target, string _description) :
112 Dialog::BooleanQuery(title,_target, _description)
113{}
114
115CommandLineDialog::BooleanCommandLineQuery::~BooleanCommandLineQuery() {}
116
117bool 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
140CommandLineDialog::StringCommandLineQuery::StringCommandLineQuery(string title,string *_target, string _description) :
141 Dialog::StringQuery(title,_target, _description)
142{}
143
144CommandLineDialog::StringCommandLineQuery::~StringCommandLineQuery() {}
145
146bool CommandLineDialog::StringCommandLineQuery::handle() {
147 if (CommandLineParser::getInstance().vm.count(getTitle())) {
148 tmp = CommandLineParser::getInstance().vm[getTitle()].as<string>();
149 return true;
150 } else
151 return false;
152}
153
154CommandLineDialog::DoubleCommandLineQuery::DoubleCommandLineQuery(string title,double *_target, string _description) :
155 Dialog::DoubleQuery(title,_target, _description)
156{}
157
158CommandLineDialog::DoubleCommandLineQuery::~DoubleCommandLineQuery() {}
159
160bool CommandLineDialog::DoubleCommandLineQuery::handle() {
161 if (CommandLineParser::getInstance().vm.count(getTitle())) {
162 tmp = CommandLineParser::getInstance().vm[getTitle()].as<double>();
163 return true;
164 } else
165 return false;
166}
167
168CommandLineDialog::AtomCommandLineQuery::AtomCommandLineQuery(string title, atom **_target, string _description) :
169 Dialog::AtomQuery(title,_target, _description)
170{}
171
172CommandLineDialog::AtomCommandLineQuery::~AtomCommandLineQuery() {}
173
174bool 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
184CommandLineDialog::MoleculeCommandLineQuery::MoleculeCommandLineQuery(string title, molecule **_target, string _description) :
185 Dialog::MoleculeQuery(title,_target, _description)
186{}
187
188CommandLineDialog::MoleculeCommandLineQuery::~MoleculeCommandLineQuery() {}
189
190bool CommandLineDialog::MoleculeCommandLineQuery::handle() {
191 int IdxOfMol = -1;
192 if (CommandLineParser::getInstance().vm.count(getTitle())) {
193 IdxOfMol = CommandLineParser::getInstance().vm[getTitle()].as<int>();
194 tmp = World::getInstance().getMolecule(MoleculeById(IdxOfMol));
195 return true;
196 } else
197 return false;
198}
199
200CommandLineDialog::VectorCommandLineQuery::VectorCommandLineQuery(string title, Vector *_target, const double *const _cellSize, bool _check, string _description) :
201 Dialog::VectorQuery(title,_target,_cellSize,_check, _description)
202{}
203
204CommandLineDialog::VectorCommandLineQuery::~VectorCommandLineQuery()
205{}
206
207bool CommandLineDialog::VectorCommandLineQuery::handle() {
208 vector<double> temp;
209 if (CommandLineParser::getInstance().vm.count(getTitle())) {
210 temp = CommandLineParser::getInstance().vm[getTitle()].as<vector<double> >();
211 assert((temp.size() == 3) && "Vector from command line does not have three components.");
212 for (int i=0;i<NDIM;i++)
213 tmp->at(i) = temp[i];
214 return true;
215 } else
216 return false;
217}
218
219
220CommandLineDialog::BoxCommandLineQuery::BoxCommandLineQuery(string title, double ** const _cellSize, string _description) :
221 Dialog::BoxQuery(title,_cellSize, _description)
222{}
223
224CommandLineDialog::BoxCommandLineQuery::~BoxCommandLineQuery()
225{}
226
227bool CommandLineDialog::BoxCommandLineQuery::handle() {
228 vector<double> temp;
229 if (CommandLineParser::getInstance().vm.count(getTitle())) {
230 temp = CommandLineParser::getInstance().vm[getTitle()].as<vector<double> >();
231 assert((temp.size() == 6) && "Symmetric box matrix from command line does not have six components.");
232 for (int i=0;i<6;i++)
233 tmp[i] = temp[i];
234 return true;
235 } else
236 return false;
237}
238
239CommandLineDialog::ElementCommandLineQuery::ElementCommandLineQuery(string title, const element **target, string _description) :
240 Dialog::ElementQuery(title,target, _description)
241{}
242
243CommandLineDialog::ElementCommandLineQuery::~ElementCommandLineQuery()
244{}
245
246bool CommandLineDialog::ElementCommandLineQuery::handle() {
247 // TODO: vector of ints and removing first is not correctly implemented yet. How to remove from a vector?
248 int Z;
249 if (CommandLineParser::getInstance().vm.count(getTitle())) {
250 vector<int> AllElements = CommandLineParser::getInstance().vm[getTitle()].as< vector<int> >();
251 vector<int>::iterator ElementRunner = AllElements.begin();
252 Z = *ElementRunner;
253 // TODO: So far, this does not really erase the element in the parsed list.
254 AllElements.erase(ElementRunner);
255 tmp = World::getInstance().getPeriode()->FindElement(Z);
256 return true;
257 } else
258 return false;
259}
Note: See TracBrowser for help on using the repository browser.