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