Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

GlobalParametersDialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   SpikeStream Application                                               *
00003  *   Copyright (C) 2007 by David Gamez                                     *
00004  *   david@davidgamez.eu                                                   *
00005  *   Version 0.1                                                           *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 
00023 //SpikeStream includes
00024 #include "GlobalParametersDialog.h"
00025 #include "Utilities.h"
00026 #include "Debug.h"
00027 
00028 //Qt includes
00029 #include <qlabel.h>
00030 #include <qpushbutton.h>
00031 #include <qmessagebox.h>
00032 
00033 //Other includes
00034 #include <mysql++.h>
00035 #include <map>
00036 #include <iostream>
00037 using namespace std;
00038 using namespace mysqlpp;
00039 
00040 
00041 /*! Constructor. */
00042 GlobalParametersDialog::GlobalParametersDialog(QWidget *parent, DBInterface *netDBInter, SimulationManager *simMan) : QDialog(parent, "GlobParamDlg", false){
00043         //Store reference to database
00044         networkDBInterface = netDBInter;
00045 
00046         //Store reference to simulation manager
00047         simulationManager = simMan;
00048 
00049         //Set caption to dialog
00050         this->setCaption("Global Parameters");
00051 
00052         //Create a validator to control inputs
00053         paramValidator = new QDoubleValidator(-100000.0, 100000.0, 4, this);
00054 
00055         //Create a vertical box to organise layout 
00056         QVBoxLayout *verticalBox = new QVBoxLayout(this, 5, 10);
00057         verticalBox->addSpacing(5);
00058 
00059         /* Create a text box and label for each of the parameters in the Global Parameters 
00060                 table that are not boolean */
00061 
00062         /* The descriptions and values are on separate lines, so store them in two temporary
00063                 maps and combine them afterwards */
00064         map<const char*, const char*, charKeyCompare> tempDescParamMap;
00065         map<const char*, double, charKeyCompare> tempValueMap;
00066 
00067         try{
00068                 Query query = networkDBInterface->getQuery();
00069                 query.reset();
00070                 query<<"SHOW COLUMNS FROM GlobalParameters";
00071                 Result showResult = query.store();
00072                 for(Result::iterator iter = showResult.begin(); iter != showResult.end(); ++iter){
00073                         Row showRow(*iter);
00074         
00075                         //Get the column name
00076                         QString fieldName((std::string)showRow["Field"]);
00077                 
00078                         //If it is a description of a parameter
00079                         if(fieldName.contains("_desc")){
00080                                 //Store key so that it does not vanish from map
00081                                 mapKeyStrList += fieldName.section("_", 0, 0);
00082         
00083                                 //Store the description to set up the label later
00084                                 labelMap[mapKeyStrList.last().ascii()] = new QString((std::string)showRow["Default"]);
00085                         }
00086                         else if(fieldName.contains("_val")){
00087                                 //Store key so that it does not vanish from map
00088                                 mapKeyStrList += fieldName.section("_", 0, 0);
00089         
00090         
00091                                 //If the value is a boolean, store this fact
00092                                 QString valueType((std::string)showRow["Type"]);
00093                                 if(valueType == "tinyint(1)"){//This value will be loaded as a check box
00094                                         int defaultValue =  Utilities::getUInt((std::string)showRow["Default"]);
00095                                         if(defaultValue == 0)
00096                                                 boolParameterMap[mapKeyStrList.last().ascii()] = false;
00097                                         else if(defaultValue == 1)
00098                                                 boolParameterMap[mapKeyStrList.last().ascii()] = true;
00099                                         else
00100                                                 cerr<<"GlobalParametersDialog: BOOLEAN VALUE NOT RECOGNIZED: "<<defaultValue<<endl;
00101                                         
00102                                         //Store in default value map for querying
00103                                         defaultValueMap[mapKeyStrList.last().ascii()] = (double)defaultValue;
00104                                         
00105                                 }
00106                                 else{
00107                                         //Get the default value for any other data types
00108                                         double defaultValue = Utilities::getDouble((std::string)showRow["Default"]);
00109         
00110                                         //Store link between description and default value
00111                                         defaultValueMap[mapKeyStrList.last().ascii()] = defaultValue;
00112                                 }
00113                         }
00114                 }
00115         }
00116     catch (const BadQuery& er) {// Handle any query errors
00117                 cerr<<"GlobalParametersDialog: MYSQL QUERY EXCEPTION \""<<er.what()<<"\""<<endl;
00118                 QString errorString = "Bad query when loading information about global parameters: \"";
00119                 errorString += er.what();
00120                 errorString += "\"";
00121                 QMessageBox::critical( 0, "Global Parameters Error", errorString);
00122                 exit(1);//Critical error
00123     }
00124     catch (const Exception& er) {// Catch-all for any other MySQL++ exceptions
00125         cerr<<"GlobalParametersDialog: MYSQL EXCEPTION \""<<er.what()<<"\""<<endl;
00126                 QString errorString = "Exception thrown when loading information about global parameters: \"";
00127                 errorString += er.what();
00128                 errorString += "\"";
00129                 QMessageBox::critical( 0, "Global Parameters Error", errorString);
00130                 exit(1);//Critical error
00131     }
00132 
00133         //Work through the parameters, and set up dialog
00134         for(map<const char *, QString*, charKeyCompare>::iterator iter = labelMap.begin(); iter != labelMap.end(); ++iter){
00135 
00136                 if(boolParameterMap.count(iter->first)){
00137                         //Create a check box
00138                         QCheckBox *tempCheckBox = new QCheckBox(*iter->second, this);
00139                         checkBoxMap[iter->first] = tempCheckBox;
00140                         QHBoxLayout *hBoxLayout = new QHBoxLayout();
00141                         hBoxLayout->addSpacing(10);
00142                         hBoxLayout->addWidget(tempCheckBox);
00143                         verticalBox->addLayout(hBoxLayout);
00144                 }
00145                 else {
00146                         //Create line edit
00147                         QLineEdit *parameterText = new QLineEdit(this);
00148                         lineEditMap[iter->first] = parameterText;
00149         
00150                         //Add a label and text box for this parameter
00151                         addParameter(iter->second, parameterText, verticalBox);
00152                 }
00153         }
00154         verticalBox->addSpacing(5);
00155 
00156         //Add buttons to apply and reset the parameters
00157         QHBoxLayout *buttonBox = new QHBoxLayout();
00158         QPushButton *okButton = new QPushButton("Ok", this);
00159         connect (okButton, SIGNAL(clicked()), this, SLOT(okButtonPressed()));
00160         buttonBox->addWidget(okButton);
00161 
00162         QPushButton *applyButton = new QPushButton("Apply", this);
00163         connect (applyButton, SIGNAL(clicked()), this, SLOT(applyButtonPressed()));
00164         buttonBox->addWidget(applyButton);
00165 
00166         QPushButton *loadDefaultsButton = new QPushButton("Load Defaults", this);
00167         connect (loadDefaultsButton, SIGNAL(clicked()), this, SLOT(loadDefaultsButtonPressed()));
00168         buttonBox->addWidget(loadDefaultsButton);
00169 
00170         QPushButton *makeDefaultsButton = new QPushButton("Make Defaults", this);
00171         connect (makeDefaultsButton, SIGNAL(clicked()), this, SLOT(makeDefaultsButtonPressed()));
00172         buttonBox->addWidget(makeDefaultsButton);
00173         
00174         QPushButton *cancelButton = new QPushButton("Cancel", this);
00175         connect (cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonPressed()));
00176         buttonBox->addWidget(cancelButton);
00177         verticalBox->addLayout(buttonBox);
00178 }
00179 
00180 
00181 /*! Destructor. */
00182 GlobalParametersDialog::~ GlobalParametersDialog(){
00183         #ifdef MEMORY_DEBUG
00184                 cout<<"DESTROYING GLOBAL PARAMETERS DIALOG"<<endl;
00185         #endif//MEMORY_DEBUG
00186 
00187         //Clean up dynamically allocated QStrings
00188         for(map<const char *, QString*, charKeyCompare>::iterator iter = labelMap.begin(); iter != labelMap.end(); ++iter)
00189                 delete iter->second;
00190 }
00191 
00192 
00193 //---------------------------------------------------------------------------
00194 //-----------------------    PUBLIC METHODS    ------------------------------
00195 //---------------------------------------------------------------------------
00196 
00197 /*! Extract the current values of the parameters from the database
00198         NOTE This assumes that there is at least 1 parameter. */
00199 bool GlobalParametersDialog::loadParameters(){
00200         try{
00201                 //Generate and execute query
00202                 Query query = networkDBInterface->getQuery();
00203                 query.reset();
00204                 QString queryString ("SELECT ");
00205                 for(map<const char*, double, charKeyCompare>::iterator iter = defaultValueMap.begin(); iter != defaultValueMap.end(); ++iter){
00206                         queryString += iter->first;
00207                         queryString += "_val, ";
00208                 }
00209                 //Trim the last comma and finish off query
00210                 queryString.remove(queryString.length() - 2, 2);
00211                 queryString += " FROM GlobalParameters";
00212         
00213                 //Execute query
00214                 query<<queryString;
00215                 Result valueResult = query.store();
00216         
00217                 //Check number of rows. Global parameters should only have one row
00218                 if(valueResult.rows() == 0){
00219                         cerr<<"Global Parameters table is empty and should have a single row"<<endl;
00220                         QMessageBox::critical( 0, "Global Parameters Error", "Global Parameters table is empty and should have a single row");
00221                         return false;
00222                 }
00223                 else if (valueResult.rows() > 1){
00224                         cerr<<"Global Parameters table has more than one row and should only have a single row"<<endl;
00225                         QMessageBox::critical( 0, "Global Parameters Error", "Global Parameters table has more than one row and should only have a single row");
00226                         return false;
00227                 }
00228                 Row valueRow(*valueResult.begin());
00229         
00230                 //Load up the double parameters
00231                 for(map<const char*, QLineEdit*, charKeyCompare>::iterator iter = lineEditMap.begin(); iter != lineEditMap.end(); ++iter){
00232                         //Get the name of the parameter value
00233                         QString paramName(iter->first);
00234                         paramName += "_val";
00235         
00236                         //Get the parameter value from the database query
00237                         double currentParamVal = Utilities::getDouble((std::string)valueRow[paramName.ascii()]);
00238                         
00239                         //Set the text on the line edit
00240                         iter->second->setText(QString::number(currentParamVal));
00241                 }
00242         
00243                 //Load up the check box parameters
00244                 //Load up the double parameters
00245                 for(map<const char*, QCheckBox*, charKeyCompare>::iterator iter = checkBoxMap.begin(); iter != checkBoxMap.end(); ++iter){
00246                         //Get the name of the parameter value
00247                         QString paramName(iter->first);
00248                         paramName += "_val";
00249         
00250                         //Get the parameter value from the database query
00251                         int currentParamVal = Utilities::getUInt((std::string)valueRow[paramName.ascii()]);
00252                         
00253                         //Set the check box appropriately
00254                         if(currentParamVal == 1)
00255                                 iter->second->setChecked(true);
00256                         else if(currentParamVal == 0)
00257                                 iter->second->setChecked(false);
00258                         else{
00259                                 cerr<<"GlobalParametersDialog: CURRENT VALUE NOT RECOGNIZED"<<currentParamVal<<endl;
00260                                 QMessageBox::critical( 0, "Global Parameters Error", "Current parameter value not recognized");
00261                                 return false;
00262                         }
00263                 }
00264         }
00265     catch (const BadQuery& er) {// Handle any query errors
00266                 cerr<<"GlobalParametersDialog: MYSQL QUERY EXCEPTION \""<<er.what()<<"\""<<endl;
00267                 QString errorString = "Bad query when loading global parameters: \"";
00268                 errorString += er.what();
00269                 errorString += "\"";
00270                 QMessageBox::critical( 0, "Global Parameters Error", errorString);
00271                 return false;
00272     }
00273     catch (const Exception& er) {// Catch-all for any other MySQL++ exceptions
00274         cerr<<"GlobalParametersDialog: MYSQL EXCEPTION \""<<er.what()<<"\""<<endl;
00275                 QString errorString = "Exception thrown when loading global parameters: \"";
00276                 errorString += er.what();
00277                 errorString += "\"";
00278                 QMessageBox::critical( 0, "Global Parameters Error", errorString);
00279                 return false;
00280     }
00281 
00282         //Everything should be ok if we have reached this point.
00283         return true;
00284 }
00285 
00286 
00287 //--------------------------------------------------------------------------
00288 //------------------------    SLOTS    -------------------------------------
00289 //--------------------------------------------------------------------------
00290 
00291 /*! Stores the global parameters when the apply button is pressed. */
00292 void GlobalParametersDialog::applyButtonPressed(){
00293         storeParameters();
00294         simulationManager->setGlobalParameters();
00295 }
00296 
00297 
00298 /*! Slot connected to cancel button that closes dialog without storing values. */
00299 void GlobalParametersDialog::cancelButtonPressed(){
00300         reject();
00301 }
00302 
00303 
00304 /*! Loads up the default parameters. */
00305 void GlobalParametersDialog::loadDefaultsButtonPressed(){
00306         //Load default double values
00307         for(map<const char*, QLineEdit*, charKeyCompare>::iterator iter = lineEditMap.begin(); iter != lineEditMap.end(); ++iter){
00308                 iter->second->setText(QString::number(defaultValueMap[iter->first]));
00309         }
00310 
00311         //Load default boolean values
00312         for(map<const char*, QCheckBox*, charKeyCompare>::iterator iter = checkBoxMap.begin(); iter != checkBoxMap.end(); ++iter){
00313                 iter->second->setChecked((boolParameterMap[iter->first]));
00314         }
00315 }
00316 
00317 
00318 /*! Makes the currently entered parameters the defaults. */
00319 void GlobalParametersDialog::makeDefaultsButtonPressed(){
00320 //FIXME NOT IMPLEMENTED YET
00321 }
00322 
00323 
00324 /*! Stores the parameters and hides the dialog. */
00325 void GlobalParametersDialog::okButtonPressed(){
00326         storeParameters();
00327         simulationManager->setGlobalParameters();
00328         accept();
00329 }
00330 
00331 
00332 //-----------------------------------------------------------------------------
00333 //-----------------------        PRIVATE METHODS     --------------------------
00334 //-----------------------------------------------------------------------------
00335 
00336 /*! Adds a parameter to the dialog. */
00337 void GlobalParametersDialog::addParameter(QString *labelText, QLineEdit *lineEdit, QVBoxLayout *vBox){
00338         lineEdit->setMaximumSize(50, 20);
00339         lineEdit->setValidator(paramValidator);
00340         QHBoxLayout *hBoxLayout = new QHBoxLayout();
00341         hBoxLayout->addSpacing(10);
00342         hBoxLayout->addWidget(new QLabel(*labelText, this));
00343         hBoxLayout->addWidget(lineEdit);
00344         hBoxLayout->addStretch(5);
00345         vBox->addLayout(hBoxLayout);
00346 }
00347 
00348 
00349 /*! Stores the parameters in the database. */
00350 void GlobalParametersDialog::storeParameters(){
00351         //Create the first part of the query
00352         QString queryString = "UPDATE GlobalParameters SET ";
00353         
00354         //First add double parameters to query
00355         for(map<const char*, QLineEdit*, charKeyCompare>::iterator iter = lineEditMap.begin(); iter != lineEditMap.end(); ++iter){
00356                 QString valueName(iter->first);
00357                 valueName += "_val";
00358                 QString valueText = iter->second->text();
00359                 queryString += valueName;
00360                 queryString += " = ";
00361                 queryString += valueText;
00362                 queryString += ", ";
00363         }
00364 
00365         //Now add any boolean values
00366         for(map<const char*, QCheckBox*, charKeyCompare>::iterator iter = checkBoxMap.begin(); iter != checkBoxMap.end(); ++iter){
00367                 QString valueName(iter->first);
00368                 valueName += "_val";
00369                 queryString += valueName;
00370                 queryString += " = ";
00371                 if(iter->second->isChecked())
00372                         queryString += "1";
00373                 else
00374                         queryString += "0";
00375                 queryString += ", ";
00376         }
00377 
00378         //Remove last comma if values have been added
00379         if(!lineEditMap.empty() || !checkBoxMap.empty())
00380                 queryString.remove(queryString.length() - 2, 2);
00381 
00382         //Execute query
00383         try{
00384                 Query query = networkDBInterface->getQuery();
00385                 query.reset();
00386                 query<<queryString;
00387                 query.execute();
00388         }
00389     catch (const BadQuery& er) {// Handle any query errors
00390                 cerr<<"ConnectionWidget: MYSQL QUERY EXCEPTION \""<<er.what()<<"\""<<endl;
00391                 QString errorString = "Bad query when storing global parameters: \"";
00392                 errorString += er.what();
00393                 errorString += "\"";
00394                 QMessageBox::critical( 0, "Global Parameters Error", errorString);
00395     }
00396     catch (const Exception& er) {// Catch-all for any other MySQL++ exceptions
00397         cerr<<"ConnectionWidget: MYSQL EXCEPTION \""<<er.what()<<"\""<<endl;
00398                 QString errorString = "Exception thrown when storing global parameters: \"";
00399                 errorString += er.what();
00400                 errorString += "\"";
00401                 QMessageBox::critical( 0, "Global Parameters Error", errorString);
00402     }
00403 }
00404 
00405 

Generated on Mon Sep 3 22:29:04 2007 for SpikeStream Application by  doxygen 1.4.4