1
/***************************************************************************
2
* Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
27
#endif // HAVE_CONFIG_H
30
#pragma warning(disable : 4005)
33
#include <QApplication>
37
#elif defined(Q_WS_X11)
38
#include <QX11EmbedWidget>
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>
59
static bool _isSetupWithoutGui = false;
61
static QWidget* setupMainWindow();
63
class QtApplication: public QApplication
66
QtApplication(int& argc, char** argv)
67
: QApplication(argc, argv)
69
bool notify(QObject* receiver, QEvent* event) override
72
return QApplication::notify(receiver, event);
74
catch (const Base::SystemExitException& e) {
75
exit(e.getExitCode());
84
LRESULT CALLBACK FilterProc(int nCode, WPARAM wParam, LPARAM lParam)
87
qApp->sendPostedEvents(0, -1); // special DeferredDelete
89
return CallNextHookEx(hhook, nCode, wParam, lParam);
93
static PyObject* FreeCADGui_showMainWindow(PyObject* /*self*/, PyObject* args)
95
if (_isSetupWithoutGui) {
96
PyErr_SetString(PyExc_RuntimeError,
97
"Cannot call showMainWindow() after calling setupWithoutGUI()\n");
101
PyObject* inThread = Py_False;
102
if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &inThread)) {
106
static bool thr = false;
108
if (Base::asBoolean(inThread) && !thr) {
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()) {
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
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)
136
static char** argv = {nullptr};
137
(void)new QApplication(argc, argv);
139
PyErr_SetString(PyExc_RuntimeError,
140
"Must construct a QApplication before a QPaintDevice\n");
145
else if (!qobject_cast<QApplication*>(qApp)) {
146
PyErr_SetString(PyExc_RuntimeError, "Cannot create widget when no GUI is being used\n");
151
if (!setupMainWindow()) {
152
PyErr_SetString(PyExc_RuntimeError, "Cannot create main window\n");
157
// if successful then enable Console logger
158
Base::ILogger* console = Base::Console().Get("Console");
160
console->bMsg = true;
161
console->bWrn = true;
162
console->bErr = true;
169
static PyObject* FreeCADGui_exec_loop(PyObject* /*self*/, PyObject* args)
171
if (!PyArg_ParseTuple(args, "")) {
176
PyErr_SetString(PyExc_RuntimeError,
177
"Must construct a QApplication before a QPaintDevice\n");
180
else if (!qobject_cast<QApplication*>(qApp)) {
181
PyErr_SetString(PyExc_RuntimeError, "Cannot create widget when no GUI is being used\n");
191
static PyObject* FreeCADGui_setupWithoutGUI(PyObject* /*self*/, PyObject* args)
193
if (!PyArg_ParseTuple(args, "")) {
197
if (!Gui::Application::Instance) {
198
static Gui::Application* app = new Gui::Application(false);
199
_isSetupWithoutGui = true;
202
if (!SoDB::isInitialized()) {
203
// init the Inventor subsystem
206
SoInteraction::init();
208
if (!Gui::SoFCDB::isInitialized()) {
216
static PyObject* FreeCADGui_embedToWindow(PyObject* /*self*/, PyObject* args)
219
if (!PyArg_ParseTuple(args, "s", &pointer)) {
223
QWidget* widget = Gui::getMainWindow();
225
PyErr_SetString(Base::PyExc_FC_GeneralError, "No main window");
229
std::string pointer_str = pointer;
230
std::stringstream str(pointer_str);
235
HWND winid = (HWND)window;
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);
243
QEvent embeddingEvent(QEvent::EmbeddingControl);
244
QApplication::sendEvent(widget, &embeddingEvent);
245
#elif defined(Q_WS_X11)
249
QX11EmbedWidget* x11 = new QX11EmbedWidget();
250
widget->setParent(x11);
251
x11->embedInto(winid);
254
PyErr_SetString(PyExc_NotImplementedError, "Not implemented for this platform");
262
struct PyMethodDef FreeCADGui_methods[] = {
264
FreeCADGui_showMainWindow,
266
"showMainWindow() -- Show the main window\n"
267
"If no main window does exist one gets created"},
269
FreeCADGui_exec_loop,
271
"exec_loop() -- Starts the event loop\n"
272
"Note: this will block the call until the event loop has terminated"},
274
FreeCADGui_setupWithoutGUI,
276
"setupWithoutGUI() -- Uses this module without starting\n"
277
"an event loop or showing up any GUI\n"},
279
FreeCADGui_embedToWindow,
281
"embedToWindow() -- Embeds the main window into another window\n"},
282
{nullptr, nullptr, 0, nullptr} /* sentinel */
285
static QWidget* setupMainWindow()
287
if (!Gui::Application::Instance) {
288
static Gui::Application* app = new Gui::Application(true);
292
if (!Gui::MainWindow::getInstance()) {
293
static bool hasMainWindow = false;
295
// if a main window existed and has been deleted it's not supported
300
Gui::StartupProcess process;
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;
309
QIcon icon = qApp->windowIcon();
312
Gui::BitmapFactory().pixmap(App::Application::Config()["AppIcon"].c_str()));
314
mw->setWindowIcon(qApp->windowIcon());
317
Gui::StartupPostProcess postProcess(mw, *Gui::Application::Instance, qApp);
318
postProcess.setLoadFromPythonModule(true);
319
postProcess.execute();
321
catch (const Base::Exception&) {
326
Gui::getMainWindow()->show();
329
return Gui::getMainWindow();
332
PyMOD_INIT_FUNC(FreeCADGui)
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";
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();
349
static struct PyModuleDef FreeCADGuiModuleDef = {PyModuleDef_HEAD_INIT,
351
"FreeCAD GUI module\n",
358
PyObject* module = PyModule_Create(&FreeCADGuiModuleDef);
361
catch (const Base::Exception& e) {
362
PyErr_Format(PyExc_ImportError, "%s\n", e.what());
365
PyErr_SetString(PyExc_ImportError, "Unknown runtime error occurred");