[d3a5ea] | 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>
|
---|
[b8d1aeb] | 12 | #include <limits>
|
---|
[d3a5ea] | 13 |
|
---|
| 14 | #include <Qt/qboxlayout.h>
|
---|
| 15 | #include <Qt/qlabel.h>
|
---|
| 16 | #include <Qt/qspinbox.h>
|
---|
[b8d1aeb] | 17 | #include <QtGui/QDoubleSpinBox>
|
---|
[d3a5ea] | 18 | #include <Qt/qlineedit.h>
|
---|
| 19 | #include <Qt/qdialogbuttonbox.h>
|
---|
| 20 | #include <Qt/qpushbutton.h>
|
---|
| 21 | #include <Qt/qcombobox.h>
|
---|
| 22 |
|
---|
[257c77] | 23 | #include "Helpers/MemDebug.hpp"
|
---|
| 24 |
|
---|
[cbf01e] | 25 | #include "World.hpp"
|
---|
| 26 | #include "periodentafel.hpp"
|
---|
[d3a5ea] | 27 | #include "atom.hpp"
|
---|
[cbf01e] | 28 | #include "element.hpp"
|
---|
[d3a5ea] | 29 | #include "molecule.hpp"
|
---|
[257c77] | 30 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
[d3a5ea] | 31 |
|
---|
| 32 |
|
---|
| 33 | using namespace std;
|
---|
| 34 |
|
---|
| 35 | QTDialog::QTDialog() :
|
---|
| 36 | QDialog(0)
|
---|
| 37 | {
|
---|
| 38 | // creating and filling of the Dialog window
|
---|
| 39 | mainLayout = new QVBoxLayout();
|
---|
| 40 | inputLayout = new QVBoxLayout();
|
---|
| 41 | buttonLayout = new QVBoxLayout();
|
---|
| 42 | setLayout(mainLayout);
|
---|
| 43 | mainLayout->addLayout(inputLayout);
|
---|
| 44 | mainLayout->addLayout(buttonLayout);
|
---|
| 45 | buttons = new QDialogButtonBox(QDialogButtonBox::Ok| QDialogButtonBox::Cancel);
|
---|
| 46 | buttonLayout->addWidget(buttons);
|
---|
| 47 |
|
---|
| 48 | // Disable the ok button until something was entered
|
---|
| 49 | buttons->button(QDialogButtonBox::Ok)->setEnabled(false);
|
---|
| 50 |
|
---|
| 51 | // connect the buttons to their appropriate slots
|
---|
| 52 | connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
|
---|
| 53 | connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | QTDialog::~QTDialog()
|
---|
| 57 | {
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | bool QTDialog::display(){
|
---|
| 61 | // Button state might have changed by some update that
|
---|
| 62 | // was done during query construction. To make sure
|
---|
| 63 | // the state is correct, we just call update one more time.
|
---|
| 64 | update();
|
---|
| 65 | if(exec()) {
|
---|
| 66 | setAll();
|
---|
| 67 | return true;
|
---|
| 68 | }
|
---|
| 69 | else {
|
---|
| 70 | return false;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void QTDialog::update(){
|
---|
| 75 | buttons->button(QDialogButtonBox::Ok)->setEnabled(checkAll());
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /************************** Query Infrastructure ************************/
|
---|
| 79 |
|
---|
[257c77] | 80 | void QTDialog::queryEmpty(char const*, string){
|
---|
| 81 | // TODO
|
---|
| 82 | ASSERT(false, "Not implemented yet");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void QTDialog::queryBoolean(char const*, bool*,string){
|
---|
| 86 | // TODO
|
---|
| 87 | ASSERT(false, "Not implemented yet");
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void QTDialog::queryAtom(char const*, atom**, string){
|
---|
| 91 | // TODO
|
---|
| 92 | ASSERT(false, "Not implemented yet");
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void QTDialog::queryBox(char const*, double**, string){
|
---|
| 96 | // TODO
|
---|
| 97 | ASSERT(false, "Not implemented yet");
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | void QTDialog::queryInt(const char *title, int *target,string)
|
---|
[d3a5ea] | 102 | {
|
---|
| 103 | registerQuery(new IntQTQuery(title,target,inputLayout,this));
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[257c77] | 106 | void QTDialog::queryDouble(const char* title, double* target,string){
|
---|
[b8d1aeb] | 107 | registerQuery(new DoubleQTQuery(title,target,inputLayout,this));
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[257c77] | 110 | void QTDialog::queryString(const char* title, std::string *target,string)
|
---|
[d3a5ea] | 111 | {
|
---|
| 112 | registerQuery(new StringQTQuery(title,target,inputLayout,this));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[257c77] | 115 | void QTDialog::queryMolecule(const char *title,molecule **target,string)
|
---|
[d3a5ea] | 116 | {
|
---|
[257c77] | 117 | registerQuery(new MoleculeQTQuery(title,target,inputLayout,this));
|
---|
[d3a5ea] | 118 | }
|
---|
| 119 |
|
---|
[257c77] | 120 | void QTDialog::queryVector(const char* title, Vector *target,const double *const cellSize, bool check,string) {
|
---|
[b8d1aeb] | 121 | registerQuery(new VectorQTQuery(title,target,cellSize,check,inputLayout,this));
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[257c77] | 124 | void QTDialog::queryElement(const char* title, std::vector<element *> *target,string){
|
---|
[cbf01e] | 125 | registerQuery(new ElementQTQuery(title,target,inputLayout,this));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[b8d1aeb] | 128 | /************************** Query Objects *******************************/
|
---|
| 129 |
|
---|
[d3a5ea] | 130 | QTDialog::IntQTQuery::IntQTQuery(string _title,int *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
| 131 | Dialog::IntQuery(_title,_target),
|
---|
| 132 | parent(_parent)
|
---|
| 133 | {
|
---|
| 134 | thisLayout = new QHBoxLayout();
|
---|
| 135 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
| 136 | inputBox = new QSpinBox();
|
---|
| 137 | inputBox->setValue(0);
|
---|
| 138 | parent->addLayout(thisLayout);
|
---|
| 139 | thisLayout->addWidget(titleLabel);
|
---|
| 140 | thisLayout->addWidget(inputBox);
|
---|
| 141 |
|
---|
| 142 | pipe = new IntQTQueryPipe(&tmp,_dialog);
|
---|
| 143 | pipe->update(inputBox->value());
|
---|
| 144 | connect(inputBox,SIGNAL(valueChanged(int)),pipe,SLOT(update(int)));
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | QTDialog::IntQTQuery::~IntQTQuery()
|
---|
| 148 | {
|
---|
| 149 | delete pipe;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | // Handling is easy since the GUI makes it impossible to enter invalid values
|
---|
| 153 | bool QTDialog::IntQTQuery::handle()
|
---|
| 154 | {
|
---|
| 155 | return true;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[b8d1aeb] | 158 | QTDialog::DoubleQTQuery::DoubleQTQuery(string title,double *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
| 159 | Dialog::DoubleQuery(title,_target),
|
---|
| 160 | parent(_parent)
|
---|
| 161 | {
|
---|
| 162 | thisLayout = new QHBoxLayout();
|
---|
| 163 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
| 164 | inputBox = new QDoubleSpinBox();
|
---|
| 165 | inputBox->setValue(0);
|
---|
| 166 | inputBox->setRange(-numeric_limits<double>::max(),numeric_limits<double>::max());
|
---|
| 167 | inputBox->setDecimals(3);
|
---|
| 168 | parent->addLayout(thisLayout);
|
---|
| 169 | thisLayout->addWidget(titleLabel);
|
---|
| 170 | thisLayout->addWidget(inputBox);
|
---|
| 171 |
|
---|
| 172 | pipe = new DoubleQTQueryPipe(&tmp,_dialog);
|
---|
| 173 | pipe->update(inputBox->value());
|
---|
| 174 | connect(inputBox,SIGNAL(valueChanged(double)),pipe,SLOT(update(double)));
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | QTDialog::DoubleQTQuery::~DoubleQTQuery()
|
---|
| 178 | {
|
---|
| 179 | delete pipe;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | bool QTDialog::DoubleQTQuery::handle() {
|
---|
| 183 | return true;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[d3a5ea] | 186 |
|
---|
| 187 | QTDialog::StringQTQuery::StringQTQuery(string _title,string *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
| 188 | Dialog::StringQuery(_title,_target),
|
---|
| 189 | parent(_parent)
|
---|
| 190 | {
|
---|
| 191 | thisLayout = new QHBoxLayout();
|
---|
| 192 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
| 193 | inputBox = new QLineEdit();
|
---|
| 194 | parent->addLayout(thisLayout);
|
---|
| 195 | thisLayout->addWidget(titleLabel);
|
---|
| 196 | thisLayout->addWidget(inputBox);
|
---|
| 197 |
|
---|
| 198 | pipe = new StringQTQueryPipe(&tmp,_dialog);
|
---|
| 199 | pipe->update(inputBox->text());
|
---|
| 200 | connect(inputBox,SIGNAL(textChanged(const QString&)),pipe,SLOT(update(const QString&)));
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | QTDialog::StringQTQuery::~StringQTQuery()
|
---|
| 204 | {
|
---|
| 205 | delete pipe;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | // All values besides the empty string are valid
|
---|
| 209 | bool QTDialog::StringQTQuery::handle()
|
---|
| 210 | {
|
---|
| 211 | return tmp!="";
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[257c77] | 214 | QTDialog::MoleculeQTQuery::MoleculeQTQuery(string _title, molecule **_target, QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
| 215 | Dialog::MoleculeQuery(_title,_target),
|
---|
[d3a5ea] | 216 | parent(_parent)
|
---|
| 217 | {
|
---|
| 218 | thisLayout = new QHBoxLayout();
|
---|
| 219 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
| 220 | inputBox = new QComboBox();
|
---|
| 221 | // add all molecules to the combo box
|
---|
[257c77] | 222 | vector<molecule*> molecules = World::getInstance().getAllMolecules();
|
---|
| 223 | for(vector<molecule*>::iterator iter = molecules.begin();
|
---|
| 224 | iter != molecules.end();
|
---|
[cbf01e] | 225 | ++iter) {
|
---|
[d3a5ea] | 226 | stringstream sstr;
|
---|
[6adb96] | 227 | sstr << (*iter)->IndexNr << "\t" << (*iter)->getName();
|
---|
[d3a5ea] | 228 | inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter)->IndexNr));
|
---|
| 229 | }
|
---|
| 230 | parent->addLayout(thisLayout);
|
---|
| 231 | thisLayout->addWidget(titleLabel);
|
---|
| 232 | thisLayout->addWidget(inputBox);
|
---|
| 233 |
|
---|
[257c77] | 234 | pipe = new MoleculeQTQueryPipe(&tmp,_dialog,inputBox);
|
---|
[d3a5ea] | 235 | pipe->update(inputBox->currentIndex());
|
---|
| 236 | connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | QTDialog::MoleculeQTQuery::~MoleculeQTQuery()
|
---|
| 240 | {
|
---|
| 241 | delete pipe;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | // Handling is easy, since the GUI makes it impossible to select invalid values
|
---|
| 245 | bool QTDialog::MoleculeQTQuery::handle()
|
---|
| 246 | {
|
---|
| 247 | return true;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[b8d1aeb] | 250 | QTDialog::VectorQTQuery::VectorQTQuery(std::string title, Vector *_target, const double *const _cellSize, bool _check,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
| 251 | Dialog::VectorQuery(title,_target,_cellSize,_check),
|
---|
| 252 | parent(_parent)
|
---|
| 253 | {
|
---|
| 254 | // 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
|
---|
| 255 | int j = -1;
|
---|
| 256 | const char *coords[3] = {"x:","y:","z:"};
|
---|
| 257 | mainLayout= new QHBoxLayout();
|
---|
| 258 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
| 259 | mainLayout->addWidget(titleLabel);
|
---|
| 260 | subLayout = new QVBoxLayout();
|
---|
| 261 | mainLayout->addLayout(subLayout);
|
---|
| 262 | for(int i=0; i<3; i++) {
|
---|
| 263 | j+=i+1;
|
---|
| 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,cellSize[j]);
|
---|
| 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 |
|
---|
[d3a5ea] | 287 |
|
---|
[257c77] | 288 | QTDialog::ElementQTQuery::ElementQTQuery(std::string _title, vector<element *> *_target, QBoxLayout *_parent, QTDialog *_dialog) :
|
---|
[cbf01e] | 289 | Dialog::ElementQuery(_title,_target),
|
---|
| 290 | parent(_parent)
|
---|
| 291 | {
|
---|
[cd032d] | 292 | periodentafel *periode = World::getInstance().getPeriode();
|
---|
[cbf01e] | 293 | thisLayout = new QHBoxLayout();
|
---|
| 294 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
| 295 | inputBox = new QComboBox();
|
---|
[68d781] | 296 | for(periodentafel::const_iterator iter = periode->begin();
|
---|
| 297 | iter!=periode->end();
|
---|
| 298 | ++iter)
|
---|
[cbf01e] | 299 | {
|
---|
| 300 | stringstream sstr;
|
---|
[68d781] | 301 | sstr << (*iter).first << "\t" << (*iter).second->name;
|
---|
| 302 | inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter).first));
|
---|
[cbf01e] | 303 | }
|
---|
| 304 | parent->addLayout(thisLayout);
|
---|
| 305 | thisLayout->addWidget(titleLabel);
|
---|
| 306 | thisLayout->addWidget(inputBox);
|
---|
| 307 |
|
---|
[257c77] | 308 | pipe = new ElementQTQueryPipe(&elements,_dialog,inputBox);
|
---|
[cbf01e] | 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 |
|
---|
[d3a5ea] | 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 |
|
---|
[b8d1aeb] | 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 |
|
---|
[257c77] | 363 | MoleculeQTQueryPipe::MoleculeQTQueryPipe(molecule **_content, QTDialog *_dialog, QComboBox *_theBox) :
|
---|
[d3a5ea] | 364 | content(_content),
|
---|
| 365 | dialog(_dialog),
|
---|
[257c77] | 366 | theBox(_theBox)
|
---|
[d3a5ea] | 367 | {}
|
---|
| 368 |
|
---|
| 369 | MoleculeQTQueryPipe::~MoleculeQTQueryPipe()
|
---|
| 370 | {}
|
---|
| 371 |
|
---|
| 372 | void MoleculeQTQueryPipe::update(int newIndex) {
|
---|
| 373 | QVariant data = theBox->itemData(newIndex);
|
---|
| 374 | int idx = data.toInt();
|
---|
[257c77] | 375 | (*content) = World::getInstance().getMolecule(MoleculeById(idx));
|
---|
[d3a5ea] | 376 | dialog->update();
|
---|
| 377 | }
|
---|
| 378 |
|
---|
[257c77] | 379 | ElementQTQueryPipe::ElementQTQueryPipe(std::vector<element *> *_content, QTDialog *_dialog, QComboBox *_theBox) :
|
---|
[cbf01e] | 380 | content(_content),
|
---|
| 381 | dialog(_dialog),
|
---|
| 382 | theBox(_theBox)
|
---|
[257c77] | 383 | {
|
---|
| 384 | content->resize(1);
|
---|
| 385 | }
|
---|
[cbf01e] | 386 |
|
---|
| 387 | ElementQTQueryPipe::~ElementQTQueryPipe()
|
---|
| 388 | {}
|
---|
| 389 |
|
---|
| 390 | void ElementQTQueryPipe::update(int newIndex) {
|
---|
| 391 | QVariant data = theBox->itemData(newIndex);
|
---|
| 392 | int idx = data.toInt();
|
---|
[257c77] | 393 | (*content)[0] = World::getInstance().getPeriode()->FindElement(idx);
|
---|
[cbf01e] | 394 | dialog->update();
|
---|
| 395 | }
|
---|
| 396 |
|
---|