Gui: ActiveObjectList API changes
Support sub-object in ActiveObjectList. This means that it can now distinguish the same object being activated under different parent, which may be in a different document through external linking.
This commit is contained in:
@@ -28,44 +28,116 @@
|
||||
|
||||
#endif
|
||||
|
||||
#include <Base/Console.h>
|
||||
#include "ActiveObjectList.h"
|
||||
#include <Gui/Application.h>
|
||||
#include <Gui/Document.h>
|
||||
#include <Gui/Selection.h>
|
||||
#include <Gui/ViewProviderDocumentObject.h>
|
||||
#include "Tree.h"
|
||||
|
||||
|
||||
FC_LOG_LEVEL_INIT("MDIView",true,true);
|
||||
|
||||
using namespace Gui;
|
||||
|
||||
|
||||
void Gui::ActiveObjectList::setObject(App::DocumentObject* obj, const char* name, const Gui::HighlightMode& mode)
|
||||
App::DocumentObject *ActiveObjectList::getObject(const ObjectInfo &info, bool resolve,
|
||||
App::DocumentObject **parent, std::string *subname) const
|
||||
{
|
||||
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/TreeView");
|
||||
bool autoExpand = hGrp->GetBool("TreeActiveAutoExpand", true);
|
||||
if(parent) *parent = info.obj;
|
||||
if(subname) *subname = info.subname;
|
||||
auto obj = info.obj;
|
||||
if(!obj || !obj->getNameInDocument())
|
||||
return 0;
|
||||
if(info.subname.size()) {
|
||||
obj = obj->getSubObject(info.subname.c_str());
|
||||
if(!obj)
|
||||
return 0;
|
||||
}
|
||||
return resolve?obj->getLinkedObject(true):obj;
|
||||
}
|
||||
|
||||
if (hasObject(name)) {
|
||||
App::DocumentObject* act = getObject<App::DocumentObject*>(name);
|
||||
Gui::Document* doc = Application::Instance->getDocument(act->getDocument());
|
||||
Gui::ViewProviderDocumentObject* viewProvider = static_cast
|
||||
<Gui::ViewProviderDocumentObject*>(doc->getViewProvider(act));
|
||||
doc->signalHighlightObject(*viewProvider, mode, false);
|
||||
if (autoExpand)
|
||||
doc->signalExpandObject(*viewProvider, Gui::CollapseItem);
|
||||
void ActiveObjectList::setHighlight(const ObjectInfo &info, HighlightMode mode, bool enable) {
|
||||
auto obj = getObject(info,false);
|
||||
if(!obj) return;
|
||||
auto vp = dynamic_cast<ViewProviderDocumentObject*>(Application::Instance->getViewProvider(obj));
|
||||
if(!vp) return;
|
||||
|
||||
if(FC_TREEPARAM(TreeActiveAutoExpand))
|
||||
vp->getDocument()->signalExpandObject(*vp,
|
||||
enable?Gui::ExpandPath:Gui::CollapseItem, info.obj, info.subname.c_str());
|
||||
|
||||
vp->getDocument()->signalHighlightObject(*vp, mode,enable,info.obj,info.subname.c_str());
|
||||
}
|
||||
|
||||
Gui::ActiveObjectList::ObjectInfo Gui::ActiveObjectList::getObjectInfo(
|
||||
App::DocumentObject *obj, const char *subname) const
|
||||
{
|
||||
ObjectInfo info;
|
||||
info.obj = 0;
|
||||
if(!obj || !obj->getNameInDocument())
|
||||
return info;
|
||||
if(subname) {
|
||||
info.obj = obj;
|
||||
if(subname) info.subname = subname;
|
||||
}else{
|
||||
// If the input object is not from this document, it must be brought in
|
||||
// by some link type object of this document. We only accept the object
|
||||
// if we can find such object in the current selection.
|
||||
auto sels = Gui::Selection().getSelection(_Doc->getDocument()->getName(),false);
|
||||
for(auto &sel : sels) {
|
||||
if(sel.pObject == obj || sel.pObject->getLinkedObject(true)==obj) {
|
||||
info.obj = sel.pObject;
|
||||
break;
|
||||
}
|
||||
for(auto dot=strchr(sel.SubName,'.');dot;dot=strchr(dot+1,'.')) {
|
||||
std::string subname(sel.SubName,dot-sel.SubName+1);
|
||||
auto sobj = sel.pObject->getSubObject(subname.c_str());
|
||||
if(!sobj) break;
|
||||
if(sobj == obj || sobj->getLinkedObject(true) == obj) {
|
||||
info.obj = sel.pObject;
|
||||
info.subname = subname;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(info.obj) break;
|
||||
}
|
||||
if(!info.obj && obj->getDocument()==_Doc->getDocument())
|
||||
info.obj = obj;
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
bool Gui::ActiveObjectList::hasObject(App::DocumentObject *obj,
|
||||
const char *name, const char *subname) const
|
||||
{
|
||||
auto it = _ObjectMap.find(name);
|
||||
if(it==_ObjectMap.end())
|
||||
return false;
|
||||
auto info = getObjectInfo(obj,subname);
|
||||
return info.obj==it->second.obj && info.subname==it->second.subname;
|
||||
}
|
||||
|
||||
void Gui::ActiveObjectList::setObject(App::DocumentObject* obj, const char* name,
|
||||
const char *subname, const Gui::HighlightMode& mode)
|
||||
{
|
||||
auto it = _ObjectMap.find(name);
|
||||
if(it!=_ObjectMap.end()) {
|
||||
setHighlight(it->second,mode,false);
|
||||
_ObjectMap.erase(it);
|
||||
}
|
||||
if(!obj) return;
|
||||
|
||||
auto info = getObjectInfo(obj,subname);
|
||||
if(!info.obj) {
|
||||
FC_ERR("Cannot set active object "
|
||||
<< obj->getFullName() << '.' << (subname?subname:"")
|
||||
<< " in document '" << _Doc->getDocument()->getName()
|
||||
<< "'. Not found in current selection");
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj) {
|
||||
Gui::Document* doc = Application::Instance->getDocument(obj->getDocument());
|
||||
Gui::ViewProviderDocumentObject* viewProvider = static_cast
|
||||
<Gui::ViewProviderDocumentObject*>(doc->getViewProvider(obj));
|
||||
doc->signalHighlightObject(*viewProvider, mode, true);
|
||||
if (autoExpand)
|
||||
doc->signalExpandObject(*viewProvider, Gui::ExpandPath);
|
||||
_ObjectMap[name] = obj;
|
||||
}
|
||||
else {
|
||||
if (hasObject(name))
|
||||
_ObjectMap.erase(name);
|
||||
}
|
||||
_ObjectMap[name] = info;
|
||||
setHighlight(info,mode,true);
|
||||
}
|
||||
|
||||
bool Gui::ActiveObjectList::hasObject(const char*name)const
|
||||
@@ -73,14 +145,12 @@ bool Gui::ActiveObjectList::hasObject(const char*name)const
|
||||
return _ObjectMap.find(name) != _ObjectMap.end();
|
||||
}
|
||||
|
||||
void ActiveObjectList::objectDeleted(const ViewProviderDocumentObject& viewProviderIn)
|
||||
void ActiveObjectList::objectDeleted(const ViewProviderDocumentObject &vp)
|
||||
{
|
||||
App::DocumentObject* object = viewProviderIn.getObject();
|
||||
//maybe boost::bimap or boost::multi_index
|
||||
std::map<std::string, App::DocumentObject*>::iterator it;
|
||||
for (it = _ObjectMap.begin(); it != _ObjectMap.end(); ++it)
|
||||
for (auto it = _ObjectMap.begin(); it != _ObjectMap.end(); ++it)
|
||||
{
|
||||
if (it->second == object)
|
||||
if (it->second.obj == vp.getObject())
|
||||
{
|
||||
_ObjectMap.erase(it);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user