From 2286a6140a3b466885db95ec03497e005d0b9fc8 Mon Sep 17 00:00:00 2001 From: Pieter Hijma Date: Sun, 5 Oct 2025 13:46:12 +0200 Subject: [PATCH] Gui: Fix console messages Add Property dialog --- src/Gui/Dialogs/DlgAddProperty.cpp | 91 ++++++++++++++++++++++-------- src/Gui/Dialogs/DlgAddProperty.h | 16 +++++- 2 files changed, 79 insertions(+), 28 deletions(-) diff --git a/src/Gui/Dialogs/DlgAddProperty.cpp b/src/Gui/Dialogs/DlgAddProperty.cpp index 34dd2f4e34..be048a8503 100644 --- a/src/Gui/Dialogs/DlgAddProperty.cpp +++ b/src/Gui/Dialogs/DlgAddProperty.cpp @@ -26,6 +26,7 @@ # include # include # include +#include #include #include @@ -37,6 +38,8 @@ #include #include "Dialogs/DlgAddProperty.h" +#include "Application.h" +#include "Macro.h" #include "ui_DlgAddProperty.h" #include "ViewProviderVarSet.h" #include "propertyeditor/PropertyItem.h" @@ -58,7 +61,8 @@ const std::string DlgAddProperty::GroupBase = "Base"; * - keep the value if the name of the property is changed, * - support units (see #15557), * - support enumerations (see #15553), - * - make OK available as soon as there is a valid property (see #17474), and + * - make OK available as soon as there is a valid property (see #17474), + * - useful Python console commands (see #23760), * - support expressions (see #19716). * * Especially supporting expressions in the value field makes the logic @@ -145,21 +149,29 @@ DlgAddProperty::DlgAddProperty(QWidget* parent, DlgAddProperty::DlgAddProperty(QWidget* parent, App::PropertyContainer* container, ViewProviderVarSet* viewProvider) - : QDialog(parent), - container(container), - ui(new Ui_DlgAddProperty), - comboBoxGroup(this), - completerType(this), - editor(nullptr), - transactionID(0) + : QDialog(parent) + , container(container) + , ui(new Ui_DlgAddProperty) + , comboBoxGroup(this) + , completerType(this) + , editor(nullptr) + , transactionID(0) { ui->setupUi(this); - + setupMacroRedirector(); initializeWidgets(viewProvider); } DlgAddProperty::~DlgAddProperty() = default; +void DlgAddProperty::setupMacroRedirector() +{ + setValueRedirector = std::make_unique([this](MacroManager::LineType /*type*/, + const char* line) { + this->setValueCommand = line; + }); +} + int DlgAddProperty::findLabelRow(const char* labelName, QFormLayout* layout) { for (int row = 0; row < layout->rowCount(); ++row) { @@ -337,9 +349,8 @@ void DlgAddProperty::addEnumEditor(PropertyItem* propertyItem) void DlgAddProperty::addNormalEditor(PropertyItem* propertyItem) { - editor.reset(propertyItem->createEditor(this, [this]() { - this->valueChanged(); - }, FrameOption::WithFrame)); + editor.reset(propertyItem->createEditor(this, []() {}, + FrameOption::WithFrame)); } void DlgAddProperty::addEditor(PropertyItem* propertyItem) @@ -651,11 +662,6 @@ void DlgAddProperty::setEditor(bool valueNeedsReset) else { initializeValue(); } - - if (editor) { - QVariant data = propertyItem->editorData(editor.get()); - propertyItem->setData(data); - } } void DlgAddProperty::setPropertyItem(App::Property* prop, bool supportsExpressions) @@ -800,12 +806,6 @@ void DlgAddProperty::valueChangedEnum() propEnum->setEnums(enumValuesVec); } -void DlgAddProperty::valueChanged() -{ - QVariant data = propertyItem->editorData(editor.get()); - propertyItem->setData(data); -} - /* We use these functions rather than the functions provided by App::Document * because this dialog may be opened when another transaction is in progress. * An example is opening a sketch. If this dialog uses the functions provided @@ -826,6 +826,29 @@ void DlgAddProperty::critical(const QString& title, const QString& text) { } } +void DlgAddProperty::recordMacroAdd(const App::PropertyContainer* container, + const std::string& type, const std::string& name, + const std::string& group, const std::string& doc) const +{ + std::ostringstream command; + command << "App.getDocument('"; + const App::Document* document = freecad_cast(container); + const App::DocumentObject* object = freecad_cast(container); + if (document) { + command << document->getName() << "')"; + } + else if (object) { + command << object->getDocument()->getName() << "')." << object->getNameInDocument(); + } + else { + FC_ERR("Cannot record macro for container of type " << container->getTypeId().getName()); + return; + } + command << ".addProperty('" << type << "', '" << name << "', '" << + group << "', '" << doc + "')"; + Application::Instance->macroManager()->addLine(Gui::MacroManager::App, command.str().c_str()); +} + App::Property* DlgAddProperty::createProperty() { std::string name = ui->lineEditName->text().toStdString(); @@ -833,9 +856,16 @@ App::Property* DlgAddProperty::createProperty() std::string type = ui->comboBoxType->currentText().toStdString(); std::string doc = ui->lineEditToolTip->text().toStdString(); + auto recordAddCommand = [this](MacroManager::LineType, const char* line) { + this->addCommand = line; + }; + try { - return container->addDynamicProperty(type.c_str(), name.c_str(), - group.c_str(), doc.c_str()); + App::Property* prop = container->addDynamicProperty(type.c_str(), name.c_str(), + group.c_str(), doc.c_str()); + MacroManager::MacroRedirector redirector(recordAddCommand); + recordMacroAdd(container, type, name, group, doc); + return prop; } catch (Base::Exception& e) { e.reportException(); @@ -893,12 +923,23 @@ void DlgAddProperty::addDocumentation() { void DlgAddProperty::accept() { + if (editor) { + QVariant data = propertyItem->editorData(editor.get()); + propertyItem->setData(data); + } addDocumentation(); auto* object = freecad_cast(container); if (object) { object->ExpressionEngine.execute(); } closeTransaction(TransactionOption::Commit); + + setValueRedirector = nullptr; + Application::Instance->macroManager()->addLine(MacroManager::LineType::App, addCommand.c_str()); + Application::Instance->macroManager()->addLine(MacroManager::LineType::App, + setValueCommand.c_str()); + setupMacroRedirector(); + std::string group = comboBoxGroup.currentText().toStdString(); std::string type = ui->comboBoxType->currentText().toStdString(); auto paramGroup = App::GetApplication().GetParameterGroupByPath( diff --git a/src/Gui/Dialogs/DlgAddProperty.h b/src/Gui/Dialogs/DlgAddProperty.h index c2e0de1b69..325110f964 100644 --- a/src/Gui/Dialogs/DlgAddProperty.h +++ b/src/Gui/Dialogs/DlgAddProperty.h @@ -36,6 +36,7 @@ #include #include "propertyeditor/PropertyItem.h" +#include "Macro.h" namespace Gui { @@ -94,7 +95,6 @@ public: QLayout* layout); public Q_SLOTS: - void valueChanged(); void valueChangedEnum(); private: @@ -108,8 +108,11 @@ private: Type }; - DlgAddProperty(QWidget* parent, App::PropertyContainer* container, - ViewProviderVarSet* viewProvider); + DlgAddProperty(QWidget* parent, + App::PropertyContainer* container, + ViewProviderVarSet* viewProvider); + + void setupMacroRedirector(); void initializeGroup(); @@ -156,6 +159,9 @@ private: void openTransaction(); void critical(const QString& title, const QString& text); + void recordMacroAdd(const App::PropertyContainer* container, + const std::string& type, const std::string& name, + const std::string& group, const std::string& doc) const; App::Property* createProperty(); void closeTransaction(TransactionOption option); void clearFields(); @@ -181,6 +187,10 @@ private: QMetaObject::Connection connComboBoxGroup; QMetaObject::Connection connComboBoxType; QMetaObject::Connection connLineEditNameTextChanged; + + std::unique_ptr setValueRedirector; + std::string addCommand; + std::string setValueCommand; }; } // namespace Dialog