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.
This commit is contained in:
Pieter Hijma
2025-08-28 12:54:28 +02:00
parent 37a49a2fbf
commit ab28433b96
3 changed files with 59 additions and 12 deletions

View File

@@ -1512,6 +1512,39 @@ void TreeWidget::setupResizableColumn(TreeWidget *tree) {
}
}
std::vector<Document*> TreeWidget::getSelectedDocuments() {
std::vector<Document*> 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<DocumentItem*>(ti);
auto doc = item->document();
if (!doc || !doc->getDocument()) {
FC_WARN("skip invalid document");
continue;
}
ret.push_back(doc);
}
return ret;
}
std::vector<TreeWidget::SelInfo> TreeWidget::getSelection(App::Document* doc)
{
std::vector<SelInfo> ret;

View File

@@ -92,6 +92,7 @@ public:
* which Gui::Selection() cannot provide.
*/
static std::vector<SelInfo> getSelection(App::Document *doc=nullptr);
static std::vector<Document*> getSelectedDocuments();
static TreeWidget *instance();

View File

@@ -40,6 +40,8 @@
#include <Base/Console.h>
#include <Base/Tools.h>
#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<Gui::Document*> docs = Gui::TreeWidget::getSelectedDocuments();
if (docs.size() == 1) {
return docs[0]->getDocument();
}
return nullptr;
}
std::unordered_set<App::Property*> PropertyEditor::acquireSelectedProperties() const
{
std::unordered_set<App::Property*> 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<App::PropertyContainer*> 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;
}