add prototype wrapping of the qt sequence browser
[mussa.git] / qui / mussagl.cpp
index 3d45cb7dc9c164afd1f3ed6a82337aa87e4c6d2c..d0df4c6c05dfc5e56c0e9ed73e6f8ade53c71176 100644 (file)
@@ -1,18 +1,42 @@
 #include <boost/filesystem/operations.hpp>
+using namespace boost::filesystem;
+
 #ifdef USE_PYTHON
-#include <boost/python.hpp>
-namespace py = boost::python;
+#include "py/python.hpp"
+extern "C" void initmussa();
+//extern "C" void initmussaqui();
 #endif
 
-using namespace boost::filesystem;
+#include "qui/MussaWindow.hpp"
+#include "qui/threading/ThreadManager.hpp"
+#include "qui/threading/InterpreterThread.hpp"
+#include "alg/parse_options.hpp"
+#include "mussa_exceptions.hpp"
 
 #include <stdlib.h>
 #include <iostream>
 #include <QApplication>
 
-#include "qui/MussaWindow.hpp"
-#include "alg/parse_options.hpp"
-#include "mussa_exceptions.hpp"
+#if defined(Q_WS_MAC)
+#include <CoreFoundation/CoreFoundation.h>
+#endif 
+
+std::string get_doc_dir()
+{
+#if defined(Q_WS_MAC)
+  CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
+  CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef,
+                                         kCFURLPOSIXPathStyle);
+  const char *pathPtr = CFStringGetCStringPtr(macPath,
+                                         CFStringGetSystemEncoding());
+  std::string doc_dir(pathPtr);
+  qDebug("Path = %s", pathPtr);
+  CFRelease(pluginRef);
+  CFRelease(macPath);
+#else
+  return std::string(".");
+#endif
+}
 
 int main(int argc, char **argv)
 {
@@ -25,44 +49,43 @@ int main(int argc, char **argv)
     std::clog << "DISPLAY not set, running in console mode only" << std::endl;
   }
 #endif
-
+  //opts.doc_dir = get_doc_dir();
   QApplication app(argc, argv, opts.useGUI);
-  Q_INIT_RESOURCE(icons);
 
   initialize_mussa(opts, argc, argv);
-
   if (opts.analysis == 0) {
     return 1;
   }
-
+  
   try {
 #ifdef USE_PYTHON
     if (opts.runAsPythonInterpeter) {
-      Py_Initialize();
-      py::object main_module((py::handle<>(py::borrowed(PyImport_AddModule("__main__")))));
-      py::object main_namespace = main_module.attr("__dict__");
-
-      py::handle<> ignored((PyRun_String(
-                        //"from IPython.Shell import IPShellEmbed\n"
-                        //"shell = IPShellEmbed(['mussa'], '')\n",
-                        //"import mussa\n"
-                        "def square(x):\n"
-                        "  r = x * x\n"
-                        "  print 'the square is %d' % (r)\n"
-                        "  return r\n",
-                        Py_file_input, 
-                        main_namespace.ptr(),
-                        main_namespace.ptr())
-                      ));
-      py::object square = main_module.attr("square");
-      int result = py::extract<int>(square(3));
-      std::cout << "C++ has " << result << std::endl;
-      PyRun_InteractiveLoop(stdin, "mussa");
+      ThreadManager &thread = ThreadManagerFactory();
+      // allow the user to keep the interpreter open even after
+      // closing all the windows
+      app.setQuitOnLastWindowClosed(false);
+      InterpreterThread *interp = thread.create_interpreter();
+      if (!interp) {
+        std::cerr << "Unable to initialize interpeter thread" << std::endl;
+        return 1;
+      }
+      MussaPython *py = interp->get_py_ptr();
+      if (!py) {
+        std::cerr << "Unable to initialize python interpreter" << std::endl;
+        return 1;
+      }
+      py->add_module("mussa", &initmussa);
+      //py->add_module("mussaqui", &initmussaqui);
+      // quit when the interpreter exits
+      QObject::connect(interp, SIGNAL(finished()),
+                       &app, SLOT(quit()));
+      interp->start();
+      app.exec();
     } else 
 #endif /* USE_PYTHON */
     if (opts.useGUI) { 
-      MussaWindow win(opts.analysis);
-      win.show();
+      MussaWindow *win = new MussaWindow(opts.analysis);
+      win->show();
       app.exec();
     }
     return 0;
@@ -71,11 +94,11 @@ int main(int argc, char **argv)
   } catch (boost::filesystem::filesystem_error e) {
     qFatal(e.what());
   }
-#ifdef USE_PYTHON
-  catch (py::error_already_set e) {
+#ifdef USE_PYTHON   
+  catch( boost::python::error_already_set ) {
     PyErr_Print();
   }
-#endif /* USE_PYTHON */
+#endif    
   catch (std::runtime_error e) {
     qFatal(e.what());
   } catch (...) {