From 4a390ddeb62108bbca669d113448841e502519bf Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 24 Feb 2023 13:52:42 +0100 Subject: [PATCH] PD: refactor the constructor of TaskDialogPython --- src/Gui/TaskView/TaskDialogPython.cpp | 122 +++++++++++++++----------- src/Gui/TaskView/TaskDialogPython.h | 3 + 2 files changed, 74 insertions(+), 51 deletions(-) diff --git a/src/Gui/TaskView/TaskDialogPython.cpp b/src/Gui/TaskView/TaskDialogPython.cpp index d0f15658f3..7d05b12411 100644 --- a/src/Gui/TaskView/TaskDialogPython.cpp +++ b/src/Gui/TaskView/TaskDialogPython.cpp @@ -521,57 +521,8 @@ Py::Object TaskDialogPy::reject(const Py::Tuple& args) TaskDialogPython::TaskDialogPython(const Py::Object& o) : dlg(o) { - if (dlg.hasAttr(std::string("ui"))) { - UiLoader loader; - loader.setLanguageChangeEnabled(true); - QString fn, icon; - Py::String ui(dlg.getAttr(std::string("ui"))); - std::string path = static_cast(ui); - fn = QString::fromUtf8(path.c_str()); - - QFile file(fn); - QWidget* form = nullptr; - if (file.open(QFile::ReadOnly)) - form = loader.load(&file, nullptr); - file.close(); - if (form) { - form->installEventFilter(this); - auto taskbox = new Gui::TaskView::TaskBox( - QPixmap(icon), form->windowTitle(), true, nullptr); - taskbox->groupLayout()->addWidget(form); - Content.push_back(taskbox); - } - else { - Base::Console().Error("Failed to load UI file from '%s'\n", - (const char*)fn.toUtf8()); - } - } - else if (dlg.hasAttr(std::string("form"))) { - Py::Object f(dlg.getAttr(std::string("form"))); - Py::List widgets; - if (f.isList()) { - widgets = f; - } - else { - widgets.append(f); - } - - Gui::PythonWrapper wrap; - if (wrap.loadCoreModule()) { - for (Py::List::iterator it = widgets.begin(); it != widgets.end(); ++it) { - QObject* object = wrap.toQObject(*it); - if (object) { - QWidget* form = qobject_cast(object); - if (form) { - form->installEventFilter(this); - auto taskbox = new Gui::TaskView::TaskBox( - form->windowIcon().pixmap(32), form->windowTitle(), true, nullptr); - taskbox->groupLayout()->addWidget(form); - Content.push_back(taskbox); - } - } - } - } + if (!tryLoadUiFile()) { + tryLoadForm(); } } @@ -590,6 +541,75 @@ TaskDialogPython::~TaskDialogPython() Content.insert(Content.begin(), guarded.begin(), guarded.end()); } +bool TaskDialogPython::tryLoadUiFile() +{ + if (dlg.hasAttr(std::string("ui"))) { + UiLoader loader; + loader.setLanguageChangeEnabled(true); + QString fn, icon; + Py::String ui(dlg.getAttr(std::string("ui"))); + std::string path = static_cast(ui); + fn = QString::fromUtf8(path.c_str()); + + QFile file(fn); + QWidget* form = nullptr; + if (file.open(QFile::ReadOnly)) + form = loader.load(&file, nullptr); + file.close(); + if (form) { + appendForm(form, QPixmap(icon)); + } + else { + Base::Console().Error("Failed to load UI file from '%s'\n", + (const char*)fn.toUtf8()); + } + + return true; + } + + return false; +} + +bool TaskDialogPython::tryLoadForm() +{ + if (dlg.hasAttr(std::string("form"))) { + Py::Object f(dlg.getAttr(std::string("form"))); + Py::List widgets; + if (f.isList()) { + widgets = f; + } + else { + widgets.append(f); + } + + Gui::PythonWrapper wrap; + if (wrap.loadCoreModule()) { + for (Py::List::iterator it = widgets.begin(); it != widgets.end(); ++it) { + QObject* object = wrap.toQObject(*it); + if (object) { + QWidget* form = qobject_cast(object); + if (form) { + appendForm(form, form->windowIcon().pixmap(32)); + } + } + } + } + + return true; + } + + return false; +} + +void TaskDialogPython::appendForm(QWidget* form, const QPixmap& icon) +{ + form->installEventFilter(this); + auto taskbox = new Gui::TaskView::TaskBox( + icon, form->windowTitle(), true, nullptr); + taskbox->groupLayout()->addWidget(form); + Content.push_back(taskbox); +} + void TaskDialogPython::clearForm() { try { diff --git a/src/Gui/TaskView/TaskDialogPython.h b/src/Gui/TaskView/TaskDialogPython.h index 4d19284ac3..274a5c4a4d 100644 --- a/src/Gui/TaskView/TaskDialogPython.h +++ b/src/Gui/TaskView/TaskDialogPython.h @@ -176,6 +176,9 @@ public: bool eventFilter(QObject *watched, QEvent *event) override; private: + bool tryLoadUiFile(); + bool tryLoadForm(); + void appendForm(QWidget* widget, const QPixmap& icon); void clearForm(); private: