File size: 12,385 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************

 *   Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net>     *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/

#include <FCConfig.h>

#if HAVE_CONFIG_H
# include <config.h>
#endif  // HAVE_CONFIG_H

#ifdef _MSC_VER
# pragma warning(disable : 4005)
#endif

#include <QApplication>
#include <QIcon>
#if defined(Q_OS_WIN)
# include <windows.h>
#elif defined(Q_WS_X11)
# include <QX11EmbedWidget>
#endif
#include <thread>

// FreeCAD Base header
#include <App/Application.h>
#include <Base/Exception.h>
#include <Base/Factory.h>
#include <Base/Interpreter.h>
#include <Base/PyObjectBase.h>
#include <Gui/Application.h>
#include <Gui/BitmapFactory.h>
#include <Gui/MainWindow.h>
#include <Gui/StartupProcess.h>
#include <Gui/SoFCDB.h>
#include <Gui/Quarter/Quarter.h>
#include <Inventor/SoDB.h>
#include <Inventor/SoInteraction.h>
#include <Inventor/nodekits/SoNodeKit.h>


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) override

    {
        try {
            return QApplication::notify(receiver, event);
        }
        catch (const Base::SystemExitException& e) {
            exit(e.getExitCode());
            return true;
        }
    }
};

#if defined(Q_OS_WIN)
HHOOK hhook;

LRESULT CALLBACK FilterProc(int nCode, WPARAM wParam, LPARAM lParam)

{
    if (qApp) {
        qApp->sendPostedEvents(0, -1);  // special DeferredDelete
    }
    return CallNextHookEx(hhook, nCode, wParam, lParam);
}
#endif

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 nullptr;
    }

    static bool thr = false;
    if (!qApp) {
        if (Base::asBoolean(inThread) && !thr) {
            thr = true;
            std::thread t([]() {

                static int argc = 0;

                static char** argv = {nullptr};

                QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

                // This only works well if the QApplication is the very first created instance

                // of a QObject. Otherwise the application lives in a different thread than the

                // main thread which will cause hazardous behaviour.

                QtApplication app(argc, argv);

                if (setupMainWindow()) {

                    app.exec();

                }

            });
            t.detach();
        }
        else {
            // In order to get Jupiter notebook integration working we must create a direct instance
            // of QApplication. Not even a sub-class can be used because otherwise PySide2 wraps it
            // with a QtCore.QCoreApplication which will raise an exception in ipykernel
#if defined(Q_OS_WIN)
            static int argc = 0;
            static char** argv = {0};
            (void)new QApplication(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 = {nullptr};
            (void)new QApplication(argc, argv);
#else
            PyErr_SetString(PyExc_RuntimeError, "Must construct a QApplication before a QPaintDevice\n");
            return nullptr;
#endif
        }
    }
    else if (!qobject_cast<QApplication*>(qApp)) {
        PyErr_SetString(PyExc_RuntimeError, "Cannot create widget when no GUI is being used\n");
        return nullptr;
    }

    if (!thr) {
        if (!setupMainWindow()) {
            PyErr_SetString(PyExc_RuntimeError, "Cannot create main window\n");
            return nullptr;
        }
    }

    // if successful then enable Console logger
    Base::ILogger* console = Base::Console().get("Console");
    if (console) {
        console->bMsg = true;
        console->bWrn = true;
        console->bErr = true;
    }

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject* FreeCADGui_exec_loop(PyObject* /*self*/, PyObject* args)

{
    if (!PyArg_ParseTuple(args, "")) {
        return nullptr;
    }

    if (!qApp) {
        PyErr_SetString(PyExc_RuntimeError, "Must construct a QApplication before a QPaintDevice\n");
        return nullptr;
    }
    else if (!qobject_cast<QApplication*>(qApp)) {
        PyErr_SetString(PyExc_RuntimeError, "Cannot create widget when no GUI is being used\n");
        return nullptr;
    }

    qApp->exec();

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject* FreeCADGui_setupWithoutGUI(PyObject* /*self*/, PyObject* args)

{
    if (!PyArg_ParseTuple(args, "")) {
        return nullptr;
    }

    if (!Gui::Application::Instance) {
        static Gui::Application* app = new Gui::Application(false);
        _isSetupWithoutGui = true;
        Q_UNUSED(app);
    }
    if (!SoDB::isInitialized()) {
        // init the Inventor subsystem
        SoDB::init();
        SoNodeKit::init();
        SoInteraction::init();
    }
    if (!Gui::SoFCDB::isInitialized()) {
        Gui::SoFCDB::init();
    }

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject* FreeCADGui_embedToWindow(PyObject* /*self*/, PyObject* args)

{
    char* pointer;
    if (!PyArg_ParseTuple(args, "s", &pointer)) {
        return nullptr;
    }

    QWidget* widget = Gui::getMainWindow();
    if (!widget) {
        PyErr_SetString(Base::PyExc_FC_GeneralError, "No main window");
        return nullptr;
    }

    std::string pointer_str = pointer;
    std::stringstream str(pointer_str);

#if defined(Q_OS_WIN)
    void* window = 0;
    str >> window;
    HWND winid = (HWND)window;

    LONG oldLong = GetWindowLong(winid, GWL_STYLE);
    SetWindowLong(winid, GWL_STYLE, oldLong | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
    // SetWindowLong(widget->winId(), GWL_STYLE,
    //     WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
    SetParent((HWND)widget->winId(), winid);

    QEvent embeddingEvent(QEvent::EmbeddingControl);
    QApplication::sendEvent(widget, &embeddingEvent);
#elif defined(Q_WS_X11)
    WId winid;
    str >> winid;

    QX11EmbedWidget* x11 = new QX11EmbedWidget();
    widget->setParent(x11);
    x11->embedInto(winid);
    x11->show();
#else
    PyErr_SetString(PyExc_NotImplementedError, "Not implemented for this platform");
    return nullptr;
#endif

    Py_INCREF(Py_None);
    return Py_None;
}

struct PyMethodDef FreeCADGui_methods[] = {
    {"showMainWindow",
     FreeCADGui_showMainWindow,
     METH_VARARGS,
     "showMainWindow() -- Show the main window\n"
     "If no main window does exist one gets created"},
    {"exec_loop",
     FreeCADGui_exec_loop,
     METH_VARARGS,
     "exec_loop() -- Starts the event loop\n"
     "Note: this will block the call until the event loop has terminated"},
    {"setupWithoutGUI",
     FreeCADGui_setupWithoutGUI,
     METH_VARARGS,
     "setupWithoutGUI() -- Uses this module without starting\n"
     "an event loop or showing up any GUI\n"},
    {"embedToWindow",
     FreeCADGui_embedToWindow,
     METH_VARARGS,
     "embedToWindow() -- Embeds the main window into another window\n"},
    {nullptr, nullptr, 0, nullptr} /* sentinel */
};

static QWidget* setupMainWindow()

{
    if (!Gui::Application::Instance) {
        static Gui::Application* app = new Gui::Application(true);
        Q_UNUSED(app);
    }

    if (!Gui::MainWindow::getInstance()) {
        static bool hasMainWindow = false;
        if (hasMainWindow) {
            // if a main window existed and has been deleted it's not supported
            // to re-create it
            return nullptr;
        }

        Gui::StartupProcess process;
        process.execute();

        Base::PyGILStateLocker lock;
        // It's sufficient to create the config key
        App::Application::Config()["DontOverrideStdIn"] = "";
        Gui::MainWindow* mw = new Gui::MainWindow();
        hasMainWindow = true;

        QIcon icon = qApp->windowIcon();
        if (icon.isNull()) {
            qApp->setWindowIcon(
                Gui::BitmapFactory().pixmap(App::Application::Config()["AppIcon"].c_str())
            );
        }
        mw->setWindowIcon(qApp->windowIcon());

        try {
            Gui::StartupPostProcess postProcess(mw, *Gui::Application::Instance, qApp);
            postProcess.setLoadFromPythonModule(true);
            postProcess.execute();
        }
        catch (const Base::Exception&) {
            return nullptr;
        }
    }
    else {
        Gui::getMainWindow()->show();
    }

    return Gui::getMainWindow();
}

PyMOD_INIT_FUNC(FreeCADGui)
{
    try {
        // clang-format off
        Base::Interpreter().loadModule("FreeCAD");
        App::Application::Config()["AppIcon"] = "freecad";
        App::Application::Config()["SplashScreen"] = "freecadsplash";
        App::Application::Config()["CopyrightInfo"] = "\xc2\xa9 Juergen Riegel, Werner Mayer, Yorik van Havre and others 2001-2025\n";
        App::Application::Config()["LicenseInfo"] = "FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.\n";
        App::Application::Config()["CreditsInfo"] = "FreeCAD would not be possible without the FreeCAD community.\n";
        // clang-format on

        // it's possible that the GUI is already initialized when the Gui version of the executable
        // is started in command mode
        if (Base::Type::fromName("Gui::BaseView").isBad()) {
            Gui::Application::initApplication();
        }
        static struct PyModuleDef FreeCADGuiModuleDef = {
            PyModuleDef_HEAD_INIT,
            "FreeCADGui",
            "FreeCAD GUI module\n",
            -1,
            FreeCADGui_methods,
            nullptr,
            nullptr,
            nullptr,
            nullptr
        };
        PyObject* module = PyModule_Create(&FreeCADGuiModuleDef);
        return module;
    }
    catch (const Base::Exception& e) {
        PyErr_Format(PyExc_ImportError, "%s\n", e.what());
    }
    catch (...) {
        PyErr_SetString(PyExc_ImportError, "Unknown runtime error occurred");
    }
    return nullptr;
}