Skip to content

Commit

Permalink
Main: Apply clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jun 3, 2024
1 parent 859d47b commit fb7b531
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 274 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
files: |
(?x)^(
src/Base|
src/Main|
src/Tools|
tests/src|
src/Mod/AddonManager|
Expand Down
136 changes: 76 additions & 60 deletions src/Main/FreeCADGuiPy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#include <FCConfig.h>

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

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

#include <QApplication>
Expand Down Expand Up @@ -58,15 +58,16 @@

static bool _isSetupWithoutGui = false;

static
QWidget* setupMainWindow();
static QWidget* setupMainWindow();

class QtApplication : public QApplication {
class QtApplication: public QApplication
{
public:
QtApplication(int &argc, char **argv)
: QApplication(argc, argv) {
}
bool notify (QObject * receiver, QEvent * event) override {
QtApplication(int& argc, char** argv)
: QApplication(argc, argv)
{}
bool notify(QObject* receiver, QEvent* event) override
{
try {
return QApplication::notify(receiver, event);
}
Expand All @@ -80,33 +81,35 @@ class QtApplication : public QApplication {
#if defined(Q_OS_WIN)
HHOOK hhook;

LRESULT CALLBACK
FilterProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (qApp)
qApp->sendPostedEvents(0, -1); // special DeferredDelete
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)
static PyObject* FreeCADGui_showMainWindow(PyObject* /*self*/, PyObject* args)
{
if (_isSetupWithoutGui) {
PyErr_SetString(PyExc_RuntimeError, "Cannot call showMainWindow() after calling setupWithoutGUI()\n");
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))
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};
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
Expand All @@ -124,18 +127,18 @@ FreeCADGui_showMainWindow(PyObject * /*self*/, PyObject *args)
// with a QtCore.QCoreApplication which will raise an exception in ipykernel
#if defined(Q_OS_WIN)
static int argc = 0;
static char **argv = {0};
static char** argv = {0};
(void)new QApplication(argc, argv);
// When QApplication is constructed
hhook = SetWindowsHookEx(WH_GETMESSAGE,
FilterProc, 0, GetCurrentThreadId());
hhook = SetWindowsHookEx(WH_GETMESSAGE, FilterProc, 0, GetCurrentThreadId());
#elif !defined(QT_NO_GLIB)
static int argc = 0;
static char **argv = {nullptr};
static char** argv = {nullptr};
(void)new QApplication(argc, argv);
#else
PyErr_SetString(PyExc_RuntimeError, "Must construct a QApplication before a QPaintDevice\n");
return NULL;
PyErr_SetString(PyExc_RuntimeError,
"Must construct a QApplication before a QPaintDevice\n");
return nullptr;
#endif
}
}
Expand All @@ -152,7 +155,7 @@ FreeCADGui_showMainWindow(PyObject * /*self*/, PyObject *args)
}

// if successful then enable Console logger
Base::ILogger *console = Base::Console().Get("Console");
Base::ILogger* console = Base::Console().Get("Console");
if (console) {
console->bMsg = true;
console->bWrn = true;
Expand All @@ -163,14 +166,15 @@ FreeCADGui_showMainWindow(PyObject * /*self*/, PyObject *args)
return Py_None;
}

static PyObject *
FreeCADGui_exec_loop(PyObject * /*self*/, PyObject *args)
static PyObject* FreeCADGui_exec_loop(PyObject* /*self*/, PyObject* args)
{
if (!PyArg_ParseTuple(args, ""))
if (!PyArg_ParseTuple(args, "")) {
return nullptr;
}

if (!qApp) {
PyErr_SetString(PyExc_RuntimeError, "Must construct a QApplication before a QPaintDevice\n");
PyErr_SetString(PyExc_RuntimeError,
"Must construct a QApplication before a QPaintDevice\n");
return nullptr;
}
else if (!qobject_cast<QApplication*>(qApp)) {
Expand All @@ -184,14 +188,14 @@ FreeCADGui_exec_loop(PyObject * /*self*/, PyObject *args)
return Py_None;
}

static PyObject *
FreeCADGui_setupWithoutGUI(PyObject * /*self*/, PyObject *args)
static PyObject* FreeCADGui_setupWithoutGUI(PyObject* /*self*/, PyObject* args)
{
if (!PyArg_ParseTuple(args, ""))
if (!PyArg_ParseTuple(args, "")) {
return nullptr;
}

if (!Gui::Application::Instance) {
static Gui::Application *app = new Gui::Application(false);
static Gui::Application* app = new Gui::Application(false);
_isSetupWithoutGui = true;
Q_UNUSED(app);
}
Expand All @@ -209,12 +213,12 @@ FreeCADGui_setupWithoutGUI(PyObject * /*self*/, PyObject *args)
return Py_None;
}

static PyObject *
FreeCADGui_embedToWindow(PyObject * /*self*/, PyObject *args)
static PyObject* FreeCADGui_embedToWindow(PyObject* /*self*/, PyObject* args)
{
char* pointer;
if (!PyArg_ParseTuple(args, "s", &pointer))
if (!PyArg_ParseTuple(args, "s", &pointer)) {
return nullptr;
}

QWidget* widget = Gui::getMainWindow();
if (!widget) {
Expand All @@ -231,10 +235,9 @@ FreeCADGui_embedToWindow(PyObject * /*self*/, PyObject *args)
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);
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);
Expand All @@ -257,25 +260,32 @@ FreeCADGui_embedToWindow(PyObject * /*self*/, PyObject *args)
}

struct PyMethodDef FreeCADGui_methods[] = {
{"showMainWindow",FreeCADGui_showMainWindow,METH_VARARGS,
{"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",
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",
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",
FreeCADGui_embedToWindow,
METH_VARARGS,
"embedToWindow() -- Embeds the main window into another window\n"},
{nullptr, nullptr, 0, nullptr} /* sentinel */
{nullptr, nullptr, 0, nullptr} /* sentinel */
};

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

Expand All @@ -293,12 +303,13 @@ QWidget* setupMainWindow()
Base::PyGILStateLocker lock;
// It's sufficient to create the config key
App::Application::Config()["DontOverrideStdIn"] = "";
Gui::MainWindow *mw = new Gui::MainWindow();
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()));
qApp->setWindowIcon(
Gui::BitmapFactory().pixmap(App::Application::Config()["AppIcon"].c_str()));
}
mw->setWindowIcon(qApp->windowIcon());

Expand All @@ -310,7 +321,6 @@ QWidget* setupMainWindow()
catch (const Base::Exception&) {
return nullptr;
}

}
else {
Gui::getMainWindow()->show();
Expand All @@ -322,22 +332,29 @@ QWidget* setupMainWindow()
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-2024\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 wouldn't be possible without 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())
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
};
}
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;
}
Expand All @@ -349,4 +366,3 @@ PyMOD_INIT_FUNC(FreeCADGui)
}
return nullptr;
}

Loading

0 comments on commit fb7b531

Please sign in to comment.