From aea110ba15897661a59f04ec52c701a00daea430 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 9 Apr 2022 23:07:02 +0200 Subject: [PATCH] Gui: clean-up Selection API Replace the int of the SubType of SelectionChanges with an enum class. The meaning of it is nowhere documented and some magic numbers like 0,1,2 are used in several places in the code. --- src/Gui/DlgPropertyLink.cpp | 5 +++-- src/Gui/Selection.cpp | 25 +++++++++++++++---------- src/Gui/Selection.h | 22 ++++++++++++++++------ src/Gui/TaskElementColors.cpp | 3 ++- src/Gui/Tree.cpp | 5 +++-- src/Gui/View3DInventorViewer.cpp | 2 +- 6 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/Gui/DlgPropertyLink.cpp b/src/Gui/DlgPropertyLink.cpp index 4d72d36da0..5632a03c43 100644 --- a/src/Gui/DlgPropertyLink.cpp +++ b/src/Gui/DlgPropertyLink.cpp @@ -724,7 +724,7 @@ void DlgPropertyLink::onTimer() { Gui::Selection().setPreselect(sobj.getDocumentName().c_str(), sobj.getObjectName().c_str(), sobj.getSubName().c_str(), - 0,0,0,2); + 0, 0, 0, Gui::SelectionChanges::MsgSource::TreeView); } QList DlgPropertyLink::currentLinks() const @@ -857,7 +857,8 @@ void DlgPropertyLink::itemSearch(const QString &text, bool select) { obj->getNameInDocument(),subname); }else{ Selection().setPreselect(obj->getDocument()->getName(), - obj->getNameInDocument(), subname,0,0,0,2); + obj->getNameInDocument(), subname, 0, 0, 0, + Gui::SelectionChanges::MsgSource::TreeView); searchItem = item; ui->treeWidget->scrollToItem(searchItem); bgBrush = searchItem->background(0); diff --git a/src/Gui/Selection.cpp b/src/Gui/Selection.cpp index b45738e4ed..9d2d3d6f51 100644 --- a/src/Gui/Selection.cpp +++ b/src/Gui/Selection.cpp @@ -782,15 +782,16 @@ void SelectionSingleton::slotSelectionChanged(const SelectionChanges& msg) } } -int SelectionSingleton::setPreselect(const char* pDocName, const char* pObjectName, const char* pSubName, float x, float y, float z, int signal) +int SelectionSingleton::setPreselect(const char* pDocName, const char* pObjectName, const char* pSubName, + float x, float y, float z, SelectionChanges::MsgSource signal) { - if(!pDocName || !pObjectName) { + if (!pDocName || !pObjectName) { rmvPreselect(); return 0; } - if(!pSubName) pSubName = ""; + if (!pSubName) pSubName = ""; - if(DocName==pDocName && FeatName==pObjectName && SubName==pSubName) { + if (DocName==pDocName && FeatName==pObjectName && SubName==pSubName) { // MovePreselect is likely going to slow down large scene rendering. // Disable it for now. #if 0 @@ -808,7 +809,7 @@ int SelectionSingleton::setPreselect(const char* pDocName, const char* pObjectNa rmvPreselect(); - if (ActiveGate && signal!=1) { + if (ActiveGate && signal != SelectionChanges::MsgSource::Internal) { App::Document* pDoc = getDocument(pDocName); if (!pDoc || !pObjectName) return 0; @@ -860,18 +861,22 @@ int SelectionSingleton::setPreselect(const char* pDocName, const char* pObjectNa hz = z; // set up the change object - SelectionChanges Chng(signal==1?SelectionChanges::SetPreselectSignal:SelectionChanges::SetPreselect, + SelectionChanges Chng(signal == SelectionChanges::MsgSource::Internal + ? SelectionChanges::SetPreselectSignal + : SelectionChanges::SetPreselect, DocName,FeatName,SubName,std::string(),x,y,z,signal); - if(Chng.Type==SelectionChanges::SetPreselect) { + if (Chng.Type==SelectionChanges::SetPreselect) { CurrentPreselection = Chng; FC_TRACE("preselect "<getDocument()->getName(), docObj->getNameInDocument(), - subname,x,y,z,type); + subname,x,y,z, static_cast(type)); Py_Return; } diff --git a/src/Gui/Selection.h b/src/Gui/Selection.h index 9b3545d652..4400d16de4 100644 --- a/src/Gui/Selection.h +++ b/src/Gui/Selection.h @@ -83,12 +83,19 @@ public: RmvPreselectSignal, // to request 3D view to remove preselect MovePreselect, // to signal observer the mouse movement when preselect }; + enum class MsgSource { + Any = 0, + Internal = 1, + TreeView = 2 + }; SelectionChanges(MsgType type = ClrSelection, const char *docName=nullptr, const char *objName=nullptr, const char *subName=nullptr, const char *typeName=nullptr, - float x=0, float y=0, float z=0, int subtype=0) - : Type(type),SubType(subtype) + float x=0, float y=0, float z=0, + MsgSource subtype=MsgSource::Any) + : Type(type) + , SubType(subtype) , x(x),y(y),z(z) , Object(docName,objName,subName) { @@ -104,8 +111,10 @@ public: const std::string &objName, const std::string &subName, const std::string &typeName = std::string(), - float x=0,float y=0,float z=0, int subtype=0) - : Type(type), SubType(subtype) + float x=0,float y=0,float z=0, + MsgSource subtype=MsgSource::Any) + : Type(type) + , SubType(subtype) , x(x),y(y),z(z) , Object(docName.c_str(), objName.c_str(), subName.c_str()) , TypeName(typeName) @@ -157,7 +166,7 @@ public: } MsgType Type; - int SubType; + MsgSource SubType; const char* pDocName; const char* pObjectName; @@ -402,7 +411,8 @@ public: /// set the preselected object (mostly by the 3D view) int setPreselect(const char* pDocName, const char* pObjectName, - const char* pSubName, float x=0, float y=0, float z=0, int signal=0); + const char* pSubName, float x=0, float y=0, float z=0, + SelectionChanges::MsgSource signal=SelectionChanges::MsgSource::Any); /// remove the present preselection void rmvPreselect(bool signal=false); /// sets different coords for the preselection diff --git a/src/Gui/TaskElementColors.cpp b/src/Gui/TaskElementColors.cpp index 635e4410a9..40a9007556 100644 --- a/src/Gui/TaskElementColors.cpp +++ b/src/Gui/TaskElementColors.cpp @@ -487,7 +487,8 @@ void ElementColors::on_elementList_itemEntered(QListWidgetItem *item) { } Selection().setPreselect(d->editDoc.c_str(), d->editObj.c_str(), (d->editSub+name).c_str(),0,0,0, - d->ui->onTop->isChecked()?2:1); + d->ui->onTop->isChecked() ? Gui::SelectionChanges::MsgSource::TreeView + : Gui::SelectionChanges::MsgSource::Internal); } void ElementColors::on_elementList_itemSelectionChanged() { diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index d27301a4a5..33ee98aee6 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -767,7 +767,8 @@ void TreeWidget::itemSearch(const QString& text, bool select) { } scrollToItem(item); Selection().setPreselect(obj->getDocument()->getName(), - obj->getNameInDocument(), subname.c_str(), 0, 0, 0, 2); + obj->getNameInDocument(), subname.c_str(), 0, 0, 0, + SelectionChanges::MsgSource::TreeView); if (select) { Gui::Selection().selStackPush(); Gui::Selection().clearSelection(); @@ -2726,7 +2727,7 @@ void TreeWidget::onPreSelectTimer() { else if (!obj->redirectSubName(ss, parent, nullptr)) ss << obj->getNameInDocument() << '.'; Selection().setPreselect(parent->getDocument()->getName(), parent->getNameInDocument(), - ss.str().c_str(), 0, 0, 0, 2); + ss.str().c_str(), 0, 0, 0, SelectionChanges::MsgSource::TreeView); } void TreeWidget::onItemCollapsed(QTreeWidgetItem* item) diff --git a/src/Gui/View3DInventorViewer.cpp b/src/Gui/View3DInventorViewer.cpp index adf2a22175..af8128478f 100644 --- a/src/Gui/View3DInventorViewer.cpp +++ b/src/Gui/View3DInventorViewer.cpp @@ -893,7 +893,7 @@ void View3DInventorViewer::onSelectionChanged(const SelectionChanges &_Reason) Reason.Type = SelectionChanges::RmvSelection; // fall through case SelectionChanges::SetPreselect: - if(Reason.SubType!=2) // 2 means it is triggered from tree view + if(Reason.SubType != SelectionChanges::MsgSource::TreeView) break; // fall through case SelectionChanges::RmvPreselect: