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

ConnectionParameterViewer.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 "ConnectionParameterViewer.h"
00025 #include "Debug.h"
00026 
00027 //Qt includes
00028 #include <qmessagebox.h>
00029 #include <qpushbutton.h>
00030 #include <qlabel.h>
00031 
00032 //Other includes
00033 #include <string>
00034 #include <mysql++.h>
00035 using namespace std;
00036 using namespace mysqlpp;
00037 
00038 //Local debug defines
00039 //#define XML_PARSE_DEBUG
00040 
00041 
00042 /*! Constructor. */
00043 ConnectionParameterViewer::ConnectionParameterViewer(QWidget* parent, DBInterface* netDBInter, unsigned int cGrpID) : QDialog(parent){
00044         //Store references and information passed in constructor
00045         networkDBInterface = netDBInter;
00046         connGrpID = cGrpID;
00047 
00048         //Set the caption
00049         QString captionStr = "Connection Group Parameters [";
00050         captionStr += QString::number(connGrpID);
00051         captionStr += "]";
00052         this->setCaption(captionStr);
00053 
00054         //Create a vertical box to organise the layout
00055         mainVerticalBox = new QVBoxLayout(this, 5, 10, "vertical");
00056 
00057         //Error state is used to track any errors during start up
00058         errorState = false;
00059 
00060         //Make it non modal
00061         this->setModal(false);
00062         
00063         //Load up XML string from database and parse it if it is not null
00064         string paramXmlString("");
00065         try{
00066                 Query query = networkDBInterface->getQuery();
00067                 query.reset();
00068                 query<<"SELECT Parameters FROM ConnectionGroups WHERE ConnGrpID = "<<connGrpID;
00069                 Result paramRes = query.store();
00070                 Row paramRow(*paramRes.begin());//ConnGrpID is unique so will be only one row.
00071                 paramXmlString = (std::string) paramRow["Parameters"];
00072         }
00073     catch (const BadQuery& er) {// Handle any query errors
00074                 cerr<<"ConnectionParameterViewer: MYSQL QUERY EXCEPTION \""<<er.what()<<"\""<<endl;
00075                 QString errorString = "Bad query selecting connection parameters: \"";
00076                 errorString += er.what();
00077                 errorString += "\"";
00078                 QMessageBox::critical( 0, "Connection Parameter Error", errorString);
00079                 errorState = true;
00080                 return;
00081     }
00082     catch (const Exception& er) {// Catch-all for any other MySQL++ exceptions
00083         cerr<<"ConnectionParameterViewer: MYSQL EXCEPTION \""<<er.what()<<"\""<<endl;
00084                 QString errorString = "Exception thrown selecting network parameters: \"";
00085                 errorString += er.what();
00086                 errorString += "\"";
00087                 QMessageBox::critical( 0, "Connection Parameter Error", errorString);
00088                 errorState = true;
00089                 return;
00090     }
00091 
00092         QXmlSimpleReader xmlReader;
00093         QXmlInputSource xmlInput;
00094         xmlReader.setContentHandler(this);
00095         xmlReader.setErrorHandler(this);
00096 
00097         if(paramXmlString != "NULL" && paramXmlString != ""){
00098                 xmlInput.setData(paramXmlString);
00099                 xmlReader.parse(xmlInput);
00100                 if(parseError){
00101                         QMessageBox::critical( 0, "Error Parsing Connection Parameters", parseErrorString);
00102                         errorState = true;
00103                         return;
00104                 }
00105         }
00106         else{
00107                 currentHBox = new QHBoxLayout();
00108                 currentHBox->addWidget(new QLabel("No parameters stored.", this));
00109                 currentHBox->addStretch(5);
00110                 mainVerticalBox->addLayout(currentHBox);
00111         }
00112         mainVerticalBox->addStretch(5);
00113 
00114         //Set up buttons
00115         QHBoxLayout *buttonBox = new QHBoxLayout();
00116         QPushButton *okButton = new QPushButton("Ok", this);
00117         buttonBox->addWidget(okButton);
00118         connect (okButton, SIGNAL(clicked()), this, SLOT(okButtonPressed()));
00119 
00120         QPushButton *cancelButton = new QPushButton("Cancel", this);    
00121         buttonBox->addWidget(cancelButton);
00122         connect (cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonPressed()));
00123         mainVerticalBox->addLayout(buttonBox);
00124 
00125         //Make viewer visible           
00126         this->resize(400, 200);
00127 }
00128 
00129 
00130 /*! Destructor. */
00131 ConnectionParameterViewer::~ConnectionParameterViewer(){
00132         #ifdef MEMORY_DEBUG
00133                 cout<<"DESTROYING CONNECTION PARAMETER VIEWER"<<endl;
00134         #endif//MEMORY_DEBUG
00135 }
00136 
00137 
00138 //-------------------------------------------------------------------------
00139 //-------------------------   PUBLIC METHODS  -----------------------------
00140 //-------------------------------------------------------------------------
00141 
00142 bool ConnectionParameterViewer::loadError(){
00143         return errorState;
00144 }
00145 
00146 
00147 //-------------------------------------------------------------------------
00148 //------------------------- PROTECTED METHODS -----------------------------
00149 //-------------------------------------------------------------------------
00150 
00151 /*! Called when parser encounters characters. */
00152 bool ConnectionParameterViewer::characters(const QString& chars){
00153         #ifdef XML_PARSE_DEBUG
00154                 cout<<"Characters: "<<chars<<" in element "<<currentElement<<endl;
00155         #endif//XML_PARSE_DEBUG
00156         if(currentElement == "description"){
00157                 currentHBox->addWidget(new QLabel(chars, this));
00158         }
00159         else if(currentElement == "value"){
00160                 currentHBox->addWidget(new QLabel(chars, this));
00161                 currentHBox->addStretch(5);
00162                 mainVerticalBox->addLayout(currentHBox);
00163         }
00164         else if(currentElement == "min_delay"){
00165                 currentHBox = new QHBoxLayout();
00166                 currentHBox->addWidget(new QLabel("Delay range: Min", this));
00167                 currentHBox->addWidget(new QLabel(chars, this));
00168         }
00169         else if(currentElement == "max_delay"){
00170                 currentHBox->addWidget(new QLabel("Max", this));
00171                 currentHBox->addWidget(new QLabel(chars, this));
00172                 currentHBox->addStretch(5);
00173                 mainVerticalBox->addLayout(currentHBox);
00174         }
00175         else{
00176                 parseError = true;
00177                 parseErrorString += "Unrecognized element.";
00178                 cerr<<"ConnectionParameterViewer: UNRECOGNIZED ELEMENT"<<endl;
00179         }
00180         return true;
00181 }
00182 
00183 
00184 /*! Called when the parser encounters the end of an element. */
00185 bool ConnectionParameterViewer::endElement( const QString&, const QString&, const QString& qName){
00186         #ifdef XML_PARSE_DEBUG
00187                 cout<<"End element: "<<qName<<endl;
00188         #endif//XML_PARSE_DEBUG
00189         return true;
00190 }
00191 
00192 
00193 /*! Called when the parser generates an error. */
00194 bool ConnectionParameterViewer::error ( const QXmlParseException& parseEx){
00195         cerr<<"ConnectionParameterViewer: PARSING ERROR"<<endl;
00196         parseError = true;
00197         parseErrorString += parseEx.message();
00198         return true;
00199 }
00200 
00201 
00202 /*! Returns a default error string. */
00203 QString ConnectionParameterViewer::errorString (){
00204         return QString("ConnectionParameterViewer: Default error string");
00205 
00206 }
00207 
00208 
00209 /*! Called when the parser generates a fatal error. */
00210 bool ConnectionParameterViewer::fatalError ( const QXmlParseException& parseEx){
00211         cerr<<"ConnectionParameterViewer: PARSING FATAL ERROR"<<endl;
00212         parseError = true;
00213         parseErrorString += parseEx.message();
00214         return true;
00215 }
00216 
00217 
00218 /*! Called when parser reaches the start of the document. */
00219 bool ConnectionParameterViewer::startDocument(){
00220         parseError = false;
00221         parseErrorString = "";
00222         return true;
00223 }
00224 
00225 
00226 /*! Called when parser reaches the start of an element. */
00227 bool ConnectionParameterViewer::startElement(const QString&, const QString&, const QString& qName, const QXmlAttributes&){
00228         #ifdef XML_PARSE_DEBUG
00229                 cout<<"Start element: "<<qName<<endl;
00230         #endif//XML_PARSE_DEBUG
00231         currentElement = qName;
00232         if(currentElement == "parameter"){
00233                 currentHBox = new QHBoxLayout();
00234         }
00235         return true;
00236 }
00237 
00238 
00239 /*! Called when the parser generates a warning. */
00240 bool ConnectionParameterViewer::warning ( const QXmlParseException& ){
00241         cerr<<"ConnectionParameterViewer: PARSING WARNING"<<endl;
00242         return true;
00243 }
00244 
00245 
00246 //-------------------------------------------------------------------------
00247 //-------------------------   PRIVATE SLOTS   -----------------------------
00248 //-------------------------------------------------------------------------
00249 
00250 /*! Hides the dialog. */
00251 void ConnectionParameterViewer::cancelButtonPressed(){
00252         this->accept();
00253 }
00254 
00255 
00256 /*! Hides the dialog. */
00257 void ConnectionParameterViewer::okButtonPressed(){
00258         this->accept();
00259 }
00260 
00261 
00262 

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