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*, bool*,string){
|
---|
88 | // TODO
|
---|
89 | ASSERT(false, "Not implemented yet");
|
---|
90 | }
|
---|
91 |
|
---|
92 | void QTDialog::queryAtom(char const*, atom**, string){
|
---|
93 | // TODO
|
---|
94 | ASSERT(false, "Not implemented yet");
|
---|
95 | }
|
---|
96 |
|
---|
97 | void QTDialog::queryBox(char const*, Box*, string){
|
---|
98 | // TODO
|
---|
99 | ASSERT(false, "Not implemented yet");
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | void QTDialog::queryInt(const char *title, int *target,string)
|
---|
104 | {
|
---|
105 | registerQuery(new IntQTQuery(title,target,inputLayout,this));
|
---|
106 | }
|
---|
107 |
|
---|
108 | void QTDialog::queryDouble(const char* title, double* target,string){
|
---|
109 | registerQuery(new DoubleQTQuery(title,target,inputLayout,this));
|
---|
110 | }
|
---|
111 |
|
---|
112 | void QTDialog::queryString(const char* title, std::string *target,string)
|
---|
113 | {
|
---|
114 | registerQuery(new StringQTQuery(title,target,inputLayout,this));
|
---|
115 | }
|
---|
116 |
|
---|
117 | void QTDialog::queryMolecule(const char *title,molecule **target,string)
|
---|
118 | {
|
---|
119 | registerQuery(new MoleculeQTQuery(title,target,inputLayout,this));
|
---|
120 | }
|
---|
121 |
|
---|
122 | void QTDialog::queryVector(const char* title, Vector *target, bool check,string) {
|
---|
123 | registerQuery(new VectorQTQuery(title,target,check,inputLayout,this));
|
---|
124 | }
|
---|
125 |
|
---|
126 | void QTDialog::queryElement(const char* title, std::vector<element *> *target,string){
|
---|
127 | registerQuery(new ElementQTQuery(title,target,inputLayout,this));
|
---|
128 | }
|
---|
129 |
|
---|
130 | /************************** Query Objects *******************************/
|
---|
131 |
|
---|
132 | QTDialog::IntQTQuery::IntQTQuery(string _title,int *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
133 | Dialog::IntQuery(_title,_target),
|
---|
134 | parent(_parent)
|
---|
135 | {
|
---|
136 | thisLayout = new QHBoxLayout();
|
---|
137 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
138 | inputBox = new QSpinBox();
|
---|
139 | inputBox->setValue(0);
|
---|
140 | parent->addLayout(thisLayout);
|
---|
141 | thisLayout->addWidget(titleLabel);
|
---|
142 | thisLayout->addWidget(inputBox);
|
---|
143 |
|
---|
144 | pipe = new IntQTQueryPipe(&tmp,_dialog);
|
---|
145 | pipe->update(inputBox->value());
|
---|
146 | connect(inputBox,SIGNAL(valueChanged(int)),pipe,SLOT(update(int)));
|
---|
147 | }
|
---|
148 |
|
---|
149 | QTDialog::IntQTQuery::~IntQTQuery()
|
---|
150 | {
|
---|
151 | delete pipe;
|
---|
152 | }
|
---|
153 |
|
---|
154 | // Handling is easy since the GUI makes it impossible to enter invalid values
|
---|
155 | bool QTDialog::IntQTQuery::handle()
|
---|
156 | {
|
---|
157 | return true;
|
---|
158 | }
|
---|
159 |
|
---|
160 | QTDialog::DoubleQTQuery::DoubleQTQuery(string title,double *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
161 | Dialog::DoubleQuery(title,_target),
|
---|
162 | parent(_parent)
|
---|
163 | {
|
---|
164 | thisLayout = new QHBoxLayout();
|
---|
165 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
166 | inputBox = new QDoubleSpinBox();
|
---|
167 | inputBox->setValue(0);
|
---|
168 | inputBox->setRange(-numeric_limits<double>::max(),numeric_limits<double>::max());
|
---|
169 | inputBox->setDecimals(3);
|
---|
170 | parent->addLayout(thisLayout);
|
---|
171 | thisLayout->addWidget(titleLabel);
|
---|
172 | thisLayout->addWidget(inputBox);
|
---|
173 |
|
---|
174 | pipe = new DoubleQTQueryPipe(&tmp,_dialog);
|
---|
175 | pipe->update(inputBox->value());
|
---|
176 | connect(inputBox,SIGNAL(valueChanged(double)),pipe,SLOT(update(double)));
|
---|
177 | }
|
---|
178 |
|
---|
179 | QTDialog::DoubleQTQuery::~DoubleQTQuery()
|
---|
180 | {
|
---|
181 | delete pipe;
|
---|
182 | }
|
---|
183 |
|
---|
184 | bool QTDialog::DoubleQTQuery::handle() {
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | QTDialog::StringQTQuery::StringQTQuery(string _title,string *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
190 | Dialog::StringQuery(_title,_target),
|
---|
191 | parent(_parent)
|
---|
192 | {
|
---|
193 | thisLayout = new QHBoxLayout();
|
---|
194 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
195 | inputBox = new QLineEdit();
|
---|
196 | parent->addLayout(thisLayout);
|
---|
197 | thisLayout->addWidget(titleLabel);
|
---|
198 | thisLayout->addWidget(inputBox);
|
---|
199 |
|
---|
200 | pipe = new StringQTQueryPipe(&tmp,_dialog);
|
---|
201 | pipe->update(inputBox->text());
|
---|
202 | connect(inputBox,SIGNAL(textChanged(const QString&)),pipe,SLOT(update(const QString&)));
|
---|
203 | }
|
---|
204 |
|
---|
205 | QTDialog::StringQTQuery::~StringQTQuery()
|
---|
206 | {
|
---|
207 | delete pipe;
|
---|
208 | }
|
---|
209 |
|
---|
210 | // All values besides the empty string are valid
|
---|
211 | bool QTDialog::StringQTQuery::handle()
|
---|
212 | {
|
---|
213 | return tmp!="";
|
---|
214 | }
|
---|
215 |
|
---|
216 | QTDialog::MoleculeQTQuery::MoleculeQTQuery(string _title, molecule **_target, QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
217 | Dialog::MoleculeQuery(_title,_target),
|
---|
218 | parent(_parent)
|
---|
219 | {
|
---|
220 | thisLayout = new QHBoxLayout();
|
---|
221 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
222 | inputBox = new QComboBox();
|
---|
223 | // add all molecules to the combo box
|
---|
224 | vector<molecule*> molecules = World::getInstance().getAllMolecules();
|
---|
225 | for(vector<molecule*>::iterator iter = molecules.begin();
|
---|
226 | iter != molecules.end();
|
---|
227 | ++iter) {
|
---|
228 | stringstream sstr;
|
---|
229 | sstr << (*iter)->IndexNr << "\t" << (*iter)->getName();
|
---|
230 | inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter)->IndexNr));
|
---|
231 | }
|
---|
232 | parent->addLayout(thisLayout);
|
---|
233 | thisLayout->addWidget(titleLabel);
|
---|
234 | thisLayout->addWidget(inputBox);
|
---|
235 |
|
---|
236 | pipe = new MoleculeQTQueryPipe(&tmp,_dialog,inputBox);
|
---|
237 | pipe->update(inputBox->currentIndex());
|
---|
238 | connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
|
---|
239 | }
|
---|
240 |
|
---|
241 | QTDialog::MoleculeQTQuery::~MoleculeQTQuery()
|
---|
242 | {
|
---|
243 | delete pipe;
|
---|
244 | }
|
---|
245 |
|
---|
246 | // Handling is easy, since the GUI makes it impossible to select invalid values
|
---|
247 | bool QTDialog::MoleculeQTQuery::handle()
|
---|
248 | {
|
---|
249 | return true;
|
---|
250 | }
|
---|
251 |
|
---|
252 | QTDialog::VectorQTQuery::VectorQTQuery(std::string title, Vector *_target, bool _check,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
253 | Dialog::VectorQuery(title,_target,_check),
|
---|
254 | parent(_parent)
|
---|
255 | {
|
---|
256 | const Matrix& M = World::getInstance().getDomain().getM();
|
---|
257 | const char *coords[3] = {"x:","y:","z:"};
|
---|
258 | mainLayout= new QHBoxLayout();
|
---|
259 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
260 | mainLayout->addWidget(titleLabel);
|
---|
261 | subLayout = new QVBoxLayout();
|
---|
262 | mainLayout->addLayout(subLayout);
|
---|
263 | for(int i=0; i<3; i++) {
|
---|
264 | coordLayout[i] = new QHBoxLayout();
|
---|
265 | subLayout->addLayout(coordLayout[i]);
|
---|
266 | coordLabel[i] = new QLabel(QString(coords[i]));
|
---|
267 | coordLayout[i]->addWidget(coordLabel[i]);
|
---|
268 | coordInput[i] = new QDoubleSpinBox();
|
---|
269 | coordInput[i]->setRange(0,M.at(i,i));
|
---|
270 | coordInput[i]->setDecimals(3);
|
---|
271 | coordLayout[i]->addWidget(coordInput[i]);
|
---|
272 | pipe[i] = new DoubleQTQueryPipe(&((*tmp)[i]),_dialog);
|
---|
273 | pipe[i]->update(coordInput[i]->value());
|
---|
274 | connect(coordInput[i],SIGNAL(valueChanged(double)),pipe[i],SLOT(update(double)));
|
---|
275 |
|
---|
276 | }
|
---|
277 | parent->addLayout(mainLayout);
|
---|
278 | }
|
---|
279 |
|
---|
280 | QTDialog::VectorQTQuery::~VectorQTQuery()
|
---|
281 | {}
|
---|
282 |
|
---|
283 | bool QTDialog::VectorQTQuery::handle() {
|
---|
284 | return true;
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | QTDialog::ElementQTQuery::ElementQTQuery(std::string _title, vector<element *> *_target, QBoxLayout *_parent, QTDialog *_dialog) :
|
---|
289 | Dialog::ElementQuery(_title,_target),
|
---|
290 | parent(_parent)
|
---|
291 | {
|
---|
292 | periodentafel *periode = World::getInstance().getPeriode();
|
---|
293 | thisLayout = new QHBoxLayout();
|
---|
294 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
295 | inputBox = new QComboBox();
|
---|
296 | for(periodentafel::const_iterator iter = periode->begin();
|
---|
297 | iter!=periode->end();
|
---|
298 | ++iter)
|
---|
299 | {
|
---|
300 | stringstream sstr;
|
---|
301 | sstr << (*iter).first << "\t" << (*iter).second->name;
|
---|
302 | inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter).first));
|
---|
303 | }
|
---|
304 | parent->addLayout(thisLayout);
|
---|
305 | thisLayout->addWidget(titleLabel);
|
---|
306 | thisLayout->addWidget(inputBox);
|
---|
307 |
|
---|
308 | pipe = new ElementQTQueryPipe(&elements,_dialog,inputBox);
|
---|
309 | pipe->update(inputBox->currentIndex());
|
---|
310 | connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
|
---|
311 | }
|
---|
312 |
|
---|
313 | QTDialog::ElementQTQuery::~ElementQTQuery()
|
---|
314 | {
|
---|
315 | delete pipe;
|
---|
316 | }
|
---|
317 |
|
---|
318 | bool QTDialog::ElementQTQuery::handle(){
|
---|
319 | return true;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /*************************** Plumbing *******************************/
|
---|
323 |
|
---|
324 | StringQTQueryPipe::StringQTQueryPipe(string *_content, QTDialog *_dialog) :
|
---|
325 | content(_content),
|
---|
326 | dialog(_dialog)
|
---|
327 | {}
|
---|
328 |
|
---|
329 | StringQTQueryPipe::~StringQTQueryPipe()
|
---|
330 | {}
|
---|
331 |
|
---|
332 | void StringQTQueryPipe::update(const QString& newText) {
|
---|
333 | content->assign(newText.toStdString());
|
---|
334 | dialog->update();
|
---|
335 | }
|
---|
336 |
|
---|
337 | IntQTQueryPipe::IntQTQueryPipe(int *_content, QTDialog *_dialog) :
|
---|
338 | content(_content),
|
---|
339 | dialog(_dialog)
|
---|
340 | {}
|
---|
341 |
|
---|
342 | IntQTQueryPipe::~IntQTQueryPipe()
|
---|
343 | {}
|
---|
344 |
|
---|
345 | void IntQTQueryPipe::update(int newInt) {
|
---|
346 | (*content) = newInt;
|
---|
347 | dialog->update();
|
---|
348 | }
|
---|
349 |
|
---|
350 | DoubleQTQueryPipe::DoubleQTQueryPipe(double *_content, QTDialog *_dialog) :
|
---|
351 | content(_content),
|
---|
352 | dialog(_dialog)
|
---|
353 | {}
|
---|
354 |
|
---|
355 | DoubleQTQueryPipe::~DoubleQTQueryPipe()
|
---|
356 | {}
|
---|
357 |
|
---|
358 | void DoubleQTQueryPipe::update(double newDbl) {
|
---|
359 | (*content) = newDbl;
|
---|
360 | dialog->update();
|
---|
361 | }
|
---|
362 |
|
---|
363 | MoleculeQTQueryPipe::MoleculeQTQueryPipe(molecule **_content, QTDialog *_dialog, QComboBox *_theBox) :
|
---|
364 | content(_content),
|
---|
365 | dialog(_dialog),
|
---|
366 | theBox(_theBox)
|
---|
367 | {}
|
---|
368 |
|
---|
369 | MoleculeQTQueryPipe::~MoleculeQTQueryPipe()
|
---|
370 | {}
|
---|
371 |
|
---|
372 | void MoleculeQTQueryPipe::update(int newIndex) {
|
---|
373 | QVariant data = theBox->itemData(newIndex);
|
---|
374 | int idx = data.toInt();
|
---|
375 | (*content) = World::getInstance().getMolecule(MoleculeById(idx));
|
---|
376 | dialog->update();
|
---|
377 | }
|
---|
378 |
|
---|
379 | ElementQTQueryPipe::ElementQTQueryPipe(std::vector<element *> *_content, QTDialog *_dialog, QComboBox *_theBox) :
|
---|
380 | content(_content),
|
---|
381 | dialog(_dialog),
|
---|
382 | theBox(_theBox)
|
---|
383 | {
|
---|
384 | content->resize(1);
|
---|
385 | }
|
---|
386 |
|
---|
387 | ElementQTQueryPipe::~ElementQTQueryPipe()
|
---|
388 | {}
|
---|
389 |
|
---|
390 | void ElementQTQueryPipe::update(int newIndex) {
|
---|
391 | QVariant data = theBox->itemData(newIndex);
|
---|
392 | int idx = data.toInt();
|
---|
393 | (*content)[0] = World::getInstance().getPeriode()->FindElement(idx);
|
---|
394 | dialog->update();
|
---|
395 | }
|
---|
396 |
|
---|