be more informative when DISPLAY isn't set under unix
[mussa.git] / qui / mussagl.cpp
1 #include <boost/filesystem/operations.hpp>
2 #ifdef USE_PYTHON
3 #include <boost/python.hpp>
4 namespace py = boost::python;
5 #endif
6
7 using namespace boost::filesystem;
8
9 #include <stdlib.h>
10 #include <iostream>
11 #include <QApplication>
12
13 #include "qui/MussaWindow.hpp"
14 #include "alg/parse_options.hpp"
15 #include "mussa_exceptions.hpp"
16
17 int main(int argc, char **argv)
18 {
19   MussaOptions opts;
20
21   // if we're under unix and don't have a display, see if we can still run
22 #ifdef Q_WS_X11
23   opts.useGUI = getenv("DISPLAY") != 0;
24   if (opts.useGUI == false) {
25     std::clog << "DISPLAY not set, running in console mode only" << std::endl;
26   }
27 #endif
28
29   QApplication app(argc, argv, opts.useGUI);
30   Q_INIT_RESOURCE(icons);
31
32   initialize_mussa(opts, argc, argv);
33
34   if (opts.analysis == 0) {
35     return 1;
36   }
37
38   try {
39 #ifdef USE_PYTHON
40     if (opts.runAsPythonInterpeter) {
41       Py_Initialize();
42       py::object main_module((py::handle<>(py::borrowed(PyImport_AddModule("__main__")))));
43       py::object main_namespace = main_module.attr("__dict__");
44
45       py::handle<> ignored((PyRun_String(
46                         //"from IPython.Shell import IPShellEmbed\n"
47                         //"shell = IPShellEmbed(['mussa'], '')\n",
48                         //"import mussa\n"
49                         "def square(x):\n"
50                         "  r = x * x\n"
51                         "  print 'the square is %d' % (r)\n"
52                         "  return r\n",
53                         Py_file_input, 
54                         main_namespace.ptr(),
55                         main_namespace.ptr())
56                       ));
57       py::object square = main_module.attr("square");
58       int result = py::extract<int>(square(3));
59       std::cout << "C++ has " << result << std::endl;
60       PyRun_InteractiveLoop(stdin, "mussa");
61     } else 
62 #endif /* USE_PYTHON */
63     if (opts.useGUI) { 
64       MussaWindow win(opts.analysis);
65       win.show();
66       app.exec();
67     }
68     return 0;
69   } catch (mussa_error e) {
70     qFatal(e.what());
71   } catch (boost::filesystem::filesystem_error e) {
72     qFatal(e.what());
73   }
74 #ifdef USE_PYTHON
75   catch (py::error_already_set e) {
76     PyErr_Print();
77   }
78 #endif /* USE_PYTHON */
79   catch (std::runtime_error e) {
80     qFatal(e.what());
81   } catch (...) {
82     qFatal("unrecognized exception");
83   }
84   return 1; 
85 }