| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include <FCConfig.h> |
| 25 |
|
| 26 |
#if HAVE_CONFIG_H |
| 27 |
# include <config.h> |
| 28 |
#endif // HAVE_CONFIG_H |
| 29 |
|
| 30 |
#include <Build/Version.h> // For FCCopyrightYear |
| 31 |
|
| 32 |
#ifdef _MSC_VER |
| 33 |
# pragma warning(disable : 4005) |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <QApplication> |
| 37 |
#include <QIcon> |
| 38 |
#if defined(Q_OS_WIN) |
| 39 |
# include <windows.h> |
| 40 |
#elif defined(Q_WS_X11) |
| 41 |
# include <QX11EmbedWidget> |
| 42 |
#endif |
| 43 |
#include <thread> |
| 44 |
|
| 45 |
// FreeCAD Base header |
| 46 |
#include <App/Application.h> |
| 47 |
#include <Base/Exception.h> |
| 48 |
#include <Base/Factory.h> |
| 49 |
#include <Base/Interpreter.h> |
| 50 |
#include <Base/PyObjectBase.h> |
| 51 |
#include <Gui/Application.h> |
| 52 |
#include <Gui/BitmapFactory.h> |
| 53 |
#include <Gui/MainWindow.h> |
| 54 |
#include <Gui/StartupProcess.h> |
| 55 |
#include <Gui/SoFCDB.h> |
| 56 |
#include <Gui/Quarter/Quarter.h> |
| 57 |
#include <Inventor/SoDB.h> |
| 58 |
#include <Inventor/SoInteraction.h> |
| 59 |
#include <Inventor/nodekits/SoNodeKit.h> |
| 60 |
|
| 61 |
|
| 62 |
static bool _isSetupWithoutGui = false; |
| 63 |
|
| 64 |
static QWidget* setupMainWindow(); |
| 65 |
|
| 66 |
class QtApplication: public QApplication |
| 67 |
{ |
| 68 |
public: |
| 69 |
QtApplication(int& argc, char** argv) |
| 70 |
: QApplication(argc, argv) |
| 71 |
{} |
| 72 |
bool notify(QObject* receiver, QEvent* event) override |
| 73 |
{ |
| 74 |
try { |
| 75 |
return QApplication::notify(receiver, event); |
| 76 |
} |
| 77 |
catch (const Base::SystemExitException& e) { |
| 78 |
exit(e.getExitCode()); |
| 79 |
return true; |
| 80 |
} |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
#if defined(Q_OS_WIN) |
| 85 |
HHOOK hhook; |
| 86 |
|
| 87 |
LRESULT CALLBACK FilterProc(int nCode, WPARAM wParam, LPARAM lParam) |
| 88 |
{ |
| 89 |
if (qApp) { |
| 90 |
qApp->sendPostedEvents(0, -1); // special DeferredDelete |
| 91 |
} |
| 92 |
return CallNextHookEx(hhook, nCode, wParam, lParam); |
| 93 |
} |
| 94 |
#endif |
| 95 |
|
| 96 |
static PyObject* FreeCADGui_showMainWindow(PyObject* /*self*/, PyObject* args) |
| 97 |
{ |
| 98 |
if (_isSetupWithoutGui) { |
| 99 |
PyErr_SetString( |
| 100 |
PyExc_RuntimeError, |
| 101 |
"Cannot call showMainWindow() after calling setupWithoutGUI()\n" |
| 102 |
); |
| 103 |
return nullptr; |
| 104 |
} |
| 105 |
|
| 106 |
PyObject* inThread = Py_False; |
| 107 |
if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &inThread)) { |
| 108 |
return nullptr; |
| 109 |
} |
| 110 |
|
| 111 |
static bool thr = false; |
| 112 |
if (!qApp) { |
| 113 |
if (Base::asBoolean(inThread) && !thr) { |
| 114 |
thr = true; |
| 115 |
std::thread t([]() { |
| 116 |
static int argc = 0; |
| 117 |
static char** argv = {nullptr}; |
| 118 |
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts); |
| 119 |
// This only works well if the QApplication is the very first created instance |
| 120 |
// of a QObject. Otherwise the application lives in a different thread than the |
| 121 |
// main thread which will cause hazardous behaviour. |
| 122 |
QtApplication app(argc, argv); |
| 123 |
if (setupMainWindow()) { |
| 124 |
app.exec(); |
| 125 |
} |
| 126 |
}); |
| 127 |
t.detach(); |
| 128 |
} |
| 129 |
else { |
| 130 |
// In order to get Jupiter notebook integration working we must create a direct instance |
| 131 |
// of QApplication. Not even a sub-class can be used because otherwise PySide2 wraps it |
| 132 |
// with a QtCore.QCoreApplication which will raise an exception in ipykernel |
| 133 |
#if defined(Q_OS_WIN) |
| 134 |
static int argc = 0; |
| 135 |
static char** argv = {0}; |
| 136 |
(void)new QApplication(argc, argv); |
| 137 |
// When QApplication is constructed |
| 138 |
hhook = SetWindowsHookEx(WH_GETMESSAGE, FilterProc, 0, GetCurrentThreadId()); |
| 139 |
#elif !defined(QT_NO_GLIB) |
| 140 |
static int argc = 0; |
| 141 |
static char** argv = {nullptr}; |
| 142 |
(void)new QApplication(argc, argv); |
| 143 |
#else |
| 144 |
PyErr_SetString(PyExc_RuntimeError, "Must construct a QApplication before a QPaintDevice\n"); |
| 145 |
return nullptr; |
| 146 |
#endif |
| 147 |
} |
| 148 |
} |
| 149 |
else if (!qobject_cast<QApplication*>(qApp)) { |
| 150 |
PyErr_SetString(PyExc_RuntimeError, "Cannot create widget when no GUI is being used\n"); |
| 151 |
return nullptr; |
| 152 |
} |
| 153 |
|
| 154 |
if (!thr) { |
| 155 |
if (!setupMainWindow()) { |
| 156 |
PyErr_SetString(PyExc_RuntimeError, "Cannot create main window\n"); |
| 157 |
return nullptr; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// if successful then enable Console logger |
| 162 |
Base::ILogger* console = Base::Console().get("Console"); |
| 163 |
if (console) { |
| 164 |
console->bMsg = true; |
| 165 |
console->bWrn = true; |
| 166 |
console->bErr = true; |
| 167 |
} |
| 168 |
|
| 169 |
Py_INCREF(Py_None); |
| 170 |
return Py_None; |
| 171 |
} |
| 172 |
|
| 173 |
static PyObject* FreeCADGui_exec_loop(PyObject* /*self*/, PyObject* args) |
| 174 |
{ |
| 175 |
if (!PyArg_ParseTuple(args, "")) { |
| 176 |
return nullptr; |
| 177 |
} |
| 178 |
|
| 179 |
if (!qApp) { |
| 180 |
PyErr_SetString(PyExc_RuntimeError, "Must construct a QApplication before a QPaintDevice\n"); |
| 181 |
return nullptr; |
| 182 |
} |
| 183 |
else if (!qobject_cast<QApplication*>(qApp)) { |
| 184 |
PyErr_SetString(PyExc_RuntimeError, "Cannot create widget when no GUI is being used\n"); |
| 185 |
return nullptr; |
| 186 |
} |
| 187 |
|
| 188 |
qApp->exec(); |
| 189 |
|
| 190 |
Py_INCREF(Py_None); |
| 191 |
return Py_None; |
| 192 |
} |
| 193 |
|
| 194 |
static PyObject* FreeCADGui_setupWithoutGUI(PyObject* /*self*/, PyObject* args) |
| 195 |
{ |
| 196 |
if (!PyArg_ParseTuple(args, "")) { |
| 197 |
return nullptr; |
| 198 |
} |
| 199 |
|
| 200 |
if (!Gui::Application::Instance) { |
| 201 |
static Gui::Application* app = new Gui::Application(false); |
| 202 |
_isSetupWithoutGui = true; |
| 203 |
Q_UNUSED(app); |
| 204 |
} |
| 205 |
if (!SoDB::isInitialized()) { |
| 206 |
// init the Inventor subsystem |
| 207 |
SoDB::init(); |
| 208 |
SoNodeKit::init(); |
| 209 |
SoInteraction::init(); |
| 210 |
} |
| 211 |
if (!Gui::SoFCDB::isInitialized()) { |
| 212 |
Gui::SoFCDB::init(); |
| 213 |
} |
| 214 |
|
| 215 |
Py_INCREF(Py_None); |
| 216 |
return Py_None; |
| 217 |
} |
| 218 |
|
| 219 |
static PyObject* FreeCADGui_embedToWindow(PyObject* /*self*/, PyObject* args) |
| 220 |
{ |
| 221 |
char* pointer; |
| 222 |
if (!PyArg_ParseTuple(args, "s", &pointer)) { |
| 223 |
return nullptr; |
| 224 |
} |
| 225 |
|
| 226 |
QWidget* widget = Gui::getMainWindow(); |
| 227 |
if (!widget) { |
| 228 |
PyErr_SetString(Base::PyExc_FC_GeneralError, "No main window"); |
| 229 |
return nullptr; |
| 230 |
} |
| 231 |
|
| 232 |
std::string pointer_str = pointer; |
| 233 |
std::stringstream str(pointer_str); |
| 234 |
|
| 235 |
#if defined(Q_OS_WIN) |
| 236 |
void* window = 0; |
| 237 |
str >> window; |
| 238 |
HWND winid = (HWND)window; |
| 239 |
|
| 240 |
LONG oldLong = GetWindowLong(winid, GWL_STYLE); |
| 241 |
SetWindowLong(winid, GWL_STYLE, oldLong | WS_CLIPCHILDREN | WS_CLIPSIBLINGS); |
| 242 |
// SetWindowLong(widget->winId(), GWL_STYLE, |
| 243 |
// WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS); |
| 244 |
SetParent((HWND)widget->winId(), winid); |
| 245 |
|
| 246 |
QEvent embeddingEvent(QEvent::EmbeddingControl); |
| 247 |
QApplication::sendEvent(widget, &embeddingEvent); |
| 248 |
#elif defined(Q_WS_X11) |
| 249 |
WId winid; |
| 250 |
str >> winid; |
| 251 |
|
| 252 |
QX11EmbedWidget* x11 = new QX11EmbedWidget(); |
| 253 |
widget->setParent(x11); |
| 254 |
x11->embedInto(winid); |
| 255 |
x11->show(); |
| 256 |
#else |
| 257 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented for this platform"); |
| 258 |
return nullptr; |
| 259 |
#endif |
| 260 |
|
| 261 |
Py_INCREF(Py_None); |
| 262 |
return Py_None; |
| 263 |
} |
| 264 |
|
| 265 |
struct PyMethodDef FreeCADGui_methods[] = { |
| 266 |
{"showMainWindow", |
| 267 |
FreeCADGui_showMainWindow, |
| 268 |
METH_VARARGS, |
| 269 |
"showMainWindow() -- Show the main window\n" |
| 270 |
"If no main window does exist one gets created"}, |
| 271 |
{"exec_loop", |
| 272 |
FreeCADGui_exec_loop, |
| 273 |
METH_VARARGS, |
| 274 |
"exec_loop() -- Starts the event loop\n" |
| 275 |
"Note: this will block the call until the event loop has terminated"}, |
| 276 |
{"setupWithoutGUI", |
| 277 |
FreeCADGui_setupWithoutGUI, |
| 278 |
METH_VARARGS, |
| 279 |
"setupWithoutGUI() -- Uses this module without starting\n" |
| 280 |
"an event loop or showing up any GUI\n"}, |
| 281 |
{"embedToWindow", |
| 282 |
FreeCADGui_embedToWindow, |
| 283 |
METH_VARARGS, |
| 284 |
"embedToWindow() -- Embeds the main window into another window\n"}, |
| 285 |
{nullptr, nullptr, 0, nullptr} /* sentinel */ |
| 286 |
}; |
| 287 |
|
| 288 |
static QWidget* setupMainWindow() |
| 289 |
{ |
| 290 |
if (!Gui::Application::Instance) { |
| 291 |
static Gui::Application* app = new Gui::Application(true); |
| 292 |
Q_UNUSED(app); |
| 293 |
} |
| 294 |
|
| 295 |
if (!Gui::MainWindow::getInstance()) { |
| 296 |
static bool hasMainWindow = false; |
| 297 |
if (hasMainWindow) { |
| 298 |
// if a main window existed and has been deleted it's not supported |
| 299 |
// to re-create it |
| 300 |
return nullptr; |
| 301 |
} |
| 302 |
|
| 303 |
Gui::StartupProcess process; |
| 304 |
process.execute(); |
| 305 |
|
| 306 |
Base::PyGILStateLocker lock; |
| 307 |
// It's sufficient to create the config key |
| 308 |
App::Application::Config()["DontOverrideStdIn"] = ""; |
| 309 |
Gui::MainWindow* mw = new Gui::MainWindow(); |
| 310 |
hasMainWindow = true; |
| 311 |
|
| 312 |
QIcon icon = qApp->windowIcon(); |
| 313 |
if (icon.isNull()) { |
| 314 |
qApp->setWindowIcon( |
| 315 |
Gui::BitmapFactory().pixmap(App::Application::Config()["AppIcon"].c_str()) |
| 316 |
); |
| 317 |
} |
| 318 |
mw->setWindowIcon(qApp->windowIcon()); |
| 319 |
|
| 320 |
try { |
| 321 |
Gui::StartupPostProcess postProcess(mw, *Gui::Application::Instance, qApp); |
| 322 |
postProcess.setLoadFromPythonModule(true); |
| 323 |
postProcess.execute(); |
| 324 |
} |
| 325 |
catch (const Base::Exception&) { |
| 326 |
return nullptr; |
| 327 |
} |
| 328 |
} |
| 329 |
else { |
| 330 |
Gui::getMainWindow()->show(); |
| 331 |
} |
| 332 |
|
| 333 |
return Gui::getMainWindow(); |
| 334 |
} |
| 335 |
|
| 336 |
PyMOD_INIT_FUNC(FreeCADGui) |
| 337 |
{ |
| 338 |
try { |
| 339 |
// clang-format off |
| 340 |
Base::Interpreter().loadModule("FreeCAD"); |
| 341 |
App::Application::Config()["AppIcon"] = "freecad"; |
| 342 |
App::Application::Config()["SplashScreen"] = "freecadsplash"; |
| 343 |
App::Application::Config()["CopyrightInfo"] = fmt::format("\xc2\xa9 Juergen Riegel, Werner Mayer, Yorik van Havre and others 2001-{}\n", FCCopyrightYear); |
| 344 |
App::Application::Config()["LicenseInfo"] = "FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.\n"; |
| 345 |
App::Application::Config()["CreditsInfo"] = "FreeCAD would not be possible without the FreeCAD community.\n"; |
| 346 |
// clang-format on |
| 347 |
|
| 348 |
// it's possible that the GUI is already initialized when the Gui version of the executable |
| 349 |
// is started in command mode |
| 350 |
if (Base::Type::fromName("Gui::BaseView").isBad()) { |
| 351 |
Gui::Application::initApplication(); |
| 352 |
} |
| 353 |
static struct PyModuleDef FreeCADGuiModuleDef = { |
| 354 |
PyModuleDef_HEAD_INIT, |
| 355 |
"FreeCADGui", |
| 356 |
"FreeCAD GUI module\n", |
| 357 |
-1, |
| 358 |
FreeCADGui_methods, |
| 359 |
nullptr, |
| 360 |
nullptr, |
| 361 |
nullptr, |
| 362 |
nullptr |
| 363 |
}; |
| 364 |
PyObject* module = PyModule_Create(&FreeCADGuiModuleDef); |
| 365 |
return module; |
| 366 |
} |
| 367 |
catch (const Base::Exception& e) { |
| 368 |
PyErr_Format(PyExc_ImportError, "%s\n", e.what()); |
| 369 |
} |
| 370 |
catch (...) { |
| 371 |
PyErr_SetString(PyExc_ImportError, "Unknown runtime error occurred"); |
| 372 |
} |
| 373 |
return nullptr; |
| 374 |
} |