From 7009abc8f7b06f4e543f2ed8f3ca60b249c403fa Mon Sep 17 00:00:00 2001 From: PaddleStroke Date: Thu, 17 Oct 2024 14:17:53 +0200 Subject: [PATCH] Task attacher: Handle initial selection when no refs. --- src/Mod/Part/Gui/TaskAttacher.cpp | 102 ++++++++++++++++++++++-------- src/Mod/Part/Gui/TaskAttacher.h | 3 + 2 files changed, 77 insertions(+), 28 deletions(-) diff --git a/src/Mod/Part/Gui/TaskAttacher.cpp b/src/Mod/Part/Gui/TaskAttacher.cpp index eb0ae41413..3dac3f92d9 100644 --- a/src/Mod/Part/Gui/TaskAttacher.cpp +++ b/src/Mod/Part/Gui/TaskAttacher.cpp @@ -231,6 +231,8 @@ TaskAttacher::TaskAttacher(Gui::ViewProviderDocumentObject* ViewProvider, QWidge Gui::Document* document = Gui::Application::Instance->getDocument(ViewProvider->getObject()->getDocument()); connectDelObject = document->signalDeletedObject.connect(bnd1); connectDelDocument = document->signalDeleteDocument.connect(bnd2); + + handleInitialSelection(); } TaskAttacher::~TaskAttacher() @@ -463,6 +465,32 @@ void TaskAttacher::processSelection(App::DocumentObject*& rootObj, std::string& rootObj = nullptr; } +void TaskAttacher::handleInitialSelection() +{ + // We handle initial selection only if it is not attached yet. + App::DocumentObject* obj = ViewProvider->getObject(); + Part::AttachExtension* pcAttach = obj->getExtensionByType(); + std::vector refs = pcAttach->AttachmentSupport.getValues(); + + if (!refs.empty()) { + return; + } + std::vector objNames; + std::vector subNames; + + auto sel = Gui::Selection().getSelectionEx("", + App::DocumentObject::getClassTypeId(), Gui::ResolveMode::NoResolve); + for (auto& selObj : sel) { + std::vector subs = selObj.getSubNames(); + const char* objName = selObj.getFeatName(); + for (auto& sub : subs) { + objNames.push_back(objName); + subNames.push_back(sub); + } + } + addToReference(objNames, subNames); +} + void TaskAttacher::onSelectionChanged(const Gui::SelectionChanges& msg) { if (!ViewProvider) { @@ -470,18 +498,26 @@ void TaskAttacher::onSelectionChanged(const Gui::SelectionChanges& msg) } if (msg.Type == Gui::SelectionChanges::AddSelection) { - if (iActiveRef < 0) { - return; - } + addToReference(msg.pObjectName, msg.pSubName); + } +} - // Note: The validity checking has already been done in ReferenceSelection.cpp - App::DocumentObject* obj = ViewProvider->getObject(); - Part::AttachExtension* pcAttach = obj->getExtensionByType(); +void TaskAttacher::addToReference(std::vector objNames, std::vector subNames) +{ + if (iActiveRef < 0 || objNames.size() != subNames.size()) { + return; + } + + // Note: The validity checking has already been done in ReferenceSelection.cpp + App::DocumentObject* obj = ViewProvider->getObject(); + Part::AttachExtension* pcAttach = obj->getExtensionByType(); + + for (size_t i = 0; i < objNames.size(); ++i) { std::vector refs = pcAttach->AttachmentSupport.getValues(); std::vector refnames = pcAttach->AttachmentSupport.getSubValues(); - App::DocumentObject* selObj = obj->getDocument()->getObject(msg.pObjectName); - std::string subname = msg.pSubName; + App::DocumentObject* selObj = obj->getDocument()->getObject(objNames[i].c_str()); + std::string subname = subNames[i]; processSelection(selObj, subname); if (!selObj) { return; @@ -518,26 +554,29 @@ void TaskAttacher::onSelectionChanged(const Gui::SelectionChanges& msg) refnames.push_back(subname); } - //bool error = false; - try { - pcAttach->AttachmentSupport.setValues(refs, refnames); - updateListOfModes(); - eMapMode mmode = getActiveMapMode();//will be mmDeactivated, if selected or if no modes are available - if (mmode == mmDeactivated) { - //error = true; - this->completed = false; + pcAttach->AttachmentSupport.setValues(refs, refnames); + + if (i == objNames.size() - 1) { + // We check for the moed only for the last ref added. This is to avoid unnecessary warnings + // when we handle initial selection. + try { + updateListOfModes(); + eMapMode mmode = getActiveMapMode();//will be mmDeactivated, if selected or if no modes are available + if (mmode == mmDeactivated) { + //error = true; + this->completed = false; + } + else { + this->completed = true; + } + pcAttach->MapMode.setValue(mmode); + selectMapMode(mmode); + updatePreview(); } - else { - this->completed = true; + catch (Base::Exception& e) { + ui->message->setText(QCoreApplication::translate("Exception", e.what())); + ui->message->setStyleSheet(QString::fromLatin1("QLabel{color: red;}")); } - pcAttach->MapMode.setValue(mmode); - selectMapMode(mmode); - updatePreview(); - } - catch (Base::Exception& e) { - //error = true; - ui->message->setText(QCoreApplication::translate("Exception", e.what())); - ui->message->setStyleSheet(QString::fromLatin1("QLabel{color: red;}")); } QLineEdit* line = getLine(iActiveRef); @@ -559,9 +598,16 @@ void TaskAttacher::onSelectionChanged(const Gui::SelectionChanges& msg) iActiveRef++; } } - - updateReferencesUI(); } + + updateReferencesUI(); +} + +void TaskAttacher::addToReference(const char* objName, const char* subName) +{ + std::string objname = objName; + std::string subname = subName; + addToReference({ objname }, { subname }); } void TaskAttacher::onAttachmentOffsetChanged(double /*val*/, int idx) diff --git a/src/Mod/Part/Gui/TaskAttacher.h b/src/Mod/Part/Gui/TaskAttacher.h index c8cb3fce9f..2833bf3ab8 100644 --- a/src/Mod/Part/Gui/TaskAttacher.h +++ b/src/Mod/Part/Gui/TaskAttacher.h @@ -114,6 +114,9 @@ private: void updateAttachmentOffsetUI(); void processSelection(App::DocumentObject*& obj, std::string& sub); + void handleInitialSelection(); + void addToReference(const char* objName, const char* subName); + void addToReference(std::vector objNames, std::vector subNames); /** * @brief updateListOfModes Fills the mode list with modes that apply to