From ab28433b96ee5d1218cebc4ec601acd09d0a8cac Mon Sep 17 00:00:00 2001 From: Pieter Hijma Date: Thu, 28 Aug 2025 12:54:28 +0200 Subject: [PATCH] Gui: Disallow adding props to multiple objects This commit is in preparation for switching the old Add Property dialog to the Add Property VarSet dialog. For now, this dialog does not handle adding properties for multiple objects. --- src/Gui/Tree.cpp | 33 ++++++++++++++++++++ src/Gui/Tree.h | 1 + src/Gui/propertyeditor/PropertyEditor.cpp | 37 +++++++++++++++-------- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index b6b68ed2b5..93321f06b6 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -1512,6 +1512,39 @@ void TreeWidget::setupResizableColumn(TreeWidget *tree) { } } +std::vector TreeWidget::getSelectedDocuments() { + std::vector ret; + TreeWidget* tree = instance(); + if (!tree || !tree->isSelectionAttached()) { + for (auto pTree : Instances) + if (pTree->isSelectionAttached()) { + tree = pTree; + break; + } + } + if (!tree) + return ret; + + if (tree->selectTimer->isActive()) + tree->onSelectTimer(); + else + tree->_updateStatus(false); + + const auto items = tree->selectedItems(); + for (auto ti : items) { + if (ti->type() != DocumentType) + continue; + auto item = static_cast(ti); + auto doc = item->document(); + if (!doc || !doc->getDocument()) { + FC_WARN("skip invalid document"); + continue; + } + ret.push_back(doc); + } + return ret; +} + std::vector TreeWidget::getSelection(App::Document* doc) { std::vector ret; diff --git a/src/Gui/Tree.h b/src/Gui/Tree.h index 72a048ef62..9555d1254f 100644 --- a/src/Gui/Tree.h +++ b/src/Gui/Tree.h @@ -92,6 +92,7 @@ public: * which Gui::Selection() cannot provide. */ static std::vector getSelection(App::Document *doc=nullptr); + static std::vector getSelectedDocuments(); static TreeWidget *instance(); diff --git a/src/Gui/propertyeditor/PropertyEditor.cpp b/src/Gui/propertyeditor/PropertyEditor.cpp index 6a6f62fad5..0ce318c66a 100644 --- a/src/Gui/propertyeditor/PropertyEditor.cpp +++ b/src/Gui/propertyeditor/PropertyEditor.cpp @@ -40,6 +40,8 @@ #include #include +#include "Document.h" +#include "Tree.h" #include "PropertyEditor.h" #include "Dialogs/DlgAddProperty.h" #include "MainWindow.h" @@ -775,6 +777,19 @@ enum MenuAction MA_Copy, }; +static App::PropertyContainer* getSelectedPropertyContainer() +{ + auto sels = Gui::Selection().getSelection("*"); + if (sels.size() == 1) { + return sels[0].pObject; + } + std::vector docs = Gui::TreeWidget::getSelectedDocuments(); + if (docs.size() == 1) { + return docs[0]->getDocument(); + } + return nullptr; +} + std::unordered_set PropertyEditor::acquireSelectedProperties() const { std::unordered_set props; @@ -831,7 +846,11 @@ void PropertyEditor::contextMenuEvent(QContextMenuEvent*) } // add property - menu.addAction(tr("Add Property"))->setData(QVariant(MA_AddProp)); + if (getSelectedPropertyContainer()) { + menu.addAction(tr("Add Property"))->setData(QVariant(MA_AddProp)); + } + + // rename property group if (!props.empty() && std::all_of(props.begin(), props.end(), [](auto prop) { return prop->testStatus(App::Property::PropDynamic) && !boost::starts_with(prop->getName(), prop->getGroup()); @@ -989,18 +1008,12 @@ void PropertyEditor::contextMenuEvent(QContextMenuEvent*) } break; case MA_AddProp: { + App::PropertyContainer* container = getSelectedPropertyContainer(); + if (container == nullptr) { + return; + } App::AutoTransaction committer("Add property"); - std::unordered_set containers; - auto sels = Gui::Selection().getSelection("*"); - if (sels.size() == 1) { - containers.insert(sels[0].pObject); - } - else { - for (auto prop : props) { - containers.insert(prop->getContainer()); - } - } - Gui::Dialog::DlgAddProperty dlg(Gui::getMainWindow(), std::move(containers)); + Gui::Dialog::DlgAddProperty dlg(Gui::getMainWindow(), container); dlg.exec(); return; }