provide get_py and get_py_ptr
[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   const QIcon mussa_icon(":/icons/mussa.png");
56   app.setWindowIcon(mussa_icon);
57
58   initialize_mussa(opts, argc, argv);
59   if (opts.analysis == 0) {
60     return 1;
61   }
62
63   ThreadManager &thread = ThreadManagerFactory();
64   try {
65 #ifdef USE_PYTHON
66     if (opts.runAsPythonInterpeter) {
67       // allow the user to keep the interpreter open even after
68       // closing all the windows
69       app.setQuitOnLastWindowClosed(false);
70       InterpreterThread *interp = thread.create_interpreter();
71       if (!interp) {
72         std::cerr << "Unable to initialize interpeter thread" << std::endl;
73         return 1;
74       }
75       MussaPython *py = interp->get_py_ptr();
76       if (!py) {
77         std::cerr << "Unable to initialize python interpreter" << std::endl;
78         return 1;
79       }
80       py->add_module("mussa", &initmussa);
81       py->add_module("mussaqui", &initmussaqui);
82       // quit when the interpreter exits
83       QObject::connect(interp, SIGNAL(finished()),
84                        &app, SLOT(quit()));
85       interp->start();
86       app.exec();
87     } else 
88 #endif /* USE_PYTHON */
89     if (opts.useGUI) { 
90       MussaWindow win(opts.analysis);
91       win.show();
92       app.exec();
93     }
94     return 0;
95   } catch (mussa_error e) {
96     qFatal(e.what());
97   } catch (boost::filesystem::filesystem_error e) {
98     qFatal(e.what());
99   }
100 #ifdef USE_PYTHON   
101   catch( boost::python::error_already_set ) {
102     PyErr_Print();
103   }
104 #endif    
105   catch (std::runtime_error e) {
106     qFatal(e.what());
107   } catch (...) {
108     qFatal("unrecognized exception");
109   }
110   return 1; 
111 }