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