try to reduce number of moveToThread warnings
[mussa.git] / qui / mussagl.cpp
1 #include <boost/filesystem/operations.hpp>
2 using namespace boost::filesystem;
3
4 #ifdef USE_PYTHON
5 #include "py/python.hpp"
6 extern "C" void initmussa();
7 extern "C" void initmussaqui();
8 #endif
9
10 #include "qui/MussaWindow.hpp"
11 #include "qui/threading/ThreadManager.hpp"
12 #include "qui/threading/InterpreterThread.hpp"
13 #include "alg/parse_options.hpp"
14 #include "mussa_exceptions.hpp"
15
16 #include <stdlib.h>
17 #include <iostream>
18 #include <QApplication>
19
20 #if defined(Q_WS_MAC)
21 #include <CoreFoundation/CoreFoundation.h>
22 #endif 
23
24 std::string get_doc_dir()
25 {
26 #if defined(Q_WS_MAC)
27   CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
28   CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef,
29                                          kCFURLPOSIXPathStyle);
30   const char *pathPtr = CFStringGetCStringPtr(macPath,
31                                          CFStringGetSystemEncoding());
32   std::string doc_dir(pathPtr);
33   qDebug("Path = %s", pathPtr);
34   CFRelease(pluginRef);
35   CFRelease(macPath);
36 #else
37   return std::string(".");
38 #endif
39 }
40
41 int main(int argc, char **argv)
42 {
43   MussaOptions opts;
44
45   // if we're under unix and don't have a display, see if we can still run
46 #ifdef Q_WS_X11
47   opts.useGUI = getenv("DISPLAY") != 0;
48   if (opts.useGUI == false) {
49     std::clog << "DISPLAY not set, running in console mode only" << std::endl;
50   }
51 #endif
52   //opts.doc_dir = get_doc_dir();
53   QApplication app(argc, argv, opts.useGUI);
54   Q_INIT_RESOURCE(icons);
55
56   initialize_mussa(opts, argc, argv);
57   if (opts.analysis == 0) {
58     return 1;
59   }
60   
61   try {
62 #ifdef USE_PYTHON
63     if (opts.runAsPythonInterpeter) {
64       ThreadManager &thread = ThreadManagerFactory();
65       // allow the user to keep the interpreter open even after
66       // closing all the windows
67       app.setQuitOnLastWindowClosed(false);
68       InterpreterThread *interp = thread.create_interpreter();
69       if (!interp) {
70         std::cerr << "Unable to initialize interpeter thread" << std::endl;
71         return 1;
72       }
73       MussaPython *py = interp->get_py_ptr();
74       if (!py) {
75         std::cerr << "Unable to initialize python interpreter" << std::endl;
76         return 1;
77       }
78       py->add_module("mussa", &initmussa);
79       py->add_module("mussaqui", &initmussaqui);
80       // quit when the interpreter exits
81       QObject::connect(interp, SIGNAL(finished()),
82                        &app, SLOT(quit()));
83       interp->start();
84       app.exec();
85     } else 
86 #endif /* USE_PYTHON */
87     if (opts.useGUI) { 
88       MussaWindow *win = new MussaWindow(opts.analysis);
89       win->show();
90       app.exec();
91     }
92     return 0;
93   } catch (mussa_error e) {
94     qFatal(e.what());
95   } catch (boost::filesystem::filesystem_error e) {
96     qFatal(e.what());
97   }
98 #ifdef USE_PYTHON   
99   catch( boost::python::error_already_set ) {
100     PyErr_Print();
101   }
102 #endif    
103   catch (std::runtime_error e) {
104     qFatal(e.what());
105   } catch (...) {
106     qFatal("unrecognized exception");
107   }
108   return 1; 
109 }