QtFileDialog.cxx
Go to the documentation of this file.
1 
12 #ifdef HAVE_CONFIG_H
13 // for have root cfitsio
14 #include "config.h"
15 #endif
16 
17 #include "QtFileDialog.h"
18 
19 #include "CanvasWindow.h"
20 #include "WindowController.h"
21 #include "Inspector.h"
22 #include "ListDialog.h"
23 
25 #include "datasrcs/DataSource.h"
27 
28 #ifdef HAVE_CFITSIO
29 #include "fits/FitsController.h"
30 #endif
31 
32 #ifdef HAVE_ROOT
33 #include "root/RootController.h"
34 #endif
35 
36 #include <qmessagebox.h>
37 
38 
39 using std::exception;
40 using std::runtime_error;
41 using std::string;
42 using std::vector;
43 
44 using namespace hippodraw;
45 
46 /* Use `=' syntax so that Doxygen picks it up. */
47 const string QtFileDialog::s_doc_suffix = ".hpo";
48 const string QtFileDialog::s_data_suffix = ".tnt";
49 string QtFileDialog::s_open_filter = "";
50 
53 {
54 }
55 
56 const string &
59 {
60  const string doc ( s_doc_suffix );
61  const string xml ( ".xml" );
62  const string tnt ( ".tnt" );
63 
64 #if __GNUC__ < 3
65 #define STRING_CLEAR_DEFECT
66 #endif
67 
68 #ifdef STRING_CLEAR_DEFECT
69  s_open_filter.erase();
70 #else
71  s_open_filter.clear();
72 #endif
73 
74  s_open_filter = "All files (";
75  s_open_filter += "*";
76  s_open_filter += ");;";
77 
78  s_open_filter += "Documents (*";
79  s_open_filter += doc;
80  s_open_filter += " *" + xml;
81 
82  s_open_filter += ");;Text ntuples (*";
83  s_open_filter += tnt;
84  s_open_filter += ")";
85 
86 #ifdef HAVE_ROOT
87  const string root ( ".root" );
88  s_open_filter += ";;ROOT ntuples(*";
89  s_open_filter += root;
90  s_open_filter += ")";
91 #endif
92 
93 #ifdef HAVE_CFITSIO
94  const string fits ( ".fits" );
95  s_open_filter += ";;FITS ntuples(*";
96  s_open_filter += fits;
97  s_open_filter += ")";
98 
99  const string gzfits ( ".fits.gz" );
100  s_open_filter += ";;Compressed FITS ntuples(*";
101  s_open_filter += gzfits;
102  s_open_filter += ")";
103 #endif
104 
105  return s_open_filter;
106 }
107 
108 const string &
111 {
112 #ifdef STRING_CLEAR_DEFECT
113  s_open_filter.erase();
114 #else
115  s_open_filter.clear();
116 #endif
117 
118  s_open_filter += "Text ntuples (*";
119  s_open_filter += ".tnt";
120  s_open_filter += ")";
121 
122 #ifdef HAVE_CFITSIO
123  const string fits ( ".fits" );
124  s_open_filter += ";;FITS ntuples(*";
125  s_open_filter += fits;
126  s_open_filter += ")";
127 
128  const string gzfits ( ".fits.gz" );
129  s_open_filter += ";;Compressed FITS ntuples(*";
130  s_open_filter += gzfits;
131  s_open_filter += ")";
132 #endif
133 
134  return s_open_filter;
135 }
136 
137 
138 const string &
141 {
142  const string doc ( s_doc_suffix );
143  const string xml ( ".xml" );
144  const string tnt ( ".tnt" );
145 
146 #if __GNUC__ < 3
147 #define STRING_CLEAR_DEFECT
148 #endif
149 
150 #ifdef STRING_CLEAR_DEFECT
151  s_open_filter.erase();
152 #else
153  s_open_filter.clear();
154 #endif
155  s_open_filter = "Documents (*";
156  s_open_filter += doc;
157  s_open_filter += " *" + xml;
158  s_open_filter += ")";
159 
160  s_open_filter += ";;All files (";
161  s_open_filter += "*.*";
162  s_open_filter += ")";
163 
164  return s_open_filter;
165 }
166 
167 const std::string &
170 {
171  return s_doc_suffix;
172 }
173 
174 const std::string &
177 {
178  return s_data_suffix;
179 }
180 
181 bool
183 isDocSuffix ( const std::string & suffix )
184 {
185  return suffix == s_doc_suffix || suffix == ".xml";
186 }
187 
188 bool
190 isTextSuffix ( const std::string & suffix )
191 {
192  return suffix == s_data_suffix;
193 }
194 
195 bool
197 isRootSuffix ( const std::string & suffix )
198 {
199  return suffix == ".root";
200 }
201 
202 bool
204 isFitsSuffix ( const std::string & suffix )
205 {
206  return suffix == ".fits";
207 }
208 
209 bool
211 isZippedFitsSuffix ( const std::string & suffix)
212 {
213  return suffix == ".gz";
214 }
215 
216 std::string
219 {
220  const string & filter = createExportFilter ();
221  QString filename;
222 
223  while (true)
224  {
225  string tmp;
226  QString extname = 0;
227  filename
228 #if QT_VERSION < 0x040000
229  = QFileDialog::getSaveFileName ( QString::null, // starting directory
230 #else
231  = Q3FileDialog::getSaveFileName ( QString::null, // starting directory
232 #endif
233  filter.c_str(),
234  widget, // parent
235  "export", // name
236  "Save data source to file ...",
237  & extname ); //selected filter
238 
239  if ( filename == QString::null ) return tmp;
240 
241  if (( filename.endsWith ( ".tnt" ) == false ) &&
242  ( filename.endsWith ( ".fits" ) == false ) &&
243  ( filename.endsWith ( ".fits.gz" )==false ))
244 
245  {
246  // Add default suffix if not end with .tnt or .fits.
247  if ( extname == "FITS ntuples(*.fits)" )
248  {
249  filename+=".fits";
250  }
251  else if (extname == "Compressed FITS ntuples(*.fits.gz)")
252  {
253  filename+=".fits.gz";
254  }
255  else
256  {
257  filename+=".tnt";
258  }
259  }
260 
261  QFileInfo info ( filename );
262  filename = info.absFilePath();
263 
264  bool yes = info.exists ();
265  if ( yes == false ) break;
266 
267  string message ( "File exists. \n\n" );
268  message += "Over write existing file?";
269  int result = QMessageBox::warning ( widget, // parent
270  "Warning", // caption
271  message.c_str (), //
272  QMessageBox::Yes,
273  QMessageBox::No );
274  if ( result == QMessageBox::Yes ) break;
275  }
276 
277  string name = filename.latin1();
278 
279  return name;
280 }
281 
282 
283 const string &
286 {
287  s_open_filter.erase();
288  s_open_filter = "Doucuments (*";
289  s_open_filter += s_doc_suffix.c_str();
290  s_open_filter += ")";
291 
292  return s_open_filter;
293 }
294 
295 void
297 openTextTuple ( const std::string & filename )
298 {
300  try {
301  DataSource * tuple =
302  controller -> createNTuple ( filename );
303  }
304  catch ( const std::exception & e )
305  {
306 
307  QString message( "Unable to open file\n" );
308  message.append(e.what ());
309 
310  QMessageBox::critical ( 0, // parent
311  "HippoDraw",
312  message,
313  QMessageBox::Ok,
314  Qt::NoButton );
315  }
316 
317 }
318 
319 bool
321 isFitsFile ( const std::string & fn )
322 {
323  bool yes = false;
324 #ifdef HAVE_CFITSIO
325  FitsController * controller = FitsController::instance ();
326  FitsFile * fits_file = 0;
327  try {
328  fits_file = controller ->openFile ( fn );
329  if ( fits_file != 0 ) yes = true;
330  }
331  catch ( ... ) {
332  // ignore, was not FITS file
333  }
334 #endif
335 
336  return yes;
337 }
338 
339 void
341  checkDuplicateLabels ( const DataSource * source, QWidget * parent )
342 {
343  const vector < string > & dups = source -> getDuplicatedLabels ();
344  if ( dups.empty () == false ) {
345  QString message ( "This data source contains two or more columns with\n"
346  "the same label. The offending labels were\n\n" );
347  for ( unsigned int i = 0; i < dups.size(); i++ ) {
348  message += dups[i].c_str();
349  message += "\n";
350  }
351  message += "\nThe first column encountered will be used";
352  QMessageBox::information ( parent, // parent
353  "HippoDraw", // caption
354  message,
355  QMessageBox::Ok,
356  Qt::NoButton );
357  }
358 }
359 
360 void
362 openFitsTuple ( const std::string & filename, QWidget * parent )
363 {
364 #ifdef HAVE_CFITSIO
365  unsigned int index = 0;
366 
367  FitsController * controller = FitsController::instance ();
368  const vector < string > & names
369  = controller -> getNTupleNames ( filename );
370 
371  if ( names.size () > 1 ) {
372  if ( names.size () >= 2 ) {
373  ListDialog * dialog = new ListDialog ( parent ); // take defaults for rest
374  dialog -> setNames ( names );
375  int retval = dialog -> exec ();
376 
377  if ( retval == QDialog::Accepted ) {
378  index = dialog -> selectedItem ();
379  }
380  delete dialog;
381  if ( retval == QDialog::Rejected ) return;
382  }
383  else { // was your typical empty image HDU
384  index = 1;
385  }
386  }
387 
388  try {
389  DataSource * ds =
390  controller -> createNTuple ( filename, names [ index ], index );
391  checkDuplicateLabels ( ds, parent );
392  }
393  catch ( const runtime_error & e ) {
394  QMessageBox::information ( parent, // parent
395  "HippoDraw", // caption
396  e.what (),
397  QMessageBox::Ok,
398  Qt::NoButton );
399  return;
400  }
401 
402 #endif
403 }
404 
405 void
407 openRootTuple ( const std::string & filename, QWidget * parent )
408 {
409 #ifdef HAVE_ROOT
410  unsigned int index = 0;
411 
412  RootController * controller = RootController::instance ();
413  const vector < string > & tree_names
414  = controller -> getNTupleNames ( filename );
415  if ( tree_names.size () > 1 ) {
416  ListDialog * dialog = new ListDialog ( parent ); // take defaults for rest
417  dialog -> setNames ( tree_names );
418 
419  int retval = dialog -> exec ();
420 
421  if ( retval == QDialog::Rejected ) return;
422  index = dialog -> selectedItem ();
423  delete dialog;
424  }
425  if ( tree_names.empty () ) {
426  string message ( "There were no ntuple objects found in this file." );
427  message += "\nFile not opened.";
428 
429  QMessageBox::information ( parent,
430  "HippoDraw", // caption
431  message.c_str(),
432  QMessageBox::Ok,
433  Qt::NoButton );
434  return;
435  }
436 
437  try {
438  DataSource * tuple =
439  controller -> createNTuple ( filename, tree_names[index] );
440  checkDuplicateLabels ( tuple, parent );
441  }
442  catch ( const runtime_error & e ) {
443  QMessageBox::information ( parent,
444  "HippoDraw", // caption
445  e.what (),
446  QMessageBox::Ok,
447  Qt::NoButton );
448  return;
449  }
450 
451 #endif
452 }
453 
454 #ifdef HAVE_CFITSIO
455 void
457 saveFitsTuple ( const std::string & filename, QWidget * parent )
458 {
459  const string tuple_name =
461  -> getInspector()
462  -> getSelectedDataSourceName ();
463 
464  try {
466  ->writeNTupleToFile ( tuple_name, filename );
467  }
468  catch ( const std::exception & e ) {
469  QString message ( "An error occurred in writing file.\n\n" );
470  message += e.what();
471  QMessageBox::critical ( parent,
472  QString ( "Write error" ), // caption
473  message,
474  QMessageBox::Ok,
475  QMessageBox::NoButton,
476  QMessageBox::NoButton );
477  }
478 }
479 #endif
480 
481 void
483 saveTextTuple ( const std::string & filename, QWidget * parent )
484 {
485  const string tuple_name =
487  -> getInspector()
488  -> getSelectedDataSourceName ();
489 
490  try {
492  ->writeNTupleToFile ( tuple_name, filename );
493  }
494 
495  catch ( const std::exception & e ) {
496  QString message ( "An error occurred in writing file.\n\n" );
497  message += e.what();
498  QMessageBox::critical ( parent,
499  QString ( "Write error" ), // caption
500  message,
501  QMessageBox::Ok,
502  QMessageBox::NoButton,
503  QMessageBox::NoButton );
504  }
505 }

Generated for HippoDraw Class Library by doxygen