create an options structure to indicate if we need a gui
authorDiane Trout <diane@caltech.edu>
Thu, 13 Apr 2006 00:39:51 +0000 (00:39 +0000)
committerDiane Trout <diane@caltech.edu>
Thu, 13 Apr 2006 00:39:51 +0000 (00:39 +0000)
My last attempt at making mussagl not need to run a gui on linux caused os x
to break. This implements a more complicated scheme that involves
initialize_mussa learning how to pass multiple options and a hidden
argument to QApplication to not initialize the gui when (in X11) $DISPLAY
isn't set.

alg/parse_options.cpp
alg/parse_options.hpp
qui/mussagl.cpp

index 116a16dcaa5d881dcf39e47c559c6d2df48c06df..ce253246fb3d162b9c7d80d27c7d85ebee3fa8a9 100644 (file)
@@ -7,7 +7,13 @@ namespace po = boost::program_options;
 #include "alg/mussa.hpp"
 #include "alg/parse_options.hpp"
 
-Mussa *initialize_mussa(int argc, char **argv)
+MussaOptions::MussaOptions()
+  : analysis(0),
+    useGUI(true)
+{
+}
+
+void initialize_mussa(MussaOptions& opts, int argc, char **argv)
 {
   po::options_description options("Mussa options");
   po::positional_options_description pos_options;
@@ -28,25 +34,22 @@ Mussa *initialize_mussa(int argc, char **argv)
 
   if (vm.count("help")) {
     std::cout << options << std::endl;
-    return 0;
+    return;
   }
 
-  Mussa *m = new Mussa();
+  opts.analysis = new Mussa();
   // currently we can only have one analysis loaded, so 
   // running trumps viewing.
   if (vm.count("run-analysis")) {
-    m->load_mupa_file( vm["run-analysis"].as< std::string >() );
+    opts.analysis->load_mupa_file( vm["run-analysis"].as< std::string >() );
     std::cout << "I apologize for blocking the gui while running the analysis"
               << std::endl;
-    m->analyze();
+    opts.analysis->analyze();
   }
   else if (vm.count("view-analysis")) {
-    m->load( vm["view-analysis"].as< std::string >() );
+    opts.analysis->load( vm["view-analysis"].as< std::string >() );
   }
   if (vm.count("no-gui")) {
-    return 0;
-  } else {
-    return m;
+    opts.useGUI=false;
   }
 }
-
index 1dd3291d0181d1a13369e8c3846ed185030d5f26..f048b8e955a9b5708ea2b28c41e25b36b1545eb6 100644 (file)
@@ -3,7 +3,19 @@
 
 class Mussa;
 
+//! collect information about runtime options
+struct MussaOptions 
+{
+  //! set defaults for our options
+  MussaOptions();
+
+  //! pointer to our analysis, if its null, feel free to die
+  Mussa *analysis;
+  //! should we use the gui?
+  bool useGUI;
+};
+
 //! initialize a mussa analysis from command line arguments
-Mussa *initialize_mussa(int argc, char** argv);
+void initialize_mussa(MussaOptions& opts, int argc, char** argv);
 #endif
 
index d7b685cac1738776f0231dfd2b43ce2e8821cc5d..bc6260f42813ac2df668728bad966642d2548768 100644 (file)
@@ -1,31 +1,35 @@
 #include <boost/filesystem/operations.hpp>
 using namespace boost::filesystem;
 
+#include <stdlib.h>
 #include <QApplication>
 
 #include "qui/MussaWindow.hpp"
 #include "alg/parse_options.hpp"
 
-int startgui(Mussa *analysis, int argc, char **argv)
+int main(int argc, char **argv)
 {
-  QApplication app(argc, argv);
-  Q_INIT_RESOURCE(icons);
-  MussaWindow win(analysis);
-  win.show();
-  app.exec();
+  MussaOptions opts;
 
-  return 0;
-}
+  // if we're under unix and don't have a display, see if we can still run
+#ifdef Q_WS_X11
+  opts.useGUI = getenv("DISPLAY") != 0;
+#endif
 
-int main(int argc, char **argv)
-{
-  // let boost::filesystem store the path we were initially launched in
-  initial_path();
+  QApplication app(argc, argv, opts.useGUI);
+  Q_INIT_RESOURCE(icons);
+
+  initialize_mussa(opts, argc, argv);
 
-  Mussa *analysis = initialize_mussa(argc, argv);
-  if (analysis == 0) {
+  if (opts.analysis == 0) {
     return 1;
   }
 
-  return startgui(analysis, argc, argv);
+  if (opts.useGUI) { 
+    MussaWindow win(opts.analysis);
+    win.show();
+    app.exec();
+  }
+
+  return 0;
 }