Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

omgexperimentselector.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Tim Sutton                                      *
00003  *   tim@linfiniti.com                                                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 #include "omgexperimentselector.h"
00011 #include <omgui.h>
00012 
00013 //qt includes
00014 #include <QApplication>
00015 #include <QCoreApplication>
00016 #include <QDir>
00017 #include <QDomDocument>
00018 #include <QDomElement>
00019 #include <QFileInfo>
00020 #include <QLabel>
00021 #include <QListView>
00022 #include <QMessageBox>
00023 #include <QPixmap>
00024 #include <QProgressBar>
00025 #include <QSettings>
00026 #include <QString>
00027 #include <QStringList>
00028 #include <QXmlStreamReader> //efficient xml parser not using dom or sax
00029 #include <QXmlStreamAttributes>
00030 //standard includes
00031 #include <iostream>
00032 
00033 
00034 OmgExperimentSelector::OmgExperimentSelector( QWidget* parent, Qt::WFlags fl )
00035  : QDialog(parent,fl)
00036 {
00037   setupUi(this);
00038 
00039   QSettings mySettings;
00040   QString myWorkDir = mySettings.value("dataDirs/dataDir",QDir::homePath() + QString("/.omgui/modelOutputs/")).toString();
00041   
00042   QFileInfo myFileInfo(myWorkDir);
00043   if ( (myFileInfo.exists() && !myWorkDir.isEmpty()) )
00044   {
00045     updateExperimentList();
00046   }
00047   else
00048   {
00049     QMessageBox::warning( this,tr("openModeller Desktop"),tr("No existing experiments could be found in your work directory") + " (" + myWorkDir + ")");
00050   }
00051 }
00052 
00053 OmgExperimentSelector::~OmgExperimentSelector()
00054 {
00055 
00056 }
00057 
00058 void OmgExperimentSelector::updateExperimentList()
00059 {
00060   QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
00061   QStringList myList = Omgui::getExperimentsList();
00062   QStringListIterator myIterator(myList);
00063   while (myIterator.hasNext())
00064   {
00065     QString myFileName = myIterator.next();
00066     QFile myFile( myFileName );
00067     if ( !myFile.open( QIODevice::ReadOnly ) )
00068     {
00069       continue;
00070     }
00071     else
00072     {
00073 #ifdef WIN32
00074       //there is currently an encoding problem reading experiments
00075       //that is win32 specific ... this is a temporary workaroumd
00076       QString myString = myFile.readLine();
00077       myString += "</Experiment>";
00078       //qDebug(myString.toLocal8Bit());
00079       QDomDocument myDocument("mydocument");
00080       QString myError;
00081       int myLine=0;
00082       int myCol=0;
00083       bool myResult = myDocument.setContent(myString,&myError,&myLine,&myCol);
00084       if (!myResult)
00085       {
00086         qDebug("Error setting document content in OmgExperimentSelector:");
00087         qDebug(myError.toLocal8Bit());
00088         qDebug("Line " + QString::number(myLine).toLocal8Bit());
00089         qDebug("Col " + QString::number(myCol).toLocal8Bit());
00090       }
00091       QDomElement myRootElement = myDocument.firstChildElement("Experiment");
00092       if (!myRootElement.isNull())
00093       {
00094         QString myName = Omgui::xmlDecode(myRootElement.attribute("Name"));
00095         QString myGuid = myRootElement.attribute("Guid");
00096         QListWidgetItem * mypItem = new QListWidgetItem(myName,lstExperiments);
00097         mypItem->setData(Qt::UserRole,myGuid);
00098         QIcon myIcon;
00099         myIcon.addFile(":/filenewExperiment.png");
00100         mypItem->setIcon(myIcon);
00101       }
00102 
00103 
00104 
00105 #else
00106       // Parse the document using QXmlStreamReader which is more efficient
00107       // that dom or sax (see qt docs)
00108       //loop till we find an experiment attribute, get its
00109       //name and guid attribue then exit the parse
00110       bool myExperimentFoundFlag = false;
00111       QXmlStreamReader myReader;
00112       myReader.setDevice(&myFile);
00113       while (!myReader.atEnd() && !myExperimentFoundFlag && !myReader.hasError()) 
00114       {
00115         QXmlStreamReader::TokenType myToken = myReader.readNext();
00116         if (myToken == QXmlStreamReader::StartElement)
00117         {
00118           if (myReader.name() == "Experiment")
00119           {
00120             QString myName = myReader.attributes().value("Name").toString();
00121             QListWidgetItem * mypItem = new QListWidgetItem(myName,lstExperiments);
00122             mypItem->setData(Qt::UserRole,myReader.attributes().value("Guid").toString());
00123             //display an icon - at the moment I use teh same icon
00124             //for all items - later I will implement that show experiment
00125             //status (complete, incomplete etc)
00126             QIcon myIcon;
00127             myIcon.addFile(":/filenewExperiment.png");
00128             mypItem->setIcon(myIcon);
00129             myExperimentFoundFlag = true;
00130           }
00131         }
00132       }//end of xml stream reader loop
00133       if (myReader.hasError()) 
00134       {
00135         // do error handling
00136         qDebug("Error loading experiment for experiment selector:");
00137         qDebug(myFileName);
00138         qDebug(myReader.errorString());
00139         qDebug("Line: " + QString::number(myReader.lineNumber()).toLocal8Bit());
00140         qDebug("Column: " + QString::number(myReader.columnNumber()).toLocal8Bit());
00141         qDebug("Char Offset: " + QString::number(myReader.characterOffset()).toLocal8Bit());
00142       }//has error
00143 #endif
00144       myFile.close();
00145     }//end of file open test
00146   }//end of myList Iterator
00147   QApplication::restoreOverrideCursor();
00148 }
00149 
00150 void OmgExperimentSelector::on_pbnOK_clicked()
00151 {
00152   OmgExperiment * mypExperiment = new OmgExperiment();
00153   QSettings mySettings;
00154   QString myFileName;
00155   QString myWorkDir = mySettings.value("dataDirs/dataDir",QDir::homePath()+QDir::separator()+".omgui" ).toString()
00156                     + QString("/modelOutputs/");
00157   if (lstExperiments->currentRow() >=0)
00158   {
00159     //add the guid of the selected item to determin teh actual xml file name
00160     QString myGuid = lstExperiments->currentItem()->data(Qt::UserRole).toString();
00161     QString myDirName = lstExperiments->currentItem()->text();
00162     if (!myGuid.isEmpty())
00163     {
00164       myFileName = myWorkDir + myDirName + QDir::separator() + myGuid + ".xml";
00165       myFileName = QDir::toNativeSeparators (myFileName);
00166       if (QFile::exists(myFileName))
00167       {
00168         QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
00169         mypExperiment->fromXmlFile(myFileName);
00170         mypExperiment->setWorkDir(myWorkDir + QDir::separator() + myDirName + QDir::separator());
00171         QApplication::restoreOverrideCursor();
00172         emit loadExperiment(mypExperiment);
00173         accept();
00174       }
00175       else
00176       {
00177         QMessageBox::information(window(),tr("Error"), tr("Error opening the experiment file : ") + 
00178           "\n" + myFileName + "\n" + tr("(File does not exist)."));
00179       }
00180     }
00181   }
00182   else
00183   {
00184     //if any of the above didnt work just close
00185     close();
00186   }
00187 }
00188 
00189 
00190 void OmgExperimentSelector::on_pbnCancel_clicked()
00191 {
00192   close();
00193 }
00194 
00195 
00196 void OmgExperimentSelector::refresh()
00197 {
00198   QCoreApplication::processEvents();
00199 }
00200 

Generated on Mon Apr 28 15:07:13 2008 for openModellerDesktop by  doxygen 1.4.1-20050210