FreeCAD

Форк
0
/
FreeCADGuiPy.cpp 
368 строк · 12.4 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include <FCConfig.h>
24

25
#if HAVE_CONFIG_H
26
#include <config.h>
27
#endif  // HAVE_CONFIG_H
28

29
#ifdef _MSC_VER
30
#pragma warning(disable : 4005)
31
#endif
32

33
#include <QApplication>
34
#include <QIcon>
35
#if defined(Q_OS_WIN)
36
#include <windows.h>
37
#elif defined(Q_WS_X11)
38
#include <QX11EmbedWidget>
39
#endif
40
#include <thread>
41

42
// FreeCAD Base header
43
#include <App/Application.h>
44
#include <Base/Exception.h>
45
#include <Base/Factory.h>
46
#include <Base/Interpreter.h>
47
#include <Base/PyObjectBase.h>
48
#include <Gui/Application.h>
49
#include <Gui/BitmapFactory.h>
50
#include <Gui/MainWindow.h>
51
#include <Gui/StartupProcess.h>
52
#include <Gui/SoFCDB.h>
53
#include <Gui/Quarter/Quarter.h>
54
#include <Inventor/SoDB.h>
55
#include <Inventor/SoInteraction.h>
56
#include <Inventor/nodekits/SoNodeKit.h>
57

58

59
static bool _isSetupWithoutGui = false;
60

61
static QWidget* setupMainWindow();
62

63
class QtApplication: public QApplication
64
{
65
public:
66
    QtApplication(int& argc, char** argv)
67
        : QApplication(argc, argv)
68
    {}
69
    bool notify(QObject* receiver, QEvent* event) override
70
    {
71
        try {
72
            return QApplication::notify(receiver, event);
73
        }
74
        catch (const Base::SystemExitException& e) {
75
            exit(e.getExitCode());
76
            return true;
77
        }
78
    }
79
};
80

81
#if defined(Q_OS_WIN)
82
HHOOK hhook;
83

84
LRESULT CALLBACK FilterProc(int nCode, WPARAM wParam, LPARAM lParam)
85
{
86
    if (qApp) {
87
        qApp->sendPostedEvents(0, -1);  // special DeferredDelete
88
    }
89
    return CallNextHookEx(hhook, nCode, wParam, lParam);
90
}
91
#endif
92

93
static PyObject* FreeCADGui_showMainWindow(PyObject* /*self*/, PyObject* args)
94
{
95
    if (_isSetupWithoutGui) {
96
        PyErr_SetString(PyExc_RuntimeError,
97
                        "Cannot call showMainWindow() after calling setupWithoutGUI()\n");
98
        return nullptr;
99
    }
100

101
    PyObject* inThread = Py_False;
102
    if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &inThread)) {
103
        return nullptr;
104
    }
105

106
    static bool thr = false;
107
    if (!qApp) {
108
        if (Base::asBoolean(inThread) && !thr) {
109
            thr = true;
110
            std::thread t([]() {
111
                static int argc = 0;
112
                static char** argv = {nullptr};
113
                QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
114
                // This only works well if the QApplication is the very first created instance
115
                // of a QObject. Otherwise the application lives in a different thread than the
116
                // main thread which will cause hazardous behaviour.
117
                QtApplication app(argc, argv);
118
                if (setupMainWindow()) {
119
                    app.exec();
120
                }
121
            });
122
            t.detach();
123
        }
124
        else {
125
            // In order to get Jupiter notebook integration working we must create a direct instance
126
            // of QApplication. Not even a sub-class can be used because otherwise PySide2 wraps it
127
            // with a QtCore.QCoreApplication which will raise an exception in ipykernel
128
#if defined(Q_OS_WIN)
129
            static int argc = 0;
130
            static char** argv = {0};
131
            (void)new QApplication(argc, argv);
132
            // When QApplication is constructed
133
            hhook = SetWindowsHookEx(WH_GETMESSAGE, FilterProc, 0, GetCurrentThreadId());
134
#elif !defined(QT_NO_GLIB)
135
            static int argc = 0;
136
            static char** argv = {nullptr};
137
            (void)new QApplication(argc, argv);
138
#else
139
            PyErr_SetString(PyExc_RuntimeError,
140
                            "Must construct a QApplication before a QPaintDevice\n");
141
            return nullptr;
142
#endif
143
        }
144
    }
145
    else if (!qobject_cast<QApplication*>(qApp)) {
146
        PyErr_SetString(PyExc_RuntimeError, "Cannot create widget when no GUI is being used\n");
147
        return nullptr;
148
    }
149

150
    if (!thr) {
151
        if (!setupMainWindow()) {
152
            PyErr_SetString(PyExc_RuntimeError, "Cannot create main window\n");
153
            return nullptr;
154
        }
155
    }
156

157
    // if successful then enable Console logger
158
    Base::ILogger* console = Base::Console().Get("Console");
159
    if (console) {
160
        console->bMsg = true;
161
        console->bWrn = true;
162
        console->bErr = true;
163
    }
164

165
    Py_INCREF(Py_None);
166
    return Py_None;
167
}
168

169
static PyObject* FreeCADGui_exec_loop(PyObject* /*self*/, PyObject* args)
170
{
171
    if (!PyArg_ParseTuple(args, "")) {
172
        return nullptr;
173
    }
174

175
    if (!qApp) {
176
        PyErr_SetString(PyExc_RuntimeError,
177
                        "Must construct a QApplication before a QPaintDevice\n");
178
        return nullptr;
179
    }
180
    else if (!qobject_cast<QApplication*>(qApp)) {
181
        PyErr_SetString(PyExc_RuntimeError, "Cannot create widget when no GUI is being used\n");
182
        return nullptr;
183
    }
184

185
    qApp->exec();
186

187
    Py_INCREF(Py_None);
188
    return Py_None;
189
}
190

191
static PyObject* FreeCADGui_setupWithoutGUI(PyObject* /*self*/, PyObject* args)
192
{
193
    if (!PyArg_ParseTuple(args, "")) {
194
        return nullptr;
195
    }
196

197
    if (!Gui::Application::Instance) {
198
        static Gui::Application* app = new Gui::Application(false);
199
        _isSetupWithoutGui = true;
200
        Q_UNUSED(app);
201
    }
202
    if (!SoDB::isInitialized()) {
203
        // init the Inventor subsystem
204
        SoDB::init();
205
        SoNodeKit::init();
206
        SoInteraction::init();
207
    }
208
    if (!Gui::SoFCDB::isInitialized()) {
209
        Gui::SoFCDB::init();
210
    }
211

212
    Py_INCREF(Py_None);
213
    return Py_None;
214
}
215

216
static PyObject* FreeCADGui_embedToWindow(PyObject* /*self*/, PyObject* args)
217
{
218
    char* pointer;
219
    if (!PyArg_ParseTuple(args, "s", &pointer)) {
220
        return nullptr;
221
    }
222

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

229
    std::string pointer_str = pointer;
230
    std::stringstream str(pointer_str);
231

232
#if defined(Q_OS_WIN)
233
    void* window = 0;
234
    str >> window;
235
    HWND winid = (HWND)window;
236

237
    LONG oldLong = GetWindowLong(winid, GWL_STYLE);
238
    SetWindowLong(winid, GWL_STYLE, oldLong | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
239
    // SetWindowLong(widget->winId(), GWL_STYLE,
240
    //     WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
241
    SetParent((HWND)widget->winId(), winid);
242

243
    QEvent embeddingEvent(QEvent::EmbeddingControl);
244
    QApplication::sendEvent(widget, &embeddingEvent);
245
#elif defined(Q_WS_X11)
246
    WId winid;
247
    str >> winid;
248

249
    QX11EmbedWidget* x11 = new QX11EmbedWidget();
250
    widget->setParent(x11);
251
    x11->embedInto(winid);
252
    x11->show();
253
#else
254
    PyErr_SetString(PyExc_NotImplementedError, "Not implemented for this platform");
255
    return nullptr;
256
#endif
257

258
    Py_INCREF(Py_None);
259
    return Py_None;
260
}
261

262
struct PyMethodDef FreeCADGui_methods[] = {
263
    {"showMainWindow",
264
     FreeCADGui_showMainWindow,
265
     METH_VARARGS,
266
     "showMainWindow() -- Show the main window\n"
267
     "If no main window does exist one gets created"},
268
    {"exec_loop",
269
     FreeCADGui_exec_loop,
270
     METH_VARARGS,
271
     "exec_loop() -- Starts the event loop\n"
272
     "Note: this will block the call until the event loop has terminated"},
273
    {"setupWithoutGUI",
274
     FreeCADGui_setupWithoutGUI,
275
     METH_VARARGS,
276
     "setupWithoutGUI() -- Uses this module without starting\n"
277
     "an event loop or showing up any GUI\n"},
278
    {"embedToWindow",
279
     FreeCADGui_embedToWindow,
280
     METH_VARARGS,
281
     "embedToWindow() -- Embeds the main window into another window\n"},
282
    {nullptr, nullptr, 0, nullptr} /* sentinel */
283
};
284

285
static QWidget* setupMainWindow()
286
{
287
    if (!Gui::Application::Instance) {
288
        static Gui::Application* app = new Gui::Application(true);
289
        Q_UNUSED(app);
290
    }
291

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

300
        Gui::StartupProcess process;
301
        process.execute();
302

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

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

316
        try {
317
            Gui::StartupPostProcess postProcess(mw, *Gui::Application::Instance, qApp);
318
            postProcess.setLoadFromPythonModule(true);
319
            postProcess.execute();
320
        }
321
        catch (const Base::Exception&) {
322
            return nullptr;
323
        }
324
    }
325
    else {
326
        Gui::getMainWindow()->show();
327
    }
328

329
    return Gui::getMainWindow();
330
}
331

332
PyMOD_INIT_FUNC(FreeCADGui)
333
{
334
    try {
335
        // clang-format off
336
        Base::Interpreter().loadModule("FreeCAD");
337
        App::Application::Config()["AppIcon"] = "freecad";
338
        App::Application::Config()["SplashScreen"] = "freecadsplash";
339
        App::Application::Config()["CopyrightInfo"] = "\xc2\xa9 Juergen Riegel, Werner Mayer, Yorik van Havre and others 2001-2024\n";
340
        App::Application::Config()["LicenseInfo"] = "FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.\n";
341
        App::Application::Config()["CreditsInfo"] = "FreeCAD wouldn't be possible without FreeCAD community.\n";
342
        // clang-format on
343

344
        // it's possible that the GUI is already initialized when the Gui version of the executable
345
        // is started in command mode
346
        if (Base::Type::fromName("Gui::BaseView").isBad()) {
347
            Gui::Application::initApplication();
348
        }
349
        static struct PyModuleDef FreeCADGuiModuleDef = {PyModuleDef_HEAD_INIT,
350
                                                         "FreeCADGui",
351
                                                         "FreeCAD GUI module\n",
352
                                                         -1,
353
                                                         FreeCADGui_methods,
354
                                                         nullptr,
355
                                                         nullptr,
356
                                                         nullptr,
357
                                                         nullptr};
358
        PyObject* module = PyModule_Create(&FreeCADGuiModuleDef);
359
        return module;
360
    }
361
    catch (const Base::Exception& e) {
362
        PyErr_Format(PyExc_ImportError, "%s\n", e.what());
363
    }
364
    catch (...) {
365
        PyErr_SetString(PyExc_ImportError, "Unknown runtime error occurred");
366
    }
367
    return nullptr;
368
}
369

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.