diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 66524c71af..dbb6c88a3d 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -147,14 +147,17 @@ namespace Gui { // Pimpl class struct ApplicationP { - ApplicationP() : + ApplicationP(bool GUIenabled) : activeDocument(0L), editDocument(0L), isClosing(false), startingUp(true) { // create the macro manager - macroMngr = new MacroManager(); + if (GUIenabled) + macroMngr = new MacroManager(); + else + macroMngr = nullptr; } ~ApplicationP() @@ -451,7 +454,7 @@ Application::Application(bool GUIenabled) View3DInventorViewerPy ::init_type(); AbstractSplitViewPy ::init_type(); - d = new ApplicationP; + d = new ApplicationP(GUIenabled); // global access Instance = this; diff --git a/src/Main/FreeCADGuiPy.cpp b/src/Main/FreeCADGuiPy.cpp index 55646793f4..3d7b5488a0 100644 --- a/src/Main/FreeCADGuiPy.cpp +++ b/src/Main/FreeCADGuiPy.cpp @@ -52,10 +52,30 @@ #include #include #include +#include +#include + +static bool _isSetupWithoutGui = false; static QWidget* setupMainWindow(); +class QtApplication : public QApplication { +public: + QtApplication(int &argc, char **argv) + : QApplication(argc, argv) { + } + bool notify (QObject * receiver, QEvent * event) { + try { + return QApplication::notify(receiver, event); + } + catch (const Base::SystemExitException& e) { + exit(e.getExitCode()); + return true; + } + } +}; + #if defined(Q_OS_WIN) HHOOK hhook; @@ -70,6 +90,11 @@ FilterProc(int nCode, WPARAM wParam, LPARAM lParam) { static PyObject * FreeCADGui_showMainWindow(PyObject * /*self*/, PyObject *args) { + if (_isSetupWithoutGui) { + PyErr_SetString(PyExc_RuntimeError, "Cannot call showMainWindow() after calling setupWithoutGUI()\n"); + return nullptr; + } + PyObject* inThread = Py_False; if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &inThread)) return NULL; @@ -84,7 +109,7 @@ FreeCADGui_showMainWindow(PyObject * /*self*/, PyObject *args) #if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)) QApplication::setAttribute(Qt::AA_ShareOpenGLContexts); #endif - QApplication app(argc, argv); + QtApplication app(argc, argv); if (setupMainWindow()) { app.exec(); } @@ -95,14 +120,14 @@ FreeCADGui_showMainWindow(PyObject * /*self*/, PyObject *args) #if defined(Q_OS_WIN) static int argc = 0; static char **argv = {0}; - (void)new QApplication(argc, argv); + (void)new QtApplication(argc, argv); // When QApplication is constructed hhook = SetWindowsHookEx(WH_GETMESSAGE, FilterProc, 0, GetCurrentThreadId()); #elif !defined(QT_NO_GLIB) static int argc = 0; static char **argv = {0}; - (void)new QApplication(argc, argv); + (void)new QtApplication(argc, argv); #else PyErr_SetString(PyExc_RuntimeError, "Must construct a QApplication before a QPaintDevice\n"); return NULL; @@ -154,6 +179,7 @@ FreeCADGui_setupWithoutGUI(PyObject * /*self*/, PyObject *args) if (!Gui::Application::Instance) { static Gui::Application *app = new Gui::Application(false); + _isSetupWithoutGui = true; Q_UNUSED(app); } else { @@ -164,7 +190,8 @@ FreeCADGui_setupWithoutGUI(PyObject * /*self*/, PyObject *args) if (!SoDB::isInitialized()) { // init the Inventor subsystem SoDB::init(); - SIM::Coin3D::Quarter::Quarter::init(); + SoNodeKit::init(); + SoInteraction::init(); } if (!Gui::SoFCDB::isInitialized()) { Gui::SoFCDB::init();