1 | /*
|
---|
2 | * QTDialog.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jan 18, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "UIElements/QT4/QTDialog.hpp"
|
---|
9 |
|
---|
10 | #include <string>
|
---|
11 | #include <sstream>
|
---|
12 | #include <limits>
|
---|
13 |
|
---|
14 | #include <Qt/qboxlayout.h>
|
---|
15 | #include <Qt/qlabel.h>
|
---|
16 | #include <Qt/qspinbox.h>
|
---|
17 | #include <QtGui/QDoubleSpinBox>
|
---|
18 | #include <Qt/qlineedit.h>
|
---|
19 | #include <Qt/qdialogbuttonbox.h>
|
---|
20 | #include <Qt/qpushbutton.h>
|
---|
21 | #include <Qt/qcombobox.h>
|
---|
22 |
|
---|
23 | #include "Helpers/MemDebug.hpp"
|
---|
24 |
|
---|
25 | #include "World.hpp"
|
---|
26 | #include "periodentafel.hpp"
|
---|
27 | #include "atom.hpp"
|
---|
28 | #include "element.hpp"
|
---|
29 | #include "molecule.hpp"
|
---|
30 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
31 | #include "Matrix.hpp"
|
---|
32 | #include "Box.hpp"
|
---|
33 |
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 |
|
---|
37 | QTDialog::QTDialog() :
|
---|
38 | QDialog(0)
|
---|
39 | {
|
---|
40 | // creating and filling of the Dialog window
|
---|
41 | mainLayout = new QVBoxLayout();
|
---|
42 | inputLayout = new QVBoxLayout();
|
---|
43 | buttonLayout = new QVBoxLayout();
|
---|
44 | setLayout(mainLayout);
|
---|
45 | mainLayout->addLayout(inputLayout);
|
---|
46 | mainLayout->addLayout(buttonLayout);
|
---|
47 | buttons = new QDialogButtonBox(QDialogButtonBox::Ok| QDialogButtonBox::Cancel);
|
---|
48 | buttonLayout->addWidget(buttons);
|
---|
49 |
|
---|
50 | // Disable the ok button until something was entered
|
---|
51 | buttons->button(QDialogButtonBox::Ok)->setEnabled(false);
|
---|
52 |
|
---|
53 | // connect the buttons to their appropriate slots
|
---|
54 | connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
|
---|
55 | connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
|
---|
56 | }
|
---|
57 |
|
---|
58 | QTDialog::~QTDialog()
|
---|
59 | {
|
---|
60 | }
|
---|
61 |
|
---|
62 | bool QTDialog::display(){
|
---|
63 | // Button state might have changed by some update that
|
---|
64 | // was done during query construction. To make sure
|
---|
65 | // the state is correct, we just call update one more time.
|
---|
66 | update();
|
---|
67 | if(exec()) {
|
---|
68 | setAll();
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | void QTDialog::update(){
|
---|
77 | buttons->button(QDialogButtonBox::Ok)->setEnabled(checkAll());
|
---|
78 | }
|
---|
79 |
|
---|
80 | /************************** Query Infrastructure ************************/
|
---|
81 |
|
---|
82 | void QTDialog::queryEmpty(char const*, string){
|
---|
83 | // TODO
|
---|
84 | ASSERT(false, "Not implemented yet");
|
---|
85 | }
|
---|
86 |
|
---|
87 | void QTDialog::queryBoolean(char const*,string){
|
---|
88 | // TODO
|
---|
89 | ASSERT(false, "Not implemented yet");
|
---|
90 | }
|
---|
91 |
|
---|
92 | void QTDialog::queryAtom(char const*, string){
|
---|
93 | // TODO
|
---|
94 | ASSERT(false, "Not implemented yet");
|
---|
95 | }
|
---|
96 |
|
---|
97 | void QTDialog::queryBox(char const*, string){
|
---|
98 | // TODO
|
---|
99 | ASSERT(false, "Not implemented yet");
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | void QTDialog::queryInt(const char *title,string)
|
---|
104 | {
|
---|
105 | registerQuery(new IntQTQuery(title,inputLayout,this));
|
---|
106 | }
|
---|
107 |
|
---|
108 | void QTDialog::queryDouble(const char* title,string){
|
---|
109 | registerQuery(new DoubleQTQuery(title,inputLayout,this));
|
---|
110 | }
|
---|
111 |
|
---|
112 | void QTDialog::queryString(const char* title,string)
|
---|
113 | {
|
---|
114 | registerQuery(new StringQTQuery(title,inputLayout,this));
|
---|
115 | }
|
---|
116 |
|
---|
117 | void QTDialog::queryStrings(const char* title,string)
|
---|
118 | {
|
---|
119 | registerQuery(new StringsQTQuery(title,inputLayout,this));
|
---|
120 | }
|
---|
121 |
|
---|
122 | void QTDialog::queryMolecule(const char *title,string)
|
---|
123 | {
|
---|
124 | registerQuery(new MoleculeQTQuery(title,inputLayout,this));
|
---|
125 | }
|
---|
126 |
|
---|
127 | void QTDialog::queryVector(const char* title, bool check,string) {
|
---|
128 | registerQuery(new VectorQTQuery(title,check,inputLayout,this));
|
---|
129 | }
|
---|
130 |
|
---|
131 | void QTDialog::queryElement(const char* title, string){
|
---|
132 | registerQuery(new ElementQTQuery(title,inputLayout,this));
|
---|
133 | }
|
---|
134 |
|
---|
135 | /************************** Query Objects *******************************/
|
---|
136 |
|
---|
137 | QTDialog::IntQTQuery::IntQTQuery(string _title,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
138 | Dialog::IntQuery(_title),
|
---|
139 | parent(_parent)
|
---|
140 | {
|
---|
141 | thisLayout = new QHBoxLayout();
|
---|
142 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
143 | inputBox = new QSpinBox();
|
---|
144 | inputBox->setValue(0);
|
---|
145 | parent->addLayout(thisLayout);
|
---|
146 | thisLayout->addWidget(titleLabel);
|
---|
147 | thisLayout->addWidget(inputBox);
|
---|
148 |
|
---|
149 | pipe = new IntQTQueryPipe(&tmp,_dialog);
|
---|
150 | pipe->update(inputBox->value());
|
---|
151 | connect(inputBox,SIGNAL(valueChanged(int)),pipe,SLOT(update(int)));
|
---|
152 | }
|
---|
153 |
|
---|
154 | QTDialog::IntQTQuery::~IntQTQuery()
|
---|
155 | {
|
---|
156 | delete pipe;
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Handling is easy since the GUI makes it impossible to enter invalid values
|
---|
160 | bool QTDialog::IntQTQuery::handle()
|
---|
161 | {
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 |
|
---|
165 | QTDialog::DoubleQTQuery::DoubleQTQuery(string title,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
166 | Dialog::DoubleQuery(title),
|
---|
167 | parent(_parent)
|
---|
168 | {
|
---|
169 | thisLayout = new QHBoxLayout();
|
---|
170 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
171 | inputBox = new QDoubleSpinBox();
|
---|
172 | inputBox->setValue(0);
|
---|
173 | inputBox->setRange(-numeric_limits<double>::max(),numeric_limits<double>::max());
|
---|
174 | inputBox->setDecimals(3);
|
---|
175 | parent->addLayout(thisLayout);
|
---|
176 | thisLayout->addWidget(titleLabel);
|
---|
177 | thisLayout->addWidget(inputBox);
|
---|
178 |
|
---|
179 | pipe = new DoubleQTQueryPipe(&tmp,_dialog);
|
---|
180 | pipe->update(inputBox->value());
|
---|
181 | connect(inputBox,SIGNAL(valueChanged(double)),pipe,SLOT(update(double)));
|
---|
182 | }
|
---|
183 |
|
---|
184 | QTDialog::DoubleQTQuery::~DoubleQTQuery()
|
---|
185 | {
|
---|
186 | delete pipe;
|
---|
187 | }
|
---|
188 |
|
---|
189 | bool QTDialog::DoubleQTQuery::handle() {
|
---|
190 | return true;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | QTDialog::StringQTQuery::StringQTQuery(string _title,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
195 | Dialog::StringQuery(_title),
|
---|
196 | parent(_parent)
|
---|
197 | {
|
---|
198 | thisLayout = new QHBoxLayout();
|
---|
199 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
200 | inputBox = new QLineEdit();
|
---|
201 | parent->addLayout(thisLayout);
|
---|
202 | thisLayout->addWidget(titleLabel);
|
---|
203 | thisLayout->addWidget(inputBox);
|
---|
204 |
|
---|
205 | pipe = new StringQTQueryPipe(&tmp,_dialog);
|
---|
206 | pipe->update(inputBox->text());
|
---|
207 | connect(inputBox,SIGNAL(textChanged(const QString&)),pipe,SLOT(update(const QString&)));
|
---|
208 | }
|
---|
209 |
|
---|
210 | QTDialog::StringQTQuery::~StringQTQuery()
|
---|
211 | {
|
---|
212 | delete pipe;
|
---|
213 | }
|
---|
214 |
|
---|
215 | // All values besides the empty string are valid
|
---|
216 | bool QTDialog::StringQTQuery::handle()
|
---|
217 | {
|
---|
218 | return tmp!="";
|
---|
219 | }
|
---|
220 |
|
---|
221 | QTDialog::StringsQTQuery::StringsQTQuery(string _title,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
222 | Dialog::StringsQuery(_title),
|
---|
223 | parent(_parent)
|
---|
224 | {
|
---|
225 | thisLayout = new QHBoxLayout();
|
---|
226 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
227 | inputBox = new QLineEdit();
|
---|
228 | parent->addLayout(thisLayout);
|
---|
229 | thisLayout->addWidget(titleLabel);
|
---|
230 | thisLayout->addWidget(inputBox);
|
---|
231 |
|
---|
232 | pipe = new StringQTQueryPipe(&temp,_dialog);
|
---|
233 | pipe->update(inputBox->text());
|
---|
234 | connect(inputBox,SIGNAL(textChanged(const QString&)),pipe,SLOT(update(const QString&)));
|
---|
235 | }
|
---|
236 |
|
---|
237 | QTDialog::StringsQTQuery::~StringsQTQuery()
|
---|
238 | {
|
---|
239 | delete pipe;
|
---|
240 | }
|
---|
241 |
|
---|
242 | // All values besides the empty string are valid
|
---|
243 | bool QTDialog::StringsQTQuery::handle()
|
---|
244 | {
|
---|
245 | // dissect by ","
|
---|
246 | string::iterator olditer = temp.begin();
|
---|
247 | for(string::iterator iter = temp.begin(); iter != temp.end(); ++iter) {
|
---|
248 | if (*iter == ' ') {
|
---|
249 | tmp.push_back(string(iter, olditer));
|
---|
250 | olditer = iter;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | if (olditer != temp.begin()) // insert last part also
|
---|
254 | tmp.push_back(string(olditer, temp.end()));
|
---|
255 |
|
---|
256 | return temp!="";
|
---|
257 | }
|
---|
258 |
|
---|
259 | QTDialog::MoleculeQTQuery::MoleculeQTQuery(string _title, QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
260 | Dialog::MoleculeQuery(_title),
|
---|
261 | parent(_parent)
|
---|
262 | {
|
---|
263 | thisLayout = new QHBoxLayout();
|
---|
264 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
265 | inputBox = new QComboBox();
|
---|
266 | // add all molecules to the combo box
|
---|
267 | vector<molecule*> molecules = World::getInstance().getAllMolecules();
|
---|
268 | for(vector<molecule*>::iterator iter = molecules.begin();
|
---|
269 | iter != molecules.end();
|
---|
270 | ++iter) {
|
---|
271 | stringstream sstr;
|
---|
272 | sstr << (*iter)->IndexNr << "\t" << (*iter)->getName();
|
---|
273 | inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter)->IndexNr));
|
---|
274 | }
|
---|
275 | parent->addLayout(thisLayout);
|
---|
276 | thisLayout->addWidget(titleLabel);
|
---|
277 | thisLayout->addWidget(inputBox);
|
---|
278 |
|
---|
279 | pipe = new MoleculeQTQueryPipe(&tmp,_dialog,inputBox);
|
---|
280 | pipe->update(inputBox->currentIndex());
|
---|
281 | connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
|
---|
282 | }
|
---|
283 |
|
---|
284 | QTDialog::MoleculeQTQuery::~MoleculeQTQuery()
|
---|
285 | {
|
---|
286 | delete pipe;
|
---|
287 | }
|
---|
288 |
|
---|
289 | // Handling is easy, since the GUI makes it impossible to select invalid values
|
---|
290 | bool QTDialog::MoleculeQTQuery::handle()
|
---|
291 | {
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | QTDialog::VectorQTQuery::VectorQTQuery(std::string title, bool _check,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
296 | Dialog::VectorQuery(title,_check),
|
---|
297 | parent(_parent)
|
---|
298 | {
|
---|
299 | const Matrix& M = World::getInstance().getDomain().getM();
|
---|
300 | const char *coords[3] = {"x:","y:","z:"};
|
---|
301 | mainLayout= new QHBoxLayout();
|
---|
302 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
303 | mainLayout->addWidget(titleLabel);
|
---|
304 | subLayout = new QVBoxLayout();
|
---|
305 | mainLayout->addLayout(subLayout);
|
---|
306 | for(int i=0; i<3; i++) {
|
---|
307 | coordLayout[i] = new QHBoxLayout();
|
---|
308 | subLayout->addLayout(coordLayout[i]);
|
---|
309 | coordLabel[i] = new QLabel(QString(coords[i]));
|
---|
310 | coordLayout[i]->addWidget(coordLabel[i]);
|
---|
311 | coordInput[i] = new QDoubleSpinBox();
|
---|
312 | coordInput[i]->setRange(0,M.at(i,i));
|
---|
313 | coordInput[i]->setDecimals(3);
|
---|
314 | coordLayout[i]->addWidget(coordInput[i]);
|
---|
315 | pipe[i] = new DoubleQTQueryPipe(&((*tmp)[i]),_dialog);
|
---|
316 | pipe[i]->update(coordInput[i]->value());
|
---|
317 | connect(coordInput[i],SIGNAL(valueChanged(double)),pipe[i],SLOT(update(double)));
|
---|
318 |
|
---|
319 | }
|
---|
320 | parent->addLayout(mainLayout);
|
---|
321 | }
|
---|
322 |
|
---|
323 | QTDialog::VectorQTQuery::~VectorQTQuery()
|
---|
324 | {}
|
---|
325 |
|
---|
326 | bool QTDialog::VectorQTQuery::handle() {
|
---|
327 | return true;
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | QTDialog::ElementQTQuery::ElementQTQuery(std::string _title, QBoxLayout *_parent, QTDialog *_dialog) :
|
---|
332 | Dialog::ElementQuery(_title),
|
---|
333 | parent(_parent)
|
---|
334 | {
|
---|
335 | periodentafel *periode = World::getInstance().getPeriode();
|
---|
336 | thisLayout = new QHBoxLayout();
|
---|
337 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
338 | inputBox = new QComboBox();
|
---|
339 | for(periodentafel::const_iterator iter = periode->begin();
|
---|
340 | iter!=periode->end();
|
---|
341 | ++iter)
|
---|
342 | {
|
---|
343 | stringstream sstr;
|
---|
344 | sstr << (*iter).first << "\t" << (*iter).second->name;
|
---|
345 | inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter).first));
|
---|
346 | }
|
---|
347 | parent->addLayout(thisLayout);
|
---|
348 | thisLayout->addWidget(titleLabel);
|
---|
349 | thisLayout->addWidget(inputBox);
|
---|
350 |
|
---|
351 | pipe = new ElementQTQueryPipe(&tmp,_dialog,inputBox);
|
---|
352 | pipe->update(inputBox->currentIndex());
|
---|
353 | connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
|
---|
354 | }
|
---|
355 |
|
---|
356 | QTDialog::ElementQTQuery::~ElementQTQuery()
|
---|
357 | {
|
---|
358 | delete pipe;
|
---|
359 | }
|
---|
360 |
|
---|
361 | bool QTDialog::ElementQTQuery::handle(){
|
---|
362 | return true;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /*************************** Plumbing *******************************/
|
---|
366 |
|
---|
367 | StringQTQueryPipe::StringQTQueryPipe(string *_content, QTDialog *_dialog) :
|
---|
368 | content(_content),
|
---|
369 | dialog(_dialog)
|
---|
370 | {}
|
---|
371 |
|
---|
372 | StringQTQueryPipe::~StringQTQueryPipe()
|
---|
373 | {}
|
---|
374 |
|
---|
375 | void StringQTQueryPipe::update(const QString& newText) {
|
---|
376 | content->assign(newText.toStdString());
|
---|
377 | dialog->update();
|
---|
378 | }
|
---|
379 |
|
---|
380 | IntQTQueryPipe::IntQTQueryPipe(int *_content, QTDialog *_dialog) :
|
---|
381 | content(_content),
|
---|
382 | dialog(_dialog)
|
---|
383 | {}
|
---|
384 |
|
---|
385 | IntQTQueryPipe::~IntQTQueryPipe()
|
---|
386 | {}
|
---|
387 |
|
---|
388 | void IntQTQueryPipe::update(int newInt) {
|
---|
389 | (*content) = newInt;
|
---|
390 | dialog->update();
|
---|
391 | }
|
---|
392 |
|
---|
393 | DoubleQTQueryPipe::DoubleQTQueryPipe(double *_content, QTDialog *_dialog) :
|
---|
394 | content(_content),
|
---|
395 | dialog(_dialog)
|
---|
396 | {}
|
---|
397 |
|
---|
398 | DoubleQTQueryPipe::~DoubleQTQueryPipe()
|
---|
399 | {}
|
---|
400 |
|
---|
401 | void DoubleQTQueryPipe::update(double newDbl) {
|
---|
402 | (*content) = newDbl;
|
---|
403 | dialog->update();
|
---|
404 | }
|
---|
405 |
|
---|
406 | MoleculeQTQueryPipe::MoleculeQTQueryPipe(molecule **_content, QTDialog *_dialog, QComboBox *_theBox) :
|
---|
407 | content(_content),
|
---|
408 | dialog(_dialog),
|
---|
409 | theBox(_theBox)
|
---|
410 | {}
|
---|
411 |
|
---|
412 | MoleculeQTQueryPipe::~MoleculeQTQueryPipe()
|
---|
413 | {}
|
---|
414 |
|
---|
415 | void MoleculeQTQueryPipe::update(int newIndex) {
|
---|
416 | QVariant data = theBox->itemData(newIndex);
|
---|
417 | int idx = data.toInt();
|
---|
418 | (*content) = World::getInstance().getMolecule(MoleculeById(idx));
|
---|
419 | dialog->update();
|
---|
420 | }
|
---|
421 |
|
---|
422 | ElementQTQueryPipe::ElementQTQueryPipe(std::vector<element *> *_content, QTDialog *_dialog, QComboBox *_theBox) :
|
---|
423 | content(_content),
|
---|
424 | dialog(_dialog),
|
---|
425 | theBox(_theBox)
|
---|
426 | {
|
---|
427 | content->resize(1);
|
---|
428 | }
|
---|
429 |
|
---|
430 | ElementQTQueryPipe::~ElementQTQueryPipe()
|
---|
431 | {}
|
---|
432 |
|
---|
433 | void ElementQTQueryPipe::update(int newIndex) {
|
---|
434 | QVariant data = theBox->itemData(newIndex);
|
---|
435 | int idx = data.toInt();
|
---|
436 | (*content)[0] = World::getInstance().getPeriode()->FindElement(idx);
|
---|
437 | dialog->update();
|
---|
438 | }
|
---|
439 |
|
---|