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 "World.hpp"
|
---|
24 | #include "periodentafel.hpp"
|
---|
25 | #include "atom.hpp"
|
---|
26 | #include "element.hpp"
|
---|
27 | #include "molecule.hpp"
|
---|
28 |
|
---|
29 |
|
---|
30 | using namespace std;
|
---|
31 |
|
---|
32 | QTDialog::QTDialog() :
|
---|
33 | QDialog(0)
|
---|
34 | {
|
---|
35 | // creating and filling of the Dialog window
|
---|
36 | mainLayout = new QVBoxLayout();
|
---|
37 | inputLayout = new QVBoxLayout();
|
---|
38 | buttonLayout = new QVBoxLayout();
|
---|
39 | setLayout(mainLayout);
|
---|
40 | mainLayout->addLayout(inputLayout);
|
---|
41 | mainLayout->addLayout(buttonLayout);
|
---|
42 | buttons = new QDialogButtonBox(QDialogButtonBox::Ok| QDialogButtonBox::Cancel);
|
---|
43 | buttonLayout->addWidget(buttons);
|
---|
44 |
|
---|
45 | // Disable the ok button until something was entered
|
---|
46 | buttons->button(QDialogButtonBox::Ok)->setEnabled(false);
|
---|
47 |
|
---|
48 | // connect the buttons to their appropriate slots
|
---|
49 | connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
|
---|
50 | connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
|
---|
51 | }
|
---|
52 |
|
---|
53 | QTDialog::~QTDialog()
|
---|
54 | {
|
---|
55 | }
|
---|
56 |
|
---|
57 | bool QTDialog::display(){
|
---|
58 | // Button state might have changed by some update that
|
---|
59 | // was done during query construction. To make sure
|
---|
60 | // the state is correct, we just call update one more time.
|
---|
61 | update();
|
---|
62 | if(exec()) {
|
---|
63 | setAll();
|
---|
64 | return true;
|
---|
65 | }
|
---|
66 | else {
|
---|
67 | return false;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | void QTDialog::update(){
|
---|
72 | buttons->button(QDialogButtonBox::Ok)->setEnabled(checkAll());
|
---|
73 | }
|
---|
74 |
|
---|
75 | /************************** Query Infrastructure ************************/
|
---|
76 |
|
---|
77 | void QTDialog::queryInt(const char *title, int *target)
|
---|
78 | {
|
---|
79 | registerQuery(new IntQTQuery(title,target,inputLayout,this));
|
---|
80 | }
|
---|
81 |
|
---|
82 | void QTDialog::queryDouble(const char* title, double* target){
|
---|
83 | registerQuery(new DoubleQTQuery(title,target,inputLayout,this));
|
---|
84 | }
|
---|
85 |
|
---|
86 | void QTDialog::queryString(const char* title, std::string *target)
|
---|
87 | {
|
---|
88 | registerQuery(new StringQTQuery(title,target,inputLayout,this));
|
---|
89 | }
|
---|
90 |
|
---|
91 | void QTDialog::queryMolecule(const char *title,molecule **target,MoleculeListClass *molecules)
|
---|
92 | {
|
---|
93 | registerQuery(new MoleculeQTQuery(title,target,molecules,inputLayout,this));
|
---|
94 | }
|
---|
95 |
|
---|
96 | void QTDialog::queryVector(const char* title, Vector *target,const double *const cellSize, bool check) {
|
---|
97 | registerQuery(new VectorQTQuery(title,target,cellSize,check,inputLayout,this));
|
---|
98 | }
|
---|
99 |
|
---|
100 | void QTDialog::queryElement(const char* title, element **target){
|
---|
101 | registerQuery(new ElementQTQuery(title,target,inputLayout,this));
|
---|
102 | }
|
---|
103 |
|
---|
104 | /************************** Query Objects *******************************/
|
---|
105 |
|
---|
106 | QTDialog::IntQTQuery::IntQTQuery(string _title,int *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
107 | Dialog::IntQuery(_title,_target),
|
---|
108 | parent(_parent)
|
---|
109 | {
|
---|
110 | thisLayout = new QHBoxLayout();
|
---|
111 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
112 | inputBox = new QSpinBox();
|
---|
113 | inputBox->setValue(0);
|
---|
114 | parent->addLayout(thisLayout);
|
---|
115 | thisLayout->addWidget(titleLabel);
|
---|
116 | thisLayout->addWidget(inputBox);
|
---|
117 |
|
---|
118 | pipe = new IntQTQueryPipe(&tmp,_dialog);
|
---|
119 | pipe->update(inputBox->value());
|
---|
120 | connect(inputBox,SIGNAL(valueChanged(int)),pipe,SLOT(update(int)));
|
---|
121 | }
|
---|
122 |
|
---|
123 | QTDialog::IntQTQuery::~IntQTQuery()
|
---|
124 | {
|
---|
125 | delete pipe;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // Handling is easy since the GUI makes it impossible to enter invalid values
|
---|
129 | bool QTDialog::IntQTQuery::handle()
|
---|
130 | {
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 |
|
---|
134 | QTDialog::DoubleQTQuery::DoubleQTQuery(string title,double *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
135 | Dialog::DoubleQuery(title,_target),
|
---|
136 | parent(_parent)
|
---|
137 | {
|
---|
138 | thisLayout = new QHBoxLayout();
|
---|
139 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
140 | inputBox = new QDoubleSpinBox();
|
---|
141 | inputBox->setValue(0);
|
---|
142 | inputBox->setRange(-numeric_limits<double>::max(),numeric_limits<double>::max());
|
---|
143 | inputBox->setDecimals(3);
|
---|
144 | parent->addLayout(thisLayout);
|
---|
145 | thisLayout->addWidget(titleLabel);
|
---|
146 | thisLayout->addWidget(inputBox);
|
---|
147 |
|
---|
148 | pipe = new DoubleQTQueryPipe(&tmp,_dialog);
|
---|
149 | pipe->update(inputBox->value());
|
---|
150 | connect(inputBox,SIGNAL(valueChanged(double)),pipe,SLOT(update(double)));
|
---|
151 | }
|
---|
152 |
|
---|
153 | QTDialog::DoubleQTQuery::~DoubleQTQuery()
|
---|
154 | {
|
---|
155 | delete pipe;
|
---|
156 | }
|
---|
157 |
|
---|
158 | bool QTDialog::DoubleQTQuery::handle() {
|
---|
159 | return true;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | QTDialog::StringQTQuery::StringQTQuery(string _title,string *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
164 | Dialog::StringQuery(_title,_target),
|
---|
165 | parent(_parent)
|
---|
166 | {
|
---|
167 | thisLayout = new QHBoxLayout();
|
---|
168 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
169 | inputBox = new QLineEdit();
|
---|
170 | parent->addLayout(thisLayout);
|
---|
171 | thisLayout->addWidget(titleLabel);
|
---|
172 | thisLayout->addWidget(inputBox);
|
---|
173 |
|
---|
174 | pipe = new StringQTQueryPipe(&tmp,_dialog);
|
---|
175 | pipe->update(inputBox->text());
|
---|
176 | connect(inputBox,SIGNAL(textChanged(const QString&)),pipe,SLOT(update(const QString&)));
|
---|
177 | }
|
---|
178 |
|
---|
179 | QTDialog::StringQTQuery::~StringQTQuery()
|
---|
180 | {
|
---|
181 | delete pipe;
|
---|
182 | }
|
---|
183 |
|
---|
184 | // All values besides the empty string are valid
|
---|
185 | bool QTDialog::StringQTQuery::handle()
|
---|
186 | {
|
---|
187 | return tmp!="";
|
---|
188 | }
|
---|
189 |
|
---|
190 | QTDialog::MoleculeQTQuery::MoleculeQTQuery(string _title, molecule **_target, MoleculeListClass *_molecules, QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
191 | Dialog::MoleculeQuery(_title,_target,_molecules),
|
---|
192 | parent(_parent)
|
---|
193 | {
|
---|
194 | MoleculeList::iterator iter;
|
---|
195 | thisLayout = new QHBoxLayout();
|
---|
196 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
197 | inputBox = new QComboBox();
|
---|
198 | // add all molecules to the combo box
|
---|
199 | for(iter = molecules->ListOfMolecules.begin();
|
---|
200 | iter != molecules->ListOfMolecules.end();
|
---|
201 | ++iter) {
|
---|
202 | stringstream sstr;
|
---|
203 | sstr << (*iter)->IndexNr << "\t" << (*iter)->getName();
|
---|
204 | inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter)->IndexNr));
|
---|
205 | }
|
---|
206 | parent->addLayout(thisLayout);
|
---|
207 | thisLayout->addWidget(titleLabel);
|
---|
208 | thisLayout->addWidget(inputBox);
|
---|
209 |
|
---|
210 | pipe = new MoleculeQTQueryPipe(&tmp,_dialog,inputBox,_molecules);
|
---|
211 | pipe->update(inputBox->currentIndex());
|
---|
212 | connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
|
---|
213 | }
|
---|
214 |
|
---|
215 | QTDialog::MoleculeQTQuery::~MoleculeQTQuery()
|
---|
216 | {
|
---|
217 | delete pipe;
|
---|
218 | }
|
---|
219 |
|
---|
220 | // Handling is easy, since the GUI makes it impossible to select invalid values
|
---|
221 | bool QTDialog::MoleculeQTQuery::handle()
|
---|
222 | {
|
---|
223 | return true;
|
---|
224 | }
|
---|
225 |
|
---|
226 | QTDialog::VectorQTQuery::VectorQTQuery(std::string title, Vector *_target, const double *const _cellSize, bool _check,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
227 | Dialog::VectorQuery(title,_target,_cellSize,_check),
|
---|
228 | parent(_parent)
|
---|
229 | {
|
---|
230 | // About the j: I don't know why it was done this way, but it was used this way in Vector::AskPosition, so I reused it
|
---|
231 | int j = -1;
|
---|
232 | const char *coords[3] = {"x:","y:","z:"};
|
---|
233 | mainLayout= new QHBoxLayout();
|
---|
234 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
235 | mainLayout->addWidget(titleLabel);
|
---|
236 | subLayout = new QVBoxLayout();
|
---|
237 | mainLayout->addLayout(subLayout);
|
---|
238 | for(int i=0; i<3; i++) {
|
---|
239 | j+=i+1;
|
---|
240 | coordLayout[i] = new QHBoxLayout();
|
---|
241 | subLayout->addLayout(coordLayout[i]);
|
---|
242 | coordLabel[i] = new QLabel(QString(coords[i]));
|
---|
243 | coordLayout[i]->addWidget(coordLabel[i]);
|
---|
244 | coordInput[i] = new QDoubleSpinBox();
|
---|
245 | coordInput[i]->setRange(0,cellSize[j]);
|
---|
246 | coordInput[i]->setDecimals(3);
|
---|
247 | coordLayout[i]->addWidget(coordInput[i]);
|
---|
248 | pipe[i] = new DoubleQTQueryPipe(&((*tmp)[i]),_dialog);
|
---|
249 | pipe[i]->update(coordInput[i]->value());
|
---|
250 | connect(coordInput[i],SIGNAL(valueChanged(double)),pipe[i],SLOT(update(double)));
|
---|
251 |
|
---|
252 | }
|
---|
253 | parent->addLayout(mainLayout);
|
---|
254 | }
|
---|
255 |
|
---|
256 | QTDialog::VectorQTQuery::~VectorQTQuery()
|
---|
257 | {}
|
---|
258 |
|
---|
259 | bool QTDialog::VectorQTQuery::handle() {
|
---|
260 | return true;
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | QTDialog::ElementQTQuery::ElementQTQuery(std::string _title, element **_target, QBoxLayout *_parent, QTDialog *_dialog) :
|
---|
265 | Dialog::ElementQuery(_title,_target),
|
---|
266 | parent(_parent)
|
---|
267 | {
|
---|
268 | periodentafel *periode = World::get()->getPeriode();
|
---|
269 | thisLayout = new QHBoxLayout();
|
---|
270 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
271 | inputBox = new QComboBox();
|
---|
272 | element* Elemental = 0;
|
---|
273 | for(Elemental = periode->start->next;
|
---|
274 | Elemental!=periode->end;
|
---|
275 | Elemental = Elemental->next)
|
---|
276 | {
|
---|
277 | stringstream sstr;
|
---|
278 | sstr << Elemental->Z << "\t" << Elemental->name;
|
---|
279 | inputBox->addItem(QString(sstr.str().c_str()),QVariant(Elemental->Z));
|
---|
280 | }
|
---|
281 | parent->addLayout(thisLayout);
|
---|
282 | thisLayout->addWidget(titleLabel);
|
---|
283 | thisLayout->addWidget(inputBox);
|
---|
284 |
|
---|
285 | pipe = new ElementQTQueryPipe(&tmp,_dialog,inputBox);
|
---|
286 | pipe->update(inputBox->currentIndex());
|
---|
287 | connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
|
---|
288 | }
|
---|
289 |
|
---|
290 | QTDialog::ElementQTQuery::~ElementQTQuery()
|
---|
291 | {
|
---|
292 | delete pipe;
|
---|
293 | }
|
---|
294 |
|
---|
295 | bool QTDialog::ElementQTQuery::handle(){
|
---|
296 | return true;
|
---|
297 | }
|
---|
298 |
|
---|
299 | /*************************** Plumbing *******************************/
|
---|
300 |
|
---|
301 | StringQTQueryPipe::StringQTQueryPipe(string *_content, QTDialog *_dialog) :
|
---|
302 | content(_content),
|
---|
303 | dialog(_dialog)
|
---|
304 | {}
|
---|
305 |
|
---|
306 | StringQTQueryPipe::~StringQTQueryPipe()
|
---|
307 | {}
|
---|
308 |
|
---|
309 | void StringQTQueryPipe::update(const QString& newText) {
|
---|
310 | content->assign(newText.toStdString());
|
---|
311 | dialog->update();
|
---|
312 | }
|
---|
313 |
|
---|
314 | IntQTQueryPipe::IntQTQueryPipe(int *_content, QTDialog *_dialog) :
|
---|
315 | content(_content),
|
---|
316 | dialog(_dialog)
|
---|
317 | {}
|
---|
318 |
|
---|
319 | IntQTQueryPipe::~IntQTQueryPipe()
|
---|
320 | {}
|
---|
321 |
|
---|
322 | void IntQTQueryPipe::update(int newInt) {
|
---|
323 | (*content) = newInt;
|
---|
324 | dialog->update();
|
---|
325 | }
|
---|
326 |
|
---|
327 | DoubleQTQueryPipe::DoubleQTQueryPipe(double *_content, QTDialog *_dialog) :
|
---|
328 | content(_content),
|
---|
329 | dialog(_dialog)
|
---|
330 | {}
|
---|
331 |
|
---|
332 | DoubleQTQueryPipe::~DoubleQTQueryPipe()
|
---|
333 | {}
|
---|
334 |
|
---|
335 | void DoubleQTQueryPipe::update(double newDbl) {
|
---|
336 | (*content) = newDbl;
|
---|
337 | dialog->update();
|
---|
338 | }
|
---|
339 |
|
---|
340 | MoleculeQTQueryPipe::MoleculeQTQueryPipe(molecule **_content, QTDialog *_dialog, QComboBox *_theBox, MoleculeListClass *_molecules) :
|
---|
341 | content(_content),
|
---|
342 | dialog(_dialog),
|
---|
343 | theBox(_theBox),
|
---|
344 | molecules(_molecules)
|
---|
345 | {}
|
---|
346 |
|
---|
347 | MoleculeQTQueryPipe::~MoleculeQTQueryPipe()
|
---|
348 | {}
|
---|
349 |
|
---|
350 | void MoleculeQTQueryPipe::update(int newIndex) {
|
---|
351 | QVariant data = theBox->itemData(newIndex);
|
---|
352 | int idx = data.toInt();
|
---|
353 | (*content) = molecules->ReturnIndex(idx);
|
---|
354 | dialog->update();
|
---|
355 | }
|
---|
356 |
|
---|
357 | ElementQTQueryPipe::ElementQTQueryPipe(element **_content, QTDialog *_dialog, QComboBox *_theBox) :
|
---|
358 | content(_content),
|
---|
359 | dialog(_dialog),
|
---|
360 | theBox(_theBox)
|
---|
361 | {}
|
---|
362 |
|
---|
363 | ElementQTQueryPipe::~ElementQTQueryPipe()
|
---|
364 | {}
|
---|
365 |
|
---|
366 | void ElementQTQueryPipe::update(int newIndex) {
|
---|
367 | QVariant data = theBox->itemData(newIndex);
|
---|
368 | int idx = data.toInt();
|
---|
369 | (*content) = World::get()->getPeriode()->FindElement(idx);
|
---|
370 | dialog->update();
|
---|
371 | }
|
---|
372 |
|
---|