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