00001
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #endif
00015
00016 #include "QtFileDialog.h"
00017
00018 #include "CanvasWindow.h"
00019 #include "ListDialog.h"
00020
00021 #include "datasrcs/DataSourceException.h"
00022 #include "datasrcs/NTupleController.h"
00023
00024 #ifdef HAVE_CFITSIO
00025 #include "fits/FitsController.h"
00026 #endif
00027
00028 #ifdef HAVE_ROOT
00029 #include "root/RootController.h"
00030 #endif
00031
00032 #include <qmessagebox.h>
00033
00034 using std::exception;
00035 using std::string;
00036 using std::vector;
00037
00038
00039 const string QtFileDialog::s_doc_suffix = ".hpo";
00040 const string QtFileDialog::s_data_suffix = ".tnt";
00041
00042 QtFileDialog::
00043 QtFileDialog ( const QString & dirName,
00044 QWidget * parent,
00045 const char * name,
00046 bool modal )
00047 : QFileDialog ( dirName, QString::null, parent, name, modal )
00048 {
00049 }
00050
00051 void
00052 QtFileDialog::
00053 createOpenFilter ()
00054 {
00055 const string doc ( s_doc_suffix );
00056 const string xml ( ".xml" );
00057 const string tnt ( ".tnt" );
00058
00059 #if __GNUC__ < 3
00060 #define STRING_CLEAR_DEFECT
00061 #endif
00062
00063 #ifdef STRING_CLEAR_DEFECT
00064 m_open_filter.erase();
00065 #else
00066 m_open_filter.clear();
00067 #endif
00068
00069 m_open_filter = "Documents (*";
00070 m_open_filter += doc;
00071 m_open_filter += " *" + xml;
00072
00073 m_open_filter += ");;Text ntuples (*";
00074 m_open_filter += tnt;
00075 m_open_filter += ")";
00076
00077 #ifdef HAVE_ROOT
00078 const string root ( ".root" );
00079 m_open_filter += ";;ROOT ntuples(*";
00080 m_open_filter += root;
00081 m_open_filter += ")";
00082 #endif
00083
00084 #ifdef HAVE_CFITSIO
00085 const string fits ( ".fits" );
00086 m_open_filter += ";;FITS ntuples(*";
00087 m_open_filter += fits;
00088 m_open_filter += ")";
00089 #endif
00090
00091 m_open_filter += ";;All files (";
00092 m_open_filter += "*.*";
00093 m_open_filter += ")";
00094 }
00095 void
00096 QtFileDialog::
00097 createBrowseFilter ()
00098 {
00099 const string doc ( s_doc_suffix );
00100 const string xml ( ".xml" );
00101 const string tnt ( ".tnt" );
00102
00103 #if __GNUC__ < 3
00104 #define STRING_CLEAR_DEFECT
00105 #endif
00106
00107 #ifdef STRING_CLEAR_DEFECT
00108 m_open_filter.erase();
00109 #else
00110 m_open_filter.clear();
00111 #endif
00112 m_open_filter = "Documents (*";
00113 m_open_filter += doc;
00114 m_open_filter += " *" + xml;
00115 m_open_filter += ")";
00116
00117 m_open_filter += ";;All files (";
00118 m_open_filter += "*.*";
00119 m_open_filter += ")";
00120 }
00121
00122 const std::string &
00123 QtFileDialog::
00124 getDocSuffix ()
00125 {
00126 return s_doc_suffix;
00127 }
00128
00129 const std::string &
00130 QtFileDialog::
00131 getTextSuffix ()
00132 {
00133 return s_data_suffix;
00134 }
00135
00136 bool
00137 QtFileDialog::
00138 isDocSuffix ( const std::string & suffix )
00139 {
00140 return suffix == s_doc_suffix || suffix == ".xml";
00141 }
00142
00143 bool
00144 QtFileDialog::
00145 isTextSuffix ( const std::string & suffix )
00146 {
00147 return suffix == s_data_suffix;
00148 }
00149
00150 bool
00151 QtFileDialog::
00152 isRootSuffix ( const std::string & suffix ) const
00153 {
00154 return suffix == ".root";
00155 }
00156
00157 bool
00158 QtFileDialog::
00159 isFitsSuffix ( const std::string & suffix ) const
00160 {
00161 return suffix == ".fits";
00162 }
00163
00164 std::string
00165 QtFileDialog::
00166 getSaveDocFilename ()
00167 {
00168 QString filter ( "Documents (*" );
00169 #if defined(QT_NO_STL) || QT_VERSION < 0x030100
00170 filter += s_doc_suffix.c_str();
00171 #else
00172 filter += s_doc_suffix;
00173 #endif
00174
00175 filter += ")";
00176
00177 QString filename;
00178 while ( true ) {
00179 setFilters ( filter );
00180 setCaption ( "Save document as ..." );
00181 setMode ( QFileDialog::AnyFile );
00182
00183 string tmp;
00184 int retval = QFileDialog::exec ();
00185 if ( retval == QDialog::Rejected ) return tmp;
00186
00187 filename = selectedFile ();
00188 if ( filename.isEmpty () ) return tmp;
00189
00190 #if defined(QT_NO_STL) || QT_VERSION < 0x030100
00191 if ( filename.endsWith ( s_doc_suffix.c_str() ) == false ) {
00192 filename += s_doc_suffix.c_str();
00193 }
00194 #else
00195 if ( filename.endsWith ( s_doc_suffix ) == false ) {
00196 filename += s_doc_suffix;
00197 }
00198 #endif
00199
00200 QFileInfo info ( filename );
00201 filename = info.absFilePath();
00202
00203 bool yes = info.exists ();
00204 if ( yes == false ) break;
00205
00206 string message ( "File exists. \n\n" );
00207 message += "Over write existing file?";
00208 int result = QMessageBox::warning ( this,
00209 "Warning",
00210 message.c_str (),
00211 QMessageBox::Yes,
00212 QMessageBox::No );
00213 if ( result == QMessageBox::Yes ) break;
00214 }
00215 string name = filename.latin1();
00216
00217 return name;
00218 }
00219
00220 int
00221 QtFileDialog::
00222 exec ()
00223 {
00224 if ( m_open_filter.empty () ) {
00225 createOpenFilter ();
00226 }
00227 setFilters ( QString ( m_open_filter.c_str() ) );
00228 setMode ( QFileDialog::ExistingFile );
00229
00230 return QFileDialog::exec ();
00231 }
00232
00233
00234 void
00235 QtFileDialog::
00236 openTextTuple ( const std::string & filename )
00237 {
00238 NTupleController * controller = NTupleController::instance ();
00239 try {
00240
00241 controller -> createNTuple ( filename );
00242 }
00243 catch ( const DataSourceException & e )
00244 {
00245
00246 QString message( "Unable to open file\n" );
00247 message.append(e.what ());
00248
00249 QMessageBox::critical ( 0,
00250 "HippoDraw",
00251 message,
00252 QMessageBox::Ok,
00253 Qt::NoButton );
00254 }
00255
00256 }
00257
00258 bool
00259 QtFileDialog::
00260 isFitsFile ( const std::string & fn ) const
00261 {
00262 bool yes = false;
00263 #ifdef HAVE_CFITSIO
00264 FitsController * controller = FitsController::instance ();
00265 FitsFile * fits_file = 0;
00266 try {
00267 fits_file = controller ->openFile ( fn );
00268 if ( fits_file != 0 ) yes = true;
00269 }
00270 catch ( ... ) {
00271
00272 }
00273 #endif
00274
00275 return yes;
00276 }
00277
00278 void
00279 QtFileDialog::
00280 openFitsTuple ( const std::string & filename )
00281 {
00282 #ifdef HAVE_CFITSIO
00283 unsigned int index = 0;
00284
00285 FitsController * controller = FitsController::instance ();
00286 const vector < string > & names
00287 = controller -> getNTupleNames ( filename );
00288
00289 if ( names.size () > 1 ) {
00290 if ( names.size () >= 2 ) {
00291 ListDialog * dialog = new ListDialog ( this );
00292 dialog -> setNames ( names );
00293 int retval = dialog -> exec ();
00294
00295 if ( retval == QDialog::Accepted ) {
00296 index = dialog -> selectedItem ();
00297 }
00298 delete dialog;
00299 if ( retval == QDialog::Rejected ) return;
00300 }
00301 else {
00302 index = 1;
00303 }
00304 }
00305
00306 try {
00307
00308 controller -> createNTuple ( filename, names [ index ], index );
00309 }
00310 catch ( const DataSourceException & e ) {
00311 QMessageBox::information ( this,
00312 "HippoDraw",
00313 e.what (),
00314 QMessageBox::Ok,
00315 Qt::NoButton );
00316 return;
00317 }
00318
00319 #endif
00320 }
00321
00322 void
00323 QtFileDialog::
00324 openRootTuple ( const std::string & filename )
00325 {
00326 #ifdef HAVE_ROOT
00327 unsigned int index = 0;
00328
00329 RootController * controller = RootController::instance ();
00330 const vector < string > & tree_names
00331 = controller -> getNTupleNames ( filename );
00332 if ( tree_names.size () > 1 ) {
00333 ListDialog * dialog = new ListDialog ( this );
00334 dialog -> setNames ( tree_names );
00335
00336 int retval = dialog -> exec ();
00337
00338 if ( retval == QDialog::Rejected ) return;
00339 index = dialog -> selectedItem ();
00340 delete dialog;
00341 }
00342 if ( tree_names.empty () ) {
00343 string message ( "There were no ntuple objects found in this file." );
00344 message += "\nFile not opened.";
00345
00346 QMessageBox::information ( this,
00347 "HippoDraw",
00348 message.c_str(),
00349 QMessageBox::Ok,
00350 Qt::NoButton );
00351 return;
00352 }
00353
00354 try {
00355
00356 controller -> createNTuple ( filename, tree_names[index] );
00357 }
00358 catch ( const DataSourceException & e ) {
00359 QMessageBox::information ( this,
00360 "HippoDraw",
00361 e.what (),
00362 QMessageBox::Ok,
00363 Qt::NoButton );
00364 return;
00365 }
00366
00367 #endif
00368 }
00369
00370 void
00371 QtFileDialog::
00372 openFileError ( const std::string & filename, exception & e )
00373 {
00374 string message = e.what();
00375 QMessageBox::critical ( 0,
00376 "File error",
00377 message.c_str(),
00378 QMessageBox::Ok,
00379 0 );
00380 }