/* * QTDialog.cpp * * Created on: Jan 18, 2010 * Author: crueger */ #include "UIElements/QT4/QTDialog.hpp" #include #include #include #include #include #include #include #include #include #include "atom.hpp" #include "molecule.hpp" using namespace std; QTDialog::QTDialog() : QDialog(0) { // creating and filling of the Dialog window mainLayout = new QVBoxLayout(); inputLayout = new QVBoxLayout(); buttonLayout = new QVBoxLayout(); setLayout(mainLayout); mainLayout->addLayout(inputLayout); mainLayout->addLayout(buttonLayout); buttons = new QDialogButtonBox(QDialogButtonBox::Ok| QDialogButtonBox::Cancel); buttonLayout->addWidget(buttons); // Disable the ok button until something was entered buttons->button(QDialogButtonBox::Ok)->setEnabled(false); // connect the buttons to their appropriate slots connect(buttons, SIGNAL(accepted()), this, SLOT(accept())); connect(buttons, SIGNAL(rejected()), this, SLOT(reject())); } QTDialog::~QTDialog() { } bool QTDialog::display(){ // Button state might have changed by some update that // was done during query construction. To make sure // the state is correct, we just call update one more time. update(); if(exec()) { setAll(); return true; } else { return false; } } void QTDialog::update(){ buttons->button(QDialogButtonBox::Ok)->setEnabled(checkAll()); } /************************** Query Infrastructure ************************/ void QTDialog::queryInt(const char *title, int *target) { registerQuery(new IntQTQuery(title,target,inputLayout,this)); } void QTDialog::queryString(const char* title, std::string *target) { registerQuery(new StringQTQuery(title,target,inputLayout,this)); } void QTDialog::queryMolecule(const char *title,molecule **target,MoleculeListClass *molecules) { registerQuery(new MoleculeQTQuery(title,target,molecules,inputLayout,this)); } QTDialog::IntQTQuery::IntQTQuery(string _title,int *_target,QBoxLayout *_parent,QTDialog *_dialog) : Dialog::IntQuery(_title,_target), parent(_parent) { thisLayout = new QHBoxLayout(); titleLabel = new QLabel(QString(getTitle().c_str())); inputBox = new QSpinBox(); inputBox->setValue(0); parent->addLayout(thisLayout); thisLayout->addWidget(titleLabel); thisLayout->addWidget(inputBox); pipe = new IntQTQueryPipe(&tmp,_dialog); pipe->update(inputBox->value()); connect(inputBox,SIGNAL(valueChanged(int)),pipe,SLOT(update(int))); } QTDialog::IntQTQuery::~IntQTQuery() { delete pipe; } // Handling is easy since the GUI makes it impossible to enter invalid values bool QTDialog::IntQTQuery::handle() { return true; } QTDialog::StringQTQuery::StringQTQuery(string _title,string *_target,QBoxLayout *_parent,QTDialog *_dialog) : Dialog::StringQuery(_title,_target), parent(_parent) { thisLayout = new QHBoxLayout(); titleLabel = new QLabel(QString(getTitle().c_str())); inputBox = new QLineEdit(); parent->addLayout(thisLayout); thisLayout->addWidget(titleLabel); thisLayout->addWidget(inputBox); pipe = new StringQTQueryPipe(&tmp,_dialog); pipe->update(inputBox->text()); connect(inputBox,SIGNAL(textChanged(const QString&)),pipe,SLOT(update(const QString&))); } QTDialog::StringQTQuery::~StringQTQuery() { delete pipe; } // All values besides the empty string are valid bool QTDialog::StringQTQuery::handle() { return tmp!=""; } QTDialog::MoleculeQTQuery::MoleculeQTQuery(string _title, molecule **_target, MoleculeListClass *_molecules, QBoxLayout *_parent,QTDialog *_dialog) : Dialog::MoleculeQuery(_title,_target,_molecules), parent(_parent) { MoleculeList::iterator iter; thisLayout = new QHBoxLayout(); titleLabel = new QLabel(QString(getTitle().c_str())); inputBox = new QComboBox(); // add all molecules to the combo box for(iter = molecules->ListOfMolecules.begin(); iter != molecules->ListOfMolecules.end(); iter++) { stringstream sstr; sstr << (*iter)->IndexNr << "\t" << (*iter)->getName(); inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter)->IndexNr)); } parent->addLayout(thisLayout); thisLayout->addWidget(titleLabel); thisLayout->addWidget(inputBox); pipe = new MoleculeQTQueryPipe(&tmp,_dialog,inputBox,_molecules); pipe->update(inputBox->currentIndex()); connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int))); } QTDialog::MoleculeQTQuery::~MoleculeQTQuery() { delete pipe; } // Handling is easy, since the GUI makes it impossible to select invalid values bool QTDialog::MoleculeQTQuery::handle() { return true; } /*************************** Plumbing *******************************/ StringQTQueryPipe::StringQTQueryPipe(string *_content, QTDialog *_dialog) : content(_content), dialog(_dialog) {} StringQTQueryPipe::~StringQTQueryPipe() {} void StringQTQueryPipe::update(const QString& newText) { content->assign(newText.toStdString()); dialog->update(); } IntQTQueryPipe::IntQTQueryPipe(int *_content, QTDialog *_dialog) : content(_content), dialog(_dialog) {} IntQTQueryPipe::~IntQTQueryPipe() {} void IntQTQueryPipe::update(int newInt) { (*content) = newInt; dialog->update(); } MoleculeQTQueryPipe::MoleculeQTQueryPipe(molecule **_content, QTDialog *_dialog, QComboBox *_theBox, MoleculeListClass *_molecules) : content(_content), dialog(_dialog), theBox(_theBox), molecules(_molecules) {} MoleculeQTQueryPipe::~MoleculeQTQueryPipe() {} void MoleculeQTQueryPipe::update(int newIndex) { QVariant data = theBox->itemData(newIndex); int idx = data.toInt(); (*content) = molecules->ReturnIndex(idx); dialog->update(); }