Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

SIG_ExperimentListView.cpp

00001 #include <qstringlist.h>
00002 #include <qfiledialog.h>
00003 #include <qmessagebox.h>
00004 #include <qfileinfo.h>
00005 
00006 #include "SIGEL_MasterGUI/SIG_RenameDialog.h"
00007 #include "SIGEL_MasterGUI/SIG_ExperimentListView.h"
00008 #include "SIGEL_MasterGUI/SIG_ExperimentItem.h"
00009 
00010 #include "SIGEL_GP/SIG_GUIGPManager.h"
00011 
00012 #include <SIGEL_Tools/SIG_IO.h>
00013 
00014 namespace SIGEL_MasterGUI
00015 {
00016 
00017   SIG_ExperimentListView::SIG_ExperimentListView( QWidget * parent, const char * name, QWidgetStack *theWidgetStack ) : QListView( parent, name ), widgetStack( theWidgetStack ), numberOfExperiments(1)
00018 {
00019   addColumn( "Experiments" );
00020   QObject::connect( this,
00021                     SIGNAL( rightButtonClicked ( QListViewItem *, const QPoint &, int ) ),
00022                     SLOT( slotRightButtonClicked( QListViewItem *, const QPoint &, int ) ) );
00023   QObject::connect( this,
00024                     SIGNAL( selectionChanged( QListViewItem * ) ),
00025                     SLOT( slotSelectionChanged( QListViewItem * ) ));
00026   experimentListViewMenu = new QPopupMenu( this, "SIG_ExperimentListViewPopupMenu" );
00027 
00028   setRootIsDecorated( TRUE );
00029   setSorting( -1 );
00030   
00031   experimentDict.setAutoDelete( true );
00032 };
00033 
00034 SIG_ExperimentListView::~SIG_ExperimentListView(){};
00035 
00036 void SIG_ExperimentListView::slotNewExperiment()
00037 {
00038   QString project;
00039   project.setNum( numberOfExperiments++ );
00040   project.prepend("Experiment-");
00041   project.append(".exp");
00042   if( experimentExists( project ) )
00043     project = getAlternativeName( project );
00044   SIG_ExperimentItem *theNewItem = new SIG_ExperimentItem( this, project );
00045   SIG_Experiment *theNewExperiment = new SIG_Experiment( project, widgetStack, theNewItem );
00046   QObject::connect( theNewExperiment,
00047                     SIGNAL( signalEvolutionNotRunning( bool ) ),
00048                     this,
00049                     SIGNAL( evolutionNotRunning( bool ) ) );
00050   experimentDict.insert( project , theNewExperiment );
00051   emit isNotEmpty( true );
00052   this->setSelected( theNewItem, true );
00053 };
00054 
00055 void SIG_ExperimentListView::slotRenameExperiment()
00056 {
00057   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00058   if( theExperiment )
00059     {
00060       SIG_RenameDialog renameDialog( this, 0, true );
00061       QString oldName = currentlySelectedExperimentName();
00062       renameDialog.setCaption( "Rename " + oldName );
00063       renameDialog.lineeditNewName->setText( oldName );
00064       renameDialog.lineeditNewName->selectAll();
00065       renameDialog.lineeditNewName->setFocus();
00066       switch( renameDialog.exec() )
00067         {
00068         case QDialog::Accepted:
00069           QString newName = renameDialog.lineeditNewName->text();
00070           // we got a real new name so lets do it
00071           if( newName != QString::null && newName != oldName && !experimentExists( newName ) )
00072             {
00073               // the string is not empty, was really changed and there is no other experiment with that name, so lets go
00074 
00075               // append .exp if it wasn't entered
00076               if( newName.right( 4 ) != ".exp")
00077                 newName.append( ".exp" );
00078 
00079               // disable autodelete to keep the SIG_Experiment object
00080               experimentDict.setAutoDelete( false );
00081               experimentDict.remove( oldName );
00082 
00083               theExperiment->setName( newName );
00084               // insert the experiment into the experiment dictionary
00085               experimentDict.insert( newName, theExperiment );
00086       
00087               // enable autodelete
00088               experimentDict.setAutoDelete( true );
00089             } // close if
00090           else
00091             QMessageBox::information( this, "Error...", "Either you have entered no name or there is\nalready an experiment under that name." );
00092           break;
00093         } // close switch
00094     } // close if
00095 };
00096 
00097 void SIG_ExperimentListView::slotDeleteExperiment()
00098 {
00099   // Find out which experiment is selected...
00100   QListViewItem *current = currentItem();
00101   if(current)
00102     {
00103       while(current->parent() != 0)
00104         {
00105           current = current->parent();
00106         }
00107       QString name = current->text(0);
00108       // lets ask first if we really want to do this
00109       
00110       switch( QMessageBox::warning( this, "Do you really...", "Do you really want to delete\n"
00111                                     "the experiment " + name + "?", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No) )
00112         {
00113         case QMessageBox::Yes:
00114           experimentDict.remove( name );
00115           takeItem(current);
00116           delete current;
00117           setSelected( firstChild(), true );
00118           break;
00119         };
00120     }
00121   else
00122     {
00123       QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00124     };
00125   if( this->childCount() == 0 )
00126     {
00127       emit isNotEmpty( false );
00128     }
00129   else
00130     {
00131       emit isNotEmpty( true );
00132     }
00133 };
00134 
00135 void SIG_ExperimentListView::slotLoadExperiment()
00136 {
00137   QStringList filesToOpen = QFileDialog::getOpenFileNames( "Experiment Files (*.exp);;All Files (*)", QString::null, 0, "filesToOpen", "Load Experiments..." );
00138   if( !filesToOpen.isEmpty() )
00139     {
00140       for( int i = 0; i < filesToOpen.count(); i++)
00141         {
00142           QString absFileName = filesToOpen[i];
00143           int slashPosition = absFileName.findRev( "/" );
00144           QString fileName = absFileName.right( absFileName.length() - (slashPosition + 1) );
00145           if ( experimentExists( fileName ) )
00146             fileName = getAlternativeName( fileName );
00147           SIG_ExperimentItem *theNewItem = new SIG_ExperimentItem( this, fileName );
00148 
00149           // lets test something
00150           theNewItem->setOpen(false);
00151           theNewItem->setSelectable( false );
00152 
00153           SIG_Experiment *theNewExperiment = new SIG_Experiment( fileName, widgetStack, theNewItem );
00154           QObject::connect( theNewExperiment,
00155                             SIGNAL( signalEvolutionNotRunning( bool ) ),
00156                             this,
00157                             SIGNAL( evolutionNotRunning( bool ) ) );
00158           
00159           QFile file( absFileName );
00160           if( file.open(IO_ReadOnly) )
00161             {
00162               QTextStream theStream( &file );
00163               theNewExperiment->gpExperiment.loadExperiment( theStream );
00164               theNewExperiment->getAllOutOfExperiment();
00165             }
00166           else
00167             {
00168               QMessageBox::warning( 0, "File could not be opened...", "The file " + fileName + "could not be opened for reading." );
00169             }
00170           experimentDict.insert( fileName , theNewExperiment );
00171           emit isNotEmpty( true );
00172 
00173           // lets test something 2
00174           theNewItem->setSelectable( true );
00175           theNewItem->setOpen(true);
00176           
00177         } // for each filename end
00178       setSelected( firstChild(), true );
00179     };
00180 };
00181 
00182 void SIG_ExperimentListView::slotSaveExperiment()
00183 {
00184   QString currentExperiment = currentlySelectedExperimentName();
00185   if (currentExperiment != QString::null )
00186     {
00187       SIG_Experiment *theExperiment = getByExperimentName( currentExperiment );
00188       theExperiment->putAllIntoExperiment();
00189       QString fileName = QFileDialog::getSaveFileName( theExperiment->getName(), "Experiment Files (*.exp);;All Files (*)", 0, "ExperimentSaveDialog", "Save Experiment...");
00190       if( !fileName.isEmpty() )
00191         {
00192           if( fileName.right(4) != ".exp" )
00193             fileName.append( ".exp" );
00194           QFile file( fileName );
00195           if( file.exists() )
00196             switch( QMessageBox::warning( 0, "File exists...", "The file " + file.name() + " exists!\nDo you want to overwrite?", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape ) )
00197               {
00198               case QMessageBox::Yes:
00199                 if( file.open(IO_WriteOnly) )
00200                   {
00201                     QTextStream theStream( &file );
00202                     theExperiment->gpExperiment.saveExperiment( theStream );
00203                   }
00204                 file.close();
00205 
00206                 QFileInfo fileInfo( fileName );
00207 
00208                 experimentDict.setAutoDelete( false );
00209                 experimentDict.remove( currentExperiment );
00210 
00211                 theExperiment->setName( fileInfo.fileName() );
00212 
00213                 experimentDict.insert( fileInfo.fileName(), theExperiment );
00214       
00215                 experimentDict.setAutoDelete( true );
00216 
00217                 break;
00218               }
00219           else
00220             {
00221               if( file.open(IO_WriteOnly) )
00222                 {
00223                   QTextStream theStream( &file );
00224                   theExperiment->gpExperiment.saveExperiment( theStream );
00225                 }
00226               file.close();
00227 
00228               QFileInfo fileInfo( fileName );
00229 
00230               experimentDict.setAutoDelete( false );
00231               experimentDict.remove( currentExperiment );
00232 
00233               theExperiment->setName( fileInfo.fileName() );
00234 
00235               experimentDict.insert( fileInfo.fileName(), theExperiment );
00236       
00237               experimentDict.setAutoDelete( true );
00238             } // file exists?
00239         } // filename not empty
00240     }
00241   else
00242     {
00243       QMessageBox::warning( this, "No experiment selected...", "Currently there is no experiment selected!" );
00244     }
00245 };
00246 
00247 void SIG_ExperimentListView::slotRightButtonClicked( QListViewItem * theItem, const QPoint & thePoint, int inside )
00248 {
00249   if (inside==-1)
00250     experimentListViewMenu->popup( thePoint ); // the click was outside
00251   else
00252     {
00253       QString option = theItem->text(0);
00254       QString experimentName;
00255       while( theItem->parent() != 0)
00256         {
00257           theItem = theItem->parent();
00258         }
00259       experimentName = theItem->text(0);
00260       experimentDict[ experimentName ]->slotRightClick( option, thePoint );
00261     }
00262 };
00263 
00264 
00265 void SIG_ExperimentListView::slotSelectionChanged( QListViewItem * theItem )
00266 {
00267   if(theItem)
00268     {
00269       QString option = theItem->text(0);
00270       QString experimentName;
00271       while( theItem->parent() != 0)
00272         {
00273           theItem = theItem->parent();
00274         }
00275       experimentName = theItem->text(0);
00276       experimentDict[ experimentName ]->slotSelectionChanged( option );
00277       SIGEL_GP::SIG_GUIGPManager *manager = experimentDict[ experimentName ]->gpManager;
00278       if( manager )
00279         {
00280           if( manager->running() )
00281             emit evolutionNotRunning( false );
00282           else
00283             emit evolutionNotRunning( true );
00284         }
00285       else
00286         emit evolutionNotRunning( true );
00287       experimentDict[ experimentName ]->putAllIntoExperiment();
00288     }
00289 };
00290 
00291 SIG_Experiment* SIG_ExperimentListView::getByExperimentName( QString name )
00292 {
00293   if( name != QString::null )
00294     return experimentDict[ name ];
00295 };
00296 
00297 bool SIG_ExperimentListView::experimentExists( QString name )
00298 {
00299   if( experimentDict[ name ] )
00300     return true;
00301   else
00302     return false;
00303 };
00304 
00305 QString SIG_ExperimentListView::getAlternativeName( QString existingName )
00306 {
00307   int number = 1;
00308   QString alternativeName = existingName;
00309   while ( experimentExists( alternativeName ) )
00310     {
00311       alternativeName = "(" + QString::number( number++ ) + ")" + existingName;
00312     }
00313   return alternativeName; // has to be implemented right!
00314 };
00315 
00316 SIG_Experiment* SIG_ExperimentListView::currentlySelectedExperiment()
00317 {
00318   QString experimentName = currentlySelectedExperimentName();
00319   if( experimentName != QString::null )
00320     return experimentDict[ experimentName ];
00321   else
00322     return 0;
00323 };
00324 
00325 QString SIG_ExperimentListView::currentlySelectedExperimentName()
00326 {
00327   QListViewItem *theCurrentItem = currentItem();
00328   if (theCurrentItem)
00329     {
00330       while( theCurrentItem->parent() != 0)
00331         {
00332           theCurrentItem = theCurrentItem->parent();
00333         }
00334       return theCurrentItem->text( 0 );
00335     }
00336   else
00337     return QString::null;
00338 };
00339 
00340 void SIG_ExperimentListView::selectItem( QString label )
00341 {
00342   QListViewItem *theItem = currentItem();
00343   if( theItem )
00344     {
00345       while( theItem->parent() != 0)
00346         {
00347           theItem = theItem->parent();
00348         }
00349 
00350       for(; theItem; theItem = theItem->itemBelow() )
00351         {
00352           if( theItem->text( 0 ) == label )
00353             {
00354               setSelected( theItem, true );
00355               break;
00356             }
00357         }
00358 
00359       /* QListViewItemIterator it( theItem );
00360        * for( ; it.current(); it++ )
00361        *        {
00362        *          if( it.current()->text( 0 ) == label )
00363        *    {
00364        *              setSelected( it.current(), true );
00365        *              break;
00366        *            }
00367        *        } */
00368     }
00369   else
00370     {
00371       QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00372     }
00373 };
00374 
00375 void SIG_ExperimentListView::slotShowGPParameters()
00376 {
00377   selectItem( "GP-Parameters" );
00378 };
00379 
00380 void SIG_ExperimentListView::slotShowSimulationParameters()
00381 {
00382   selectItem( "Simulation-Parameters" );
00383 };
00384 
00385 void SIG_ExperimentListView::slotShowLanguageParameters()
00386 {
00387   selectItem( "Language-Parameters" );
00388 };
00389 
00390 void SIG_ExperimentListView::slotShowRobot()
00391 {
00392   selectItem( "Robot" );
00393 };
00394 
00395 void SIG_ExperimentListView::slotShowEnvironment()
00396 {
00397   selectItem( "Environment" );
00398 };
00399   
00400 void SIG_ExperimentListView::slotShowIndividuals()
00401 {
00402   selectItem( "Individuals" );
00403 };
00404 
00405 void SIG_ExperimentListView::slotGPParametersImport()
00406 {
00407   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00408   if( theExperiment )
00409     {
00410       slotShowGPParameters();
00411       theExperiment->slotGPParameterImport();
00412     }
00413   else
00414     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00415 };
00416   
00417 void SIG_ExperimentListView::slotSimulationParametersImport()
00418 {
00419   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00420   if( theExperiment )
00421     {
00422       slotShowSimulationParameters();
00423       theExperiment->slotSimulationParameterImport();
00424     }
00425   else
00426     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00427 };
00428 
00429 void SIG_ExperimentListView::slotRobotImport()
00430 {
00431   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00432   if( theExperiment )
00433     {
00434       slotShowRobot();
00435       theExperiment->slotRobotImport();
00436     }
00437   else
00438     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00439 };
00440 
00441 void SIG_ExperimentListView::slotLanguageParametersImport()
00442 {
00443   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00444   if( theExperiment )
00445     {
00446       slotShowLanguageParameters();
00447       theExperiment->slotLanguageParameterImport();
00448     }
00449   else
00450     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00451 };
00452 
00453 void SIG_ExperimentListView::slotPopulationImport()
00454 {
00455   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00456   if( theExperiment )
00457     {
00458       slotShowIndividuals();
00459       theExperiment->slotPopulationImport();
00460     }
00461   else
00462     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00463 };
00464 
00465 void SIG_ExperimentListView::slotEnvironmentImport()
00466 {
00467   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00468   if( theExperiment )
00469     {
00470       slotShowEnvironment();
00471       theExperiment->slotEnvironmentImport();
00472     }
00473   else
00474     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00475 };
00476 
00477 void SIG_ExperimentListView::slotGPParametersExport()
00478 {
00479   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00480   if( theExperiment )
00481     {
00482       slotShowGPParameters();
00483       theExperiment->slotGPParameterExport();
00484     }
00485   else
00486     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00487 };
00488 
00489 void SIG_ExperimentListView::slotSimulationParametersExport()
00490 {
00491   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00492   if( theExperiment )
00493     {
00494       slotShowSimulationParameters();
00495       theExperiment->slotSimulationParameterExport();
00496     }
00497   else
00498     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00499 };
00500 
00501 void SIG_ExperimentListView::slotLanguageParametersExport()
00502 {
00503   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00504   if( theExperiment )
00505     {
00506       slotShowLanguageParameters();
00507       theExperiment->slotLanguageParameterExport();
00508     }
00509   else
00510     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00511 };
00512 
00513 void SIG_ExperimentListView::slotPopulationExport()
00514 {
00515   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00516   if( theExperiment )
00517     {
00518       slotShowIndividuals();
00519       theExperiment->slotPopulationExport();
00520     }
00521   else
00522     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00523 };
00524 
00525 void SIG_ExperimentListView::slotEnvironmentExport()
00526 {
00527   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00528   if( theExperiment )
00529     {
00530       slotShowEnvironment();
00531       theExperiment->slotEnvironmentExport();
00532     }
00533   else
00534     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00535 };
00536 
00537 void SIG_ExperimentListView::slotGNUPlotExport()
00538 {
00539   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00540   if( theExperiment )
00541     {
00542       theExperiment->slotGNUPlotExport();
00543     }
00544   else
00545     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00546 };
00547 
00548 void SIG_ExperimentListView::slotAddIndividuals()
00549 {
00550   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00551   if( theExperiment )
00552     {
00553       slotShowIndividuals();
00554       theExperiment->allIndividualsView->slotAddIndividuals();
00555     }
00556   else
00557     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00558 };
00559 
00560 void SIG_ExperimentListView::slotDeleteIndividuals()
00561 {
00562   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00563   if( theExperiment )
00564     {
00565       slotShowIndividuals();
00566       theExperiment->allIndividualsView->slotDeleteIndividuals();
00567     }
00568   else
00569     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00570 };
00571 
00572 void SIG_ExperimentListView::slotResetIndividuals()
00573 {
00574   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00575   if( theExperiment )
00576     {
00577       slotShowIndividuals();
00578       theExperiment->allIndividualsView->slotResetPool();
00579     }
00580   else
00581     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00582 };
00583 
00584 void SIG_ExperimentListView::slotVisualizeIndividuals()
00585 {
00586   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00587   if( theExperiment )
00588     {
00589       slotShowIndividuals();
00590       theExperiment->allIndividualsView->slotVisualize();
00591     }
00592   else
00593     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00594 };
00595 
00596 void SIG_ExperimentListView::slotProgramExport()
00597 {
00598   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00599   if( theExperiment )
00600     {
00601       slotShowIndividuals();
00602       theExperiment->allIndividualsView->slotExportProgram();
00603     }
00604   else
00605     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00606 };
00607 
00608 void SIG_ExperimentListView::slotIndividualExport()
00609 {
00610   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00611   if( theExperiment )
00612     {
00613       slotShowIndividuals();
00614       theExperiment->allIndividualsView->slotExportIndividual();
00615     }
00616   else
00617     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00618 };
00619 
00620 void SIG_ExperimentListView::slotProgramImport()
00621 {
00622   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00623   if( theExperiment )
00624     {
00625       slotShowIndividuals();
00626       theExperiment->allIndividualsView->slotImportProgram();
00627     }
00628   else
00629     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00630 };
00631 
00632 void SIG_ExperimentListView::slotIndividualImport()
00633 {
00634   SIG_Experiment *theExperiment = currentlySelectedExperiment();
00635   if( theExperiment )
00636     {
00637       slotShowIndividuals();
00638       theExperiment->allIndividualsView->slotImportIndividual();
00639     }
00640   else
00641     QMessageBox::information( this, "There is no experiment selected...", "Currently there is no experiment selected." );
00642 };
00643 
00644 } // close namespace
00645 

Generated at Mon Sep 3 01:32:21 2001 for PG 368 - SIGEL by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000