source: src/UIElements/TextUI/TextDialog.cpp@ b9c847

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since b9c847 was cd8e55, checked in by Frederik Heber <heber@…>, 15 years ago

New StringsQuery that returns space-separated vector of strings.

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*
2 * TextDialog.cpp
3 *
4 * Created on: Jan 5, 2010
5 * Author: crueger
6 */
7
8#include "Helpers/MemDebug.hpp"
9
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 "TextUI/TextDialog.hpp"
17
18#include "World.hpp"
19#include "periodentafel.hpp"
20#include "log.hpp"
21#include "verbose.hpp"
22
23#include "atom.hpp"
24#include "element.hpp"
25#include "molecule.hpp"
26#include "vector.hpp"
27
28using namespace std;
29
30
31TextDialog::TextDialog()
32{
33}
34
35TextDialog::~TextDialog()
36{
37}
38
39
40void TextDialog::queryEmpty(const char* title, string description){
41 registerQuery(new EmptyTextQuery(title,description));
42}
43
44void TextDialog::queryBoolean(const char* title, bool* target, string description){
45 registerQuery(new BooleanTextQuery(title,target,description));
46}
47
48void TextDialog::queryInt(const char* title, int* target, string description){
49 registerQuery(new IntTextQuery(title,target,description));
50}
51
52void TextDialog::queryDouble(const char* title, double* target, string description){
53 registerQuery(new DoubleTextQuery(title,target,description));
54}
55
56void TextDialog::queryString(const char* title, string* target, string description){
57 registerQuery(new StringTextQuery(title,target,description));
58}
59
60void TextDialog::queryStrings(const char* title, vector<string>* target, string description){
61 registerQuery(new StringsTextQuery(title,target,description));
62}
63
64void TextDialog::queryAtom(const char* title, atom **target, string description) {
65 registerQuery(new AtomTextQuery(title,target,description));
66}
67
68void TextDialog::queryMolecule(const char* title, molecule **target, string description) {
69 registerQuery(new MoleculeTextQuery(title,target,description));
70}
71
72void TextDialog::queryVector(const char* title, Vector *target,const double *const cellSize, bool check, string description) {
73 registerQuery(new VectorTextQuery(title,target,cellSize,check,description));
74}
75
76void TextDialog::queryBox(const char* title,double ** const cellSize, string description) {
77 registerQuery(new BoxTextQuery(title,cellSize,description));
78}
79
80void TextDialog::queryElement(const char* title, std::vector<element *> *target, string description){
81 registerQuery(new ElementTextQuery(title,target,description));
82}
83
84/************************** Query Infrastructure ************************/
85
86TextDialog::EmptyTextQuery::EmptyTextQuery(string title, std::string _description) :
87 Dialog::EmptyQuery(title,_description)
88{}
89
90TextDialog::EmptyTextQuery::~EmptyTextQuery() {}
91
92bool TextDialog::EmptyTextQuery::handle() {
93 cout << "Message of " << getTitle() << ":\n" << getDescription() << "\n";
94 return true;
95}
96
97TextDialog::IntTextQuery::IntTextQuery(string title, int * _target, std::string _description) :
98 Dialog::IntQuery(title,_target,_description)
99{}
100
101TextDialog::IntTextQuery::~IntTextQuery() {}
102
103bool TextDialog::IntTextQuery::handle() {
104 bool badInput = false;
105 do{
106 badInput = false;
107 Log() << Verbose(0) << getTitle();
108 cin >> tmp;
109 if(cin.fail()){
110 badInput=true;
111 cin.clear();
112 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
113 Log() << Verbose(0) << "Input was not a number!" << endl;
114 }
115 } while(badInput);
116 // clear the input buffer of anything still in the line
117 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
118 return true;
119}
120
121TextDialog::BooleanTextQuery::BooleanTextQuery(string title, bool * _target, std::string _description) :
122 Dialog::BooleanQuery(title,_target,_description)
123{}
124
125TextDialog::BooleanTextQuery::~BooleanTextQuery() {}
126
127bool TextDialog::BooleanTextQuery::handle() {
128 bool badInput = false;
129 char input = ' ';
130 do{
131 badInput = false;
132 Log() << Verbose(0) << getTitle();
133 cin >> input;
134 if ((input == 'y' ) || (input == 'Y')) {
135 tmp = true;
136 } else if ((input == 'n' ) || (input == 'N')) {
137 tmp = false;
138 } else {
139 badInput=true;
140 cin.clear();
141 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
142 Log() << Verbose(0) << "Input was not of [yYnN]!" << endl;
143 }
144 } while(badInput);
145 // clear the input buffer of anything still in the line
146 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
147 return true;
148}
149
150TextDialog::StringTextQuery::StringTextQuery(string title,string *_target, std::string _description) :
151 Dialog::StringQuery(title,_target,_description)
152{}
153
154TextDialog::StringTextQuery::~StringTextQuery() {}
155
156bool TextDialog::StringTextQuery::handle() {
157 Log() << Verbose(0) << getTitle();
158 getline(cin,tmp);
159 return true;
160}
161
162TextDialog::StringsTextQuery::StringsTextQuery(string title,vector<string> *_target, std::string _description) :
163 Dialog::StringsQuery(title,_target,_description)
164{}
165
166TextDialog::StringsTextQuery::~StringsTextQuery() {}
167
168bool TextDialog::StringsTextQuery::handle() {
169 Log() << Verbose(0) << getTitle();
170 getline(cin,temp);
171 // dissect by ","
172 string::iterator olditer = temp.begin();
173 for(string::iterator iter = temp.begin(); iter != temp.end(); ++iter) {
174 if (*iter == ' ') {
175 tmp.push_back(string(iter, olditer));
176 olditer = iter;
177 }
178 }
179 if (olditer != temp.begin()) // insert last part also
180 tmp.push_back(string(olditer, temp.end()));
181
182 return true;
183}
184
185TextDialog::DoubleTextQuery::DoubleTextQuery(string title,double *_target, std::string _description) :
186 Dialog::DoubleQuery(title,_target,_description)
187{}
188
189TextDialog::DoubleTextQuery::~DoubleTextQuery() {}
190
191bool TextDialog::DoubleTextQuery::handle() {
192 bool badInput = false;
193 do{
194 badInput = false;
195 Log() << Verbose(0) << getTitle();
196 cin >> tmp;
197 if(cin.fail()){
198 badInput = true;
199 cin.clear();
200 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
201 Log() << Verbose(0) << "Input was not a number!" << endl;
202 }
203 }while(badInput);
204 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
205 return true;
206}
207
208TextDialog::AtomTextQuery::AtomTextQuery(string title, atom **_target, std::string _description) :
209 Dialog::AtomQuery(title,_target,_description)
210{}
211
212TextDialog::AtomTextQuery::~AtomTextQuery() {}
213
214bool TextDialog::AtomTextQuery::handle() {
215 int idxOfAtom=0;
216 bool badInput = false;
217 do{
218 badInput = false;
219 Log() << Verbose(0) << getTitle();
220 cin >> idxOfAtom;
221 if(cin.fail()){
222 badInput = true;
223 cin.clear();
224 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
225 Log() << Verbose(0) << "Input was not a number!" << endl;
226 continue;
227 }
228
229 tmp = World::getInstance().getAtom(AtomById(idxOfAtom));
230 if(!tmp && idxOfAtom!=-1){
231 Log() << Verbose(0) << "Invalid Atom Index" << endl;
232 badInput = true;
233 }
234
235 } while(badInput);
236 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
237 return (idxOfAtom!=-1);
238}
239
240TextDialog::MoleculeTextQuery::MoleculeTextQuery(string title, molecule **_target, std::string _description) :
241 Dialog::MoleculeQuery(title,_target,_description)
242{}
243
244TextDialog::MoleculeTextQuery::~MoleculeTextQuery() {}
245
246bool TextDialog::MoleculeTextQuery::handle() {
247 int idxOfMol=0;
248 bool badInput = false;
249 do{
250 badInput = false;
251 Log() << Verbose(0) << getTitle();
252 cin >> idxOfMol;
253 if(cin.fail()){
254 badInput = true;
255 cin.clear();
256 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
257 Log() << Verbose(0) << "Input was not a number!" << endl;
258 continue;
259 }
260
261 tmp = World::getInstance().getMolecule(MoleculeById(idxOfMol));
262 if(!tmp && idxOfMol!=-1){
263 Log() << Verbose(0) << "Invalid Molecule Index" << endl;
264 badInput = true;
265 }
266
267 } while(badInput);
268 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
269 return (idxOfMol!=-1);
270}
271
272TextDialog::VectorTextQuery::VectorTextQuery(std::string title, Vector *_target, const double *const _cellSize, bool _check, std::string _description) :
273 Dialog::VectorQuery(title,_target,_cellSize,_check,_description)
274{}
275
276TextDialog::VectorTextQuery::~VectorTextQuery()
277{}
278
279bool TextDialog::VectorTextQuery::handle() {
280 Log() << Verbose(0) << getTitle();
281
282 char coords[3] = {'x','y','z'};
283 int j = -1;
284 for (int i=0;i<3;i++) {
285 j += i+1;
286 do {
287 Log() << Verbose(0) << coords[i] << "[0.." << cellSize[j] << "]: ";
288 cin >> (*tmp)[i];
289 } while ((((*tmp)[i] < 0) || ((*tmp)[i] >= cellSize[j])) && (check));
290 }
291 return true;
292}
293
294TextDialog::BoxTextQuery::BoxTextQuery(std::string title, double ** const _cellSize, std::string _description) :
295 Dialog::BoxQuery(title,_cellSize,_description)
296{}
297
298TextDialog::BoxTextQuery::~BoxTextQuery()
299{}
300
301bool TextDialog::BoxTextQuery::handle() {
302 Log() << Verbose(0) << getTitle();
303
304 std::string coords[6] = {"xx","xy","xz", "yy", "yz", "zz"};
305 for (int i=0;i<6;i++) {
306 Log() << Verbose(0) << coords[i] << ": ";
307 cin >> tmp[i];
308 }
309 return true;
310}
311
312TextDialog::ElementTextQuery::ElementTextQuery(std::string title, std::vector<element *> *_target, std::string _description) :
313 Dialog::ElementQuery(title,_target,_description)
314{}
315
316TextDialog::ElementTextQuery::~ElementTextQuery()
317{}
318
319bool TextDialog::ElementTextQuery::handle() {
320 bool badInput=false;
321 bool aborted = false;
322 element * tmp = NULL;
323 do{
324 badInput = false;
325 Log() << Verbose(0) << getTitle();
326
327 // try to read as Atomic number
328 int Z;
329 cin >> Z;
330 if(!cin.fail()){
331 if(Z==-1){
332 aborted = true;
333 }
334 else{
335 tmp = World::getInstance().getPeriode()->FindElement(Z);
336 if(!tmp){
337 Log() << Verbose(0) << "No element with this atomic number!" << endl;
338 badInput = true;
339 } else {
340 elements.push_back(tmp);
341 }
342 }
343 continue;
344 }
345 else{
346 cin.clear();
347 }
348
349 // Try to read as shorthand
350 // the last buffer content was not removed, so we read the
351 // same thing again, this time as a string
352 string shorthand;
353 cin >> shorthand;
354 if(!cin.fail()){
355 if(shorthand.empty()){
356 aborted = true;
357 }
358 else{
359 tmp = World::getInstance().getPeriode()->FindElement(shorthand.c_str());
360 if(!tmp){
361 Log() << Verbose(0) << "No element with this shorthand!" << endl;
362 badInput = true;
363 } else {
364 elements.push_back(tmp);
365 }
366 }
367 }
368 else{
369 Log() << Verbose(0) << "Could not read input. Try Again." << endl;
370 cin.clear();
371 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
372 badInput = true;
373 }
374
375 }while(badInput);
376 cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
377 return !aborted;
378}
Note: See TracBrowser for help on using the repository browser.