File size: 7,854 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | /**************************************************************************
* Copyright (c) 2014 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* FreeCAD is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with FreeCAD; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
***************************************************************************/
#include <App/Document.h>
#include <Gui/Application.h>
#include <Gui/Document.h>
#include <Gui/Selection/Selection.h>
#include <Gui/ViewProviderDocumentObject.h>
#include "ActiveObjectList.h"
#include "TreeParams.h"
FC_LOG_LEVEL_INIT("MDIView", true, true)
using namespace Gui;
App::DocumentObject* ActiveObjectList::getObject(
const ObjectInfo& info,
bool resolve,
App::DocumentObject** parent,
std::string* subname
) const
{
if (parent) {
*parent = info.obj;
}
if (subname) {
*subname = info.subname;
}
auto obj = info.obj;
if (!obj || !obj->isAttachedToDocument()) {
return nullptr;
}
if (!info.subname.empty()) {
obj = obj->getSubObject(info.subname.c_str());
if (!obj) {
return nullptr;
}
}
return resolve ? obj->getLinkedObject(true) : obj;
}
void ActiveObjectList::setHighlight(const ObjectInfo& info, HighlightMode mode, bool enable)
{
auto obj = getObject(info, false);
if (!obj) {
return;
}
auto vp = freecad_cast<ViewProviderDocumentObject*>(Application::Instance->getViewProvider(obj));
if (!vp) {
return;
}
if (TreeParams::getTreeActiveAutoExpand()) {
vp->getDocument()->signalExpandObject(
*vp,
enable ? TreeItemMode::ExpandPath : TreeItemMode::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 = nullptr;
if (!obj || !obj->isAttachedToDocument()) {
return info;
}
if (subname) {
info.obj = obj;
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(), ResolveMode::NoResolve);
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) {
// No selection is found, try to obtain the object hierarchy using
// DocumentObject::getParents()
unsigned long count = 0xffffffff;
for (auto& v : obj->getParents()) {
if (v.first->getDocument() != _Doc->getDocument()) {
continue;
}
// We prioritize on non-linked group object having the least
// hierarchies.
unsigned long cnt = v.first->getSubObjectList(v.second.c_str()).size();
if (v.first->getLinkedObject(false) != v.first) {
cnt &= 0x8000000;
}
if (cnt < count) {
count = cnt;
info.obj = v.first;
info.subname = v.second;
}
}
if (!info.obj) {
if (obj->getDocument() != _Doc->getDocument()) {
return info;
}
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;
}
_ObjectMap[name] = info;
setHighlight(info, mode, true);
}
bool Gui::ActiveObjectList::hasObject(const char* name) const
{
return _ObjectMap.find(name) != _ObjectMap.end();
}
void ActiveObjectList::objectDeleted(const ViewProviderDocumentObject& vp)
{
// Hint: With C++20 std::erase_if for containers can be used
auto isEqual = [&vp](const auto& item) {
return item.second.obj == vp.getObject();
};
for (auto it = _ObjectMap.begin(); it != _ObjectMap.end();) {
if (isEqual(*it)) {
it = _ObjectMap.erase(it);
}
else {
++it;
}
}
}
App::DocumentObject* ActiveObjectList::getObjectWithExtension(const Base::Type extensionTypeId) const
{
for (const auto& pair : _ObjectMap) {
App::DocumentObject* obj = getObject(pair.second, true);
if (obj && obj->hasExtension(extensionTypeId)) {
return obj;
}
}
return nullptr;
}
|