QtFileDialog.cxx

Go to the documentation of this file.
00001 
00012 #ifdef HAVE_CONFIG_H
00013 // for have root cfitsio
00014 #include "config.h"
00015 #endif
00016 
00017 #include "QtFileDialog.h"
00018 
00019 #include "CanvasWindow.h"
00020 #include "WindowController.h"
00021 #include "Inspector.h"
00022 #include "ListDialog.h"
00023 
00024 #include "datasrcs/DataSourceController.h"
00025 #include "datasrcs/DataSource.h"
00026 #include "datasrcs/NTupleController.h"
00027 
00028 #ifdef HAVE_CFITSIO
00029 #include "fits/FitsController.h"
00030 #endif
00031 
00032 #ifdef HAVE_ROOT
00033 #include "root/RootController.h"
00034 #endif
00035 
00036 #include <qmessagebox.h>
00037 
00038 
00039 using std::exception;
00040 using std::runtime_error;
00041 using std::string;
00042 using std::vector;
00043 
00044 using namespace hippodraw;
00045 
00046 /* Use `=' syntax so that Doxygen picks it up. */
00047 const string QtFileDialog::s_doc_suffix = ".hpo";
00048 const string QtFileDialog::s_data_suffix = ".tnt";
00049 string QtFileDialog::s_open_filter = "";
00050 
00051 QtFileDialog::
00052 QtFileDialog ()
00053 {
00054 }
00055 
00056 const string &
00057 QtFileDialog::
00058 createOpenFilter ()
00059 {
00060   const string doc ( s_doc_suffix );
00061   const string xml ( ".xml" );
00062   const string tnt ( ".tnt" );
00063 
00064 #if __GNUC__ < 3
00065 #define STRING_CLEAR_DEFECT
00066 #endif
00067 
00068 #ifdef STRING_CLEAR_DEFECT
00069   s_open_filter.erase();
00070 #else
00071   s_open_filter.clear();
00072 #endif
00073 
00074   s_open_filter = "All files (";
00075   s_open_filter += "*";
00076   s_open_filter += ");;";
00077 
00078   s_open_filter += "Documents (*";
00079   s_open_filter += doc;
00080   s_open_filter += " *" + xml;
00081 
00082   s_open_filter += ");;Text ntuples (*";
00083   s_open_filter += tnt;
00084   s_open_filter += ")";
00085 
00086 #ifdef HAVE_ROOT
00087   const string root ( ".root" );
00088   s_open_filter += ";;ROOT ntuples(*";
00089   s_open_filter += root;
00090   s_open_filter += ")";
00091 #endif
00092 
00093 #ifdef HAVE_CFITSIO
00094   const string fits ( ".fits" );
00095   s_open_filter += ";;FITS ntuples(*";
00096   s_open_filter += fits;
00097   s_open_filter += ")";
00098 
00099   const string gzfits ( ".fits.gz" );
00100   s_open_filter += ";;Compressed FITS ntuples(*";
00101   s_open_filter += gzfits;
00102   s_open_filter += ")";
00103 #endif
00104 
00105   return s_open_filter;
00106 }
00107 
00108 const string &
00109 QtFileDialog::
00110 createExportFilter ()
00111 {
00112 #ifdef STRING_CLEAR_DEFECT
00113   s_open_filter.erase();
00114 #else
00115   s_open_filter.clear();
00116 #endif
00117 
00118   s_open_filter += "Text ntuples (*";
00119   s_open_filter += ".tnt";
00120   s_open_filter += ")";
00121 
00122 #ifdef HAVE_CFITSIO
00123   const string fits ( ".fits" );
00124   s_open_filter += ";;FITS ntuples(*";
00125   s_open_filter += fits;
00126   s_open_filter += ")";
00127 
00128   const string gzfits ( ".fits.gz" );
00129   s_open_filter += ";;Compressed FITS ntuples(*";
00130   s_open_filter += gzfits;
00131   s_open_filter += ")";
00132 #endif
00133 
00134   return s_open_filter;
00135 }
00136 
00137 
00138 const string &
00139 QtFileDialog::
00140 createBrowseFilter ()
00141 {
00142   const string doc ( s_doc_suffix );
00143   const string xml ( ".xml" );
00144   const string tnt ( ".tnt" );
00145 
00146 #if __GNUC__ < 3
00147 #define STRING_CLEAR_DEFECT
00148 #endif
00149 
00150 #ifdef STRING_CLEAR_DEFECT
00151   s_open_filter.erase();
00152 #else
00153   s_open_filter.clear();
00154 #endif
00155   s_open_filter = "Documents (*";
00156   s_open_filter += doc;
00157   s_open_filter += " *" + xml;
00158   s_open_filter += ")";
00159 
00160   s_open_filter += ";;All files (";
00161   s_open_filter += "*.*";
00162   s_open_filter += ")";
00163 
00164   return s_open_filter;
00165 }
00166 
00167 const std::string &
00168 QtFileDialog::
00169 getDocSuffix ()
00170 {
00171   return s_doc_suffix;
00172 }
00173 
00174 const std::string &
00175 QtFileDialog::
00176 getTextSuffix ()
00177 {
00178   return s_data_suffix;
00179 }
00180 
00181 bool
00182 QtFileDialog::
00183 isDocSuffix ( const std::string & suffix )
00184 {
00185   return suffix == s_doc_suffix || suffix == ".xml";
00186 }
00187 
00188 bool
00189 QtFileDialog::
00190 isTextSuffix ( const std::string & suffix )
00191 {
00192   return suffix == s_data_suffix;
00193 }
00194 
00195 bool
00196 QtFileDialog::
00197 isRootSuffix ( const std::string & suffix )
00198 {
00199   return suffix == ".root";
00200 }
00201 
00202 bool
00203 QtFileDialog::
00204 isFitsSuffix ( const std::string & suffix )
00205 {
00206   return suffix == ".fits";
00207 }
00208 
00209 bool
00210 QtFileDialog::
00211 isZippedFitsSuffix ( const std::string & suffix)
00212 {
00213   return suffix == ".gz";
00214 }
00215 
00216 std::string
00217 QtFileDialog::
00218 getExportTupleFilename( QWidget * widget )
00219 {
00220   const string & filter = createExportFilter ();
00221   QString filename;
00222 
00223   while (true)
00224     {
00225       string tmp;
00226       QString extname = 0;
00227       filename 
00228 #if QT_VERSION < 0x040000
00229         = QFileDialog::getSaveFileName ( QString::null, // starting directory
00230 #else
00231         = Q3FileDialog::getSaveFileName ( QString::null, // starting directory
00232 #endif
00233                                          filter.c_str(),
00234                                          widget, // parent
00235                                          "export", // name
00236                                          "Save data source to file ...",
00237                                          & extname ); //selected filter
00238 
00239      if ( filename == QString::null ) return tmp;
00240 
00241       if (( filename.endsWith ( ".tnt" ) == false ) &&
00242           ( filename.endsWith ( ".fits" ) == false ) &&
00243           ( filename.endsWith ( ".fits.gz" )==false ))
00244                                          
00245         {
00246           // Add default suffix if not end with .tnt or .fits.
00247           if ( extname == "FITS ntuples(*.fits)" )
00248             {
00249               filename+=".fits";
00250             }
00251           else if (extname == "Compressed FITS ntuples(*.fits.gz)")
00252             {
00253               filename+=".fits.gz";
00254             }
00255           else
00256             { 
00257               filename+=".tnt";
00258             }
00259         }     
00260 
00261       QFileInfo info ( filename );
00262       filename = info.absFilePath();
00263       
00264       bool yes = info.exists ();
00265       if ( yes == false ) break;
00266       
00267       string message ( "File exists. \n\n" );
00268       message += "Over write existing file?";
00269       int result = QMessageBox::warning ( widget, // parent
00270                                           "Warning", // caption
00271                                           message.c_str (), //
00272                                           QMessageBox::Yes,
00273                                           QMessageBox::No );
00274       if ( result == QMessageBox::Yes ) break;
00275     }
00276 
00277   string name = filename.latin1();
00278 
00279   return name;
00280 }
00281       
00282 
00283 const string &
00284 QtFileDialog::
00285 createDocumentFilter ()
00286 {
00287   s_open_filter.erase();
00288   s_open_filter = "Doucuments (*";
00289   s_open_filter += s_doc_suffix.c_str();
00290   s_open_filter += ")";
00291 
00292   return s_open_filter;
00293 }
00294 
00295 void
00296 QtFileDialog::
00297 openTextTuple ( const std::string & filename )
00298 {
00299   NTupleController * controller = NTupleController::instance ();
00300   try {
00301         DataSource * tuple =
00302     controller -> createNTuple ( filename );
00303   }
00304   catch ( const std::exception & e )
00305     {
00306 
00307       QString message( "Unable to open file\n" );
00308       message.append(e.what ());
00309 
00310       QMessageBox::critical ( 0, // parent
00311                               "HippoDraw",
00312                               message,
00313                               QMessageBox::Ok,
00314                               Qt::NoButton );
00315     }
00316 
00317 }
00318 
00319 bool
00320 QtFileDialog::
00321 isFitsFile ( const std::string & fn )
00322 {
00323   bool yes = false;
00324 #ifdef HAVE_CFITSIO
00325   FitsController * controller = FitsController::instance ();
00326   FitsFile * fits_file = 0;
00327   try {
00328     fits_file = controller ->openFile ( fn );
00329    if ( fits_file != 0 ) yes = true;
00330   }
00331   catch ( ... ) {
00332     // ignore, was not FITS file
00333   }
00334 #endif
00335 
00336   return yes;
00337 }
00338 
00339 void
00340 QtFileDialog::
00341   checkDuplicateLabels ( const DataSource * source, QWidget * parent )
00342 {
00343   const vector < string > & dups = source -> getDuplicatedLabels ();
00344   if ( dups.empty () == false ) {
00345     QString message ( "This data source contains two or more columns with\n"
00346                       "the same label.  The offending labels were\n\n" );
00347     for ( unsigned int i = 0; i < dups.size(); i++ ) {
00348       message += dups[i].c_str();
00349       message += "\n";
00350     }
00351     message += "\nThe first column encountered will be used";
00352     QMessageBox::information (  parent, // parent
00353                                "HippoDraw", // caption
00354                                message,
00355                                QMessageBox::Ok,
00356                                Qt::NoButton );
00357   }
00358 }
00359 
00360 void
00361 QtFileDialog::
00362 openFitsTuple ( const std::string & filename, QWidget * parent )
00363 {
00364 #ifdef HAVE_CFITSIO
00365   unsigned int index = 0;
00366 
00367   FitsController * controller = FitsController::instance ();
00368   const vector < string > & names
00369     = controller -> getNTupleNames ( filename );
00370 
00371   if ( names.size () > 1 ) {
00372     if ( names.size () >= 2  ) {
00373       ListDialog * dialog = new ListDialog ( parent ); // take defaults for rest
00374       dialog -> setNames ( names );
00375       int retval = dialog -> exec ();
00376 
00377       if ( retval == QDialog::Accepted ) {
00378         index = dialog -> selectedItem ();
00379       }
00380       delete dialog;
00381       if ( retval == QDialog::Rejected ) return;
00382     }
00383     else { // was your typical empty image HDU
00384       index = 1;
00385     }
00386   }
00387 
00388   try {
00389     DataSource * ds = 
00390       controller -> createNTuple ( filename, names [ index ], index );
00391     checkDuplicateLabels ( ds, parent );
00392   }
00393   catch ( const runtime_error & e ) {
00394     QMessageBox::information ( parent, // parent
00395                                "HippoDraw", // caption
00396                                e.what (),
00397                                QMessageBox::Ok,
00398                                Qt::NoButton );
00399     return;
00400   }
00401 
00402 #endif
00403 }
00404 
00405 void
00406 QtFileDialog::
00407 openRootTuple ( const std::string & filename, QWidget * parent )
00408 {
00409 #ifdef HAVE_ROOT
00410   unsigned int index = 0;
00411 
00412   RootController * controller = RootController::instance ();
00413   const vector < string > & tree_names
00414     = controller -> getNTupleNames ( filename );
00415   if ( tree_names.size () > 1 ) {
00416     ListDialog * dialog = new ListDialog ( parent ); // take defaults for rest
00417     dialog -> setNames ( tree_names );
00418 
00419     int retval = dialog -> exec ();
00420 
00421     if ( retval == QDialog::Rejected ) return;
00422     index = dialog -> selectedItem ();
00423     delete dialog;
00424   }
00425   if ( tree_names.empty () ) {
00426     string message ( "There were no ntuple objects found in this file." );
00427     message += "\nFile not opened.";
00428 
00429     QMessageBox::information ( parent,
00430                                "HippoDraw", // caption
00431                                message.c_str(),
00432                                QMessageBox::Ok,
00433                                Qt::NoButton );
00434     return;
00435   }
00436 
00437   try {
00438     DataSource * tuple =
00439       controller -> createNTuple ( filename, tree_names[index] );
00440     checkDuplicateLabels ( tuple, parent );
00441   }
00442   catch ( const runtime_error & e ) {
00443     QMessageBox::information ( parent,
00444                                "HippoDraw", // caption
00445                                e.what (),
00446                                QMessageBox::Ok,
00447                                Qt::NoButton );
00448     return;
00449   }
00450 
00451 #endif
00452 }
00453 
00454 #ifdef HAVE_CFITSIO
00455 void
00456 QtFileDialog::
00457 saveFitsTuple ( const std::string & filename, QWidget * parent )
00458 {
00459   const string tuple_name = 
00460     WindowController::instance () 
00461     -> getInspector() 
00462     -> getSelectedDataSourceName ();
00463 
00464   try {
00465     FitsController::instance ()
00466       ->writeNTupleToFile ( tuple_name, filename );
00467   }
00468   catch ( const std::exception & e ) {
00469     QString message ( "An error occurred in writing file.\n\n" );
00470     message += e.what();
00471     QMessageBox::critical ( parent,
00472                             QString ( "Write error" ), // caption
00473                             message,
00474                             QMessageBox::Ok,
00475                             QMessageBox::NoButton,
00476                             QMessageBox::NoButton );
00477   }
00478 }
00479 #endif
00480 
00481 void
00482 QtFileDialog::
00483 saveTextTuple ( const std::string & filename, QWidget * parent )
00484 {
00485   const string tuple_name = 
00486     WindowController::instance () 
00487     -> getInspector() 
00488     -> getSelectedDataSourceName ();
00489 
00490   try {
00491     NTupleController::instance ()
00492       ->writeNTupleToFile ( tuple_name, filename );
00493   }
00494 
00495   catch ( const std::exception & e ) {
00496     QString message ( "An error occurred in writing file.\n\n" );
00497     message += e.what();
00498     QMessageBox::critical ( parent,
00499                             QString ( "Write error" ), // caption
00500                             message,
00501                             QMessageBox::Ok,
00502                             QMessageBox::NoButton,
00503                             QMessageBox::NoButton );
00504   }
00505 }

Generated for HippoDraw Class Library by doxygen