Gui: code-refactoring

Split selection handling from View3DInventorViewer and move to View3DInventorSelection
This commit is contained in:
wmayer
2022-11-16 20:27:14 +01:00
parent e1485388d4
commit 308cf4ffb0
5 changed files with 354 additions and 225 deletions

View File

@@ -427,34 +427,7 @@ void View3DInventorViewer::init()
pcViewProviderRoot->addChild(cb);
#endif
pcGroupOnTop = new SoSeparator;
pcGroupOnTop->ref();
pcViewProviderRoot->addChild(pcGroupOnTop);
auto pcGroupOnTopPickStyle = new SoPickStyle;
pcGroupOnTopPickStyle->style = SoPickStyle::UNPICKABLE;
pcGroupOnTopPickStyle->setOverride(true);
pcGroupOnTop->addChild(pcGroupOnTopPickStyle);
coin_setenv("COIN_SEPARATE_DIFFUSE_TRANSPARENCY_OVERRIDE", "1", TRUE);
auto pcOnTopMaterial = new SoMaterial;
pcOnTopMaterial->transparency = 0.5;
pcOnTopMaterial->diffuseColor.setIgnored(true);
pcOnTopMaterial->setOverride(true);
pcGroupOnTop->addChild(pcOnTopMaterial);
auto selRoot = new SoFCSelectionRoot;
selRoot->selectionStyle = SoFCSelectionRoot::PassThrough;
pcGroupOnTopSel = selRoot;
pcGroupOnTopSel->setName("GroupOnTopSel");
pcGroupOnTopSel->ref();
pcGroupOnTop->addChild(pcGroupOnTopSel);
selRoot = new SoFCSelectionRoot;
selRoot->selectionStyle = SoFCSelectionRoot::PassThrough;
pcGroupOnTopPreSel = selRoot;
pcGroupOnTopPreSel->setName("GroupOnTopPreSel");
pcGroupOnTopPreSel->ref();
pcGroupOnTop->addChild(pcGroupOnTopPreSel);
inventorSelection = std::make_unique<View3DInventorSelection>(selectionRoot);
pcClipPlane = nullptr;
@@ -558,14 +531,12 @@ View3DInventorViewer::~View3DInventorViewer()
this->backlight->unref();
this->backlight = nullptr;
this->pcGroupOnTop->unref();
this->pcGroupOnTopPreSel->unref();
this->pcGroupOnTopSel->unref();
inventorSelection.reset(nullptr);
this->pcEditingRoot->unref();
this->pcEditingTransform->unref();
if(this->pcClipPlane)
if (this->pcClipPlane)
this->pcClipPlane->unref();
delete this->navigation;
@@ -639,6 +610,7 @@ void View3DInventorViewer::setDocument(Gui::Document* pcDocument)
// write the document the viewer belongs to the selection node
guiDocument = pcDocument;
selectionRoot->pcDocument = pcDocument;
inventorSelection->setDocument(pcDocument);
if(pcDocument) {
const auto &sels = Selection().getSelection(pcDocument->getDocument()->getName(), ResolveMode::NoResolve);
@@ -664,191 +636,6 @@ void View3DInventorViewer::initialize()
this->axiscrossSize = 10;
}
void View3DInventorViewer::clearGroupOnTop() {
if(!objectsOnTop.empty() || !objectsOnTopPreSel.empty()) {
objectsOnTop.clear();
objectsOnTopPreSel.clear();
SoSelectionElementAction action(SoSelectionElementAction::None,true);
action.apply(pcGroupOnTopPreSel);
action.apply(pcGroupOnTopSel);
coinRemoveAllChildren(pcGroupOnTopSel);
coinRemoveAllChildren(pcGroupOnTopPreSel);
FC_LOG("clear annotation");
}
}
void View3DInventorViewer::checkGroupOnTop(const SelectionChanges &Reason) {
if(Reason.Type == SelectionChanges::SetSelection || Reason.Type == SelectionChanges::ClrSelection) {
clearGroupOnTop();
if(Reason.Type == SelectionChanges::ClrSelection)
return;
}
if(Reason.Type == SelectionChanges::RmvPreselect ||
Reason.Type == SelectionChanges::RmvPreselectSignal)
{
SoSelectionElementAction action(SoSelectionElementAction::None,true);
action.apply(pcGroupOnTopPreSel);
coinRemoveAllChildren(pcGroupOnTopPreSel);
objectsOnTopPreSel.clear();
return;
}
if(!getDocument() || !Reason.pDocName || !Reason.pDocName[0] || !Reason.pObjectName)
return;
auto obj = getDocument()->getDocument()->getObject(Reason.pObjectName);
if(!obj || !obj->getNameInDocument())
return;
std::string key(obj->getNameInDocument());
key += '.';
auto subname = Reason.pSubName;
if(subname)
key += subname;
if(Reason.Type == SelectionChanges::RmvSelection) {
auto &objs = objectsOnTop;
auto pcGroup = pcGroupOnTopSel;
auto it = objs.find(key.c_str());
if(it == objs.end())
return;
int index = pcGroup->findChild(it->second);
if(index >= 0) {
auto node = static_cast<SoFCPathAnnotation*>(it->second);
SoSelectionElementAction action(node->getDetail()?
SoSelectionElementAction::Remove:SoSelectionElementAction::None,true);
auto path = node->getPath();
SoTempPath tmpPath(2 + (path ? path->getLength() : 0));
tmpPath.ref();
tmpPath.append(pcGroup);
tmpPath.append(node);
tmpPath.append(node->getPath());
action.setElement(node->getDetail());
action.apply(&tmpPath);
tmpPath.unrefNoDelete();
pcGroup->removeChild(index);
FC_LOG("remove annotation " << Reason.Type << " " << key);
}else
FC_LOG("remove annotation object " << Reason.Type << " " << key);
objs.erase(it);
return;
}
auto &objs = Reason.Type==SelectionChanges::SetPreselect?objectsOnTopPreSel:objectsOnTop;
auto pcGroup = Reason.Type==SelectionChanges::SetPreselect?pcGroupOnTopPreSel:pcGroupOnTopSel;
if(objs.find(key.c_str())!=objs.end())
return;
auto vp = dynamic_cast<ViewProviderDocumentObject*>(
Application::Instance->getViewProvider(obj));
if(!vp || !vp->isSelectable() || !vp->isShow())
return;
auto svp = vp;
if(subname && *subname) {
auto sobj = obj->getSubObject(subname);
if(!sobj || !sobj->getNameInDocument())
return;
if(sobj!=obj) {
svp = dynamic_cast<ViewProviderDocumentObject*>(
Application::Instance->getViewProvider(sobj));
if(!svp || !svp->isSelectable())
return;
}
}
int onTop;
// onTop==2 means on top only if whole object is selected,
// onTop==3 means on top only if some sub-element is selected
// onTop==1 means either
if(Gui::Selection().needPickedList())
onTop = 1;
else if(vp->OnTopWhenSelected.getValue())
onTop = vp->OnTopWhenSelected.getValue();
else
onTop = svp->OnTopWhenSelected.getValue();
if(Reason.Type == SelectionChanges::SetPreselect) {
SoHighlightElementAction action;
action.setHighlighted(true);
action.setColor(selectionRoot->colorHighlight.getValue());
action.apply(pcGroupOnTopPreSel);
if(!onTop)
onTop = 2;
}else {
if(!onTop)
return;
SoSelectionElementAction action(SoSelectionElementAction::All);
action.setColor(selectionRoot->colorSelection.getValue());
action.apply(pcGroupOnTopSel);
}
if(onTop==2 || onTop==3) {
if(subname && *subname) {
size_t len = strlen(subname);
if(subname[len-1]=='.') {
// ending with '.' means whole object selection
if(onTop == 3)
return;
}else if(onTop==2)
return;
}else if(onTop==3)
return;
}
std::vector<ViewProvider*> groups;
auto grpVp = vp;
for(auto childVp=vp;;childVp=grpVp) {
auto grp = App::GeoFeatureGroupExtension::getGroupOfObject(childVp->getObject());
if(!grp || !grp->getNameInDocument()) break;
grpVp = dynamic_cast<ViewProviderDocumentObject*>(
Application::Instance->getViewProvider(grp));
if(!grpVp) break;
auto childRoot = grpVp->getChildRoot();
auto modeSwitch = grpVp->getModeSwitch();
auto idx = modeSwitch->whichChild.getValue();
if(idx<0 || idx>=modeSwitch->getNumChildren() ||
modeSwitch->getChild(idx)!=childRoot)
{
FC_LOG("skip " << obj->getFullName() << '.' << (subname?subname:"")
<< ", hidden inside geo group");
return;
}
if(childRoot->findChild(childVp->getRoot())<0) {
FC_LOG("cannot find '" << childVp->getObject()->getFullName()
<< "' in geo group '" << grp->getNameInDocument() << "'");
break;
}
groups.push_back(grpVp);
}
SoTempPath path(10);
path.ref();
for(auto it=groups.rbegin();it!=groups.rend();++it) {
auto grpVp = *it;
path.append(grpVp->getRoot());
path.append(grpVp->getModeSwitch());
path.append(grpVp->getChildRoot());
}
SoDetail *det = nullptr;
if(vp->getDetailPath(subname, &path,true,det) && path.getLength()) {
auto node = new SoFCPathAnnotation;
node->setPath(&path);
pcGroup->addChild(node);
if(det) {
SoSelectionElementAction action(SoSelectionElementAction::Append,true);
action.setElement(det);
SoTempPath tmpPath(path.getLength()+2);
tmpPath.ref();
tmpPath.append(pcGroup);
tmpPath.append(node);
tmpPath.append(&path);
action.apply(&tmpPath);
tmpPath.unrefNoDelete();
node->setDetail(det);
det = nullptr;
}
FC_LOG("add annotation " << Reason.Type << " " << key);
objs[key.c_str()] = node;
}
delete det;
path.unrefNoDelete();
}
/// @cond DOXERR
void View3DInventorViewer::onSelectionChanged(const SelectionChanges &_Reason)
{
@@ -879,7 +666,7 @@ void View3DInventorViewer::onSelectionChanged(const SelectionChanges &_Reason)
case SelectionChanges::AddSelection:
case SelectionChanges::RmvSelection:
case SelectionChanges::ClrSelection:
checkGroupOnTop(Reason);
inventorSelection->checkGroupOnTop(Reason);
break;
case SelectionChanges::SetPreselectSignal:
break;