[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>
|
---|
| 12 |
|
---|
| 13 | #include <Qt/qboxlayout.h>
|
---|
| 14 | #include <Qt/qlabel.h>
|
---|
| 15 | #include <Qt/qspinbox.h>
|
---|
| 16 | #include <Qt/qlineedit.h>
|
---|
| 17 | #include <Qt/qdialogbuttonbox.h>
|
---|
| 18 | #include <Qt/qpushbutton.h>
|
---|
| 19 | #include <Qt/qcombobox.h>
|
---|
| 20 |
|
---|
| 21 | #include "atom.hpp"
|
---|
| 22 | #include "molecule.hpp"
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | using namespace std;
|
---|
| 26 |
|
---|
| 27 | QTDialog::QTDialog() :
|
---|
| 28 | QDialog(0)
|
---|
| 29 | {
|
---|
| 30 | // creating and filling of the Dialog window
|
---|
| 31 | mainLayout = new QVBoxLayout();
|
---|
| 32 | inputLayout = new QVBoxLayout();
|
---|
| 33 | buttonLayout = new QVBoxLayout();
|
---|
| 34 | setLayout(mainLayout);
|
---|
| 35 | mainLayout->addLayout(inputLayout);
|
---|
| 36 | mainLayout->addLayout(buttonLayout);
|
---|
| 37 | buttons = new QDialogButtonBox(QDialogButtonBox::Ok| QDialogButtonBox::Cancel);
|
---|
| 38 | buttonLayout->addWidget(buttons);
|
---|
| 39 |
|
---|
| 40 | // Disable the ok button until something was entered
|
---|
| 41 | buttons->button(QDialogButtonBox::Ok)->setEnabled(false);
|
---|
| 42 |
|
---|
| 43 | // connect the buttons to their appropriate slots
|
---|
| 44 | connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
|
---|
| 45 | connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | QTDialog::~QTDialog()
|
---|
| 49 | {
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | bool QTDialog::display(){
|
---|
| 53 | // Button state might have changed by some update that
|
---|
| 54 | // was done during query construction. To make sure
|
---|
| 55 | // the state is correct, we just call update one more time.
|
---|
| 56 | update();
|
---|
| 57 | if(exec()) {
|
---|
| 58 | setAll();
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 | else {
|
---|
| 62 | return false;
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void QTDialog::update(){
|
---|
| 67 | buttons->button(QDialogButtonBox::Ok)->setEnabled(checkAll());
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /************************** Query Infrastructure ************************/
|
---|
| 71 |
|
---|
| 72 | void QTDialog::queryInt(const char *title, int *target)
|
---|
| 73 | {
|
---|
| 74 | registerQuery(new IntQTQuery(title,target,inputLayout,this));
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void QTDialog::queryString(const char* title, std::string *target)
|
---|
| 78 | {
|
---|
| 79 | registerQuery(new StringQTQuery(title,target,inputLayout,this));
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void QTDialog::queryMolecule(const char *title,molecule **target,MoleculeListClass *molecules)
|
---|
| 83 | {
|
---|
| 84 | registerQuery(new MoleculeQTQuery(title,target,molecules,inputLayout,this));
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | QTDialog::IntQTQuery::IntQTQuery(string _title,int *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
| 88 | Dialog::IntQuery(_title,_target),
|
---|
| 89 | parent(_parent)
|
---|
| 90 | {
|
---|
| 91 | thisLayout = new QHBoxLayout();
|
---|
| 92 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
| 93 | inputBox = new QSpinBox();
|
---|
| 94 | inputBox->setValue(0);
|
---|
| 95 | parent->addLayout(thisLayout);
|
---|
| 96 | thisLayout->addWidget(titleLabel);
|
---|
| 97 | thisLayout->addWidget(inputBox);
|
---|
| 98 |
|
---|
| 99 | pipe = new IntQTQueryPipe(&tmp,_dialog);
|
---|
| 100 | pipe->update(inputBox->value());
|
---|
| 101 | connect(inputBox,SIGNAL(valueChanged(int)),pipe,SLOT(update(int)));
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | QTDialog::IntQTQuery::~IntQTQuery()
|
---|
| 105 | {
|
---|
| 106 | delete pipe;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | // Handling is easy since the GUI makes it impossible to enter invalid values
|
---|
| 110 | bool QTDialog::IntQTQuery::handle()
|
---|
| 111 | {
|
---|
| 112 | return true;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | QTDialog::StringQTQuery::StringQTQuery(string _title,string *_target,QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
| 117 | Dialog::StringQuery(_title,_target),
|
---|
| 118 | parent(_parent)
|
---|
| 119 | {
|
---|
| 120 | thisLayout = new QHBoxLayout();
|
---|
| 121 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
| 122 | inputBox = new QLineEdit();
|
---|
| 123 | parent->addLayout(thisLayout);
|
---|
| 124 | thisLayout->addWidget(titleLabel);
|
---|
| 125 | thisLayout->addWidget(inputBox);
|
---|
| 126 |
|
---|
| 127 | pipe = new StringQTQueryPipe(&tmp,_dialog);
|
---|
| 128 | pipe->update(inputBox->text());
|
---|
| 129 | connect(inputBox,SIGNAL(textChanged(const QString&)),pipe,SLOT(update(const QString&)));
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | QTDialog::StringQTQuery::~StringQTQuery()
|
---|
| 133 | {
|
---|
| 134 | delete pipe;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | // All values besides the empty string are valid
|
---|
| 138 | bool QTDialog::StringQTQuery::handle()
|
---|
| 139 | {
|
---|
| 140 | return tmp!="";
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | QTDialog::MoleculeQTQuery::MoleculeQTQuery(string _title, molecule **_target, MoleculeListClass *_molecules, QBoxLayout *_parent,QTDialog *_dialog) :
|
---|
| 144 | Dialog::MoleculeQuery(_title,_target,_molecules),
|
---|
| 145 | parent(_parent)
|
---|
| 146 | {
|
---|
| 147 | MoleculeList::iterator iter;
|
---|
| 148 | thisLayout = new QHBoxLayout();
|
---|
| 149 | titleLabel = new QLabel(QString(getTitle().c_str()));
|
---|
| 150 | inputBox = new QComboBox();
|
---|
| 151 | // add all molecules to the combo box
|
---|
| 152 | for(iter = molecules->ListOfMolecules.begin();
|
---|
| 153 | iter != molecules->ListOfMolecules.end();
|
---|
| 154 | iter++) {
|
---|
| 155 | stringstream sstr;
|
---|
[6adb96] | 156 | sstr << (*iter)->IndexNr << "\t" << (*iter)->getName();
|
---|
[d3a5ea] | 157 | inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter)->IndexNr));
|
---|
| 158 | }
|
---|
| 159 | parent->addLayout(thisLayout);
|
---|
| 160 | thisLayout->addWidget(titleLabel);
|
---|
| 161 | thisLayout->addWidget(inputBox);
|
---|
| 162 |
|
---|
| 163 | pipe = new MoleculeQTQueryPipe(&tmp,_dialog,inputBox,_molecules);
|
---|
| 164 | pipe->update(inputBox->currentIndex());
|
---|
| 165 | connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | QTDialog::MoleculeQTQuery::~MoleculeQTQuery()
|
---|
| 169 | {
|
---|
| 170 | delete pipe;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | // Handling is easy, since the GUI makes it impossible to select invalid values
|
---|
| 174 | bool QTDialog::MoleculeQTQuery::handle()
|
---|
| 175 | {
|
---|
| 176 | return true;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | /*************************** Plumbing *******************************/
|
---|
| 181 |
|
---|
| 182 | StringQTQueryPipe::StringQTQueryPipe(string *_content, QTDialog *_dialog) :
|
---|
| 183 | content(_content),
|
---|
| 184 | dialog(_dialog)
|
---|
| 185 | {}
|
---|
| 186 |
|
---|
| 187 | StringQTQueryPipe::~StringQTQueryPipe()
|
---|
| 188 | {}
|
---|
| 189 |
|
---|
| 190 | void StringQTQueryPipe::update(const QString& newText) {
|
---|
| 191 | content->assign(newText.toStdString());
|
---|
| 192 | dialog->update();
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | IntQTQueryPipe::IntQTQueryPipe(int *_content, QTDialog *_dialog) :
|
---|
| 196 | content(_content),
|
---|
| 197 | dialog(_dialog)
|
---|
| 198 | {}
|
---|
| 199 |
|
---|
| 200 | IntQTQueryPipe::~IntQTQueryPipe()
|
---|
| 201 | {}
|
---|
| 202 |
|
---|
| 203 | void IntQTQueryPipe::update(int newInt) {
|
---|
| 204 | (*content) = newInt;
|
---|
| 205 | dialog->update();
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | MoleculeQTQueryPipe::MoleculeQTQueryPipe(molecule **_content, QTDialog *_dialog, QComboBox *_theBox, MoleculeListClass *_molecules) :
|
---|
| 209 | content(_content),
|
---|
| 210 | dialog(_dialog),
|
---|
| 211 | theBox(_theBox),
|
---|
| 212 | molecules(_molecules)
|
---|
| 213 | {}
|
---|
| 214 |
|
---|
| 215 | MoleculeQTQueryPipe::~MoleculeQTQueryPipe()
|
---|
| 216 | {}
|
---|
| 217 |
|
---|
| 218 | void MoleculeQTQueryPipe::update(int newIndex) {
|
---|
| 219 | QVariant data = theBox->itemData(newIndex);
|
---|
| 220 | int idx = data.toInt();
|
---|
| 221 | (*content) = molecules->ReturnIndex(idx);
|
---|
| 222 | dialog->update();
|
---|
| 223 | }
|
---|
| 224 |
|
---|