X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=blobdiff_plain;f=qui%2Fmussagl.cpp;h=d0df4c6c05dfc5e56c0e9ed73e6f8ade53c71176;hb=02fc4144044a430f8bee6617c0c9871d4ebe5e4d;hp=1b6dd95464f83a4ed851d23f95705e0dd79a904b;hpb=ad9038e6fc43fef0b3b3dd974be37c0eaeec3187;p=mussa.git diff --git a/qui/mussagl.cpp b/qui/mussagl.cpp index 1b6dd95..d0df4c6 100644 --- a/qui/mussagl.cpp +++ b/qui/mussagl.cpp @@ -1,16 +1,43 @@ #include -#include - using namespace boost::filesystem; -#include -#include -#include +#ifdef USE_PYTHON +#include "py/python.hpp" +extern "C" void initmussa(); +//extern "C" void initmussaqui(); +#endif #include "qui/MussaWindow.hpp" +#include "qui/threading/ThreadManager.hpp" +#include "qui/threading/InterpreterThread.hpp" #include "alg/parse_options.hpp" #include "mussa_exceptions.hpp" +#include +#include +#include + +#if defined(Q_WS_MAC) +#include +#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) { MussaOptions opts; @@ -18,43 +45,47 @@ int main(int argc, char **argv) // if we're under unix and don't have a display, see if we can still run #ifdef Q_WS_X11 opts.useGUI = getenv("DISPLAY") != 0; + if (opts.useGUI == false) { + 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); - //PyImport_AppendInittab("mussa", initmussa); - Py_Initialize(); initialize_mussa(opts, argc, argv); - if (opts.analysis == 0) { return 1; } - + try { +#ifdef USE_PYTHON if (opts.runAsPythonInterpeter) { - boost::python::object main_module((boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("__main__"))))); - boost::python::object main_namespace = main_module.attr("__dict__"); - - boost::python::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()) - )); - boost::python::object square = main_module.attr("square"); - int result = boost::python::extract(square(3)); - std::cout << "C++ has " << result << std::endl; - PyRun_InteractiveLoop(stdin, "mussa"); - } else if (opts.useGUI) { - MussaWindow win(opts.analysis); - win.show(); + 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 = new MussaWindow(opts.analysis); + win->show(); app.exec(); } return 0; @@ -62,13 +93,16 @@ int main(int argc, char **argv) qFatal(e.what()); } catch (boost::filesystem::filesystem_error e) { qFatal(e.what()); - } catch (boost::python::error_already_set e) { + } +#ifdef USE_PYTHON + catch( boost::python::error_already_set ) { PyErr_Print(); - } catch (std::runtime_error e) { + } +#endif + catch (std::runtime_error e) { qFatal(e.what()); } catch (...) { qFatal("unrecognized exception"); } - Py_Finalize(); return 1; }