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.
This commit is contained in:
wmayer
2022-04-09 23:07:02 +02:00
parent 0e40b9956e
commit aea110ba15
6 changed files with 40 additions and 22 deletions

View File

@@ -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<App::SubObjectT> 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);

View File

@@ -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 "<<DocName<<'#'<<FeatName<<'.'<<SubName);
}else
}
else {
FC_TRACE("preselect signal "<<DocName<<'#'<<FeatName<<'.'<<SubName);
}
notify(Chng);
if(signal==1 && DocName.size()) {
if (signal == SelectionChanges::MsgSource::Internal && DocName.size()) {
FC_TRACE("preselect "<<DocName<<'#'<<FeatName<<'.'<<SubName);
Chng.Type = SelectionChanges::SetPreselect;
CurrentPreselection = Chng;
@@ -2311,7 +2316,7 @@ PyObject *SelectionSingleton::sSetPreselection(PyObject * /*self*/, PyObject *ar
Selection().setPreselect(docObj->getDocument()->getName(),
docObj->getNameInDocument(),
subname,x,y,z,type);
subname,x,y,z, static_cast<SelectionChanges::MsgSource>(type));
Py_Return;
}

View File

@@ -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

View File

@@ -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() {

View File

@@ -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)

View File

@@ -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: