SpikeStream Application Library  0.2
AbstractParametersEditDialog.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "AbstractParametersEditDialog.h"
00003 #include "Globals.h"
00004 #include "Util.h"
00005 #include "SpikeStreamException.h"
00006 using namespace spikestream;
00007 
00008 //Qt includes
00009 #include <QDebug>
00010 #include <QLabel>
00011 #include <QPixmap>
00012 
00013 
00015 AbstractParametersEditDialog::AbstractParametersEditDialog(const QList<ParameterInfo>& paramInfoList, QWidget* parent) : QDialog(parent) {
00016         this->parameterInfoList = paramInfoList;
00017 }
00018 
00019 
00021 AbstractParametersEditDialog::~AbstractParametersEditDialog(){
00022 }
00023 
00024 
00025 /*----------------------------------------------------------*/
00026 /*-----               PROTECTED METHODS                -----*/
00027 /*----------------------------------------------------------*/
00028 
00030 void AbstractParametersEditDialog::addParameters(QVBoxLayout* mainVLayout){
00031         int cntr = 0;
00032         QGridLayout* gridLayout = new QGridLayout();
00033 
00034         //Create validators
00035         QDoubleValidator* doubleValidator = new QDoubleValidator(-100000.0, 100000.0, 5, this);
00036         QDoubleValidator* positiveDoubleValidator = new QDoubleValidator(0.0, 100000.0, 5, this);
00037         QIntValidator* intValidator = new QIntValidator(-1000000, 1000000, this);
00038         QIntValidator* unsignedIntValidator = new QIntValidator(0, 1000000, this);
00039 
00040         //Add parameters to the layout
00041         foreach(ParameterInfo info, parameterInfoList){
00042                 //Add double parameter
00043                 if(info.getType() == ParameterInfo::DOUBLE){
00044                         gridLayout->addWidget(new QLabel(info.getName()), cntr, 0);
00045                         QLineEdit* tmpLineEdit = new QLineEdit();
00046                         tmpLineEdit->setValidator(doubleValidator);
00047                         lineEditMap[info.getName()] = tmpLineEdit;
00048                         gridLayout->addWidget(tmpLineEdit, cntr, 1);
00049                 }
00050 
00051                 //Add positive double parameter
00052                 else if(info.getType() == ParameterInfo::POSITIVE_DOUBLE){
00053                         gridLayout->addWidget(new QLabel(info.getName()), cntr, 0);
00054                         QLineEdit* tmpLineEdit = new QLineEdit();
00055                         tmpLineEdit->setValidator(positiveDoubleValidator);
00056                         lineEditMap[info.getName()] = tmpLineEdit;
00057                         gridLayout->addWidget(tmpLineEdit, cntr, 1);
00058                 }
00059 
00060                 //Add integer parameter
00061                 else if(info.getType() == ParameterInfo::INTEGER){
00062                         gridLayout->addWidget(new QLabel(info.getName()), cntr, 0);
00063                         QLineEdit* tmpLineEdit = new QLineEdit();
00064                         tmpLineEdit->setValidator(intValidator);
00065                         lineEditMap[info.getName()] = tmpLineEdit;
00066                         gridLayout->addWidget(tmpLineEdit, cntr, 1);
00067                 }
00068 
00069                 //Add unsigned integer parameter
00070                 else if(info.getType() == ParameterInfo::UNSIGNED_INTEGER){
00071                         gridLayout->addWidget(new QLabel(info.getName()), cntr, 0);
00072                         QLineEdit* tmpLineEdit = new QLineEdit();
00073                         tmpLineEdit->setValidator(unsignedIntValidator);
00074                         lineEditMap[info.getName()] = tmpLineEdit;
00075                         gridLayout->addWidget(tmpLineEdit, cntr, 1);
00076                 }
00077 
00078                 //Add boolean parameter
00079                 else if(info.getType() == ParameterInfo::BOOLEAN){
00080                         QCheckBox* chkBox = new QCheckBox(info.getName());
00081                         checkBoxMap[info.getName()] = chkBox;
00082                         gridLayout->addWidget(chkBox, cntr, 0);
00083                 }
00084 
00085                 //Add option parameter - a list in a combo box
00086                 else if(info.getType() == ParameterInfo::OPTION){
00087                         gridLayout->addWidget(new QLabel(info.getName()), cntr, 0);
00088                         QComboBox* tmpComboBox = new QComboBox();
00089                         foreach(QString optionName, info.getOptionNames())
00090                                 tmpComboBox->addItem(optionName);
00091                         comboMap[info.getName()] = tmpComboBox;
00092                         gridLayout->addWidget(tmpComboBox, cntr, 1);
00093                 }
00094 
00095                 //Unknown parameter type
00096                 else{
00097                         throw SpikeStreamException("Parameter type not recognized: " + info.getType());
00098                 }
00099 
00100                 //Add help tool tip
00101                 QLabel* tmpLabel = new QLabel();
00102                 tmpLabel->setPixmap(QPixmap(Globals::getSpikeStreamRoot() + "/images/help.png"));
00103                 tmpLabel->setToolTip(info.getDescription());
00104                 gridLayout->addWidget(tmpLabel, cntr, 2);
00105                 ++cntr;
00106         }
00107         mainVLayout->addLayout(gridLayout);
00108 }
00109 
00110 
00112 void AbstractParametersEditDialog::addButtons(QVBoxLayout* mainVLayout){
00113         QHBoxLayout *buttonBox = new QHBoxLayout();
00114         QPushButton* cancelButton = new QPushButton("Cancel");
00115         buttonBox->addWidget(cancelButton);
00116         connect (cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
00117         QPushButton* defaultsButton = new QPushButton("Load defaults");
00118         buttonBox->addWidget(defaultsButton);
00119         connect (defaultsButton, SIGNAL(clicked()), this, SLOT(defaultButtonClicked()));
00120         QPushButton* okButton = new QPushButton("Ok");
00121         buttonBox->addWidget(okButton);
00122         connect (okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
00123         mainVLayout->addLayout(buttonBox);
00124         okButton->setFocus(Qt::OtherFocusReason);
00125 }
00126 
00127 
00129 QHash<QString, double> AbstractParametersEditDialog::getParameterValues(){
00130         QHash<QString, double> paramMap;
00131 
00132         //Extract double, int and unsigned int parameters
00133         for(QHash<QString, QLineEdit*>::iterator iter = lineEditMap.begin(); iter != lineEditMap.end(); ++iter){
00134                 QString paramStr = iter.value()->text();
00135                 if(paramStr.isEmpty())
00136                         throw SpikeStreamException(iter.key() + " has not been entered.");
00137 
00138                 paramMap[iter.key()] = Util::getDouble(paramStr);
00139         }
00140 
00141         //Extract boolean parameters - store as 1 or 0 in parameter map
00142         for(QHash<QString, QCheckBox*>::iterator iter = checkBoxMap.begin(); iter != checkBoxMap.end(); ++iter){
00143                 if(iter.value()->isChecked())
00144                         paramMap[iter.key()] = 1.0;
00145                 else
00146                         paramMap[iter.key()] = 0.0;
00147         }
00148 
00149         //Extract option parameters - store the option index in the parameter map
00150         for(QHash<QString, QComboBox*>::iterator iter = comboMap.begin(); iter != comboMap.end(); ++iter){
00151                 paramMap[iter.key()] = iter.value()->currentIndex();
00152         }
00153 
00154         //Check all parameters have been extracted
00155         if(paramMap.size() != parameterInfoList.size()){
00156                 throw SpikeStreamException("Failed to find all parameters in list or map has too many entries.");
00157         }
00158 
00159         //Return populated map
00160         return paramMap;
00161 }
00162 
00163 
00165 void AbstractParametersEditDialog::setParameterValues(const QHash<QString, double>& paramMap){
00166         //Run some basic checks
00167         if(paramMap.size() != parameterInfoList.size())
00168                 throw SpikeStreamException("Parameter map size does not match list of parameters.");
00169         if(paramMap.size() != ( lineEditMap.size() + checkBoxMap.size() + comboMap.size() ) )
00170                 throw SpikeStreamException("Mismatch between the parameter map and the stored parameters.");
00171 
00172         //Set values in the line edits
00173         for(QHash<QString, QLineEdit*>::iterator iter = lineEditMap.begin(); iter != lineEditMap.end(); ++iter){
00174                 if( !paramMap.contains(iter.key()) )
00175                         throw SpikeStreamException("A value for parameter " + iter.key() + " cannot be found in the parameter map.");
00176                 iter.value()->setText(QString::number(paramMap[iter.key()]));
00177         }
00178 
00179         //Set check box values
00180         for(QHash<QString, QCheckBox*>::iterator iter = checkBoxMap.begin(); iter != checkBoxMap.end(); ++iter){
00181                 if( !paramMap.contains(iter.key()) )
00182                         throw SpikeStreamException("A value for parameter " + iter.key() + " cannot be found in the parameter map.");
00183                 if(paramMap[iter.key()] == 0)
00184                         iter.value()->setChecked(false);
00185                 else
00186                         iter.value()->setChecked(true);
00187         }
00188 
00189         //Set option values
00190         for(QHash<QString, QComboBox*>::iterator iter = comboMap.begin(); iter != comboMap.end(); ++iter){
00191                 if( !paramMap.contains(iter.key()) )
00192                         throw SpikeStreamException("A value for parameter " + iter.key() + " cannot be found in the parameter map.");
00193 
00194                 //Check that the index is valid
00195                 int index = (int)paramMap[iter.key()];
00196                 if(index < 0 || index >= iter.value()->count())
00197                         throw SpikeStreamException("Index out of range for combo box: " + QString::number(index));
00198 
00199                 //Set the combo to the current index
00200                 iter.value()->setCurrentIndex(index);
00201         }
00202 }
00203 
 All Classes Files Functions Variables Typedefs Friends Defines